Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna committed Dec 3, 2024
1 parent d5047ba commit 5c51517
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 165 deletions.
8 changes: 4 additions & 4 deletions client/electron/go_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import {pathToBackendLibrary} from './app_paths';

let invokeGoAPIFunc: Function | undefined;

export type GoApiName = 'FetchResource';
export type GoApiName = string;

/**
* Calls a Go function by invoking the `InvokeGoAPI` function in the native backend library.
*
* @param api The name of the Go API to invoke.
* @param method The name of the Go method to invoke.
* @param input The input string to pass to the API.
* @returns A Promise that resolves to the output string returned by the API.
* @throws An Error containing PlatformError details if the API call fails.
Expand All @@ -35,7 +35,7 @@ export type GoApiName = 'FetchResource';
* in `./client/go/outline/electron/go_plugin.go`.
*/
export async function invokeGoApi(
api: GoApiName,
method: GoApiName,
input: string
): Promise<string> {
if (!invokeGoAPIFunc) {
Expand All @@ -59,7 +59,7 @@ export async function invokeGoApi(
}

console.debug('[Backend] - calling InvokeGoAPI ...');
const result = await invokeGoAPIFunc(api, input);
const result = await invokeGoAPIFunc(method, input);
console.debug('[Backend] - InvokeGoAPI returned', result);
if (result.ErrorJson) {
throw Error(result.ErrorJson);
Expand Down
16 changes: 16 additions & 0 deletions client/electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,22 @@ function main() {
invokeGoApi(api, input)
);

// This IPC handler allows the renderer process to call functions exposed by the backend.
// It takes two arguments:
// - method: The name of the method to call.
// - params: A string representing the input data to the function.
//
// The handler returns the output string from the Go function if successful.
// Both the input string and output string need to be interpreted by the renderer process according
// to the specific API being called.
// If the function encounters an error, it throws an Error that can be parsed by the `PlatformError`.
ipcMain.handle(
'outline-ipc-method-call',
(_, method: string, params: string): Promise<string> => {
return invokeGoApi(method, params);
}
);

// Connects to a proxy server specified by a config.
//
// If any issues occur, an Error will be thrown, which you can try-catch around
Expand Down
23 changes: 7 additions & 16 deletions client/src/www/app/main.cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ import {
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';
import {legacyParseTunnelConfig} from './outline_server_repository/config';
import {OutlinePluginError} from '../model/errors';

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

Expand Down Expand Up @@ -79,16 +77,13 @@ class CordovaErrorReporter extends SentryErrorReporter {
}

class CordovaMethodChannel implements MethodChannel {
async invokeMethod(methodName: string, ...args: string[]): Promise<unknown> {
async invokeMethod(methodName: string, params: string): Promise<string> {
switch (methodName) {
case 'parseTunnelConfig':
if (typeof args?.[0] === 'string') {
return legacyParseTunnelConfig(args[0]);
} else {
throw new Error(`invalid arguments for parseTunnelConfig ${args}`);
}
case 'fetchResource':
// TODO(fortuna): wire generic calls in the Cordova plugin.
return pluginExecWithErrorCode<string>('fetchResource', params);
default:
return await pluginExecWithErrorCode(methodName, args);
return await pluginExecWithErrorCode(methodName, params);
}
}
}
Expand Down Expand Up @@ -139,10 +134,6 @@ class CordovaPlatform implements OutlinePlatform {
return new NoOpVpnInstaller();
}

getResourceFetcher(): ResourceFetcher {
return new CordovaResourceFetcher();
}

quitApplication() {
// Only used in macOS because menu bar apps provide no alternative way of quitting.
cordova.exec(
Expand Down
18 changes: 7 additions & 11 deletions client/src/www/app/main.electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ 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';
import {AbstractUpdater} from './updater';
import {UrlInterceptor} from './url_interceptor';
import {VpnInstaller} from './vpn_installer';
Expand All @@ -34,7 +33,7 @@ import {
OutlineErrorReporter,
Tags,
} from '../shared/error_reporter';
import { legacyParseTunnelConfig } from './outline_server_repository/config';
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 @@ -130,16 +129,14 @@ class ElectronErrorReporter implements OutlineErrorReporter {
}

class ElectronMethodChannel implements MethodChannel {
async invokeMethod(methodName: string, ...args: string[]): Promise<unknown> {
async invokeMethod(methodName: string, params: string): Promise<string> {
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);
return await window.electron.methodChannel.invoke(
'method-call',
methodName,
params
);
}
}
}
Expand All @@ -157,6 +154,5 @@ main({
getErrorReporter: _ => new ElectronErrorReporter(),
getUpdater: () => new ElectronUpdater(),
getVpnServiceInstaller: () => new ElectronVpnInstaller(),
getResourceFetcher: () => new ElectronResourceFetcher(),
quitApplication: () => window.electron.methodChannel.send('quit-app'),
});
7 changes: 2 additions & 5 deletions client/src/www/app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
FakeVpnApi,
} from './outline_server_repository/vpn.fake';
import {OutlinePlatform} from './platform';
import {BrowserResourceFetcher} from './resource_fetcher';
import {Settings} from './settings';
import {EventQueue} from '../model/events';

Expand Down Expand Up @@ -61,8 +60,7 @@ function createServerRepo(platform: OutlinePlatform, eventQueue: EventQueue) {
vpnApi,
eventQueue,
window.localStorage,
localize,
platform.getResourceFetcher()
localize
);
}

Expand All @@ -71,8 +69,7 @@ function createServerRepo(platform: OutlinePlatform, eventQueue: EventQueue) {
new FakeVpnApi(),
eventQueue,
window.localStorage,
localize,
new BrowserResourceFetcher()
localize
);

if (repo.getAll().length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/www/app/method_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

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

let defaultMethodChannel: MethodChannel;
Expand Down
13 changes: 2 additions & 11 deletions client/src/www/app/outline_server_repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {TunnelStatus, VpnApi} from './vpn';
import * as errors from '../../model/errors';
import * as events from '../../model/events';
import {ServerRepository} from '../../model/server';
import {ResourceFetcher} from '../resource_fetcher';

// DEPRECATED: V0 server persistence format.
interface ServersStorageV0Config {
Expand Down Expand Up @@ -71,8 +70,7 @@ export class OutlineServerRepository implements ServerRepository {
private vpnApi: VpnApi,
private eventQueue: events.EventQueue,
private storage: Storage,
private localize: Localizer,
readonly urlFetcher: ResourceFetcher
private localize: Localizer
) {
console.debug('OutlineServerRepository is initializing');
this.loadServers();
Expand Down Expand Up @@ -273,13 +271,6 @@ export class OutlineServerRepository implements ServerRepository {
accessKey: string,
name?: string
): OutlineServer {
return new OutlineServer(
this.vpnApi,
this.urlFetcher,
id,
name,
accessKey,
this.localize
);
return new OutlineServer(this.vpnApi, id, name, accessKey, this.localize);
}
}
14 changes: 6 additions & 8 deletions client/src/www/app/outline_server_repository/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {StartRequestJson, VpnApi} from './vpn';
import * as errors from '../../model/errors';
import {PlatformError} from '../../model/platform_error';
import {Server, ServerType} from '../../model/server';
import {ResourceFetcher} from '../resource_fetcher';
import {getDefaultMethodChannel} from '../method_channel';

// PLEASE DON'T use this class outside of this `outline_server_repository` folder!

Expand All @@ -39,7 +39,6 @@ export class OutlineServer implements Server {

constructor(
private vpnApi: VpnApi,
readonly urlFetcher: ResourceFetcher,
readonly id: string,
public name: string,
readonly accessKey: string,
Expand Down Expand Up @@ -88,10 +87,7 @@ export class OutlineServer implements Server {
async connect() {
let tunnelConfig: TunnelConfigJson;
if (this.type === ServerType.DYNAMIC_CONNECTION) {
tunnelConfig = await fetchTunnelConfig(
this.urlFetcher,
this.tunnelConfigLocation
);
tunnelConfig = await fetchTunnelConfig(this.tunnelConfigLocation);
this.displayAddress = net.joinHostPort(
tunnelConfig.firstHop.host,
tunnelConfig.firstHop.port.toString()
Expand Down Expand Up @@ -147,11 +143,13 @@ export class OutlineServer implements Server {
/** fetchTunnelConfig fetches information from a dynamic access key and attempts to parse it. */
// TODO(daniellacosse): unit tests
async function fetchTunnelConfig(
urlFetcher: ResourceFetcher,
configLocation: URL
): Promise<TunnelConfigJson> {
const responseBody = (
await urlFetcher.fetch(configLocation.toString())
await getDefaultMethodChannel().invokeMethod(
'FetchResource',
configLocation.toString()
)
).trim();
if (!responseBody) {
throw new errors.ServerAccessKeyInvalid(
Expand Down
3 changes: 0 additions & 3 deletions client/src/www/app/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import {Clipboard} from './clipboard';
import {EnvironmentVariables} from './environment';
import {VpnApi} from './outline_server_repository/vpn';
import {ResourceFetcher} from './resource_fetcher';
import {Updater} from './updater';
import {UrlInterceptor} from './url_interceptor';
import {VpnInstaller} from './vpn_installer';
Expand All @@ -36,7 +35,5 @@ export interface OutlinePlatform {

getVpnServiceInstaller(): VpnInstaller;

getResourceFetcher(): ResourceFetcher;

quitApplication(): void;
}
25 changes: 0 additions & 25 deletions client/src/www/app/resource_fetcher.cordova.ts

This file was deleted.

33 changes: 0 additions & 33 deletions client/src/www/app/resource_fetcher.electron.ts

This file was deleted.

48 changes: 0 additions & 48 deletions client/src/www/app/resource_fetcher.ts

This file was deleted.

0 comments on commit 5c51517

Please sign in to comment.