fix vpn close when getting new subnet route (#179)
Some checks are pending
EasyTier Core / pre_job (push) Waiting to run
EasyTier Core / build (macos-latest, aarch64-apple-darwin) (push) Blocked by required conditions
EasyTier Core / build (macos-latest, x86_64-apple-darwin) (push) Blocked by required conditions
EasyTier Core / build (ubuntu-latest, aarch64-unknown-linux-musl) (push) Blocked by required conditions
EasyTier Core / build (ubuntu-latest, arm-unknown-linux-musleabi) (push) Blocked by required conditions
EasyTier Core / build (ubuntu-latest, arm-unknown-linux-musleabihf) (push) Blocked by required conditions
EasyTier Core / build (ubuntu-latest, armv7-unknown-linux-musleabi) (push) Blocked by required conditions
EasyTier Core / build (ubuntu-latest, armv7-unknown-linux-musleabihf) (push) Blocked by required conditions
EasyTier Core / build (ubuntu-latest, mips-unknown-linux-musl) (push) Blocked by required conditions
EasyTier Core / build (ubuntu-latest, mipsel-unknown-linux-musl) (push) Blocked by required conditions
EasyTier Core / build (ubuntu-latest, x86_64-unknown-linux-musl) (push) Blocked by required conditions
EasyTier Core / build (windows-latest, x86_64-pc-windows-msvc) (push) Blocked by required conditions
EasyTier Core / core-result (push) Blocked by required conditions
EasyTier GUI / pre_job (push) Waiting to run
EasyTier GUI / build-gui (aarch64-apple-darwin, macos-latest, aarch64-apple-darwin) (push) Blocked by required conditions
EasyTier GUI / build-gui (aarch64-unknown-linux-gnu, ubuntu-latest, aarch64-unknown-linux-musl) (push) Blocked by required conditions
EasyTier GUI / build-gui (x86_64-apple-darwin, macos-latest, x86_64-apple-darwin) (push) Blocked by required conditions
EasyTier GUI / build-gui (x86_64-pc-windows-msvc, windows-latest, x86_64-pc-windows-msvc) (push) Blocked by required conditions
EasyTier GUI / build-gui (x86_64-unknown-linux-gnu, ubuntu-latest, x86_64-unknown-linux-musl) (push) Blocked by required conditions
EasyTier GUI / gui-result (push) Blocked by required conditions
EasyTier Mobile / pre_job (push) Waiting to run
EasyTier Mobile / build-mobile (ubuntu-latest, android) (push) Blocked by required conditions
EasyTier Mobile / mobile-result (push) Blocked by required conditions
EasyTier Test / pre_job (push) Waiting to run
EasyTier Test / test (push) Blocked by required conditions

This commit is contained in:
Sijie.Sun 2024-07-18 08:48:45 +08:00 committed by GitHub
parent 5987528f59
commit 8858492fb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 31 deletions

View File

@ -39,6 +39,7 @@ async function doStopVpn() {
await waitVpnStatus(false, 3)
curVpnStatus.ipv4Addr = undefined
curVpnStatus.routes = []
}
async function doStartVpn(ipv4Addr: string, cidr: number, routes: string[]) {
@ -73,8 +74,6 @@ async function onVpnServiceStart(payload: any) {
async function onVpnServiceStop(payload: any) {
console.log('vpn service stop', JSON.stringify(payload))
curVpnStatus.running = false
networkStore.clearNetworkInstances()
await retainNetworkInstance(networkStore.networkInstanceIds)
}
async function registerVpnServiceListener() {
@ -111,8 +110,7 @@ function getRoutesForVpn(routes: Route[]): string[] {
return Array.from(new Set(ret)).sort()
}
async function watchNetworkInstance() {
networkStore.$subscribe(async () => {
async function onNetworkInstanceChange() {
let insts = networkStore.networkInstanceIds
if (!insts) {
await doStopVpn()
@ -138,10 +136,38 @@ async function watchNetworkInstance() {
if (ipChanged || routesChanged) {
console.log('virtual ip changed', JSON.stringify(curVpnStatus), virtual_ip)
try {
await doStopVpn()
} catch (e) {
console.error(e)
}
try {
await doStartVpn(virtual_ip, 24, routes)
} catch (e) {
console.error("start vpn failed, clear all network insts.", e)
networkStore.clearNetworkInstances()
await retainNetworkInstance(networkStore.networkInstanceIds)
}
return
}
}
async function watchNetworkInstance() {
var subscribe_running = false
networkStore.$subscribe(async () => {
if (subscribe_running) {
return
}
console.log('network instance change')
subscribe_running = true
try {
await onNetworkInstanceChange()
} catch (_) {
}
console.log('network instance change done')
subscribe_running = false
})
}

View File

@ -57,22 +57,24 @@ class TauriVpnService : VpnService() {
override fun onDestroy() {
println("vpn on destroy")
self = null
super.onDestroy()
disconnect()
self = null
}
override fun onRevoke() {
println("vpn on revoke")
self = null
super.onRevoke()
disconnect()
self = null
}
private fun disconnect() {
if (self == this && this::vpnInterface.isInitialized) {
triggerCallback("vpn_service_stop", JSObject())
vpnInterface.close()
}
}
private fun createVpnInterface(args: Bundle?): ParcelFileDescriptor {
var builder = Builder()