Skip to content

Commit

Permalink
Made fast reconnect when network becomes up. (#28)
Browse files Browse the repository at this point in the history
* Made fast reconnect when network becomes up.
* Use `retryPeersNow`
  • Loading branch information
Revertron authored Nov 12, 2022
1 parent 41569a9 commit c9476a7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "eu.neilalexander.yggdrasil"
minSdkVersion 21
targetSdkVersion 29
versionCode 9
versionName "0.1"
versionCode 10
versionName "0.1-010"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="eu.neilalexander.yggdrasil">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name=".GlobalApplication"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class GlobalApplication: Application() {
override fun onCreate() {
super.onCreate()
config = ConfigurationProxy(applicationContext)
val callback = NetworkStateCallback(this)
}

fun subscribe() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package eu.neilalexander.yggdrasil

import android.content.Context
import android.content.Intent
import android.net.*
import android.util.Log


private const val TAG = "Network"

class NetworkStateCallback(val context: Context) : ConnectivityManager.NetworkCallback() {

init {
val request = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
.build()

val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
manager.registerNetworkCallback(request, this)
}

override fun onAvailable(network: Network) {
super.onAvailable(network)
Log.d(TAG, "onAvailable")

Thread {
// The message often arrives before the connection is fully established
Thread.sleep(1000)
val intent = Intent(context, PacketTunnelProvider::class.java)
intent.action = PacketTunnelProvider.ACTION_CONNECT
context.startService(intent)
}.start()
}

override fun onLost(network: Network) {
super.onLost(network)
Log.d(TAG, "onLost")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PacketTunnelProvider: VpnService() {

const val ACTION_START = "eu.neilalexander.yggdrasil.PacketTunnelProvider.START"
const val ACTION_STOP = "eu.neilalexander.yggdrasil.PacketTunnelProvider.STOP"
const val ACTION_CONNECT = "eu.neilalexander.yggdrasil.PacketTunnelProvider.CONNECT"
}

private var yggdrasil = Yggdrasil()
Expand Down Expand Up @@ -56,6 +57,10 @@ class PacketTunnelProvider: VpnService() {
Log.d(TAG, "Stopping...")
stop(); START_NOT_STICKY
}
ACTION_CONNECT -> {
Log.d(TAG, "Connecting...")
connect(); START_STICKY
}
else -> {
Log.d(TAG, "Starting...")
start(); START_STICKY
Expand Down Expand Up @@ -181,6 +186,13 @@ class PacketTunnelProvider: VpnService() {
stopSelf()
}

private fun connect() {
if (!started.get()) {
return
}
yggdrasil.retryPeersNow()
}

private fun updater() {
updates@ while (started.get()) {
if ((application as GlobalApplication).needUiUpdates()) {
Expand Down Expand Up @@ -260,4 +272,4 @@ class PacketTunnelProvider: VpnService() {
readerStream = null
}
}
}
}

0 comments on commit c9476a7

Please sign in to comment.