Skip to content

Commit

Permalink
Initial MethodChannel wiring
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna committed Dec 2, 2024
1 parent b0c3d25 commit d5047ba
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
25 changes: 24 additions & 1 deletion client/src/www/app/main.cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@ import * as Sentry from '@sentry/browser';
import {AbstractClipboard} from './clipboard';
import {EnvironmentVariables} from './environment';
import {main} from './main';
import {installDefaultMethodChannel, MethodChannel} from './method_channel';
import {VpnApi} from './outline_server_repository/vpn';
import {CordovaVpnApi} from './outline_server_repository/vpn.cordova';
import {OutlinePlatform} from './platform';
import {OUTLINE_PLUGIN_NAME, pluginExec} from './plugin.cordova';
import {
OUTLINE_PLUGIN_NAME,
pluginExec,
pluginExecWithErrorCode,
} from './plugin.cordova';
import {ResourceFetcher} from './resource_fetcher';
import {CordovaResourceFetcher} from './resource_fetcher.cordova';
import {AbstractUpdater} from './updater';
import * as interceptors from './url_interceptor';
import {NoOpVpnInstaller, VpnInstaller} from './vpn_installer';
import {SentryErrorReporter, Tags} from '../shared/error_reporter';
import { legacyParseTunnelConfig } from './outline_server_repository/config';
import { OutlinePluginError } from '../model/errors';

const hasDeviceSupport = cordova.platformId !== 'browser';

Expand Down Expand Up @@ -71,6 +78,21 @@ class CordovaErrorReporter extends SentryErrorReporter {
}
}

class CordovaMethodChannel implements MethodChannel {
async invokeMethod(methodName: string, ...args: string[]): Promise<unknown> {
switch (methodName) {
case 'parseTunnelConfig':
if (typeof args?.[0] === 'string') {
return legacyParseTunnelConfig(args[0]);
} else {
throw new Error(`invalid arguments for parseTunnelConfig ${args}`);
}
default:
return await pluginExecWithErrorCode(methodName, args);
}
}
}

// This class should only be instantiated after Cordova fires the deviceready event.
class CordovaPlatform implements OutlinePlatform {
getVpnApi(): VpnApi | undefined {
Expand Down Expand Up @@ -148,5 +170,6 @@ window.handleOpenURL = (url: string) => {
};

onceDeviceReady.then(() => {
installDefaultMethodChannel(new CordovaMethodChannel());
main(new CordovaPlatform());
});
19 changes: 19 additions & 0 deletions client/src/www/app/main.electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as Sentry from '@sentry/electron/renderer';

import {AbstractClipboard} from './clipboard';
import {getLocalizationFunction, main} from './main';
import {installDefaultMethodChannel, MethodChannel} from './method_channel';
import {VpnApi} from './outline_server_repository/vpn';
import {ElectronVpnApi} from './outline_server_repository/vpn.electron';
import {ElectronResourceFetcher} from './resource_fetcher.electron';
Expand All @@ -33,6 +34,7 @@ import {
OutlineErrorReporter,
Tags,
} from '../shared/error_reporter';
import { legacyParseTunnelConfig } from './outline_server_repository/config';

const isWindows = window.electron.os.platform === 'win32';
const isLinux = window.electron.os.platform === 'linux';
Expand Down Expand Up @@ -127,6 +129,23 @@ class ElectronErrorReporter implements OutlineErrorReporter {
}
}

class ElectronMethodChannel implements MethodChannel {
async invokeMethod(methodName: string, ...args: string[]): Promise<unknown> {
switch (methodName) {
case 'parseTunnelConfig':
if (typeof args?.[0] === 'string') {
return legacyParseTunnelConfig(args[0]);
} else {
throw new Error(`invalid arguments for parseTunnelConfig ${args}`);
}
default:
return await window.electron.methodChannel.invoke(methodName, args);
}
}
}

installDefaultMethodChannel(new ElectronMethodChannel());

main({
getVpnApi(): VpnApi | undefined {
if (isOsSupported) {
Expand Down
29 changes: 29 additions & 0 deletions client/src/www/app/method_channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.

export interface MethodChannel {
invokeMethod(methodName: string, ...args: string[]): Promise<unknown>;
}

let defaultMethodChannel: MethodChannel;

export function installDefaultMethodChannel(
methodChannel: MethodChannel
): void {
defaultMethodChannel = methodChannel;
}

export function getDefaultMethodChannel(): MethodChannel {
return defaultMethodChannel;
}
7 changes: 6 additions & 1 deletion client/src/www/app/outline_server_repository/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import {SHADOWSOCKS_URI} from 'ShadowsocksConfig';

import * as errors from '../../model/errors';
import { getDefaultMethodChannel } from '../method_channel';

export const TEST_ONLY = {
getAddressFromTransportConfig: getAddressFromTransportConfig,
Expand Down Expand Up @@ -109,7 +110,11 @@ export function setTransportConfigHost(
* This is used by the server to parse the config fetched from the dynamic key, and to parse
* static keys as tunnel configs (which may be present in the dynamic config).
*/
export function parseTunnelConfig(
export async function parseTunnelConfig(tunnelConfigText: string): TunnelConfigJson | null {
return await getDefaultMethodChannel().invokeMethod("parseTunnelConfig", tunnelConfigText);
}

export function legacyParseTunnelConfig(
tunnelConfigText: string
): TunnelConfigJson | null {
tunnelConfigText = tunnelConfigText.trim();
Expand Down

0 comments on commit d5047ba

Please sign in to comment.