From 1317044508a9b7adf0d4d9faf4b9f75f5834a0c9 Mon Sep 17 00:00:00 2001 From: Emiliano Ciavatta Date: Sat, 18 Jul 2020 13:09:18 +0200 Subject: Add service_port filter --- frontend/src/components/Connection.js | 31 +++++---- frontend/src/components/Connection.scss | 4 ++ frontend/src/components/ConnectionContent.js | 31 +++------ frontend/src/components/ServicePortFilter.js | 91 ++++++++++++++++++++++++++ frontend/src/components/ServicePortFilter.scss | 61 +++++++++++++++++ frontend/src/views/App.js | 1 - frontend/src/views/Connections.js | 86 +++++++++++++++++------- frontend/src/views/Header.js | 15 ++++- frontend/src/views/Header.scss | 10 +++ 9 files changed, 268 insertions(+), 62 deletions(-) create mode 100644 frontend/src/components/ServicePortFilter.js create mode 100644 frontend/src/components/ServicePortFilter.scss (limited to 'frontend/src') diff --git a/frontend/src/components/Connection.js b/frontend/src/components/Connection.js index 8121d51..e41f542 100644 --- a/frontend/src/components/Connection.js +++ b/frontend/src/components/Connection.js @@ -67,6 +67,9 @@ class Connection extends Component { if (this.props.selected) { classes += " connection-selected"; } + if (this.props.containsFlag) { + classes += " contains-flag"; + } const popoverFor = function (name, content) { return @@ -93,27 +96,27 @@ class Connection extends Component { + }} onClick={() => this.props.addServicePortFilter(conn.port_dst)}>{serviceName} - this.props.onSelected()}>{conn.ip_src} - this.props.onSelected()}>{conn.port_src} - this.props.onSelected()}>{conn.ip_dst} - this.props.onSelected()}>{conn.port_dst} - this.props.onSelected()}> + {conn.ip_src} + {conn.port_src} + {conn.ip_dst} + {conn.port_dst} + {duration}s - this.props.onSelected()}>{conn.client_bytes} - this.props.onSelected()}>{conn.server_bytes} - - Hide this connection from the list)}> - this.handleAction("hide")}>% - + {conn.client_bytes} + {conn.server_bytes} + + {/*Hide this connection from the list)}>*/} + {/* this.handleAction("hide")}>%*/} + {/**/} Mark this connection)}> nope; } - const format = this.state.format !== "default" ? `/${this.state.format}` : ""; - const redirect = ; - let payload = content.map((c, i) => - + {c.content} ); @@ -63,11 +52,11 @@ class ConnectionContent extends Component { return (
- - - ciao - - + {/**/} + {/* */} + {/* ciao*/} + {/* */} + {/**/} @@ -92,10 +81,6 @@ class ConnectionContent extends Component {
{payload}
- - {/*{redirect}*/} - -
); } @@ -103,4 +88,4 @@ class ConnectionContent extends Component { } -export default withRouter(ConnectionContent); +export default ConnectionContent; diff --git a/frontend/src/components/ServicePortFilter.js b/frontend/src/components/ServicePortFilter.js new file mode 100644 index 0000000..72f2643 --- /dev/null +++ b/frontend/src/components/ServicePortFilter.js @@ -0,0 +1,91 @@ +import React, {Component} from 'react'; +import './ServicePortFilter.scss'; +import {withRouter} from "react-router-dom"; +import {Redirect} from "react-router"; + +class ServicePortFilter extends Component { + + constructor(props) { + super(props); + this.state = { + servicePort: "", + servicePortUrl: null, + timeoutHandle: null + }; + + this.servicePortChanged = this.servicePortChanged.bind(this); + } + + componentDidMount() { + let params = new URLSearchParams(this.props.location.search); + let servicePort = params.get("service_port"); + if (servicePort !== null) { + this.setState({ + servicePort: servicePort, + servicePortUrl: servicePort + }); + } + } + + servicePortChanged(event) { + let value = event.target.value.replace(/[^\d]/gi, ''); + if (value.startsWith("0")) { + return; + } + if (value !== "") { + let port = parseInt(value); + if (port > 65565) { + return; + } + } + + if (this.state.timeoutHandle !== null) { + clearTimeout(this.state.timeoutHandle); + } + this.setState({ + servicePort: value, + timeoutHandle: setTimeout(() => + this.setState({servicePortUrl: value === "" ? null : value}), 300) + }); + } + + render() { + let redirect = null; + let urlParams = new URLSearchParams(this.props.location.search); + if (urlParams.get("service_port") !== this.state.servicePortUrl) { + if (this.state.servicePortUrl !== null) { + urlParams.set("service_port", this.state.servicePortUrl); + } else { + urlParams.delete("service_port"); + } + redirect = ; + } + let active = this.state.servicePort !== ""; + + return ( +
+
+
+ service_port: +
+
+ + { active && +
+ this.setState({ + servicePort: "", + servicePortUrl: null + })}>del +
+ } + + {redirect} +
+ ); + } + +} + +export default withRouter(ServicePortFilter); diff --git a/frontend/src/components/ServicePortFilter.scss b/frontend/src/components/ServicePortFilter.scss new file mode 100644 index 0000000..2b23444 --- /dev/null +++ b/frontend/src/components/ServicePortFilter.scss @@ -0,0 +1,61 @@ +@import '../colors.scss'; + +.filter { + margin: 0 10px; + position: relative; + + .filter-name-wrapper { + background-color: $color-primary-2; + padding: 3px 7px; + border-top-left-radius: 5px; + border-bottom-left-radius: 5px; + } + + .filter-name { + font-size: 13px; + } + + .filter-value { + font-size: 13px; + padding-left: 0; + border-radius: 5px; + + &:focus { + background-color: $color-primary-2; + } + } + + &.filter-active { + .filter-name-wrapper { + background-color: $color-primary-4; + color: $color-primary-3; + } + + .filter-value { + font-size: 13px; + background-color: $color-primary-4; + color: $color-primary-3; + + &:focus { + background-color: $color-primary-4; + } + } + } + + .filter-delete { + position: absolute; + right: 10px; + top: 10px; + + z-index: 10; + font-size: 11px; + letter-spacing: -0.5px; + + color: $color-primary-2; + cursor: pointer; + + .filter-delete-icon { + font-weight: 800; + } + } +} \ No newline at end of file diff --git a/frontend/src/views/App.js b/frontend/src/views/App.js index 34e980e..e3119aa 100644 --- a/frontend/src/views/App.js +++ b/frontend/src/views/App.js @@ -26,7 +26,6 @@ class App extends Component {
this.setState({servicesShow: true})}/> - }/> }/> }/> diff --git a/frontend/src/views/Connections.js b/frontend/src/views/Connections.js index e4a7b01..9b9fe35 100644 --- a/frontend/src/views/Connections.js +++ b/frontend/src/views/Connections.js @@ -5,7 +5,6 @@ import Connection from "../components/Connection"; import Table from 'react-bootstrap/Table'; import {Redirect} from 'react-router'; import {withRouter} from "react-router-dom"; -import {objectToQueryString} from "../utils"; class Connections extends Component { @@ -16,7 +15,11 @@ class Connections extends Component { connections: [], firstConnection: null, lastConnection: null, - showHidden: false + showHidden: false, + prevParams: null, + flagRule: null, + rules: null, + queryString: null }; this.scrollTopThreashold = 0.00001; @@ -26,10 +29,12 @@ class Connections extends Component { this.handleScroll = this.handleScroll.bind(this); this.connectionSelected = this.connectionSelected.bind(this); + this.addServicePortFilter = this.addServicePortFilter.bind(this); } componentDidMount() { - this.loadConnections({limit: this.queryLimit, hidden: this.state.showHidden}); + this.loadConnections({limit: this.queryLimit, hidden: this.state.showHidden}) + .then(() => this.setState({loaded: true})); } connectionSelected(c) { @@ -37,7 +42,13 @@ class Connections extends Component { this.props.onSelected(c); } - + componentDidUpdate(prevProps, prevState, snapshot) { + if (this.state.loaded && prevProps.location.search !== this.props.location.search) { + this.setState({queryString: this.props.location.search}); + this.loadConnections({limit: this.queryLimit, hidden: this.state.showHidden}) + .then(() => console.log("Connections reloaded after query string update")); + } + } handleScroll(e) { let relativeScroll = e.currentTarget.scrollTop / (e.currentTarget.scrollHeight - e.currentTarget.clientHeight); @@ -45,32 +56,38 @@ class Connections extends Component { this.loadConnections({ from: this.state.lastConnection.id, limit: this.queryLimit, hidden: this.state.showHidden - }); + }).then(() => console.log("Following connections loaded")); } if (!this.state.loading && relativeScroll < this.scrollTopThreashold) { this.loadConnections({ to: this.state.firstConnection.id, limit: this.queryLimit, hidden: this.state.showHidden - }); + }).then(() => console.log("Previous connections loaded")); } + } + addServicePortFilter(port) { + let urlParams = new URLSearchParams(this.props.location.search); + urlParams.set("service_port", port); + this.setState({queryString: "?" + urlParams}); } async loadConnections(params) { let url = "/api/connections"; - if (params !== undefined) { - const urlParams = new URLSearchParams(window.location.search); - let obj = Object.fromEntries(urlParams.entries()); - url += "?" + objectToQueryString({...params, ...obj}); // TODO: remove this shit + const urlParams = new URLSearchParams(this.props.location.search); + for (const [name, value] of Object.entries(params)) { + urlParams.set(name, value); } - this.setState({loading: true}); - let res = await axios.get(url); + + this.setState({loading: true, prevParams: params}); + let res = await axios.get(`${url}?${urlParams}`); let connections = this.state.connections; let firstConnection = this.state.firstConnection; let lastConnection = this.state.lastConnection; - if (res.data.length > 0) { - if (params !== undefined && params.from !== undefined) { + + if (params !== undefined && params.from !== undefined) { + if (res.data.length > 0) { connections = this.state.connections.concat(res.data); lastConnection = connections[connections.length - 1]; if (connections.length > this.maxConnections) { @@ -78,34 +95,55 @@ class Connections extends Component { connections.length - 1); firstConnection = connections[0]; } - } else if (params !== undefined && params.to !== undefined) { + } + } else if (params !== undefined && params.to !== undefined) { + if (res.data.length > 0) { connections = res.data.concat(this.state.connections); firstConnection = connections[0]; if (connections.length > this.maxConnections) { connections = connections.slice(0, this.maxConnections); lastConnection = connections[this.maxConnections - 1]; } - } else { + } + } else { + if (res.data.length > 0) { connections = res.data; firstConnection = connections[0]; lastConnection = connections[connections.length - 1]; + } else { + connections = []; + firstConnection = null; + lastConnection = null; } } + let flagRule = this.state.flagRule; + let rules = this.state.rules; + if (flagRule === null) { + let res = await axios.get("/api/rules"); + rules = res.data; + flagRule = rules.filter(rule => { + return rule.name === "flag"; + })[0]; + } + this.setState({ loading: false, connections: connections, + rules: res.data, + flagRule: flagRule, firstConnection: firstConnection, lastConnection: lastConnection }); } - render() { - let redirect = null; + let redirect; + let queryString = this.state.queryString !== null ? this.state.queryString : "" if (this.state.selected) { - const format = this.props.match.params.format; - redirect = ; + let format = this.props.match.params.format; + format = format !== undefined ? "/" + format : ""; + redirect = ; } let loading = null; @@ -123,8 +161,8 @@ class Connections extends Component { service srcip - dstip srcport + dstip dstport duration up @@ -137,14 +175,16 @@ class Connections extends Component { this.state.connections.map(c => this.connectionSelected(c)} selected={this.state.selected === c.id} onMarked={marked => c.marked = marked} - onEnabled={enabled => c.hidden = !enabled}/> + onEnabled={enabled => c.hidden = !enabled} + containsFlag={c.matched_rules.includes(this.state.flagRule.id)} + addServicePortFilter={this.addServicePortFilter}/> ) } {loading} - {/*{redirect}*/} + {redirect} ); } diff --git a/frontend/src/views/Header.js b/frontend/src/views/Header.js index f96b036..5118ec3 100644 --- a/frontend/src/views/Header.js +++ b/frontend/src/views/Header.js @@ -2,6 +2,7 @@ import React, {Component} from 'react'; import Typed from 'typed.js'; import './Header.scss'; import {Button} from "react-bootstrap"; +import ServicePortFilter from "../components/ServicePortFilter"; class Header extends Component { @@ -29,13 +30,25 @@ class Header extends Component { return (
-
+

{ this.el = el; }}/>

+ +
+
+
+ + {/**/} + {/**/} + +
+
+
+
diff --git a/frontend/src/views/Header.scss b/frontend/src/views/Header.scss index e5040d8..806e398 100644 --- a/frontend/src/views/Header.scss +++ b/frontend/src/views/Header.scss @@ -2,6 +2,7 @@ .header { padding: 15px 30px; + height: 80px; > .row { background-color: $color-primary-0; @@ -9,10 +10,19 @@ .header-title { margin: 5px 0 5px -5px; + width: 200px; } .header-buttons { margin: 5px 0; text-align: right; } + + .filters-bar-wrapper { + height: 50px; + padding: 8px 0; + + .filters-bar { + } + } } -- cgit v1.2.3-70-g09d2