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
|
/*
* Copyright (c) 2012-2016 Arne Schwabe
* Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
*/
package de.blinkt.openvpn.core;
import android.content.Context;
import android.os.Build;
import android.os.HandlerThread;
import android.os.Message;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Vector;
import de.blinkt.openvpn.R;
public class VpnStatus {
public static LinkedList<LogItem> logbuffer;
private static Vector<LogListener> logListener;
private static Vector<StateListener> stateListener;
private static Vector<ByteCountListener> byteCountListener;
private static String mLaststatemsg = "";
private static String mLaststate = "NOPROCESS";
private static int mLastStateresid = R.string.state_noprocess;
private static long mlastByteCount[] = {0, 0, 0, 0};
private static HandlerThread mHandlerThread;
public static void logException(LogLevel ll, String context, Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
LogItem li;
if (context != null) {
li = new LogItem(ll, R.string.unhandled_exception_context, e.getMessage(), sw.toString(), context);
} else {
li = new LogItem(ll, R.string.unhandled_exception, e.getMessage(), sw.toString());
}
newLogItem(li);
}
public static void logException(Exception e) {
logException(LogLevel.ERROR, null, e);
}
public static void logException(String context, Exception e) {
logException(LogLevel.ERROR, context, e);
}
static final int MAXLOGENTRIES = 1000;
public static String getLastCleanLogMessage(Context c) {
String message = mLaststatemsg;
switch (mLastLevel) {
case LEVEL_CONNECTED:
String[] parts = mLaststatemsg.split(",");
/*
(a) the integer unix date/time,
(b) the state name,
0 (c) optional descriptive string (used mostly on RECONNECTING
and EXITING to show the reason for the disconnect),
1 (d) optional TUN/TAP local IPv4 address
2 (e) optional address of remote server,
3 (f) optional port of remote server,
4 (g) optional local address,
5 (h) optional local port, and
6 (i) optional TUN/TAP local IPv6 address.
*/
// Return only the assigned IP addresses in the UI
if (parts.length >= 7)
message = String.format(Locale.US, "%s %s", parts[1], parts[6]);
break;
}
while (message.endsWith(","))
message = message.substring(0, message.length() - 1);
String status = mLaststate;
if (status.equals("NOPROCESS"))
return message;
String prefix = c.getString(mLastStateresid);
if (mLastStateresid == R.string.unknown_state)
message = status + message;
if (message.length() > 0)
prefix += ": ";
return prefix + message;
}
public static void initLogCache(File cacheDir) {
mHandlerThread = new HandlerThread("LogFileWriter", Thread.MIN_PRIORITY);
mHandlerThread.start();
mLogFileHandler = new LogFileHandler(mHandlerThread.getLooper());
Message m = mLogFileHandler.obtainMessage(LogFileHandler.LOG_INIT, cacheDir);
mLogFileHandler.sendMessage(m);
}
public static void flushLog() {
mLogFileHandler.sendEmptyMessage(LogFileHandler.FLUSH_TO_DISK);
}
public enum ConnectionStatus {
LEVEL_CONNECTED,
LEVEL_VPNPAUSED,
LEVEL_CONNECTING_SERVER_REPLIED,
LEVEL_CONNECTING_NO_SERVER_REPLY_YET,
LEVEL_NONETWORK,
LEVEL_NOTCONNECTED,
LEVEL_START,
LEVEL_AUTH_FAILED,
LEVEL_WAITING_FOR_USER_INPUT,
UNKNOWN_LEVEL
}
public enum LogLevel {
INFO(2),
ERROR(-2),
WARNING(1),
VERBOSE(3),
DEBUG(4);
protected int mValue;
LogLevel(int value) {
mValue = value;
}
public int getInt() {
return mValue;
}
public static LogLevel getEnumByValue(int value) {
switch (value) {
case 1:
return INFO;
case 2:
return ERROR;
case 3:
return WARNING;
case 4:
return DEBUG;
default:
return null;
}
}
}
// keytool -printcert -jarfile de.blinkt.openvpn_85.apk
public static final byte[] officalkey = {-58, -42, -44, -106, 90, -88, -87, -88, -52, -124, 84, 117, 66, 79, -112, -111, -46, 86, -37, 109};
public static final byte[] officaldebugkey = {-99, -69, 45, 71, 114, -116, 82, 66, -99, -122, 50, -70, -56, -111, 98, -35, -65, 105, 82, 43};
public static final byte[] amazonkey = {-116, -115, -118, -89, -116, -112, 120, 55, 79, -8, -119, -23, 106, -114, -85, -56, -4, 105, 26, -57};
public static final byte[] fdroidkey = {-92, 111, -42, -46, 123, -96, -60, 79, -27, -31, 49, 103, 11, -54, -68, -27, 17, 2, 121, 104};
private static ConnectionStatus mLastLevel = ConnectionStatus.LEVEL_NOTCONNECTED;
private static LogFileHandler mLogFileHandler;
static {
logbuffer = new LinkedList<>();
logListener = new Vector<>();
stateListener = new Vector<>();
byteCountListener = new Vector<>();
logInformation();
}
public interface LogListener {
void newLog(LogItem logItem);
}
public interface StateListener {
void updateState(String state, String logmessage, int localizedResId, ConnectionStatus level);
}
public interface ByteCountListener {
void updateByteCount(long in, long out, long diffIn, long diffOut);
}
public synchronized static void logMessage(LogLevel level, String prefix, String message) {
newLogItem(new LogItem(level, prefix + message));
}
public synchronized static void clearLog() {
logbuffer.clear();
logInformation();
if (mLogFileHandler != null)
mLogFileHandler.sendEmptyMessage(LogFileHandler.TRIM_LOG_FILE);
}
private static void logInformation() {
String nativeAPI;
try {
nativeAPI = NativeUtils.getNativeAPI();
} catch (UnsatisfiedLinkError ignore) {
nativeAPI = "error";
}
logInfo(R.string.mobile_info, Build.MODEL, Build.BOARD, Build.BRAND, Build.VERSION.SDK_INT,
nativeAPI, Build.VERSION.RELEASE, Build.ID, Build.FINGERPRINT, "", "");
}
public synchronized static void addLogListener(LogListener ll) {
logListener.add(ll);
}
public synchronized static void removeLogListener(LogListener ll) {
logListener.remove(ll);
}
public synchronized static void addByteCountListener(ByteCountListener bcl) {
bcl.updateByteCount(mlastByteCount[0], mlastByteCount[1], mlastByteCount[2], mlastByteCount[3]);
byteCountListener.add(bcl);
}
public synchronized static void removeByteCountListener(ByteCountListener bcl) {
byteCountListener.remove(bcl);
}
public synchronized static void addStateListener(StateListener sl) {
if (!stateListener.contains(sl)) {
stateListener.add(sl);
if (mLaststate != null)
sl.updateState(mLaststate, mLaststatemsg, mLastStateresid, mLastLevel);
}
}
private static int getLocalizedState(String state) {
switch (state) {
case "CONNECTING":
return R.string.state_connecting;
case "WAIT":
return R.string.state_wait;
case "AUTH":
return R.string.state_auth;
case "GET_CONFIG":
return R.string.state_get_config;
case "ASSIGN_IP":
return R.string.state_assign_ip;
case "ADD_ROUTES":
return R.string.state_add_routes;
case "CONNECTED":
return R.string.state_connected;
case "DISCONNECTED":
return R.string.state_disconnected;
case "RECONNECTING":
return R.string.state_reconnecting;
case "EXITING":
return R.string.state_exiting;
case "RESOLVE":
return R.string.state_resolve;
case "TCP_CONNECT":
return R.string.state_tcp_connect;
default:
return R.string.unknown_state;
}
}
public static void updateStatePause(OpenVPNManagement.pauseReason pauseReason) {
switch (pauseReason) {
case noNetwork:
VpnStatus.updateStateString("NONETWORK", "", R.string.state_nonetwork, ConnectionStatus.LEVEL_NONETWORK);
break;
case screenOff:
VpnStatus.updateStateString("SCREENOFF", "", R.string.state_screenoff, ConnectionStatus.LEVEL_VPNPAUSED);
break;
case userPause:
VpnStatus.updateStateString("USERPAUSE", "", R.string.state_userpause, ConnectionStatus.LEVEL_VPNPAUSED);
break;
}
}
private static ConnectionStatus getLevel(String state) {
String[] noreplyet = {"CONNECTING", "WAIT", "RECONNECTING", "RESOLVE", "TCP_CONNECT"};
String[] reply = {"AUTH", "GET_CONFIG", "ASSIGN_IP", "ADD_ROUTES"};
String[] connected = {"CONNECTED"};
String[] notconnected = {"DISCONNECTED", "EXITING"};
for (String x : noreplyet)
if (state.equals(x))
return ConnectionStatus.LEVEL_CONNECTING_NO_SERVER_REPLY_YET;
for (String x : reply)
if (state.equals(x))
return ConnectionStatus.LEVEL_CONNECTING_SERVER_REPLIED;
for (String x : connected)
if (state.equals(x))
return ConnectionStatus.LEVEL_CONNECTED;
for (String x : notconnected)
if (state.equals(x))
return ConnectionStatus.LEVEL_NOTCONNECTED;
return ConnectionStatus.UNKNOWN_LEVEL;
}
public synchronized static void removeStateListener(StateListener sl) {
stateListener.remove(sl);
}
synchronized public static LogItem[] getlogbuffer() {
// The stoned way of java to return an array from a vector
// brought to you by eclipse auto complete
return logbuffer.toArray(new LogItem[logbuffer.size()]);
}
public static void updateStateString(String state, String msg) {
int rid = getLocalizedState(state);
ConnectionStatus level = getLevel(state);
updateStateString(state, msg, rid, level);
}
public synchronized static void updateStateString(String state, String msg, int resid, ConnectionStatus level) {
// Workound for OpenVPN doing AUTH and wait and being connected
// Simply ignore these state
if (mLastLevel == ConnectionStatus.LEVEL_CONNECTED &&
(state.equals("WAIT") || state.equals("AUTH"))) {
newLogItem(new LogItem((LogLevel.DEBUG), String.format("Ignoring OpenVPN Status in CONNECTED state (%s->%s): %s", state, level.toString(), msg)));
return;
}
mLaststate = state;
mLaststatemsg = msg;
mLastStateresid = resid;
mLastLevel = level;
for (StateListener sl : stateListener) {
sl.updateState(state, msg, resid, level);
}
//newLogItem(new LogItem((LogLevel.DEBUG), String.format("New OpenVPN Status (%s->%s): %s",state,level.toString(),msg)));
}
public static void logInfo(String message) {
newLogItem(new LogItem(LogLevel.INFO, message));
}
public static void logDebug(String message) {
newLogItem(new LogItem(LogLevel.DEBUG, message));
}
public static void logInfo(int resourceId, Object... args) {
newLogItem(new LogItem(LogLevel.INFO, resourceId, args));
}
public static void logDebug(int resourceId, Object... args) {
newLogItem(new LogItem(LogLevel.DEBUG, resourceId, args));
}
private static void newLogItem(LogItem logItem) {
newLogItem(logItem, false);
}
synchronized static void newLogItem(LogItem logItem, boolean cachedLine) {
if (cachedLine) {
logbuffer.addFirst(logItem);
} else {
logbuffer.addLast(logItem);
if (mLogFileHandler != null) {
Message m = mLogFileHandler.obtainMessage(LogFileHandler.LOG_MESSAGE, logItem);
mLogFileHandler.sendMessage(m);
}
}
if (logbuffer.size() > MAXLOGENTRIES + MAXLOGENTRIES / 2) {
while (logbuffer.size() > MAXLOGENTRIES)
logbuffer.removeFirst();
if (mLogFileHandler != null)
mLogFileHandler.sendMessage(mLogFileHandler.obtainMessage(LogFileHandler.TRIM_LOG_FILE));
}
//if (BuildConfig.DEBUG && !cachedLine && !BuildConfig.FLAVOR.equals("test"))
// Log.d("OpenVPN", logItem.getString(null));
for (LogListener ll : logListener) {
ll.newLog(logItem);
}
}
public static void logError(String msg) {
newLogItem(new LogItem(LogLevel.ERROR, msg));
}
public static void logWarning(int resourceId, Object... args) {
newLogItem(new LogItem(LogLevel.WARNING, resourceId, args));
}
public static void logWarning(String msg) {
newLogItem(new LogItem(LogLevel.WARNING, msg));
}
public static void logError(int resourceId) {
newLogItem(new LogItem(LogLevel.ERROR, resourceId));
}
public static void logError(int resourceId, Object... args) {
newLogItem(new LogItem(LogLevel.ERROR, resourceId, args));
}
public static void logMessageOpenVPN(LogLevel level, int ovpnlevel, String message) {
newLogItem(new LogItem(level, ovpnlevel, message));
}
public static synchronized void updateByteCount(long in, long out) {
long lastIn = mlastByteCount[0];
long lastOut = mlastByteCount[1];
long diffIn = mlastByteCount[2] = Math.max(0, in - lastIn);
long diffOut = mlastByteCount[3] = Math.max(0, out - lastOut);
mlastByteCount = new long[]{in, out, diffIn, diffOut};
for (ByteCountListener bcl : byteCountListener) {
bcl.updateByteCount(in, out, diffIn, diffOut);
}
}
}
|