aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/views/Connections.js
blob: 5548c4ca85d0dde80cdb2117cfd8ebec3352adf7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, {Component} from 'react';
import './Connections.scss';
import axios from 'axios'
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 {

    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            connections: [],
            firstConnection: null,
            lastConnection: null,
            showHidden: false
        };

        this.scrollTopThreashold = 0.00001;
        this.scrollBottomThreashold = 0.99999;
        this.maxConnections = 500;
        this.queryLimit = 50;

        this.handleScroll = this.handleScroll.bind(this);
        this.connectionSelected = this.connectionSelected.bind(this);
    }

    componentDidMount() {
        this.loadConnections({limit: this.queryLimit, hidden: this.state.showHidden});
    }

    connectionSelected(c) {
        this.setState({selected: c.id});
        this.props.onSelected(c);
    }

    handleScroll(e) {
        let relativeScroll = e.currentTarget.scrollTop / (e.currentTarget.scrollHeight - e.currentTarget.clientHeight);
        if (!this.state.loading && relativeScroll > this.scrollBottomThreashold) {
            this.loadConnections({
                from: this.state.lastConnection.id, limit: this.queryLimit,
                hidden: this.state.showHidden
            });
        }
        if (!this.state.loading && relativeScroll < this.scrollTopThreashold) {
            this.loadConnections({
                to: this.state.firstConnection.id, limit: this.queryLimit,
                hidden: this.state.showHidden
            });
        }

    }

    async loadConnections(params) {
        let url = "/api/connections";
        if (params !== undefined) {
            url += "?" + objectToQueryString(params);
        }
        this.setState({loading: true});
        let res = await axios.get(url);

        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) {
                connections = this.state.connections.concat(res.data);
                lastConnection = connections[connections.length - 1];
                if (connections.length > this.maxConnections) {
                    connections = connections.slice(connections.length - this.maxConnections,
                        connections.length - 1);
                    firstConnection = connections[0];
                }
            } else if (params !== undefined && params.to !== undefined) {
                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 {
                connections = res.data;
                firstConnection = connections[0];
                lastConnection = connections[connections.length - 1];
            }
        }

        this.setState({
            loading: false,
            connections: connections,
            firstConnection: firstConnection,
            lastConnection: lastConnection
        });
    }


    render() {
        let redirect = null;
        if (this.state.selected) {
            const format = this.props.match.params.format;
            redirect = <Redirect push to={`/connections/${this.state.selected}${format !== undefined ? ("/" + format) : ""}`} />;
        }

        let loading = null;
        if (this.state.loading) {
            loading = <tr>
                <td colSpan={9}>Loading...</td>
            </tr>;
        }

        return (
            <div className="connections" onScroll={this.handleScroll}>
                <div className="connections-header-padding"/>
                <Table borderless size="sm">
                    <thead>
                    <tr>
                        <th>service</th>
                        <th>srcip</th>
                        <th>dstip</th>
                        <th>srcport</th>
                        <th>dstport</th>
                        <th>duration</th>
                        <th>up</th>
                        <th>down</th>
                        <th>actions</th>
                    </tr>
                    </thead>
                    <tbody>
                    {
                        this.state.connections.map(c =>
                            <Connection key={c.id} data={c} onSelected={() => this.connectionSelected(c)}
                                        selected={this.state.selected === c.id} onMarked={marked => c.marked = marked}
                                        onEnabled={enabled => c.hidden = !enabled}/>
                        )
                    }
                    {loading}
                    </tbody>
                </Table>

                {redirect}
            </div>
        );
    }

}


export default withRouter(Connections);