forked from Jigsaw-Code/outline-apps
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcordova_main.ts
188 lines (159 loc) · 6.28 KB
/
cordova_main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2018 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.
/// <reference types='cordova'/>
/// <reference path='../types/webintents.d.ts'/>
import '@babel/polyfill';
import 'web-animations-js/web-animations-next-lite.min.js';
import '@webcomponents/webcomponentsjs/webcomponents-bundle.js';
import {setRootPath} from '@polymer/polymer/lib/utils/settings.js';
setRootPath(location.pathname.substring(0, location.pathname.lastIndexOf('/') + 1));
import * as Sentry from '@sentry/browser';
import {AbstractClipboard} from './clipboard';
import {EnvironmentVariables} from './environment';
import {SentryErrorReporter} from './error_reporter';
import {main} from './main';
import * as errors from '../model/errors';
import {OutlinePlatform} from './platform';
import {Tunnel, TunnelStatus} from './tunnel';
import {AbstractUpdater} from './updater';
import * as interceptors from './url_interceptor';
import {FakeOutlineTunnel} from './fake_tunnel';
import {ShadowsocksSessionConfig} from './tunnel';
import {NoOpVpnInstaller, VpnInstaller} from './vpn_installer';
const OUTLINE_PLUGIN_NAME = 'OutlinePlugin';
// Pushes a clipboard event whenever the app is brought to the foreground.
class CordovaClipboard extends AbstractClipboard {
constructor() {
super();
document.addEventListener('resume', this.emitEvent.bind(this));
}
getContents() {
return new Promise<string>((resolve, reject) => {
cordova.plugins.clipboard.paste(resolve, reject);
});
}
}
// Helper function to call the Outline Cordova plugin.
function pluginExec<T>(cmd: string, ...args: unknown[]): Promise<T> {
return new Promise<T>((resolve, reject) => {
cordova.exec(resolve, reject, OUTLINE_PLUGIN_NAME, cmd, args);
});
}
async function pluginExecWithErrorCode<T>(cmd: string, ...args: unknown[]): Promise<T> {
try {
return await pluginExec<T>(cmd, ...args);
} catch (errorCode) {
throw errors.fromErrorCode(errorCode);
}
}
// Adds reports from the (native) Cordova plugin.
class CordovaErrorReporter extends SentryErrorReporter {
constructor(appVersion: string, appBuildNumber: string, dsn: string) {
super(appVersion, dsn, {'build.number': appBuildNumber});
// Initializes the error reporting framework with the supplied credentials.
// TODO(fortuna): This is an Promise that is not waited for and can cause a race condition.
// We should fix it with an async factory function for the Reporter.
pluginExec<void>('initializeErrorReporting', dsn).catch(console.error);
}
async report(userFeedback: string, feedbackCategory: string, userEmail?: string): Promise<void> {
await super.report(userFeedback, feedbackCategory, userEmail);
// Sends previously captured logs and events to the error reporting framework.
// Associates the report to the provided unique identifier.
await pluginExec<void>('reportEvents', Sentry.lastEventId() || '');
}
}
class CordovaTunnel implements Tunnel {
constructor(public id: string) {}
start(config: ShadowsocksSessionConfig) {
if (!config) {
throw new errors.IllegalServerConfiguration();
}
return pluginExecWithErrorCode<void>('start', this.id, config);
}
stop() {
return pluginExecWithErrorCode<void>('stop', this.id);
}
isRunning() {
return pluginExecWithErrorCode<boolean>('isRunning', this.id);
}
onStatusChange(listener: (status: TunnelStatus) => void): void {
const onError = (err: unknown) => {
console.warn('failed to execute status change listener', err);
};
// Can't use `pluginExec` because Cordova needs to call the listener multiple times.
cordova.exec(listener, onError, OUTLINE_PLUGIN_NAME, 'onStatusChange', [this.id]);
}
}
// This class should only be instantiated after Cordova fires the deviceready event.
class CordovaPlatform implements OutlinePlatform {
private static isBrowser() {
return cordova.platformId === 'browser';
}
hasDeviceSupport() {
return !CordovaPlatform.isBrowser();
}
getTunnelFactory() {
return (id: string) => {
return this.hasDeviceSupport() ? new CordovaTunnel(id) : new FakeOutlineTunnel(id);
};
}
getUrlInterceptor() {
if (cordova.platformId === 'ios' || cordova.platformId === 'osx') {
return new interceptors.AppleUrlInterceptor(appleLaunchUrl);
} else if (cordova.platformId === 'android') {
return new interceptors.AndroidUrlInterceptor();
}
console.warn('no intent interceptor available');
return new interceptors.UrlInterceptor();
}
getClipboard() {
return new CordovaClipboard();
}
getErrorReporter(env: EnvironmentVariables) {
return this.hasDeviceSupport()
? new CordovaErrorReporter(env.APP_VERSION, env.APP_BUILD_NUMBER, env.SENTRY_DSN || '')
: new SentryErrorReporter(env.APP_VERSION, env.SENTRY_DSN || '', {});
}
getUpdater() {
return new AbstractUpdater();
}
getVpnServiceInstaller(): VpnInstaller {
return new NoOpVpnInstaller();
}
quitApplication() {
// Only used in macOS because menu bar apps provide no alternative way of quitting.
cordova.exec(
() => {},
() => {},
OUTLINE_PLUGIN_NAME,
'quitApplication',
[]
);
}
}
// https://cordova.apache.org/docs/en/latest/cordova/events/events.html#deviceready
const onceDeviceReady = new Promise(resolve => {
document.addEventListener('deviceready', resolve);
});
// cordova-[ios|osx] call a global function with this signature when a URL is
// intercepted. We handle URL interceptions with an intent interceptor; however,
// when the app is launched via URL our start up sequence misses the call due to
// a race. Define the function temporarily here, and set a global variable.
let appleLaunchUrl: string;
window.handleOpenURL = (url: string) => {
appleLaunchUrl = url;
};
onceDeviceReady.then(() => {
main(new CordovaPlatform());
});