File tree Expand file tree Collapse file tree 5 files changed +58
-3
lines changed
java/eu/neilalexander/yggdrasil Expand file tree Collapse file tree 5 files changed +58
-3
lines changed Original file line number Diff line number Diff line change @@ -11,8 +11,8 @@ android {
1111 applicationId " eu.neilalexander.yggdrasil"
1212 minSdkVersion 21
1313 targetSdkVersion 29
14- versionCode 9
15- versionName " 0.1"
14+ versionCode 10
15+ versionName " 0.1-010 "
1616
1717 testInstrumentationRunner " androidx.test.runner.AndroidJUnitRunner"
1818 }
Original file line number Diff line number Diff line change 33 package =" eu.neilalexander.yggdrasil" >
44 <uses-permission android : name =" android.permission.INTERNET" />
55 <uses-permission android : name =" android.permission.CHANGE_WIFI_MULTICAST_STATE" />
6+ <uses-permission android : name =" android.permission.ACCESS_NETWORK_STATE" />
67
78 <application
89 android : name =" .GlobalApplication"
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ class GlobalApplication: Application() {
99 override fun onCreate () {
1010 super .onCreate()
1111 config = ConfigurationProxy (applicationContext)
12+ val callback = NetworkStateCallback (this )
1213 }
1314
1415 fun subscribe () {
Original file line number Diff line number Diff line change 1+ package eu.neilalexander.yggdrasil
2+
3+ import android.content.Context
4+ import android.content.Intent
5+ import android.net.*
6+ import android.util.Log
7+
8+
9+ private const val TAG = " Network"
10+
11+ class NetworkStateCallback (val context : Context ) : ConnectivityManager.NetworkCallback() {
12+
13+ init {
14+ val request = NetworkRequest .Builder ()
15+ .addTransportType(NetworkCapabilities .TRANSPORT_WIFI )
16+ .addTransportType(NetworkCapabilities .TRANSPORT_CELLULAR )
17+ .addTransportType(NetworkCapabilities .TRANSPORT_ETHERNET )
18+ .build()
19+
20+ val manager = context.getSystemService(Context .CONNECTIVITY_SERVICE ) as ConnectivityManager
21+ manager.registerNetworkCallback(request, this )
22+ }
23+
24+ override fun onAvailable (network : Network ) {
25+ super .onAvailable(network)
26+ Log .d(TAG , " onAvailable" )
27+
28+ Thread {
29+ // The message often arrives before the connection is fully established
30+ Thread .sleep(1000 )
31+ val intent = Intent (context, PacketTunnelProvider ::class .java)
32+ intent.action = PacketTunnelProvider .ACTION_CONNECT
33+ context.startService(intent)
34+ }.start()
35+ }
36+
37+ override fun onLost (network : Network ) {
38+ super .onLost(network)
39+ Log .d(TAG , " onLost" )
40+ }
41+ }
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ class PacketTunnelProvider: VpnService() {
2121
2222 const val ACTION_START = " eu.neilalexander.yggdrasil.PacketTunnelProvider.START"
2323 const val ACTION_STOP = " eu.neilalexander.yggdrasil.PacketTunnelProvider.STOP"
24+ const val ACTION_CONNECT = " eu.neilalexander.yggdrasil.PacketTunnelProvider.CONNECT"
2425 }
2526
2627 private var yggdrasil = Yggdrasil ()
@@ -56,6 +57,10 @@ class PacketTunnelProvider: VpnService() {
5657 Log .d(TAG , " Stopping..." )
5758 stop(); START_NOT_STICKY
5859 }
60+ ACTION_CONNECT -> {
61+ Log .d(TAG , " Connecting..." )
62+ connect(); START_STICKY
63+ }
5964 else -> {
6065 Log .d(TAG , " Starting..." )
6166 start(); START_STICKY
@@ -181,6 +186,13 @@ class PacketTunnelProvider: VpnService() {
181186 stopSelf()
182187 }
183188
189+ private fun connect () {
190+ if (! started.get()) {
191+ return
192+ }
193+ yggdrasil.retryPeersNow()
194+ }
195+
184196 private fun updater () {
185197 updates@ while (started.get()) {
186198 if ((application as GlobalApplication ).needUiUpdates()) {
@@ -260,4 +272,4 @@ class PacketTunnelProvider: VpnService() {
260272 readerStream = null
261273 }
262274 }
263- }
275+ }
You can’t perform that action at this time.
0 commit comments