Skip to content

Commit

Permalink
include shared library in the app
Browse files Browse the repository at this point in the history
  • Loading branch information
jyyi1 committed Nov 13, 2024
1 parent 3c607ed commit 10fa95e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
11 changes: 11 additions & 0 deletions client/electron/app_paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ export function pathToEmbeddedTun2socksBinary() {
);
}

export function pathToBackendLibrary() {
return path.join(
unpackedAppPath(),
'client',
'output',
'build',
isWindows ? 'windows' : 'linux',
isWindows ? 'backend.dll' : 'libbackend.so'
);
}

/**
* Get the parent directory path containing the background service binaries.
* On Windows, this folder contains `OutlineService.exe`.
Expand Down
15 changes: 9 additions & 6 deletions client/electron/go_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

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

Expand Down Expand Up @@ -66,13 +67,15 @@ export async function checkUDPConnectivity(
* @returns A Promise that resolves to the fetched content as a string.
* @throws ProcessTerminatedExitCodeError if tun2socks failed to run.
*/
export function fetchResource(
export async function fetchResource(
url: string,
debugMode: boolean = false

Check failure on line 72 in client/electron/go_helpers.ts

View workflow job for this annotation

GitHub Actions / Lint

'debugMode' is assigned a value but never used. Allowed unused vars must match /^_/u

Check failure on line 72 in client/electron/go_helpers.ts

View workflow job for this annotation

GitHub Actions / Lint

'debugMode' is assigned a value but never used. Allowed unused vars must match /^_/u
): Promise<string> {
const tun2socks = new ChildProcessHelper(pathToEmbeddedTun2socksBinary());
tun2socks.isDebugModeEnabled = debugMode;

console.debug('[tun2socks] - fetching resource ...');
return tun2socks.launch(['-fetchUrl', url]);
console.debug('[tun2socks] - preparing library calls ...');
const result = await goFetchResource(url);
console.debug('[tun2socks] - result: ', result);
if (result.Error) {
throw new Error(`Returned error handle: ${result.Error}`);
}
return result.Content;
}
57 changes: 57 additions & 0 deletions client/electron/go_plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 {promisify} from 'node:util';

Check failure on line 15 in client/electron/go_plugin.ts

View workflow job for this annotation

GitHub Actions / Lint

There should be at least one empty line between import groups
import koffi from 'koffi';

Check failure on line 16 in client/electron/go_plugin.ts

View workflow job for this annotation

GitHub Actions / Lint

There should be at least one empty line between import groups
import {pathToBackendLibrary} from './app_paths';

export type GoPlatformErrorHandle = number;

export interface GoFetchResourceResult {
Content: string;
Error: GoPlatformErrorHandle;
}

var goFetchResourceFunc: koffi.KoffiFunction | undefined;

Check failure on line 26 in client/electron/go_plugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected var, use let or const instead

export function goFetchResource(url: string): Promise<GoFetchResourceResult> {
if (!goFetchResourceFunc) {
const lib = ensureBackendLibraryLoaded();
const resultStruct = koffi.struct('FetchResourceResult', {
Content: 'CStr',
Error: 'GoPlatformErrorHandle',
});
goFetchResourceFunc = lib.func('FetchResource', resultStruct, ['str']);
}
return promisify(goFetchResourceFunc.async)(url);
}

var backendLib: koffi.IKoffiLib | undefined;

Check failure on line 40 in client/electron/go_plugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected var, use let or const instead

function ensureBackendLibraryLoaded(): koffi.IKoffiLib {
if (!backendLib) {
backendLib = koffi.load(pathToBackendLibrary());
defineCommonFunctions(backendLib);
}
return backendLib;
}

var goStr: koffi.IKoffiCType | undefined;

Check failure on line 50 in client/electron/go_plugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected var, use let or const instead

Check failure on line 50 in client/electron/go_plugin.ts

View workflow job for this annotation

GitHub Actions / Lint

'goStr' is assigned a value but never used. Allowed unused vars must match /^_/u

Check failure on line 50 in client/electron/go_plugin.ts

View workflow job for this annotation

GitHub Actions / Lint

'goStr' is assigned a value but never used. Allowed unused vars must match /^_/u
var goFreeString: koffi.KoffiFunction | undefined;

Check failure on line 51 in client/electron/go_plugin.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected var, use let or const instead

function defineCommonFunctions(lib: koffi.IKoffiLib) {
goFreeString = lib.func('FreeString', koffi.types.void, [koffi.types.str]);
goStr = koffi.disposable('CStr', koffi.types.str, goFreeString);
koffi.alias('GoPlatformErrorHandle', koffi.types.uintptr_t);
}

0 comments on commit 10fa95e

Please sign in to comment.