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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
import * as React from 'react';
import { sprintf } from 'sprintf-js';
import { TunnelState } from '../../shared/daemon-rpc-types';
import { formatRelativeDate } from '../../shared/date-helper';
import { messages } from '../../shared/gettext';
import log from '../../shared/logging';
import { IWgKey, WgKeyState } from '../redux/settings/reducers';
import * as AppButton from './AppButton';
import { AriaDescribed, AriaDescription, AriaDescriptionGroup } from './AriaGroup';
import ClipboardLabel from './ClipboardLabel';
import ImageView from './ImageView';
import { Layout } from './Layout';
import {
BackBarItem,
NavigationBar,
NavigationContainer,
NavigationItems,
TitleBarItem,
} from './NavigationBar';
import SettingsHeader, { HeaderTitle } from './SettingsHeader';
import {
StyledButtonRow,
StyledContainer,
StyledContent,
StyledLastButtonRow,
StyledMessage,
StyledMessages,
StyledNavigationScrollbars,
StyledRow,
StyledRowLabel,
StyledRowLabelSpacer,
StyledRowValue,
} from './WireguardKeysStyles';
export interface IProps {
keyState: WgKeyState;
isOffline: boolean;
tunnelState: TunnelState;
windowFocused: boolean;
onClose: () => void;
onGenerateKey: () => void;
onReplaceKey: (old: IWgKey) => void;
onVerifyKey: (publicKey: IWgKey) => void;
onVisitWebsiteKey: () => Promise<void>;
}
export interface IState {
recentlyGeneratedKey: boolean;
userHasInitiatedVerification: boolean;
ageOfKeyString: string;
}
export default class WireguardKeys extends React.Component<IProps, IState> {
public state = {
recentlyGeneratedKey: false,
userHasInitiatedVerification: false,
ageOfKeyString: WireguardKeys.ageOfKeyString(this.props.keyState),
};
private keyAgeUpdateInterval?: number;
public static getDerivedStateFromProps(props: IProps) {
return {
ageOfKeyString: WireguardKeys.ageOfKeyString(props.keyState),
};
}
public componentDidMount() {
this.verifyKey();
this.keyAgeUpdateInterval = window.setInterval(this.setAgeOfKeyStringState, 60 * 1000);
}
public componentWillUnmount() {
clearInterval(this.keyAgeUpdateInterval);
}
public componentDidUpdate(prevProps: IProps) {
const prevKey =
prevProps.keyState.type === 'key-set' ? prevProps.keyState.key.publicKey : undefined;
const key =
this.props.keyState.type === 'key-set' ? this.props.keyState.key.publicKey : undefined;
if (this.props.tunnelState.state === 'connected' && key !== undefined && key != prevKey) {
this.setState({ recentlyGeneratedKey: true });
}
if (
this.state.recentlyGeneratedKey &&
prevProps.tunnelState.state !== 'connected' &&
this.props.tunnelState.state === 'connected'
) {
this.setState({ recentlyGeneratedKey: false });
}
}
public render() {
return (
<Layout>
<StyledContainer>
<NavigationContainer>
<NavigationBar>
<NavigationItems>
<BackBarItem action={this.props.onClose}>
{
// TRANSLATORS: Back button in navigation bar
messages.pgettext('wireguard-keys-nav', 'WireGuard settings')
}
</BackBarItem>
<TitleBarItem>
{
// TRANSLATORS: Title label in navigation bar
messages.pgettext('wireguard-keys-nav', 'WireGuard key')
}
</TitleBarItem>
</NavigationItems>
</NavigationBar>
<StyledNavigationScrollbars fillContainer>
<StyledContent>
<SettingsHeader>
<HeaderTitle>
{messages.pgettext('wireguard-keys-nav', 'WireGuard key')}
</HeaderTitle>
</SettingsHeader>
<StyledRow>
<StyledRowLabel>
<span>{messages.pgettext('wireguard-key-view', 'Public key')}</span>
<StyledRowLabelSpacer />
<span>{this.keyValidityLabel()}</span>
</StyledRowLabel>
<StyledRowValue>{this.getKeyText()}</StyledRowValue>
</StyledRow>
<StyledRow>
<StyledRowLabel>
{messages.pgettext('wireguard-key-view', 'Key generated')}
</StyledRowLabel>
<StyledRowValue>{this.state.ageOfKeyString}</StyledRowValue>
</StyledRow>
<StyledMessages>{this.getStatusMessage()}</StyledMessages>
<StyledButtonRow>{this.getGenerateButton()}</StyledButtonRow>
<StyledButtonRow>
<AppButton.BlueButton
disabled={this.isVerifyButtonDisabled()}
onClick={this.handleVerifyKeyPress}>
<AppButton.Label>
{messages.pgettext('wireguard-key-view', 'Verify key')}
</AppButton.Label>
</AppButton.BlueButton>
</StyledButtonRow>
<StyledLastButtonRow>
<AppButton.BlockingButton
disabled={this.props.isOffline}
onClick={this.props.onVisitWebsiteKey}>
<AriaDescriptionGroup>
<AriaDescribed>
<AppButton.BlueButton>
<AppButton.Label>
{messages.pgettext('wireguard-key-view', 'Manage keys')}
</AppButton.Label>
<AriaDescription>
<AppButton.Icon
source="icon-extLink"
height={16}
width={16}
aria-label={messages.pgettext('accessibility', 'Opens externally')}
/>
</AriaDescription>
</AppButton.BlueButton>
</AriaDescribed>
</AriaDescriptionGroup>
</AppButton.BlockingButton>
</StyledLastButtonRow>
</StyledContent>
</StyledNavigationScrollbars>
</NavigationContainer>
</StyledContainer>
</Layout>
);
}
private isVerifyButtonDisabled(): boolean {
return this.props.keyState.type !== 'key-set';
}
private handleVerifyKeyPress = () => {
this.setState({ userHasInitiatedVerification: true });
this.verifyKey();
};
private verifyKey() {
switch (this.props.keyState.type) {
case 'key-set': {
const key = this.props.keyState.key;
this.props.onVerifyKey(key);
break;
}
default:
log.error(`onVerifyKey called from invalid state - ${this.props.keyState.type}`);
}
}
/// Action button can either generate or verify a key
private getGenerateButton() {
let buttonText = messages.pgettext('wireguard-key-view', 'Generate key');
const regenerateText = messages.pgettext('wireguard-key-view', 'Regenerate key');
let disabled = false;
let generateKey = this.props.onGenerateKey;
switch (this.props.keyState.type) {
case 'key-set': {
buttonText = regenerateText;
const key = this.props.keyState.key;
generateKey = () => this.props.onReplaceKey(key);
break;
}
case 'being-verified':
disabled = true;
buttonText = regenerateText;
break;
case 'being-replaced':
case 'being-generated':
disabled = true;
buttonText = messages.pgettext('wireguard-key-view', 'Generating key');
}
return (
<AppButton.GreenButton disabled={disabled} onClick={generateKey}>
<AppButton.Label>{buttonText}</AppButton.Label>
</AppButton.GreenButton>
);
}
private getKeyText() {
switch (this.props.keyState.type) {
case 'being-verified':
case 'key-set': {
// mimicking the truncating of the key from website
const publicKey = this.props.keyState.key.publicKey;
return (
<StyledRowValue title={this.props.keyState.key.publicKey}>
<ClipboardLabel
value={publicKey}
displayValue={publicKey.substring(0, 20) + '...'}
obscureValue={false}
/>
</StyledRowValue>
);
}
case 'being-replaced':
case 'being-generated':
return <ImageView source="icon-spinner" height={19} width={19} />;
default:
return (
<StyledRowValue>{messages.pgettext('wireguard-key-view', 'No key set')}</StyledRowValue>
);
}
}
private keyValidityLabel() {
const keyStateType = this.props.keyState.type;
if (keyStateType === 'being-verified' && this.state.userHasInitiatedVerification) {
return <ImageView source="icon-spinner" height={20} width={20} />;
} else if (this.props.keyState.type === 'key-set') {
const valid = this.props.keyState.key.valid;
const show = this.state.userHasInitiatedVerification || valid === false;
return show && valid !== undefined ? (
<StyledMessage success={valid}>
{valid
? messages.pgettext('wireguard-key-view', 'Key is valid')
: messages.pgettext('wireguard-key-view', 'Key is invalid')}
</StyledMessage>
) : null;
} else {
return null;
}
}
private static ageOfKeyString(keyState: WgKeyState): string {
switch (keyState.type) {
case 'key-set':
case 'being-verified': {
const createdDate = Math.min(Date.parse(keyState.key.created), Date.now());
return formatRelativeDate(new Date(), createdDate, true);
}
default:
return '-';
}
}
private setAgeOfKeyStringState = () => {
this.setState({
ageOfKeyString: WireguardKeys.ageOfKeyString(this.props.keyState),
});
};
private getStatusMessage() {
if (this.props.isOffline && this.state.recentlyGeneratedKey) {
return (
<StyledMessage success={this.state.recentlyGeneratedKey}>
{messages.pgettext('wireguard-key-view', 'Reconnecting with new WireGuard key...')}
</StyledMessage>
);
} else {
let message = '';
switch (this.props.keyState.type) {
case 'key-set': {
const key = this.props.keyState.key;
if (key.replacementFailure) {
switch (key.replacementFailure) {
case 'too_many_keys':
message = this.formatKeygenFailure('too-many-keys');
break;
case 'generation_failure':
message = this.formatKeygenFailure('generation-failure');
break;
}
} else if (key.verificationFailed) {
message = messages.pgettext('wireguard-key-view', 'Key verification failed');
}
break;
}
case 'too-many-keys':
case 'generation-failure':
message = this.formatKeygenFailure(this.props.keyState.type);
break;
}
return <StyledMessage success={false}>{message}</StyledMessage>;
}
}
private formatKeygenFailure(failure: 'too-many-keys' | 'generation-failure'): string {
switch (failure) {
case 'too-many-keys':
// TRANSLATORS: "%(manage)" is replaced with the text in the "Manage keys" button.
return sprintf(
messages.pgettext(
'wireguard-key-view',
'Unable to regenerate key: you already have the maximum number of keys. To generate a new key, you first need to revoke one under “Manage keys.”',
),
{ manage: messages.pgettext('wireguard-key-view', 'Manage keys') },
);
case 'generation-failure':
return messages.pgettext('wireguard-key-view', 'Failed to generate a key');
default:
return failure;
}
}
}
|