diff --git a/client/src/cordova/plugin/apple/src/OutlinePlugin.swift b/client/src/cordova/plugin/apple/src/OutlinePlugin.swift index de9a79c201..ff72faa444 100644 --- a/client/src/cordova/plugin/apple/src/OutlinePlugin.swift +++ b/client/src/cordova/plugin/apple/src/OutlinePlugin.swift @@ -22,6 +22,8 @@ import OutlineSentryLogger import OutlineNotification import OutlineTunnel +import Tun2socks + public enum TunnelStatus: Int { case connected = 0 case disconnected = 1 @@ -142,6 +144,24 @@ class OutlinePlugin: CDVPlugin { } } + func fetchResource(_ command: CDVInvokedUrlCommand) { + guard let url = command.argument(at: 0) as? String else { + return sendError("Missing URL", callbackId: command.callbackId) + } + DDLogInfo("Fetching resource from \(url)") + Task { + guard let result = OutlineFetchResource(url) else { + return self.sendError("unexpected fetching result", callbackId: command.callbackId) + } + if result.error != nil { + let errorJson = marshalErrorJson(error: OutlineError.platformError(result.error!)) + return self.sendError(errorJson, callbackId: command.callbackId) + } + DDLogInfo("Fetch resource result: \(result.content)") + self.sendSuccess(result.content, callbackId: command.callbackId) + } + } + func onStatusChange(_ command: CDVInvokedUrlCommand) { DDLogInfo("OutlinePlugin: registering status callback") if let currentCallbackId = self.statusCallbackId { @@ -271,41 +291,46 @@ class OutlinePlugin: CDVPlugin { return; // Do not report transient or invalid states. } let result = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: ["id": tunnelId, "status": Int32(tunnelStatus)]) - send(pluginResult: result, callbackId: callbackId, keepCallback: true) + self.send(pluginResult: result, callbackId: callbackId, keepCallback: true) } // MARK: - Callback helpers private func sendSuccess(callbackId: String, keepCallback: Bool = false) { - let result = CDVPluginResult(status: CDVCommandStatus_OK) - send(pluginResult: result, callbackId: callbackId, keepCallback: keepCallback) + let result = CDVPluginResult(status: CDVCommandStatus_OK) + self.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) + self.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) + let result = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: operationResult) + self.send(pluginResult: result, callbackId: callbackId, keepCallback: keepCallback) } private func sendError(_ message: String, callbackId: String, keepCallback: Bool = false) { DDLogWarn("plugin result error: \(message)") let result = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: message) - send(pluginResult: result, callbackId: callbackId, keepCallback: keepCallback) + self.send(pluginResult: result, callbackId: callbackId, keepCallback: keepCallback) } private func send(pluginResult: CDVPluginResult?, callbackId: String, keepCallback: Bool) { - guard let result = pluginResult else { - return DDLogWarn("Missing plugin result"); - } - result.setKeepCallbackAs(keepCallback) - self.commandDelegate?.send(result, callbackId: callbackId) + guard let result = pluginResult else { + return DDLogWarn("Missing plugin result"); + } + result.setKeepCallbackAs(keepCallback) + self.commandDelegate?.send(result, callbackId: callbackId) } private func removeCallback(withId callbackId: String) { - guard let result = CDVPluginResult(status: CDVCommandStatus_NO_RESULT) else { - return DDLogWarn("Missing plugin result for callback \(callbackId)"); - } - result.setKeepCallbackAs(false) - self.commandDelegate?.send(result, callbackId: callbackId) + guard let result = CDVPluginResult(status: CDVCommandStatus_NO_RESULT) else { + return DDLogWarn("Missing plugin result for callback \(callbackId)"); + } + result.setKeepCallbackAs(false) + self.commandDelegate?.send(result, callbackId: callbackId) } // Migrates local storage files from UIWebView to WKWebView. diff --git a/client/src/www/app/main.cordova.ts b/client/src/www/app/main.cordova.ts index 77061813b3..22a3446253 100644 --- a/client/src/www/app/main.cordova.ts +++ b/client/src/www/app/main.cordova.ts @@ -31,7 +31,7 @@ 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 {BrowserResourceFetcher, ResourceFetcher} from './resource_fetcher'; +import {ResourceFetcher} from './resource_fetcher'; import {CordovaResourceFetcher} from './resource_fetcher.cordova'; import {AbstractUpdater} from './updater'; import * as interceptors from './url_interceptor'; @@ -118,11 +118,7 @@ class CordovaPlatform implements OutlinePlatform { } getResourceFetcher(): ResourceFetcher { - if (cordova.platformId === 'android') { - return new CordovaResourceFetcher(); - } - // TODO: move to Go fetch implementation later - return new BrowserResourceFetcher(); + return new CordovaResourceFetcher(); } quitApplication() {