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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
import * as React from 'react';
import { sprintf } from 'sprintf-js';
import styled from 'styled-components';
import { BridgeState, RelayProtocol } from '../../shared/daemon-rpc-types';
import { messages } from '../../shared/gettext';
import { AriaDescription, AriaInput, AriaInputGroup, AriaLabel } from './AriaGroup';
import * as Cell from './cell';
import { Layout, SettingsContainer } from './Layout';
import { ModalContainer } from './Modal';
import {
BackBarItem,
NavigationBar,
NavigationContainer,
NavigationItems,
NavigationScrollbars,
TitleBarItem,
} from './NavigationBar';
import Selector, { ISelectorItem } from './cell/Selector';
import SettingsHeader, { HeaderTitle } from './SettingsHeader';
const MIN_MSSFIX_VALUE = 1000;
const MAX_MSSFIX_VALUE = 1450;
const UDP_PORTS = [1194, 1195, 1196, 1197, 1300, 1301, 1302];
const TCP_PORTS = [80, 443];
type OptionalPort = number | undefined;
type OptionalRelayProtocol = RelayProtocol | undefined;
function mapPortToSelectorItem(value: number): ISelectorItem<number> {
return { label: value.toString(), value };
}
export const StyledNavigationScrollbars = styled(NavigationScrollbars)({
flex: 1,
});
export const StyledSelectorContainer = styled.div({
flex: 0,
});
export const StyledInputFrame = styled(Cell.InputFrame)({
flex: 0,
});
interface IProps {
openvpn: {
protocol?: RelayProtocol;
port?: number;
};
mssfix?: number;
bridgeState: BridgeState;
setOpenVpnMssfix: (value: number | undefined) => void;
setOpenVpnRelayProtocolAndPort: (protocol?: RelayProtocol, port?: number) => void;
setBridgeState: (value: BridgeState) => void;
onClose: () => void;
}
export default class OpenVpnSettings extends React.Component<IProps> {
private portItems: { [key in RelayProtocol]: Array<ISelectorItem<OptionalPort>> };
private protocolItems: Array<ISelectorItem<OptionalRelayProtocol>>;
private bridgeStateItems: Array<ISelectorItem<BridgeState>>;
constructor(props: IProps) {
super(props);
const automaticPort: ISelectorItem<OptionalPort> = {
label: messages.gettext('Automatic'),
value: undefined,
};
this.portItems = {
udp: [automaticPort].concat(UDP_PORTS.map(mapPortToSelectorItem)),
tcp: [automaticPort].concat(TCP_PORTS.map(mapPortToSelectorItem)),
};
this.protocolItems = [
{
label: messages.gettext('Automatic'),
value: undefined,
},
{
label: messages.gettext('TCP'),
value: 'tcp',
},
{
label: messages.gettext('UDP'),
value: 'udp',
},
];
this.bridgeStateItems = [
{
label: messages.gettext('Automatic'),
value: 'auto',
},
{
label: messages.gettext('On'),
value: 'on',
},
{
label: messages.gettext('Off'),
value: 'off',
},
];
}
public render() {
return (
<ModalContainer>
<Layout>
<SettingsContainer>
<NavigationContainer>
<NavigationBar>
<NavigationItems>
<BackBarItem action={this.props.onClose}>
{
// TRANSLATORS: Back button in navigation bar
messages.pgettext('navigation-bar', 'Advanced')
}
</BackBarItem>
<TitleBarItem>
{
// TRANSLATORS: Title label in navigation bar
messages.pgettext('openvpn-settings-nav', 'OpenVPN settings')
}
</TitleBarItem>
</NavigationItems>
</NavigationBar>
<StyledNavigationScrollbars>
<SettingsHeader>
<HeaderTitle>
{messages.pgettext('openvpn-settings-view', 'OpenVPN settings')}
</HeaderTitle>
</SettingsHeader>
<AriaInputGroup>
<StyledSelectorContainer>
<Selector
title={messages.pgettext('openvpn-settings-view', 'Transport protocol')}
values={this.protocolItems}
value={this.props.openvpn.protocol}
onSelect={this.onSelectOpenvpnProtocol}
/>
{this.props.openvpn.protocol ? (
<Selector
title={sprintf(
// TRANSLATORS: The title for the port selector section.
// TRANSLATORS: Available placeholders:
// TRANSLATORS: %(portType)s - a selected protocol (either TCP or UDP)
messages.pgettext('openvpn-settings-view', '%(portType)s port'),
{
portType: this.props.openvpn.protocol.toUpperCase(),
},
)}
values={this.portItems[this.props.openvpn.protocol]}
value={this.props.openvpn.port}
onSelect={this.onSelectOpenVpnPort}
/>
) : undefined}
</StyledSelectorContainer>
</AriaInputGroup>
<AriaInputGroup>
<Selector
title={
// TRANSLATORS: The title for the shadowsocks bridge selector section.
messages.pgettext('openvpn-settings-view', 'Bridge mode')
}
values={this.bridgeStateItems}
value={this.props.bridgeState}
onSelect={this.onSelectBridgeState}
/>
</AriaInputGroup>
<AriaInputGroup>
<Cell.Container>
<AriaLabel>
<Cell.InputLabel>
{messages.pgettext('openvpn-settings-view', 'Mssfix')}
</Cell.InputLabel>
</AriaLabel>
<StyledInputFrame>
<AriaInput>
<Cell.AutoSizingTextInput
value={this.props.mssfix ? this.props.mssfix.toString() : ''}
inputMode={'numeric'}
maxLength={4}
placeholder={messages.gettext('Default')}
onSubmitValue={this.onMssfixSubmit}
validateValue={OpenVpnSettings.mssfixIsValid}
submitOnBlur={true}
modifyValue={OpenVpnSettings.removeNonNumericCharacters}
/>
</AriaInput>
</StyledInputFrame>
</Cell.Container>
<Cell.Footer>
<AriaDescription>
<Cell.FooterText>
{sprintf(
// TRANSLATORS: The hint displayed below the Mssfix input field.
// TRANSLATORS: Available placeholders:
// TRANSLATORS: %(max)d - the maximum possible mssfix value
// TRANSLATORS: %(min)d - the minimum possible mssfix value
messages.pgettext(
'openvpn-settings-view',
'Set OpenVPN MSS value. Valid range: %(min)d - %(max)d.',
),
{
min: MIN_MSSFIX_VALUE,
max: MAX_MSSFIX_VALUE,
},
)}
</Cell.FooterText>
</AriaDescription>
</Cell.Footer>
</AriaInputGroup>
</StyledNavigationScrollbars>
</NavigationContainer>
</SettingsContainer>
</Layout>
</ModalContainer>
);
}
private onSelectOpenvpnProtocol = (protocol?: RelayProtocol) => {
this.props.setOpenVpnRelayProtocolAndPort(protocol);
};
private onSelectOpenVpnPort = (port?: number) => {
this.props.setOpenVpnRelayProtocolAndPort(this.props.openvpn.protocol, port);
};
private onSelectBridgeState = (bridgeState: BridgeState) => {
this.props.setBridgeState(bridgeState);
};
private onMssfixSubmit = (value: string) => {
const parsedValue = value === '' ? undefined : parseInt(value, 10);
if (OpenVpnSettings.mssfixIsValid(value)) {
this.props.setOpenVpnMssfix(parsedValue);
}
};
private static removeNonNumericCharacters(value: string) {
return value.replace(/[^0-9]/g, '');
}
private static mssfixIsValid(mssfix: string): boolean {
const parsedMssFix = mssfix ? parseInt(mssfix) : undefined;
return (
parsedMssFix === undefined ||
(parsedMssFix >= MIN_MSSFIX_VALUE && parsedMssFix <= MAX_MSSFIX_VALUE)
);
}
}
|