-
Notifications
You must be signed in to change notification settings - Fork 129
/
OpenVpnService.java
512 lines (429 loc) · 14.6 KB
/
OpenVpnService.java
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
* Adapted from OpenVPN for Android
* Copyright (c) 2012-2013, Arne Schwabe
* Copyright (c) 2013, Kevin Cernekee
* All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library.
*/
package app.openconnect.core;
import android.Manifest.permission;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.VpnService;
import android.os.*;
import android.preference.PreferenceManager;
import android.util.Log;
import app.openconnect.MainActivity;
import app.openconnect.R;
import app.openconnect.VpnProfile;
import app.openconnect.api.GrantPermissionsActivity;
import app.openconnect.core.VPNLog.LogArrayAdapter;
import app.openconnect.fragments.FeedbackFragment;
import java.net.InetAddress;
import java.util.Date;
import java.util.Locale;
import org.infradead.libopenconnect.LibOpenConnect;
import org.infradead.libopenconnect.LibOpenConnect.VPNStats;
public class OpenVpnService extends VpnService {
public static final String TAG = "OpenConnect";
public static final String START_SERVICE = "app.openconnect.START_SERVICE";
public static final String START_SERVICE_STICKY = "app.openconnect.START_SERVICE_STICKY";
public static final String ALWAYS_SHOW_NOTIFICATION = "app.openconnect.NOTIFICATION_ALWAYS_VISIBLE";
public static final String ACTION_VPN_STATUS = "app.openconnect.VPN_STATUS";
public static final String EXTRA_CONNECTION_STATE = "app.openconnect.connectionState";
public static final String EXTRA_UUID = "app.openconnect.UUID";
// These are valid in the CONNECTED state
public VpnProfile profile;
public LibOpenConnect.IPInfo ipInfo;
public String serverName;
public Date startTime;
private DeviceStateReceiver mDeviceStateReceiver;
private SharedPreferences mPrefs;
private KeepAlive mKeepAlive;
private final IBinder mBinder = new LocalBinder();
private String mUUID;
private int mStartId;
private Thread mVPNThread;
private OpenConnectManagementThread mVPN;
private UserDialog mDialog;
private Context mDialogContext;
private final int NOTIFICATION_ID = 1;
private int mActivityConnections;
private boolean mNotificationActive;
private int mConnectionState = OpenConnectManagementThread.STATE_DISCONNECTED;
private String mConnectionStateNames[];
private VPNStats mStats = new VPNStats();
private VPNLog mVPNLog = new VPNLog();
private Handler mHandler = new Handler();
public class LocalBinder extends Binder {
public OpenVpnService getService() {
// Return this instance of LocalService so clients can call public methods
return OpenVpnService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
String action = intent.getAction();
if( action !=null && action.equals(START_SERVICE))
return mBinder;
else
return super.onBind(intent);
}
@Override
public void onRevoke() {
Log.i(TAG, "VPN access has been revoked");
stopVPN();
}
@Override
public void onCreate() {
// Restore service state from disk if available
// This gets overwritten if somebody calls startService()
mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
mUUID = mPrefs.getString("service_mUUID", "");
mVPNLog.restoreFromFile(getCacheDir().getAbsolutePath() + "/logdata.ser");
mConnectionStateNames = getResources().getStringArray(R.array.connection_states);
}
@Override
public void onDestroy() {
killVPNThread(true);
if (mDeviceStateReceiver != null) {
this.unregisterReceiver(mDeviceStateReceiver);
}
mVPNLog.saveToFile(getCacheDir().getAbsolutePath() + "/logdata.ser");
}
private synchronized boolean doStopVPN() {
if (mVPN != null) {
mVPN.stopVPN();
return true;
}
return false;
}
private void killVPNThread(boolean joinThread) {
if (doStopVPN() && joinThread) {
try {
mVPNThread.join(1000);
} catch (InterruptedException e) {
Log.e(TAG, "OpenConnect thread did not exit");
}
}
}
private PendingIntent getMainActivityIntent() {
// Touching "Configure" on the system VPN dialog will restore the app
// (same as clicking the launcher icon)
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent startLW = PendingIntent.getActivity(this, 0, intent, 0);
return startLW;
}
private void registerDeviceStateReceiver(OpenVPNManagement management) {
// Registers BroadcastReceiver to track network connection changes.
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(DeviceStateReceiver.PREF_CHANGED);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
mDeviceStateReceiver = new DeviceStateReceiver(management, mPrefs);
registerReceiver(mDeviceStateReceiver, filter);
}
private synchronized void registerKeepAlive() {
String DNSServer = "8.8.8.8";
try {
String dns = ipInfo.DNS.get(0);
if (InetAddress.getByName(dns) != null) {
DNSServer = dns;
}
} catch (IndexOutOfBoundsException e) {
/* empty DNS server list */
} catch (Exception e) {
Log.i(TAG, "server DNS IP is bogus, falling back to " + DNSServer + " for KeepAlive", e);
}
int idle = 1800;
try {
int val = Integer.parseInt(ipInfo.CSTPOptions.get("X-CSTP-Idle-Timeout"));
if (val >= 60 && val <= 7200) {
idle = val;
}
} catch (Exception e) {
}
// set to 40% of the idle timeout value, to buy a little margin in case
// the first 1-2 attempts fail
idle = idle * 4 / 10;
Log.d(TAG, "calculated KeepAlive interval: " + idle + " seconds");
IntentFilter filter = new IntentFilter(KeepAlive.ACTION_KEEPALIVE_ALARM);
mKeepAlive = new KeepAlive(idle, DNSServer, mDeviceStateReceiver);
registerReceiver(mKeepAlive, filter);
mKeepAlive.start(this);
}
private void unregisterReceivers() {
try {
if (mDeviceStateReceiver != null) {
unregisterReceiver(mDeviceStateReceiver);
}
mDeviceStateReceiver = null;
} catch (IllegalArgumentException iae) {
// catch "Receiver not registered" error
Log.w(TAG, "can't unregister DeviceStateReceiver", iae);
}
try {
if (mKeepAlive != null) {
mKeepAlive.stop(this);
unregisterReceiver(mKeepAlive);
}
mKeepAlive = null;
} catch (IllegalArgumentException iae) {
Log.w(TAG, "can't unregister KeepAlive", iae);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
Log.e(TAG, "OpenVpnService started with null intent");
stopSelf();
return START_NOT_STICKY;
}
String action = intent.getAction();
if (START_SERVICE.equals(action)) {
return START_NOT_STICKY;
} else if (START_SERVICE_STICKY.equals(action)) {
return START_REDELIVER_INTENT;
}
// Extract information from the intent.
mUUID = intent.getStringExtra(EXTRA_UUID);
if (mUUID == null) {
return START_NOT_STICKY;
}
mPrefs.edit().putString("service_mUUID", mUUID).apply();
profile = ProfileManager.get(mUUID);
if (profile == null) {
return START_NOT_STICKY;
}
killVPNThread(true);
// stopSelfResult(most_recent_startId) will kill the service
// stopSelfResult(previous_startId) will not
mStartId = startId;
mVPN = new OpenConnectManagementThread(getApplicationContext(), profile, this);
mVPNThread = new Thread(mVPN, "OpenVPNManagementThread");
mVPNThread.start();
unregisterReceivers();
registerDeviceStateReceiver(mVPN);
ProfileManager.setConnectedVpnProfile(profile);
return START_NOT_STICKY;
}
public Builder getVpnServiceBuilder() {
VpnService.Builder b = new VpnService.Builder();
b.setSession(profile.mName);
b.setConfigureIntent(getMainActivityIntent());
return b;
}
// From: http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
public static String humanReadableByteCount(long bytes, boolean mbit) {
if(mbit)
bytes = bytes *8;
int unit = mbit ? 1000 : 1024;
if (bytes < unit)
return bytes + (mbit ? " bit" : " B");
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (mbit ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (mbit ? "" : "");
if(mbit)
return String.format(Locale.getDefault(),"%.1f %sbit", bytes / Math.pow(unit, exp), pre);
else
return String.format(Locale.getDefault(),"%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
public static String formatElapsedTime(long startTime) {
StringBuilder sb = new StringBuilder();
startTime = (new Date().getTime() - startTime) / 1000;
if (startTime >= 60 * 60 * 24) {
// days
sb.append(String.format("%1$d:", startTime / (60 * 60 * 24)));
}
if (startTime >= 60 * 60) {
// hours
startTime %= 60 * 60 * 24;
sb.append(String.format("%1$02d:", startTime / (60 * 60)));
startTime %= 60 * 60;
}
// minutes:seconds
sb.append(String.format("%1$02d:%2$02d", startTime / 60, startTime % 60));
return sb.toString();
}
/* called from the activity on broadcast receipt, or startup */
public synchronized void startActiveDialog(Context context) {
if (mDialog != null && mDialogContext == null) {
mDialogContext = context;
mDialog.onStart(context);
}
}
/* called when the activity shuts down (mDialog will be re-rendered when the activity starts again) */
public synchronized void stopActiveDialog(Context context) {
if (mDialogContext != context) {
return;
}
if (mDialog != null) {
mDialog.onStop(mDialogContext);
}
mDialogContext = null;
}
private synchronized void setDialog(Context context, UserDialog dialog) {
mDialogContext = context;
mDialog = dialog;
}
@SuppressWarnings("deprecation")
private void updateNotification() {
if (mDialog != null && mActivityConnections == 0 && !mNotificationActive) {
mNotificationActive = true;
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_stat_vpn)
.setContentTitle(getString(R.string.notification_input_needed))
.setContentText(getString(R.string.notification_touch_here))
.setContentIntent(getMainActivityIntent());
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.getNotification());
mNotificationActive = true;
} else if ((mDialog == null || mActivityConnections > 0) && mNotificationActive) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(NOTIFICATION_ID);
mNotificationActive = false;
}
}
private void wakeUpActivity() {
mHandler.post(new Runnable() {
@Override
public void run() {
Intent vpnstatus = new Intent(ACTION_VPN_STATUS);
vpnstatus.putExtra(EXTRA_CONNECTION_STATE, mConnectionState);
vpnstatus.putExtra(EXTRA_UUID, mUUID);
sendBroadcast(vpnstatus, permission.ACCESS_NETWORK_STATE);
updateNotification();
if (mConnectionState == OpenConnectManagementThread.STATE_CONNECTED &&
mKeepAlive == null) {
registerKeepAlive();
FeedbackFragment.recordUse(getApplicationContext(), true);
}
}
});
}
public void updateActivityRefcount(int num) {
mActivityConnections += num;
updateNotification();
}
/* called from the VPN thread; blocks until user responds */
public Object promptUser(UserDialog dialog) {
Object ret;
ret = dialog.earlyReturn();
if (ret != null) {
return ret;
}
setDialog(null, dialog);
wakeUpActivity();
ret = mDialog.waitForResponse();
setDialog(null, null);
return ret;
}
public synchronized void threadDone() {
final int startId = mStartId;
Log.i(TAG, "VPN thread has terminated");
mVPN = null;
mHandler.post(new Runnable() {
@Override
public void run() {
if (stopSelfResult(startId) == false) {
Log.w(TAG, "not stopping service due to startId mismatch");
} else {
unregisterReceivers();
}
}
});
}
public synchronized void setConnectionState(int state) {
if (state == OpenConnectManagementThread.STATE_CONNECTED &&
mConnectionState != OpenConnectManagementThread.STATE_CONNECTED) {
startTime = new Date();
}
mConnectionState = state;
wakeUpActivity();
}
public synchronized int getConnectionState() {
return mConnectionState;
}
public String getConnectionStateName() {
return mConnectionStateNames[getConnectionState()];
}
public void requestStats() {
if (mVPN != null) {
mVPN.requestStats();
}
}
public synchronized void setStats(VPNStats stats) {
if (stats != null) {
mStats = stats;
}
wakeUpActivity();
}
public synchronized VPNStats getStats() {
return mStats;
}
public synchronized void setIPInfo(LibOpenConnect.IPInfo ipInfo, String serverName) {
this.ipInfo = ipInfo;
this.serverName = serverName;
}
public LogArrayAdapter getArrayAdapter(Context context) {
return mVPNLog.getArrayAdapter(context);
}
public void putArrayAdapter(LogArrayAdapter adapter) {
if (adapter != null) {
mVPNLog.putArrayAdapter(adapter);
}
}
public void log(final int level, final String msg) {
mHandler.post(new Runnable() {
@Override
public void run() {
mVPNLog.add(level, msg);
}
});
}
public void clearLog() {
mVPNLog.clear();
}
public String dumpLog() {
return mVPNLog.dump();
}
public String getReconnectName() {
VpnProfile p = ProfileManager.get(mUUID);
return p == null ? null : p.getName();
}
public void startReconnectActivity(Context context) {
Intent intent = new Intent(context, GrantPermissionsActivity.class);
intent.putExtra(getPackageName() + GrantPermissionsActivity.EXTRA_UUID, mUUID);
context.startActivity(intent);
}
public void stopVPN() {
killVPNThread(false);
ProfileManager.setConnectedVpnProfileDisconnected();
}
}