Skip to content

Commit

Permalink
refactor(client/android): move fetching logic from TypeScript to Go
Browse files Browse the repository at this point in the history
  • Loading branch information
jyyi1 committed Sep 27, 2024
1 parent d049b1e commit f9bf4d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.outline.vpn.Errors;
import org.outline.vpn.VpnServiceStarter;
import org.outline.vpn.VpnTunnelService;
import outline.Outline;
import outline.FetchDynamicKeyResult;
import platerrors.Platerrors;
import platerrors.PlatformError;

Expand All @@ -57,6 +59,7 @@ public enum Action {
STOP("stop"),
ON_STATUS_CHANGE("onStatusChange"),
IS_RUNNING("isRunning"),
FETCH_CONFIG("fetchConfig"),
INIT_ERROR_REPORTING("initializeErrorReporting"),
REPORT_EVENTS("reportEvents"),
QUIT("quitApplication");
Expand Down Expand Up @@ -202,6 +205,17 @@ private void executeAsync(
final String tunnelId = args.getString(0);
boolean isActive = isTunnelActive(tunnelId);
callback.sendPluginResult(new PluginResult(PluginResult.Status.OK, isActive));
} else if (Action.FETCH_CONFIG.is(action)) {
final String url = args.getString(0);
LOG.fine(String.format(Locale.ROOT, "Fetching dynamic config at %s ...", url));
final FetchDynamicKeyResult result = Outline.fetchDynamicKey(url);
if (result.getError() != null) {
LOG.warning(String.format(Locale.ROOT, "Fetch dynamic config failed: %s", result.getError()));
sendActionResult(callback, result.getError());
} else {
LOG.info(String.format(Locale.ROOT, "Fetch dynamic config result: %s", result.getKey()));
callback.success(result.getKey());
}

// Static actions
} else if (Action.INIT_ERROR_REPORTING.is(action)) {
Expand Down
30 changes: 17 additions & 13 deletions client/src/www/app/outline_server_repository/vpn.cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,24 @@ export class CordovaVpnApi implements VpnApi {
cordova.exec(callback, onError, OUTLINE_PLUGIN_NAME, 'onStatusChange', []);
}

// TODO: move to Go fetch implementation later
async fetchDynamicConfig(url: string): Promise<string> {
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}
);
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 (await response.text()).trim();
}
}

0 comments on commit f9bf4d8

Please sign in to comment.