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, Form, 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: [], isFileValid: true, isFileFocused: false, selectedFile: null, uploadFlushAll: false, uploadStatusCode: null, uploadOutput: null }; this.loadSessions = this.loadSessions.bind(this); this.handleFileChange = this.handleFileChange.bind(this); this.handleUploadPcap = this.handleUploadPcap.bind(this); } componentDidMount() { this.loadSessions(); } loadSessions() { backend.get("/api/pcap/sessions").then(res => this.setState({sessions: res})); } handleFileChange(file) { this.setState({ isFileValid: file != null && file.type.endsWith("pcap"), isFileFocused: false, selectedFile: file }); } handleUploadPcap() { if (this.state.selectedFile == null || !this.state.isFileValid) { this.setState({isFileFocused: true}); return; } const formData = new FormData(); formData.append( "file", this.state.selectedFile ); backend.postFile("/api/pcap/upload", formData).then(response => response.json().then(result => this.setState({ uploadStatusCode: response.status + " " + response.statusText, uploadOutput: 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.selectedFile != null && this.state.isFileValid) ? this.state.selectedFile.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
); } } export default PcapPane;