summaryrefslogtreecommitdiffhomepage
path: root/desktop/packages/mullvad-vpn/src/renderer/components/ApiAccessMethods.tsx
blob: 61779e36d3e6ee604540cb5a3c089a7fdc528bea (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
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
import { useCallback, useMemo } from 'react';
import { sprintf } from 'sprintf-js';
import styled from 'styled-components';

import { AccessMethodSetting } from '../../shared/daemon-rpc-types';
import { messages } from '../../shared/gettext';
import { RoutePath } from '../../shared/routes';
import { useAppContext } from '../context';
import { useApiAccessMethodTest } from '../lib/api-access-methods';
import { Button, Container, Flex, Spinner } from '../lib/components';
import { Switch } from '../lib/components/switch';
import { colors, spacings } from '../lib/foundations';
import { useHistory } from '../lib/history';
import { generateRoutePath } from '../lib/routeHelpers';
import { useBoolean } from '../lib/utility-hooks';
import { useSelector } from '../redux/store';
import { AppNavigationHeader } from './';
import * as Cell from './cell';
import {
  ContextMenu,
  ContextMenuContainer,
  ContextMenuItem,
  ContextMenuTrigger,
} from './ContextMenu';
import InfoButton from './InfoButton';
import { BackAction } from './KeyboardNavigation';
import { Layout, SettingsContainer, SettingsContent, SettingsNavigationScrollbars } from './Layout';
import { ModalAlert, ModalAlertType } from './Modal';
import { NavigationContainer } from './NavigationContainer';
import SettingsHeader, { HeaderSubTitle, HeaderTitle } from './SettingsHeader';

const StyledNameLabel = styled(Cell.Label)({
  display: 'block',
  overflow: 'hidden',
  textOverflow: 'ellipsis',
  whiteSpace: 'nowrap',
});

const StyledTestResultCircle = styled.div<{ $result: boolean }>((props) => ({
  width: '10px',
  height: '10px',
  borderRadius: '50%',
  backgroundColor: props.$result ? colors.green : colors.red,
  marginRight: spacings.small,
}));

// This component is the topmost component in the API access methods view.
export default function ApiAccessMethods() {
  const history = useHistory();
  const methods = useSelector((state) => state.settings.apiAccessMethods);
  const currentMethod = useSelector((state) => state.settings.currentApiAccessMethod);

  const navigateToEdit = useCallback(
    (id?: string) => {
      const path = generateRoutePath(RoutePath.editApiAccessMethods, { id });
      history.push(path);
    },
    [history],
  );

  const navigateToNew = useCallback(() => navigateToEdit(), [navigateToEdit]);

  return (
    <BackAction action={history.pop}>
      <Layout>
        <SettingsContainer>
          <NavigationContainer>
            <AppNavigationHeader
              title={
                // TRANSLATORS: Title label in navigation bar
                messages.pgettext('navigation-bar', 'API access')
              }>
              <AppNavigationHeader.InfoButton
                message={[
                  messages.pgettext(
                    'api-access-methods-view',
                    'The app needs to communicate with a Mullvad API server to log you in, fetch server lists, and other critical operations.',
                  ),
                  messages.pgettext(
                    'api-access-methods-view',
                    'On some networks, where various types of censorship are being used, the API servers might not be directly reachable.',
                  ),
                  messages.pgettext(
                    'api-access-methods-view',
                    'This feature allows you to circumvent that censorship by adding custom ways to access the API via proxies and similar methods.',
                  ),
                ]}
              />
            </AppNavigationHeader>

            <SettingsNavigationScrollbars fillContainer>
              <SettingsContent>
                <SettingsHeader>
                  <HeaderTitle>{messages.pgettext('navigation-bar', 'API access')}</HeaderTitle>
                  <HeaderSubTitle>
                    {messages.pgettext(
                      'api-access-methods-view',
                      'Manage and add custom methods to access the Mullvad API.',
                    )}
                  </HeaderSubTitle>
                </SettingsHeader>

                <Flex $flexDirection="column" $gap="large">
                  <Cell.Group $noMarginBottom>
                    <ApiAccessMethod
                      method={methods.direct}
                      inUse={methods.direct.id === currentMethod?.id}
                    />
                    <ApiAccessMethod
                      method={methods.mullvadBridges}
                      inUse={methods.mullvadBridges.id === currentMethod?.id}
                    />
                    <ApiAccessMethod
                      method={methods.encryptedDnsProxy}
                      inUse={methods.encryptedDnsProxy.id === currentMethod?.id}
                    />
                    {methods.custom.map((method) => (
                      <ApiAccessMethod
                        key={method.id}
                        method={method}
                        inUse={method.id === currentMethod?.id}
                        custom
                      />
                    ))}
                  </Cell.Group>
                  <Container size="4" $flex={1} $justifyContent="flex-end">
                    <Button width="fit" onClick={navigateToNew}>
                      <Button.Text>{messages.gettext('Add')}</Button.Text>
                    </Button>
                  </Container>
                </Flex>
              </SettingsContent>
            </SettingsNavigationScrollbars>
          </NavigationContainer>
        </SettingsContainer>
      </Layout>
    </BackAction>
  );
}

interface ApiAccessMethodProps {
  method: AccessMethodSetting;
  inUse: boolean;
  custom?: boolean;
}

function ApiAccessMethod(props: ApiAccessMethodProps) {
  const {
    setApiAccessMethod: setApiAccessMethodImpl,
    updateApiAccessMethod,
    removeApiAccessMethod,
  } = useAppContext();
  const { push } = useHistory();

  const [testing, testResult, testApiAccessMethod] = useApiAccessMethodTest();

  // State for delete confirmation dialog.
  const [removeConfirmationVisible, showRemoveConfirmation, hideRemoveConfirmation] = useBoolean();
  const confirmRemove = useCallback(() => {
    void removeApiAccessMethod(props.method.id);
    hideRemoveConfirmation();
  }, [hideRemoveConfirmation, props.method.id, removeApiAccessMethod]);

  // Toggle on/off on an access method.
  const toggle = useCallback(
    async (value: boolean) => {
      const updatedMethod = cloneMethod(props.method);
      updatedMethod.enabled = value;
      await updateApiAccessMethod(updatedMethod);
    },
    [props.method, updateApiAccessMethod],
  );

  const setApiAccessMethod = useCallback(async () => {
    const reachable = await testApiAccessMethod(props.method.id);
    if (reachable) {
      await setApiAccessMethodImpl(props.method.id);
    }
  }, [testApiAccessMethod, props.method.id, setApiAccessMethodImpl]);

  const menuItems = useMemo<Array<ContextMenuItem>>(() => {
    const items: Array<ContextMenuItem> = [
      {
        type: 'item' as const,
        label: messages.gettext('Use'),
        disabled: props.inUse,
        onClick: setApiAccessMethod,
      },
      {
        type: 'item' as const,
        label: messages.gettext('Test'),
        onClick: () => testApiAccessMethod(props.method.id),
      },
    ];

    // Edit and Delete shouldn't be available for direct, bridges or encrypted DNS proxy.
    if (props.custom) {
      items.push(
        { type: 'separator' as const },
        {
          type: 'item' as const,
          label: messages.gettext('Edit'),
          onClick: () =>
            push(generateRoutePath(RoutePath.editApiAccessMethods, { id: props.method.id })),
        },
        {
          type: 'item' as const,
          label: messages.gettext('Delete'),
          onClick: showRemoveConfirmation,
        },
      );
    }

    return items;
  }, [
    props.inUse,
    props.custom,
    props.method.id,
    setApiAccessMethod,
    testApiAccessMethod,
    showRemoveConfirmation,
    push,
  ]);

  return (
    <Cell.Row data-testid="access-method">
      <Cell.LabelContainer>
        <StyledNameLabel>{props.method.name}</StyledNameLabel>
        {testing && (
          <Cell.SubLabel>
            <Flex $gap="tiny" $alignItems="center">
              <Spinner size="small" />
              {messages.pgettext('api-access-methods-view', 'Testing...')}
            </Flex>
          </Cell.SubLabel>
        )}
        {!testing && testResult !== undefined && (
          <Cell.SubLabel>
            <StyledTestResultCircle $result={testResult} />
            {testResult
              ? messages.pgettext('api-access-methods-view', 'API reachable')
              : messages.pgettext('api-access-methods-view', 'API unreachable')}
          </Cell.SubLabel>
        )}
        {!testing && testResult === undefined && props.inUse && (
          <Cell.SubLabel>{messages.pgettext('api-access-methods-view', 'In use')}</Cell.SubLabel>
        )}
      </Cell.LabelContainer>
      <Flex $gap="small" $alignItems="center">
        {props.method.type === 'direct' && (
          <InfoButton
            message={[
              messages.pgettext(
                'api-access-methods-view',
                'With the “Direct” method, the app communicates with a Mullvad API server directly without any intermediate proxies.',
              ),
              messages.pgettext(
                'api-access-methods-view',
                'This can be useful when you are not affected by censorship.',
              ),
            ]}
          />
        )}
        {props.method.type === 'bridges' && (
          <InfoButton
            message={[
              messages.pgettext(
                'api-access-methods-view',
                'With the “Mullvad bridges” method, the app communicates with a Mullvad API server via a Mullvad bridge server. It does this by sending the traffic obfuscated by Shadowsocks.',
              ),
              messages.pgettext(
                'api-access-methods-view',
                'This can be useful if the API is censored but Mullvad’s bridge servers are not.',
              ),
            ]}
          />
        )}

        {props.method.type === 'encrypted-dns-proxy' && (
          <InfoButton
            message={[
              messages.pgettext(
                'api-access-methods-view',
                'With the “Encrypted DNS proxy” method, the app will communicate with our Mullvad API through a proxy address. It does this by retrieving an address from a DNS over HTTPS (DoH) server and then using that to reach our API servers.',
              ),
              messages.pgettext(
                'api-access-methods-view',
                'If you are not connected to our VPN, then the Encrypted DNS proxy will use your own non-VPN IP when connecting. The DoH servers are hosted by one of the following providers: Quad9 or CloudFlare.',
              ),
            ]}
          />
        )}
        <ContextMenuContainer>
          <ContextMenuTrigger />
          <ContextMenu items={menuItems} align="right" />
        </ContextMenuContainer>
        <Switch checked={props.method.enabled} onCheckedChange={toggle}>
          <Switch.Trigger>
            <Switch.Thumb />
          </Switch.Trigger>
        </Switch>
      </Flex>

      {/* Confirmation dialog for method removal */}
      <ModalAlert
        isOpen={removeConfirmationVisible}
        type={ModalAlertType.warning}
        gridButtons={[
          <Button key="cancel" onClick={hideRemoveConfirmation}>
            <Button.Text>{messages.gettext('Cancel')}</Button.Text>
          </Button>,
          <Button key="confirm" onClick={confirmRemove} variant="destructive">
            <Button.Text>{messages.gettext('Delete')}</Button.Text>
          </Button>,
        ]}
        close={hideRemoveConfirmation}
        title={sprintf(messages.pgettext('api-access-methods-view', 'Delete %(name)s?'), {
          name: props.method.name,
        })}
        message={
          props.inUse
            ? messages.pgettext(
                'api-access-methods-view',
                'The in use API access method will change.',
              )
            : undefined
        }
      />
    </Cell.Row>
  );
}

function cloneMethod<T extends AccessMethodSetting>(method: T): T {
  const clonedMethod = {
    ...method,
  };

  if (
    method.type === 'socks5-remote' &&
    clonedMethod.type === 'socks5-remote' &&
    method.authentication !== undefined
  ) {
    clonedMethod.authentication = { ...method.authentication };
  }

  return clonedMethod;
}