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
|
/*
* 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.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Handler;
import android.preference.PreferenceManager;
import de.blinkt.openvpn.R;
import de.blinkt.openvpn.core.VpnStatus.ByteCountListener;
import java.util.LinkedList;
import java.util.Objects;
import java.util.StringTokenizer;
import static de.blinkt.openvpn.core.OpenVPNManagement.pauseReason;
public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountListener, OpenVPNManagement.PausedStateCallback {
private final Handler mDisconnectHandler;
private int lastNetwork = -1;
private OpenVPNManagement mManagement;
// Window time in s
private final int TRAFFIC_WINDOW = 60;
// Data traffic limit in bytes
private final long TRAFFIC_LIMIT = 64 * 1024;
// Time to wait after network disconnect to pause the VPN
private final int DISCONNECT_WAIT = 20;
connectState network = connectState.DISCONNECTED;
connectState screen = connectState.SHOULDBECONNECTED;
connectState userpause = connectState.SHOULDBECONNECTED;
private String lastStateMsg = null;
private java.lang.Runnable mDelayDisconnectRunnable = new Runnable() {
@Override
public void run() {
if (!(network == connectState.PENDINGDISCONNECT))
return;
network = connectState.DISCONNECTED;
// Set screen state to be disconnected if disconnect pending
if (screen == connectState.PENDINGDISCONNECT)
screen = connectState.DISCONNECTED;
mManagement.pause(getPauseReason());
}
};
private NetworkInfo lastConnectedNetwork;
@Override
public boolean shouldBeRunning() {
return shouldBeConnected();
}
private enum connectState {
SHOULDBECONNECTED,
PENDINGDISCONNECT,
DISCONNECTED
}
private static class Datapoint {
private Datapoint(long t, long d) {
timestamp = t;
data = d;
}
long timestamp;
long data;
}
private LinkedList<Datapoint> trafficdata = new LinkedList<>();
@Override
public void updateByteCount(long in, long out, long diffIn, long diffOut) {
if (screen != connectState.PENDINGDISCONNECT)
return;
long total = diffIn + diffOut;
trafficdata.add(new Datapoint(System.currentTimeMillis(), total));
while (trafficdata.getFirst().timestamp <= (System.currentTimeMillis() - TRAFFIC_WINDOW * 1000)) {
trafficdata.removeFirst();
}
long windowtraffic = 0;
for (Datapoint dp : trafficdata)
windowtraffic += dp.data;
if (windowtraffic < TRAFFIC_LIMIT) {
screen = connectState.DISCONNECTED;
VpnStatus.logInfo(R.string.screenoff_pause,
OpenVPNService.humanReadableByteCount(TRAFFIC_LIMIT, false), TRAFFIC_WINDOW);
mManagement.pause(getPauseReason());
}
}
public void userPause(boolean pause) {
if (pause) {
userpause = connectState.DISCONNECTED;
// Check if we should disconnect
mManagement.pause(getPauseReason());
} else {
boolean wereConnected = shouldBeConnected();
userpause = connectState.SHOULDBECONNECTED;
if (shouldBeConnected() && !wereConnected)
mManagement.resume();
else
// Update the reason why we currently paused
mManagement.pause(getPauseReason());
}
}
public DeviceStateReceiver(OpenVPNManagement magnagement) {
super();
mManagement = magnagement;
mManagement.setPauseCallback(this);
mDisconnectHandler = new Handler();
}
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = Preferences.getDefaultSharedPreferences(context);
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
networkStateChange(context);
} else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
boolean screenOffPause = prefs.getBoolean("screenoff", false);
if (screenOffPause) {
if (ProfileManager.getLastConnectedVpn() != null && !ProfileManager.getLastConnectedVpn().mPersistTun)
VpnStatus.logError(R.string.screen_nopersistenttun);
screen = connectState.PENDINGDISCONNECT;
fillTrafficData();
if (network == connectState.DISCONNECTED || userpause == connectState.DISCONNECTED)
screen = connectState.DISCONNECTED;
}
} else if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
// Network was disabled because screen off
boolean connected = shouldBeConnected();
screen = connectState.SHOULDBECONNECTED;
/* We should connect now, cancel any outstanding disconnect timer */
mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable);
/* should be connected has changed because the screen is on now, connect the VPN */
if (shouldBeConnected() != connected)
mManagement.resume();
else if (!shouldBeConnected())
/*Update the reason why we are still paused */
mManagement.pause(getPauseReason());
}
}
private void fillTrafficData() {
trafficdata.add(new Datapoint(System.currentTimeMillis(), TRAFFIC_LIMIT));
}
public static boolean equalsObj(Object a, Object b) {
return (a == null) ? (b == null) : a.equals(b);
}
public void networkStateChange(Context context) {
NetworkInfo networkInfo = getCurrentNetworkInfo(context);
SharedPreferences prefs = Preferences.getDefaultSharedPreferences(context);
boolean sendusr1 = prefs.getBoolean("netchangereconnect", true);
String netstatestring;
if (networkInfo == null) {
netstatestring = "not connected";
} else {
String subtype = networkInfo.getSubtypeName();
if (subtype == null)
subtype = "";
String extrainfo = networkInfo.getExtraInfo();
if (extrainfo == null)
extrainfo = "";
/*
if(networkInfo.getType()==android.net.ConnectivityManager.TYPE_WIFI) {
WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiinfo = wifiMgr.getConnectionInfo();
extrainfo+=wifiinfo.getBSSID();
subtype += wifiinfo.getNetworkId();
}*/
netstatestring = String.format("%2$s %4$s to %1$s %3$s", networkInfo.getTypeName(),
networkInfo.getDetailedState(), extrainfo, subtype);
}
if (networkInfo != null && networkInfo.getState() == State.CONNECTED) {
int newnet = networkInfo.getType();
boolean pendingDisconnect = (network == connectState.PENDINGDISCONNECT);
network = connectState.SHOULDBECONNECTED;
boolean sameNetwork;
if (lastConnectedNetwork == null
|| lastConnectedNetwork.getType() != networkInfo.getType()
|| !equalsObj(lastConnectedNetwork.getExtraInfo(), networkInfo.getExtraInfo())
)
sameNetwork = false;
else
sameNetwork = true;
/* Same network, connection still 'established' */
if (pendingDisconnect && sameNetwork) {
mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable);
// Reprotect the sockets just be sure
mManagement.networkChange(true);
} else {
/* Different network or connection not established anymore */
if (screen == connectState.PENDINGDISCONNECT)
screen = connectState.DISCONNECTED;
if (shouldBeConnected()) {
mDisconnectHandler.removeCallbacks(mDelayDisconnectRunnable);
if (pendingDisconnect || !sameNetwork)
mManagement.networkChange(sameNetwork);
else
mManagement.resume();
}
lastNetwork = newnet;
lastConnectedNetwork = networkInfo;
}
} else if (networkInfo == null) {
// Not connected, stop openvpn, set last connected network to no network
lastNetwork = -1;
if (sendusr1) {
network = connectState.PENDINGDISCONNECT;
mDisconnectHandler.postDelayed(mDelayDisconnectRunnable, DISCONNECT_WAIT * 1000);
}
}
if (!netstatestring.equals(lastStateMsg))
VpnStatus.logInfo(R.string.netstatus, netstatestring);
VpnStatus.logDebug(String.format("Debug state info: %s, pause: %s, shouldbeconnected: %s, network: %s ",
netstatestring, getPauseReason(), shouldBeConnected(), network));
lastStateMsg = netstatestring;
}
public boolean isUserPaused() {
return userpause == connectState.DISCONNECTED;
}
private boolean shouldBeConnected() {
return (screen == connectState.SHOULDBECONNECTED && userpause == connectState.SHOULDBECONNECTED &&
network == connectState.SHOULDBECONNECTED);
}
private pauseReason getPauseReason() {
if (userpause == connectState.DISCONNECTED)
return pauseReason.userPause;
if (screen == connectState.DISCONNECTED)
return pauseReason.screenOff;
if (network == connectState.DISCONNECTED)
return pauseReason.noNetwork;
return pauseReason.userPause;
}
private NetworkInfo getCurrentNetworkInfo(Context context) {
ConnectivityManager conn = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
return conn.getActiveNetworkInfo();
}
}
|