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
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* 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.
*/
package de.blinkt.openvpn;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.net.VpnService;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.widget.Toast;
public class OpenVpnService extends VpnService implements Handler.Callback {
Handler mHandler;
private Thread mServiceThread;
private Vector<String> mDnslist=new Vector<String>();
private VpnProfile mProfile;
private String mDomain=null;
private Vector<CIDRIP> mRoutes=new Vector<CIDRIP>();
private CIDRIP mLocalIP=null;
private OpenVpnManagementThread mSocketManager;
private Thread mSocketManagerThread;
private int mMtu;
class CIDRIP{
String mIp;
int len;
public CIDRIP(String ip, String mask){
mIp=ip;
String[] ipt = mask.split("\\.");
long netmask=0;
netmask += Long.parseLong(ipt[0])<< 24;
netmask += Integer.parseInt(ipt[1])<< 16;
netmask += Integer.parseInt(ipt[2])<< 8;
netmask += Integer.parseInt(ipt[3]);
// Add 33. bit to ensure the loop terminates
netmask += 1l << 32;
int lenZeros = 0;
while((netmask & 0x1) == 0) {
lenZeros++;
netmask = netmask >> 1;
}
// Check if rest of netmask is only 1s
if(netmask != (0x1ffffffffl >> lenZeros)) {
// Asume no CIDR, set /32
len=32;
} else {
len =32 -lenZeros;
}
}
@Override
public String toString() {
return String.format("%s/%d",mIp,len);
}
public boolean normalise(){
long ip=0;
String[] ipt = mIp.split("\\.");
ip += Long.parseLong(ipt[0])<< 24;
ip += Integer.parseInt(ipt[1])<< 16;
ip += Integer.parseInt(ipt[2])<< 8;
ip += Integer.parseInt(ipt[3]);
long newip = ip & (0xffffffffl << (32 -len));
if (newip != ip){
mIp = String.format("%d.%d.%d.%d", (newip & 0xff000000) >> 24,(newip & 0xff0000) >> 16, (newip & 0xff00) >> 8 ,newip & 0xff);
return true;
} else {
return false;
}
}
}
@Override
public void onRevoke() {
OpenVpnManagementThread.stopOpenVPN();
mServiceThread=null;
stopSelf();
};
private LocalSocket openManagmentInterface(int tries) {
// Could take a while to open connection
String socketname = (getCacheDir().getAbsolutePath() + "/" + "mgmtsocket");
LocalSocket sock = new LocalSocket();
while(tries > 0 && !sock.isConnected()) {
try {
sock.connect(new LocalSocketAddress(socketname,
LocalSocketAddress.Namespace.FILESYSTEM));
} catch (IOException e) {
// wait 300 ms before retrying
try { Thread.sleep(300);
} catch (InterruptedException e1) {}
}
tries--;
}
return sock;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Extract information from the intent.
String prefix = getPackageName();
String[] argv = intent.getStringArrayExtra(prefix + ".ARGV");
String nativelibdir = intent.getStringExtra(prefix + ".nativelib");
String profileUUID = intent.getStringExtra(prefix + ".profileUUID");
mProfile = ProfileManager.get(profileUUID);
// The handler is only used to show messages.
if (mHandler == null) {
mHandler = new Handler(this);
}
// Stop the previous session by interrupting the thread.
if(OpenVpnManagementThread.stopOpenVPN()){
// an old was asked to exit, wait 2s
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
if (mServiceThread!=null) {
mServiceThread.interrupt();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
// See if there is a managment socket we can connect to and kill the process too
LocalSocket mgmtsocket = openManagmentInterface(1);
if(mgmtsocket!=null) {
// Fire and forget :)
new OpenVpnManagementThread(mProfile,mgmtsocket,this).managmentCommand("signal SIGINT\n");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
//checkForRemainingMiniVpns();
}
// Start a new session by creating a new thread.
OpenVPNThread serviceThread = new OpenVPNThread(this, argv,nativelibdir);
mServiceThread = new Thread(serviceThread, "OpenVPNServiceThread");
mServiceThread.start();
// Open the Management Interface
mgmtsocket = openManagmentInterface(8);
if(mgmtsocket!=null) {
// start a Thread that handles incoming messages of the managment socket
mSocketManager = new OpenVpnManagementThread(mProfile,mgmtsocket,this);
mSocketManagerThread = new Thread(mSocketManager,"OpenVPNMgmtThread");
mSocketManagerThread.start();
}
return START_NOT_STICKY;
}
private void checkForRemainingMiniVpns() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (manager == null)
return;
List<RunningAppProcessInfo> service= manager.getRunningAppProcesses();
// Does not return the minivpn binarys :S
for(RunningAppProcessInfo rapi:service){
if(rapi.processName.equals("minivpn"))
android.os.Process.killProcess(rapi.pid);
}
}
@Override
public void onDestroy() {
if (mServiceThread != null) {
mSocketManager.managmentCommand("signal SIGINT\n");
mServiceThread.interrupt();
}
}
@Override
public boolean handleMessage(Message message) {
if (message != null) {
Toast.makeText(this, message.what, Toast.LENGTH_SHORT).show();
}
return true;
}
public ParcelFileDescriptor openTun() {
Builder builder = new Builder();
if(mLocalIP==null) {
OpenVPN.logMessage(0, "", getString(R.string.opentun_no_ipaddr));
return null;
}
builder.addAddress(mLocalIP.mIp, mLocalIP.len);
for (String dns : mDnslist ) {
builder.addDnsServer(dns);
}
builder.setMtu(mMtu);
for (CIDRIP route:mRoutes) {
try {
builder.addRoute(route.mIp, route.len);
} catch (IllegalArgumentException ia) {
OpenVPN.logMessage(0, "", getString(R.string.route_rejected) + route + " " + ia.getLocalizedMessage());
}
}
if(mDomain!=null)
builder.addSearchDomain(mDomain);
String bconfig[] = new String[5];
bconfig[0]= getString(R.string.last_openvpn_tun_config);
bconfig[1] = String.format(getString(R.string.local_ip_info,mLocalIP.mIp,mLocalIP.len,mMtu));
bconfig[2] = String.format(getString(R.string.dns_server_info, joinString(mDnslist)));
bconfig[3] = String.format(getString(R.string.dns_domain_info, mDomain));
bconfig[4] = String.format(getString(R.string.routes_info, joinString(mRoutes)));
builder.setSession(mProfile.mName + " - " + mLocalIP);
OpenVPN.logBuilderConfig(bconfig);
// Reset information
mDnslist.clear();
mRoutes.clear();
mLocalIP=null;
// Let the configure Button show the Log
Intent intent = new Intent(getBaseContext(),LogWindow.class);
PendingIntent startLW = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
builder.setConfigureIntent(startLW);
try {
ParcelFileDescriptor pfd = builder.establish();
return pfd;
} catch (Exception e) {
OpenVPN.logMessage(0, "", getString(R.string.tun_open_error));
OpenVPN.logMessage(0, "", getString(R.string.error) + e.getLocalizedMessage());
OpenVPN.logMessage(0, "", getString(R.string.tun_error_helpful));
return null;
}
}
// Ugly, but java has no such method
private <T> String joinString(Vector<T> vec) {
String ret = "";
if(vec.size() > 0){
ret = vec.get(0).toString();
for(int i=1;i < vec.size();i++) {
ret = ret + ", " + vec.get(i).toString();
}
}
return ret;
}
public void addDNS(String dns) {
mDnslist.add(dns);
}
public void setDomain(String domain) {
if(mDomain==null) {
mDomain=domain;
}
}
public void addRoute(String dest, String mask) {
CIDRIP route = new CIDRIP(dest, mask);
if(route.len == 32 && !mask.equals("255.255.255.255")) {
OpenVPN.logMessage(0, "", String.format(getString(R.string.route_not_cidr,dest,mask)));
}
if(route.normalise())
OpenVPN.logMessage(0, "", String.format(getString(R.string.route_not_netip,dest,route.len,route.mIp)));
mRoutes.add(route);
}
public void setLocalIP(String local, String netmask,int mtu) {
mLocalIP = new CIDRIP(local, netmask);
mMtu = mtu;
if(mLocalIP.len == 32 && !netmask.equals("255.255.255.255")) {
OpenVPN.logMessage(0, "", String.format(getString(R.string.ip_not_cidr, local,netmask)));
}
}
public Handler getHandler() {
return mHandler;
}
}
|