Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cordova/apple/ios): remove reachability test #1663

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class OutlineVpn: NSObject {
static let restart = "restart"
static let stop = "stop"
static let getTunnelId = "getTunnelId"
static let isServerReachable = "isServerReachable"
}

private enum MessageKey {
Expand Down Expand Up @@ -109,30 +108,6 @@ public class OutlineVpn: NSObject {
stopVpn()
}

// Determines whether a server is reachable via TCP.
public func isServerReachable(host: String, port: UInt16, _ completion: @escaping Callback) {
if isVpnConnected() {
// All the device's traffic, including the Outline app, go through the VpnExtension process.
// Performing a reachability test, opening a TCP socket to a host/port, will succeed
// unconditionally as the request will not leave the device. Send a message to the
// VpnExtension process to perform the reachability test.
let message = [MessageKey.action: Action.isServerReachable, MessageKey.host: host,
MessageKey.port: port] as [String : Any]
sendVpnExtensionMessage(message) { response in
guard response != nil else {
return completion(ErrorCode.serverUnreachable)
}
let rawCode = response?[MessageKey.errorCode] as? Int ?? ErrorCode.serverUnreachable.rawValue
completion(ErrorCode(rawValue: rawCode) ?? ErrorCode.serverUnreachable)
}
} else {
DispatchQueue.global(qos: .background).async {
let isReachable = ShadowsocksCheckServerReachable(host, Int(port), nil)
completion(isReachable ? ErrorCode.noError : ErrorCode.serverUnreachable)
}
}
}

// Calls |observer| when the VPN's status changes.
public func onVpnStatusChange(_ observer: @escaping(VpnStatusObserver)) {
vpnStatusObserver = observer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
NSString *const kActionRestart = @"restart";
NSString *const kActionStop = @"stop";
NSString *const kActionGetTunnelId = @"getTunnelId";
NSString *const kActionIsServerReachable = @"isServerReachable";
NSString *const kMessageKeyAction = @"action";
NSString *const kMessageKeyTunnelId = @"tunnelId";
NSString *const kMessageKeyConfig = @"config";
Expand Down Expand Up @@ -226,19 +225,6 @@ - (void)handleAppMessage:(NSData *)messageData completionHandler:(void (^)(NSDat
error:nil];
}
completionHandler(response);
} else if ([kActionIsServerReachable isEqualToString:action]) {
NSString *host = message[kMessageKeyHost];
NSNumber *port = message[kMessageKeyPort];
if (!host || !port) {
completionHandler(nil);
return;
}
ErrorCode errorCode = noError;
if (!ShadowsocksCheckServerReachable(host, [port intValue], nil)) {
errorCode = serverUnreachable;
}
NSDictionary *response = @{kMessageKeyErrorCode : [NSNumber numberWithLong:errorCode]};
completionHandler([NSJSONSerialization dataWithJSONObject:response options:kNilOptions error:nil]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,4 @@ final class OutlineTunnelTest: XCTestCase {

XCTAssertEqual(["1.1.1.1", "9.9.9.9", "208.67.222.222", "208.67.220.220"], settings.dnsSettings?.servers)
}

func testReachability() {
// TODO(fortuna): run a local server instead.
XCTAssertTrue(ShadowsocksCheckServerReachable("8.8.8.8", 853, nil))
XCTAssertTrue(ShadowsocksCheckServerReachable("google.com", 443, nil))
XCTAssertFalse(ShadowsocksCheckServerReachable("nonexistent.getoutline.org", 443, nil))
}
}
13 changes: 0 additions & 13 deletions src/cordova/plugin/apple/src/OutlinePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,6 @@ class OutlinePlugin: CDVPlugin {
sendSuccess(OutlineVpn.shared.isActive(tunnelId), callbackId: command.callbackId)
}

func isServerReachable(_ command: CDVInvokedUrlCommand) {
DDLogInfo("isServerReachable")
guard let host = command.argument(at: 0) as? String else {
return sendError("Missing host address" , callbackId: command.callbackId)
}
guard let port = command.argument(at: 1) as? UInt16 else {
return sendError("Missing host port", callbackId: command.callbackId)
}
OutlineVpn.shared.isServerReachable(host: host, port: port) { errorCode in
self.sendSuccess(errorCode == OutlineVpn.ErrorCode.noError, callbackId: command.callbackId)
}
}

func onStatusChange(_ command: CDVInvokedUrlCommand) {
guard let tunnelId = command.argument(at: 0) as? String else {
return sendError("Missing tunnel ID", callbackId: command.callbackId)
Expand Down
76 changes: 0 additions & 76 deletions src/electron/connectivity.ts

This file was deleted.

21 changes: 1 addition & 20 deletions src/electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import * as process from 'process';
import * as url from 'url';
import autoLaunch = require('auto-launch'); // tslint:disable-line

import * as connectivity from './connectivity';
import * as errors from '../www/model/errors';

import {ShadowsocksSessionConfig} from '../www/app/tunnel';
Expand Down Expand Up @@ -72,8 +71,6 @@ const enum Options {
AUTOSTART = '--autostart',
}

const REACHABILITY_TIMEOUT_MS = 10000;

let currentTunnel: VpnTunnel | undefined;

/**
Expand Down Expand Up @@ -452,17 +449,7 @@ function main() {
mainWindow?.webContents.send('outline-ipc-push-clipboard');
});

// TODO: refactor channel name and namespace to a constant
ipcMain.handle('outline-ipc-is-server-reachable', async (_, args: {hostname: string; port: number}) => {
try {
await connectivity.isServerReachable(args.hostname || '', args.port || 0, REACHABILITY_TIMEOUT_MS);
return true;
} catch {
return false;
}
});

// Connects to the specified server, if that server is reachable and the credentials are valid.
// Connects to the specified server.
// TODO: refactor channel name and namespace to a constant
ipcMain.handle(
'outline-ipc-start-proxying',
Expand All @@ -479,12 +466,6 @@ function main() {
console.log(`connecting to ${args.id}...`);

try {
// Rather than repeadedly resolving a hostname in what may be a fingerprint-able way,
// resolve it just once, upfront.
args.config.host = await connectivity.lookupIp(args.config.host || '');

await connectivity.isServerReachable(args.config.host || '', args.config.port || 0, REACHABILITY_TIMEOUT_MS);

await startVpn(args.config, args.id);
console.log(`connected to ${args.id}`);
await setupAutoLaunch(args);
Expand Down
13 changes: 2 additions & 11 deletions src/www/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,17 +622,8 @@
private async syncServerConnectivityState(server: Server) {
try {
const isRunning = await server.checkRunning();
if (!isRunning) {
this.updateServerListItem(server.id, {connectionState: ServerConnectionState.DISCONNECTED});
return;
}
const isReachable = await server.checkReachable();
if (isReachable) {
this.updateServerListItem(server.id, {connectionState: ServerConnectionState.CONNECTED});
} else {
console.log(`Server ${server.id} reconnecting`);
this.updateServerListItem(server.id, {connectionState: ServerConnectionState.RECONNECTING});
}
const connectionState = isRunning ? ServerConnectionState.CONNECTED : ServerConnectionState.DISCONNECTED;
this.updateServerListItem(server.id, {connectionState});

Check warning on line 626 in src/www/app/app.ts

View check run for this annotation

Codecov / codecov/patch

src/www/app/app.ts#L625-L626

Added lines #L625 - L626 were not covered by tests
} catch (e) {
console.error('Failed to sync server connectivity state', e);
}
Expand Down
12 changes: 0 additions & 12 deletions src/www/app/cordova_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ import * as Sentry from '@sentry/browser';
import {AbstractClipboard} from './clipboard';
import {EnvironmentVariables} from './environment';
import {SentryErrorReporter} from './error_reporter';
import {FakeNativeNetworking} from './fake_net';
import {main} from './main';
import * as errors from '../model/errors';
import {NativeNetworking} from './net';
import {OutlinePlatform} from './platform';
import {Tunnel, TunnelStatus} from './tunnel';
import {AbstractUpdater} from './updater';
Expand Down Expand Up @@ -88,12 +86,6 @@ class CordovaErrorReporter extends SentryErrorReporter {
}
}

class CordovaNativeNetworking implements NativeNetworking {
async isServerReachable(hostname: string, port: number) {
return await pluginExecWithErrorCode<boolean>('isServerReachable', hostname, port);
}
}

class CordovaTunnel implements Tunnel {
constructor(public id: string) {}

Expand Down Expand Up @@ -131,10 +123,6 @@ class CordovaPlatform implements OutlinePlatform {
return !CordovaPlatform.isBrowser();
}

getNativeNetworking() {
return this.hasDeviceSupport() ? new CordovaNativeNetworking() : new FakeNativeNetworking();
}

getTunnelFactory() {
return (id: string) => {
return this.hasDeviceSupport() ? new CordovaTunnel(id) : new FakeOutlineTunnel(id);
Expand Down
11 changes: 0 additions & 11 deletions src/www/app/electron_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ import {ErrorCode, OutlinePluginError} from '../model/errors';
import {AbstractClipboard} from './clipboard';
import {ElectronOutlineTunnel} from './electron_outline_tunnel';
import {getSentryBrowserIntegrations, OutlineErrorReporter} from './error_reporter';
import {FakeNativeNetworking} from './fake_net';
import {FakeOutlineTunnel} from './fake_tunnel';
import {getLocalizationFunction, main} from './main';
import {NativeNetworking} from './net';
import {AbstractUpdater} from './updater';
import {UrlInterceptor} from './url_interceptor';
import {VpnInstaller} from './vpn_installer';
Expand Down Expand Up @@ -102,17 +100,8 @@ class ElectronErrorReporter implements OutlineErrorReporter {
}
}

class ElectronNativeNetworking implements NativeNetworking {
isServerReachable(hostname: string, port: number): Promise<boolean> {
return window.electron.methodChannel.invoke('is-server-reachable', {hostname, port});
}
}

main({
hasDeviceSupport: () => isOsSupported,
getNativeNetworking: () => {
return isOsSupported ? new ElectronNativeNetworking() : new FakeNativeNetworking();
},
getTunnelFactory: () => {
return (id: string) => {
return isOsSupported ? new ElectronOutlineTunnel(id) : new FakeOutlineTunnel(id);
Expand Down
21 changes: 0 additions & 21 deletions src/www/app/fake_net.ts

This file was deleted.

5 changes: 1 addition & 4 deletions src/www/app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {EventQueue} from '../model/events';

import {App} from './app';
import {onceEnvVars} from './environment';
import {NativeNetworking} from './net';
import {OutlineServerRepository} from './outline_server_repository';
import {makeConfig, SIP002_URI} from 'ShadowsocksConfig';
import {OutlinePlatform} from './platform';
Expand Down Expand Up @@ -52,10 +51,9 @@ function createServerRepo(
eventQueue: EventQueue,
storage: Storage,
deviceSupport: boolean,
net: NativeNetworking,
createTunnel: TunnelFactory
) {
const repo = new OutlineServerRepository(net, createTunnel, eventQueue, storage);
const repo = new OutlineServerRepository(createTunnel, eventQueue, storage);
if (!deviceSupport) {
console.debug('Detected development environment, using fake servers.');
if (repo.getAll().length === 0) {
Expand Down Expand Up @@ -107,7 +105,6 @@ export function main(platform: OutlinePlatform) {
eventQueue,
window.localStorage,
platform.hasDeviceSupport(),
platform.getNativeNetworking(),
platform.getTunnelFactory()
);
const settings = new Settings();
Expand Down
19 changes: 0 additions & 19 deletions src/www/app/net.ts

This file was deleted.

Loading
Loading