Skip to content

Commit

Permalink
refactor(client/ios): move fetching logic from TypeScript to Go (#2222)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyyi1 authored Nov 16, 2024
1 parent 49bf053 commit 4f62943
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
57 changes: 41 additions & 16 deletions client/src/cordova/plugin/apple/src/OutlinePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import OutlineSentryLogger
import OutlineNotification
import OutlineTunnel

import Tun2socks

public enum TunnelStatus: Int {
case connected = 0
case disconnected = 1
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 2 additions & 6 deletions client/src/www/app/main.cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 4f62943

Please sign in to comment.