aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/Connection.js
blob: 8121d51a0e20ee00454c3ee48e0a0eb38a199ae5 (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
import React, {Component} from 'react';
import './Connection.scss';
import axios from 'axios'
import {Button, Form, OverlayTrigger, Popover} from "react-bootstrap";

class Connection extends Component {

    constructor(props) {
        super(props);
        this.state = {
            update: false,
            copiedMessage: false
        };

        this.copyTextarea = React.createRef();
        this.handleAction = this.handleAction.bind(this);
    }

    handleAction(name) {
        if (name === "hide") {
            const enabled = !this.props.data.hidden;
            axios.post(`/api/connections/${this.props.data.id}/${enabled ? "hide" : "show"}`)
                .then(res => {
                    if (res.status === 202) {
                        this.props.onEnabled(!enabled);
                        this.setState({update: true});
                    }
                });
        }
        if (name === "mark") {
            const marked = this.props.data.marked;
            axios.post(`/api/connections/${this.props.data.id}/${marked ? "unmark" : "mark"}`)
                .then(res => {
                    if (res.status === 202) {
                        this.props.onMarked(!marked);
                        this.setState({update: true});
                    }
                });
        }
        if (name === "copy") {
            this.copyTextarea.current.select();
            document.execCommand('copy');
            this.setState({copiedMessage: true});
            setTimeout(() => this.setState({copiedMessage: false}), 3000);
        }
    }

    render() {
        let conn = this.props.data;
        let serviceName = "/dev/null";
        let serviceColor = "#0F192E";
        if (conn.service.port !== 0) {
            serviceName = conn.service.name;
            serviceColor = conn.service.color;
        }
        let startedAt = new Date(conn.started_at);
        let closedAt = new Date(conn.closed_at);
        let processedAt = new Date(conn.processed_at);
        let duration = ((closedAt - startedAt) / 1000).toFixed(3);
        let timeInfo = <div>
            <span>Started at {startedAt.toLocaleDateString() + " " + startedAt.toLocaleTimeString()}</span><br/>
            <span>Processed at {processedAt.toLocaleDateString() + " " + processedAt.toLocaleTimeString()}</span><br/>
            <span>Closed at {closedAt.toLocaleDateString() + " " + closedAt.toLocaleTimeString()}</span>
        </div>;

        let classes = "connection";
        if (this.props.selected) {
            classes += " connection-selected";
        }

        const popoverFor = function (name, content) {
            return <Popover id={`popover-${name}-${conn.id}`} className="connection-popover">
                <Popover.Content>
                    {content}
                </Popover.Content>
            </Popover>;
        };

        const commentPopoverContent = <div>
            <span>Click to <strong>{conn.comment.length > 0 ? "edit" : "add"}</strong> comment</span>
            {conn.comment && <Form.Control as="textarea" readOnly={true} rows={2} defaultValue={conn.comment}/>}
        </div>;

        const copyPopoverContent = <div>
            {this.state.copiedMessage ? <span><strong>Copied!</strong></span> :
                <span>Click to <strong>copy</strong> the connection id</span>}
            <Form.Control as="textarea" readOnly={true} rows={1} defaultValue={conn.id} ref={this.copyTextarea}/>
        </div>;

        return (
            <tr className={classes}>
                <td>
                    <span className="connection-service">
                        <Button size="sm" style={{
                            "backgroundColor": serviceColor
                        }}>{serviceName}</Button>
                    </span>
                </td>
                <td className="clickable" onClick={() => this.props.onSelected()}>{conn.ip_src}</td>
                <td className="clickable" onClick={() => this.props.onSelected()}>{conn.port_src}</td>
                <td className="clickable" onClick={() => this.props.onSelected()}>{conn.ip_dst}</td>
                <td className="clickable" onClick={() => this.props.onSelected()}>{conn.port_dst}</td>
                <td className="clickable" onClick={() => this.props.onSelected()}>
                    <OverlayTrigger trigger={["focus", "hover"]} placement="right"
                                    overlay={popoverFor("duration", timeInfo)}>
                        <span className="test-tooltip">{duration}s</span>
                    </OverlayTrigger>
                </td>
                <td className="clickable" onClick={() => this.props.onSelected()}>{conn.client_bytes}</td>
                <td className="clickable" onClick={() => this.props.onSelected()}>{conn.server_bytes}</td>
                <td>
                    <OverlayTrigger trigger={["focus", "hover"]} placement="right"
                                    overlay={popoverFor("hide", <span>Hide this connection from the list</span>)}>
                        <span className={"connection-icon" + (conn.hidden ? " icon-enabled" : "")}
                              onClick={() => this.handleAction("hide")}>%</span>
                    </OverlayTrigger>
                    <OverlayTrigger trigger={["focus", "hover"]} placement="right"
                                    overlay={popoverFor("hide", <span>Mark this connection</span>)}>
                        <span className={"connection-icon" + (conn.marked ? " icon-enabled" : "")}
                              onClick={() => this.handleAction("mark")}>!!</span>
                    </OverlayTrigger>
                    <OverlayTrigger trigger={["focus", "hover"]} placement="right"
                                    overlay={popoverFor("comment", commentPopoverContent)}>
                        <span className={"connection-icon" + (conn.comment ? " icon-enabled" : "")}
                              onClick={() => this.handleAction("comment")}>@</span>
                    </OverlayTrigger>
                    <OverlayTrigger trigger={["focus", "hover"]} placement="right"
                                    overlay={popoverFor("copy", copyPopoverContent)}>
                        <span className="connection-icon"
                              onClick={() => this.handleAction("copy")}>#</span>
                    </OverlayTrigger>
                </td>
            </tr>
        );
    }

}

export default Connection;