summaryrefslogtreecommitdiffhomepage
path: root/gui/src/renderer/components/ConnectionPanel.tsx
blob: 7f4c28286b906c43d8bcf177b67c1168c6988f79 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import * as React from 'react';
import { sprintf } from 'sprintf-js';
import styled from 'styled-components';

import { colors } from '../../config.json';
import {
  ObfuscationType,
  ProxyType,
  proxyTypeToString,
  RelayProtocol,
  TunnelType,
  tunnelTypeToString,
} from '../../shared/daemon-rpc-types';
import { messages } from '../../shared/gettext';
import { default as ConnectionPanelDisclosure } from '../components/ConnectionPanelDisclosure';
import { tinyText } from './common-styles';
import Marquee from './Marquee';

export interface IEndpoint {
  ip: string;
  port: number;
  protocol: RelayProtocol;
}

export interface IInAddress extends IEndpoint {
  tunnelType: TunnelType;
}

export interface IBridgeData extends IEndpoint {
  bridgeType: ProxyType;
}

export interface IObfuscationData extends IEndpoint {
  obfuscationType: ObfuscationType;
}

export interface IOutAddress {
  ipv4?: string;
  ipv6?: string;
}

interface IProps {
  isOpen: boolean;
  hostname?: string;
  bridgeHostname?: string;
  entryHostname?: string;
  inAddress?: IInAddress;
  entryLocationInAddress?: IInAddress;
  bridgeInfo?: IBridgeData;
  outAddress?: IOutAddress;
  obfuscationEndpoint?: IObfuscationData;
  onToggle: () => void;
  className?: string;
}

const Container = styled.div({
  display: 'flex',
  flexDirection: 'column',
});

const Row = styled.div({
  display: 'flex',
  marginTop: '3px',
});

const Text = styled.span(tinyText, {
  lineHeight: '15px',
  color: colors.white,
});

const Caption = styled(Text)({
  flex: 0,
  marginRight: '8px',
});

const IpAddresses = styled.div({
  display: 'flex',
  flexDirection: 'column',
});

const Header = styled.div({
  alignSelf: 'start',
  display: 'flex',
  alignItems: 'center',
  width: '100%',
});

export default class ConnectionPanel extends React.Component<IProps> {
  public render() {
    const { outAddress } = this.props;
    const entryPoint = this.getEntryPoint();

    return (
      <Container className={this.props.className}>
        {this.props.hostname && (
          <Header>
            <ConnectionPanelDisclosure pointsUp={this.props.isOpen} onToggle={this.props.onToggle}>
              <Marquee>{this.hostnameLine()}</Marquee>
            </ConnectionPanelDisclosure>
          </Header>
        )}

        {this.props.isOpen && this.props.hostname && (
          <React.Fragment>
            {this.props.inAddress && (
              <Row>
                <Text>{this.transportLine()}</Text>
              </Row>
            )}

            {entryPoint && (
              <Row>
                <Caption>{messages.pgettext('connection-info', 'In')}</Caption>
                <Text>
                  {`${entryPoint.ip}:${entryPoint.port} ${entryPoint.protocol.toUpperCase()}`}
                </Text>
              </Row>
            )}

            {outAddress && (outAddress.ipv4 || outAddress.ipv6) && (
              <Row>
                <Caption>{messages.pgettext('connection-info', 'Out')}</Caption>
                <IpAddresses>
                  {outAddress.ipv4 && <Text>{outAddress.ipv4}</Text>}
                  {outAddress.ipv6 && <Text>{outAddress.ipv6}</Text>}
                </IpAddresses>
              </Row>
            )}
          </React.Fragment>
        )}
      </Container>
    );
  }

  private getEntryPoint(): IEndpoint | undefined {
    const { obfuscationEndpoint, inAddress, entryLocationInAddress, bridgeInfo } = this.props;

    if (obfuscationEndpoint) {
      return obfuscationEndpoint;
    } else if (entryLocationInAddress && inAddress) {
      return entryLocationInAddress;
    } else if (bridgeInfo && inAddress) {
      return bridgeInfo;
    } else {
      return inAddress;
    }
  }

  private hostnameLine() {
    if (this.props.hostname && this.props.bridgeHostname) {
      return sprintf(messages.pgettext('connection-info', '%(relay)s via %(entry)s'), {
        relay: this.props.hostname,
        entry: this.props.bridgeHostname,
      });
    } else if (this.props.hostname && this.props.entryHostname) {
      return sprintf(
        // TRANSLATORS: The hostname line displayed below the country on the main screen
        // TRANSLATORS: Available placeholders:
        // TRANSLATORS: %(relay)s - the relay hostname
        // TRANSLATORS: %(entry)s - the entry relay hostname
        messages.pgettext('connection-info', '%(relay)s via %(entry)s'),
        {
          relay: this.props.hostname,
          entry: this.props.entryHostname,
        },
      );
    } else if (this.props.bridgeInfo?.ip) {
      return sprintf(messages.pgettext('connection-info', '%(relay)s via %(entry)s'), {
        relay: this.props.hostname,
        entry: this.props.bridgeInfo.ip,
      });
    } else {
      return this.props.hostname || '';
    }
  }

  private transportLine() {
    const { inAddress, bridgeInfo } = this.props;

    if (inAddress) {
      const tunnelType = tunnelTypeToString(inAddress.tunnelType);

      if (bridgeInfo) {
        const bridgeType = proxyTypeToString(bridgeInfo.bridgeType);

        return sprintf(
          // TRANSLATORS: The tunnel type line displayed below the hostname line on the main screen
          // TRANSLATORS: Available placeholders:
          // TRANSLATORS: %(tunnelType)s - the tunnel type, i.e OpenVPN
          // TRANSLATORS: %(bridgeType)s - the bridge type, i.e Shadowsocks
          messages.pgettext('connection-info', '%(tunnelType)s via %(bridgeType)s'),
          {
            tunnelType,
            bridgeType,
          },
        );
      } else {
        return tunnelType;
      }
    } else {
      return '';
    }
  }
}