Repository navigation
Expand file tree
/
Copy pathBrowserVpnService.java
More file actions
86 lines (70 loc) · 2.6 KB
/
Copy pathBrowserVpnService.java
File metadata and controls
86 lines (70 loc) · 2.6 KB
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
package com.viaadvancedbrowser.vpn;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.VpnService;
import android.os.ParcelFileDescriptor;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
public class BrowserVpnService extends VpnService {
private ParcelFileDescriptor vpnInterface;
private boolean isRunning = false;
private VPNConfig currentConfig;
private static final String TAG = "BrowserVpnService";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && "connect".equals(intent.getAction())) {
currentConfig = intent.getParcelableExtra("vpn_config");
startVPN();
} else if (intent != null && "disconnect".equals(intent.getAction())) {
stopVPN();
}
return START_STICKY;
}
private void startVPN() {
try {
Builder builder = new Builder();
builder.setSession("ViaAdvancedVPN")
.addAddress("10.0.0.2", 32)
.addDnsServer("8.8.8.8")
.addRoute("0.0.0.0", 0)
.setMtu(1500);
// Configure based on current config
if (currentConfig != null) {
for (String dns : currentConfig.getDnsServers()) {
builder.addDnsServer(dns);
}
}
vpnInterface = builder.establish();
isRunning = true;
// Send broadcast that VPN is connected
Intent connectedIntent = new Intent("VPN_STATUS_CHANGED");
connectedIntent.putExtra("status", "connected");
LocalBroadcastManager.getInstance(this).sendBroadcast(connectedIntent);
} catch (Exception e) {
stopVPN();
}
}
private void stopVPN() {
isRunning = false;
try {
if (vpnInterface != null) {
vpnInterface.close();
vpnInterface = null;
}
} catch (Exception e) {
// Ignore
}
// Send broadcast that VPN is disconnected
Intent disconnectedIntent = new Intent("VPN_STATUS_CHANGED");
disconnectedIntent.putExtra("status", "disconnected");
LocalBroadcastManager.getInstance(this).sendBroadcast(disconnectedIntent);
stopSelf();
}
@Override
public void onDestroy() {
stopVPN();
super.onDestroy();
}
public boolean isRunning() {
return isRunning;
}
}