import React, {Component} from 'react'; import './PcapPane.scss'; import Table from "react-bootstrap/Table"; import backend from "../../backend"; import {createCurlCommand, formatSize, timestampToTime2} from "../../utils"; import {Button, Col, Container, Row} from "react-bootstrap"; import InputField from "../fields/InputField"; import CheckField from "../fields/CheckField"; import TextField from "../fields/TextField"; class PcapPane extends Component { constructor(props) { super(props); this.state = { sessions: [], isUploadFileValid: true, isUploadFileFocused: false, uploadSelectedFile: null, uploadFlushAll: false, uploadStatusCode: null, uploadOutput: null, isFileValid: true, isFileFocused: false, fileValue: "", fileFlushAll: false, fileStatusCode: null, fileOutput: null, deleteOriginalFile: false }; } componentDidMount() { this.loadSessions(); } loadSessions = () => { backend.getJson("/api/pcap/sessions").then(res => this.setState({sessions: res})); }; handleUploadFileChange = (file) => { this.setState({ isUploadFileValid: file != null && file.type.endsWith("pcap"), isUploadFileFocused: false, uploadSelectedFile: file }); }; handleUploadPcap = () => { if (this.state.uploadSelectedFile == null || !this.state.isUploadFileValid) { this.setState({isUploadFileFocused: true}); return; } const formData = new FormData(); formData.append( "file", this.state.uploadSelectedFile ); backend.postFile("/api/pcap/upload", formData).then(response => response.json().then(result => this.setState({ uploadStatusCode: response.status + " " + response.statusText, uploadOutput: JSON.stringify(result) })) ); }; handleFileChange = (file) => { this.setState({ isFileValid: file !== "" && file.endsWith("pcap"), isFileFocused: false, fileValue: file }); }; handleProcessPcap = () => { if (this.state.fileValue === "" || !this.state.isFileValid) { this.setState({isFileFocused: true}); return; } backend.post("/api/pcap/file", { file: this.state.fileValue, flush_all: this.state.fileFlushAll, delete_original_file: this.state.deleteOriginalFile }).then(response => response.json().then(result => this.setState({ fileStatusCode: response.status + " " + response.statusText, fileOutput: JSON.stringify(result) })) ); }; render() { let sessions = this.state.sessions.map(s => {s["id"].substring(0, 8)} {timestampToTime2(s["started_at"])} {((new Date(s["completed_at"]) - new Date(s["started_at"])) / 1000).toFixed(3)}s {formatSize(s["size"])} {s["processed_packets"]} {s["invalid_packets"]} undefined download ); const uploadOutput = this.state.uploadOutput != null ? this.state.uploadOutput : createCurlCommand("pcap/upload", "POST", null, { file: "@" + ((this.state.uploadSelectedFile != null && this.state.isUploadFileValid) ? this.state.uploadSelectedFile.name : "invalid.pcap"), flush_all: this.state.uploadFlushAll }) ; return (
GET /api/pcap/sessions 200 OK
{sessions}
id started_at duration size processed_packets invalid_packets packets_per_service actions
POST /api/pcap/upload {this.state.uploadStatusCode}
options: this.setState({uploadFlushAll: v})}/>
POST /api/pcap/file {this.state.fileStatusCode}
this.setState({uploadFlushAll: v})}/> this.setState({uploadFlushAll: v})}/>
); } } export default PcapPane;