Skip to content

Commit

Permalink
refactor(client): more config cleanup and consolidation (#2265)
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna authored Nov 11, 2024
1 parent 25559c4 commit e96c781
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 391 deletions.
2 changes: 1 addition & 1 deletion client/electron/go_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import {pathToEmbeddedTun2socksBinary} from './app_paths';
import {ChildProcessHelper} from './process';
import {TransportConfigJson} from '../src/www/app/outline_server_repository/vpn';
import {TransportConfigJson} from '../src/www/app/outline_server_repository/config';

/**
* Verifies the UDP connectivity of the server specified in `config`.
Expand Down
6 changes: 2 additions & 4 deletions client/electron/go_vpn_tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import {checkUDPConnectivity} from './go_helpers';
import {ChildProcessHelper, ProcessTerminatedSignalError} from './process';
import {RoutingDaemon} from './routing_service';
import {VpnTunnel} from './vpn_tunnel';
import {
TransportConfigJson,
TunnelStatus,
} from '../src/www/app/outline_server_repository/vpn';
import {TransportConfigJson} from '../src/www/app/outline_server_repository/config';
import {TunnelStatus} from '../src/www/app/outline_server_repository/vpn';

const isLinux = platform() === 'linux';
const isWindows = platform() === 'win32';
Expand Down
10 changes: 4 additions & 6 deletions client/electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ import {GoVpnTunnel} from './go_vpn_tunnel';
import {installRoutingServices, RoutingDaemon} from './routing_service';
import {TunnelStore} from './tunnel_store';
import {VpnTunnel} from './vpn_tunnel';
import * as config from '../src/www/app/outline_server_repository/config';
import {
getHostFromTransportConfig,
setTransportConfigHost,
StartRequestJson,
TunnelConfigJson,
TunnelStatus,
} from '../src/www/app/outline_server_repository/vpn';
import * as errors from '../src/www/model/errors';
Expand Down Expand Up @@ -346,22 +344,22 @@ async function tearDownAutoLaunch() {
// Factory function to create a VPNTunnel instance backed by a network stack
// specified at build time.
async function createVpnTunnel(
tunnelConfig: TunnelConfigJson,
tunnelConfig: config.TunnelConfigJson,
isAutoConnect: boolean
): Promise<VpnTunnel> {
// We must convert the host from a potential "hostname" to an "IP" address
// because startVpn will add a routing table entry that prefixed with this
// host (e.g. "<host>/32"), therefore <host> must be an IP address.
// TODO: make sure we resolve it in the native code
const host = getHostFromTransportConfig(tunnelConfig.transport);
const host = config.getHostFromTransportConfig(tunnelConfig.transport);
if (!host) {
throw new errors.IllegalServerConfiguration('host is missing');
}
const hostIp = await lookupIp(host);
const routing = new RoutingDaemon(hostIp || '', isAutoConnect);
// Make sure the transport will use the IP we will allowlist.
const resolvedTransport =
setTransportConfigHost(tunnelConfig.transport, hostIp) ??
config.setTransportConfigHost(tunnelConfig.transport, hostIp) ??
tunnelConfig.transport;
const tunnel = new GoVpnTunnel(routing, resolvedTransport);
routing.onNetworkChange = tunnel.networkChanged.bind(tunnel);
Expand Down
4 changes: 3 additions & 1 deletion client/src/www/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {Clipboard} from './clipboard';
import {EnvironmentVariables} from './environment';
import {localizeErrorCode} from './error_localizer';
import {OutlineServerRepository} from './outline_server_repository';
import * as config from './outline_server_repository/config';
import {Settings, SettingsKey} from './settings';
import {Updater} from './updater';
import {UrlInterceptor} from './url_interceptor';
Expand Down Expand Up @@ -226,6 +227,7 @@ export class App {

this.eventQueue.startPublishing();

this.rootEl.$.addServerView.validateAccessKey = config.validateAccessKey;
if (!this.arePrivacyTermsAcked()) {
this.displayPrivacyView();
} else if (this.rootEl.$.serversView.shouldShowZeroState) {
Expand Down Expand Up @@ -469,7 +471,7 @@ export class App {
}
}
try {
this.serverRepo.validateAccessKey(accessKey);
config.validateAccessKey(accessKey);
addServerView.accessKey = accessKey;
addServerView.open = true;
} catch (e) {
Expand Down
67 changes: 0 additions & 67 deletions client/src/www/app/outline_server_repository/access_key.ts

This file was deleted.

135 changes: 135 additions & 0 deletions client/src/www/app/outline_server_repository/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2024 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {makeConfig, SIP002_URI} from 'ShadowsocksConfig';

import * as config from './config';

describe('getAddressFromTransport', () => {
it('extracts address', () => {
expect(
config.getAddressFromTransportConfig({host: 'example.com', port: '443'})
).toEqual('example.com:443');
expect(
config.getAddressFromTransportConfig({host: '1:2::3', port: '443'})
).toEqual('[1:2::3]:443');
expect(config.getAddressFromTransportConfig({host: 'example.com'})).toEqual(
'example.com'
);
expect(config.getAddressFromTransportConfig({host: '1:2::3'})).toEqual(
'1:2::3'
);
});

it('fails on invalid config', () => {
expect(config.getAddressFromTransportConfig({})).toBeUndefined();
});
});

describe('getHostFromTransport', () => {
it('extracts host', () => {
expect(
config.getHostFromTransportConfig({host: 'example.com', port: '443'})
).toEqual('example.com');
expect(
config.getHostFromTransportConfig({host: '1:2::3', port: '443'})
).toEqual('1:2::3');
});

it('fails on invalid config', () => {
expect(config.getHostFromTransportConfig({})).toBeUndefined();
});
});

describe('setTransportHost', () => {
it('sets host', () => {
expect(
JSON.stringify(
config.setTransportConfigHost(
{host: 'example.com', port: '443'},
'1.2.3.4'
)
)
).toEqual('{"host":"1.2.3.4","port":"443"}');
expect(
JSON.stringify(
config.setTransportConfigHost(
{host: 'example.com', port: '443'},
'1:2::3'
)
)
).toEqual('{"host":"1:2::3","port":"443"}');
expect(
JSON.stringify(
config.setTransportConfigHost({host: '1.2.3.4', port: '443'}, '1:2::3')
)
).toEqual('{"host":"1:2::3","port":"443"}');
});

it('fails on invalid config', () => {
expect(config.setTransportConfigHost({}, '1:2::3')).toBeUndefined();
});
});

describe('parseTunnelConfig', () => {
it('parse correctly', () => {
expect(
config.parseTunnelConfig(
'{"server": "example.com", "server_port": 443, "method": "METHOD", "password": "PASSWORD"}'
)
).toEqual({
transport: {
host: 'example.com',
port: 443,
method: 'METHOD',
password: 'PASSWORD',
},
});
});

it('parse prefix', () => {
expect(
config.parseTunnelConfig(
'{"server": "example.com", "server_port": 443, "method": "METHOD", "password": "PASSWORD", "prefix": "POST "}'
)
).toEqual({
transport: {
host: 'example.com',
port: 443,
method: 'METHOD',
password: 'PASSWORD',
prefix: 'POST ',
},
});
});

it('parse URL', () => {
const ssUrl = SIP002_URI.stringify(
makeConfig({
host: 'example.com',
port: 443,
method: 'chacha20-ietf-poly1305',
password: 'PASSWORD',
})
);
expect(config.parseTunnelConfig(ssUrl)).toEqual({
transport: {
host: 'example.com',
port: 443,
method: 'chacha20-ietf-poly1305',
password: 'PASSWORD',
},
});
});
});
Loading

0 comments on commit e96c781

Please sign in to comment.