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(client/ios): move fetching logic from TypeScript to Go #2222

Draft
wants to merge 1 commit into
base: junyi/fetch-in-go-android
Choose a base branch
from
Draft
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
28 changes: 28 additions & 0 deletions client/src/cordova/plugin/apple/src/OutlinePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import OutlineSentryLogger
import OutlineNotification
import OutlineTunnel

import Tun2socks

public enum TunnelStatus: Int {
case connected = 0
case disconnected = 1
Expand Down Expand Up @@ -145,6 +147,27 @@ class OutlinePlugin: CDVPlugin {
}
}

func fetchConfig(_ command: CDVInvokedUrlCommand) {
guard let url = command.argument(at: 0) as? String else {
return sendError("Missing URL", callbackId: command.callbackId)
}
DDLogInfo("Fetching dynamic config from \(url)")
Task {
guard let result = OutlineFetchDynamicKey(url) else {
return self.sendError("unexpected fetching result", callbackId: command.callbackId)
}
if result.error != nil {
var marshalErr: NSError?
var errorJson = PlaterrorsMarshalJSONString(result.error, &marshalErr)
if marshalErr != nil {
errorJson = "error code = \(result.error?.code ?? "?"), failed to fetch details"
}
return self.sendError(errorJson, callbackId: command.callbackId)
}
self.sendSuccess(result.key, callbackId: command.callbackId)
}
}

func onStatusChange(_ command: CDVInvokedUrlCommand) {
DDLogInfo("OutlinePlugin: registering status callback")
if let currentCallbackId = self.statusCallbackId {
Expand Down Expand Up @@ -284,6 +307,11 @@ class OutlinePlugin: CDVPlugin {
send(pluginResult: result, callbackId: callbackId, keepCallback: keepCallback)
}

private func sendSuccess(_ operationResult: String, callbackId: String, keepCallback: Bool = false) {
let result = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: operationResult)
send(pluginResult: result, callbackId: callbackId, keepCallback: keepCallback)
}

private func sendSuccess(_ operationResult: Bool, callbackId: String, keepCallback: Bool = false) {
let result = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: operationResult)
send(pluginResult: result, callbackId: callbackId, keepCallback: keepCallback)
Expand Down
19 changes: 1 addition & 18 deletions client/src/www/app/outline_server_repository/vpn.cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,6 @@ export class CordovaVpnApi implements VpnApi {
}

async fetchDynamicConfig(url: string): Promise<string> {
if (cordova.platformId === 'android') {
return pluginExecWithErrorCode<string>('fetchConfig', url);
} else {
// TODO: move to Go fetch implementation later
let response: Response;
try {
response = await fetch(url, {
cache: 'no-store',
redirect: 'follow',
});
} catch (cause) {
throw new errors.SessionConfigFetchFailed(
'Failed to fetch VPN information from dynamic access key.',
{cause}
);
}
return (await response.text()).trim();
}
return pluginExecWithErrorCode<string>('fetchConfig', url);
}
}
Loading