7
7
8
8
import android .content .Context ;
9
9
import android .content .Intent ;
10
- import android .os .Build ;
10
+ import android .net .VpnService .Builder ;
11
+ import android .os .Build .VERSION ;
12
+ import android .os .Build .VERSION_CODES ;
11
13
import android .os .ParcelFileDescriptor ;
12
14
import android .system .OsConstants ;
13
15
import android .util .Log ;
24
26
import com .wireguard .util .NonNullForAll ;
25
27
26
28
import java .net .InetAddress ;
27
- import java .time .Instant ;
28
29
import java .util .Collections ;
29
30
import java .util .Set ;
30
31
import java .util .concurrent .ExecutionException ;
31
32
import java .util .concurrent .FutureTask ;
32
33
import java .util .concurrent .LinkedBlockingQueue ;
33
34
import java .util .concurrent .TimeUnit ;
34
35
import java .util .concurrent .TimeoutException ;
36
+ import java .util .regex .Pattern ;
35
37
36
38
import androidx .annotation .Nullable ;
37
39
import androidx .collection .ArraySet ;
43
45
@ NonNullForAll
44
46
public final class GoBackend implements Backend {
45
47
private static final int DNS_RESOLUTION_RETRIES = 10 ;
48
+ private static final Pattern COMPILE = Pattern .compile ("\\ n" );
46
49
private static final String TAG = "WireGuard/GoBackend" ;
47
50
@ Nullable private static AlwaysOnCallback alwaysOnCallback ;
48
51
private static GhettoCompletableFuture <VpnService > vpnService = new GhettoCompletableFuture <>();
@@ -127,7 +130,7 @@ public Statistics getStatistics(final Tunnel tunnel) {
127
130
long rx = 0 ;
128
131
long tx = 0 ;
129
132
long latestHandshakeMSec = 0 ;
130
- for (final String line : config .split (" \\ n" )) {
133
+ for (final String line : COMPILE .split (config )) {
131
134
if (line .startsWith ("public_key=" )) {
132
135
if (key != null )
133
136
stats .add (key , rx , tx , latestHandshakeMSec );
@@ -212,7 +215,7 @@ public State setState(final Tunnel tunnel, State state, @Nullable final Config c
212
215
if (currentTunnel != null )
213
216
setStateInternal (currentTunnel , null , State .DOWN );
214
217
try {
215
- setStateInternal (tunnel , config , state );
218
+ setStateInternal (tunnel , config , State . UP );
216
219
} catch (final Exception e ) {
217
220
if (originalTunnel != null )
218
221
setStateInternal (originalTunnel , originalConfig , State .UP );
@@ -232,7 +235,7 @@ private void setStateInternal(final Tunnel tunnel, @Nullable final Config config
232
235
if (config == null )
233
236
throw new BackendException (Reason .TUNNEL_MISSING_CONFIG );
234
237
235
- if (VpnService .prepare (context ) != null )
238
+ if (android . net . VpnService .prepare (context ) != null )
236
239
throw new BackendException (Reason .VPN_NOT_AUTHORIZED );
237
240
238
241
final VpnService service ;
@@ -278,7 +281,7 @@ private void setStateInternal(final Tunnel tunnel, @Nullable final Config config
278
281
final String goConfig = config .toWgUserspaceString ();
279
282
280
283
// Create the vpn tunnel with android API
281
- final VpnService . Builder builder = service .getBuilder ();
284
+ final Builder builder = service .getBuilder ();
282
285
builder .setSession (tunnel .getName ());
283
286
284
287
for (final String excludedApplication : config .getInterface ().getExcludedApplications ())
@@ -313,9 +316,9 @@ private void setStateInternal(final Tunnel tunnel, @Nullable final Config config
313
316
314
317
builder .setMtu (config .getInterface ().getMtu ().orElse (1280 ));
315
318
316
- if (Build . VERSION .SDK_INT >= Build . VERSION_CODES .Q )
319
+ if (VERSION .SDK_INT >= VERSION_CODES .Q )
317
320
builder .setMetered (false );
318
- if (Build . VERSION .SDK_INT >= Build . VERSION_CODES .M )
321
+ if (VERSION .SDK_INT >= VERSION_CODES .M )
319
322
service .setUnderlyingNetworks (null );
320
323
321
324
builder .setBlocking (true );
@@ -338,7 +341,7 @@ private void setStateInternal(final Tunnel tunnel, @Nullable final Config config
338
341
Log .w (TAG , "Tunnel already down" );
339
342
return ;
340
343
}
341
- int handleToClose = currentTunnelHandle ;
344
+ final int handleToClose = currentTunnelHandle ;
342
345
currentTunnel = null ;
343
346
currentTunnelHandle = -1 ;
344
347
currentConfig = null ;
0 commit comments