Skip to content

Commit

Permalink
Build 8 (improve always-on VPN)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilalexander committed Oct 29, 2021
1 parent 5887ae4 commit d10bb85
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
applicationId "eu.neilalexander.yggdrasil"
minSdkVersion 21
targetSdkVersion 29
versionCode 6
versionCode 7
versionName "0.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eu.neilalexander.yggdrasil">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />

Expand Down
37 changes: 20 additions & 17 deletions app/src/main/java/eu/neilalexander/yggdrasil/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.neilalexander.yggdrasil

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
Expand All @@ -10,6 +11,7 @@ import android.os.Bundle
import android.widget.Switch
import android.widget.TableRow
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import mobile.Mobile
Expand All @@ -27,6 +29,18 @@ class MainActivity : AppCompatActivity() {
private lateinit var peersRow: TableRow
private lateinit var settingsRow: TableRow

private fun start() {
val intent = Intent(this, PacketTunnelProvider::class.java)
intent.action = PacketTunnelProvider.ACTION_START
startService(intent)
}

private var startVpnActivity = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
start()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -44,16 +58,14 @@ class MainActivity : AppCompatActivity() {

enabledLabel.setTextColor(Color.GRAY)

VpnService.prepare(this)

enabledSwitch.setOnCheckedChangeListener { _, isChecked ->
when (isChecked) {
true -> {
val vpnintent = VpnService.prepare(this)
if (vpnintent != null) {
startActivityForResult(vpnintent, 0)
val vpnIntent = VpnService.prepare(this)
if (vpnIntent != null) {
startVpnActivity.launch(vpnIntent)
} else {
onActivityResult(0, RESULT_OK, vpnintent)
start()
}
}
false -> {
Expand Down Expand Up @@ -89,6 +101,7 @@ class MainActivity : AppCompatActivity() {
when (intent.getStringExtra("type")) {
"state" -> {
enabledLabel.text = if (intent.getBooleanExtra("started", false)) {
enabledSwitch.isChecked = true
if (state.dhtCount() == 0) {
enabledLabel.setTextColor(Color.RED)
"No connectivity"
Expand All @@ -97,6 +110,7 @@ class MainActivity : AppCompatActivity() {
"Enabled"
}
} else {
enabledSwitch.isChecked = false
enabledLabel.setTextColor(Color.GRAY)
"Not enabled"
}
Expand All @@ -117,15 +131,4 @@ class MainActivity : AppCompatActivity() {
super.onPause()
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (resultCode) {
RESULT_OK -> {
val intent = Intent(this, PacketTunnelProvider::class.java)
intent.action = PacketTunnelProvider.ACTION_START
startService(intent)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.neilalexander.yggdrasil

import android.content.Intent
import android.content.*
import android.net.Uri
import android.net.VpnService
import android.os.Handler
Expand Down Expand Up @@ -50,17 +50,17 @@ class PacketTunnelProvider: VpnService() {

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent == null) {
Log.d("PacketTunnelProvider", "Intent is null")
return START_NOT_STICKY
}
return when (intent.action ?: ACTION_STOP) {
ACTION_START -> {
start(); START_STICKY
}
ACTION_STOP -> {
Log.d("PacketTunnelProvider", "Stopping...")
stop(); START_NOT_STICKY
}
else -> {
stop(); START_NOT_STICKY
Log.d("PacketTunnelProvider", "Starting...")
start(); START_STICKY
}
}
}
Expand Down Expand Up @@ -169,7 +169,7 @@ class PacketTunnelProvider: VpnService() {
break@updates
}
try {
Thread.sleep(2000)
Thread.sleep(1000)
} catch (e: java.lang.InterruptedException) {
return
}
Expand Down

0 comments on commit d10bb85

Please sign in to comment.