From 382f083fd1f82fe70e06c7af2f52c59ff96c9657 Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Fri, 20 Dec 2019 10:51:55 +0800 Subject: [PATCH] fix crash on tile start reset clash bandwidth on start add version name to about dialog --- app/build.gradle | 4 +- .../com/github/kr328/clash/MainActivity.kt | 10 +- app/src/main/res/layout/dialog_about.xml | 2 +- core/src/main/golang/bridge/init.go | 9 ++ core/src/main/golang/bridge/profiles.go | 4 - core/src/main/golang/clash | 2 +- .../java/com/github/kr328/clash/core/Clash.kt | 8 +- .../kr328/clash/service/ClashEventPoll.kt | 6 + .../kr328/clash/service/ClashEventService.kt | 132 ++++++++++-------- .../kr328/clash/service/ClashServiceImpl.kt | 1 + .../github/kr328/clash/service/TunService.kt | 14 +- 11 files changed, 111 insertions(+), 81 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7d9c0a7..bf8fd5e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,8 +13,8 @@ android { applicationId "com.github.kr328.clash" minSdkVersion 24 targetSdkVersion 29 - versionCode 10027 - versionName "1.0.27-alpha" + versionCode 10028 + versionName "1.0.28-alpha" } buildTypes { release { diff --git a/app/src/main/java/com/github/kr328/clash/MainActivity.kt b/app/src/main/java/com/github/kr328/clash/MainActivity.kt index 42c8bac..1b504ea 100644 --- a/app/src/main/java/com/github/kr328/clash/MainActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/MainActivity.kt @@ -3,8 +3,12 @@ package com.github.kr328.clash import android.app.Activity import android.app.AlertDialog import android.content.Intent +import android.content.pm.PackageInfo import android.os.Bundle +import android.view.LayoutInflater import android.view.View +import android.view.ViewGroup +import android.widget.TextView import com.github.kr328.clash.core.event.* import com.github.kr328.clash.core.model.General import com.github.kr328.clash.core.utils.ByteFormatter @@ -194,7 +198,11 @@ class MainActivity : BaseActivity() { } private fun showAboutDialog() { - AlertDialog.Builder(this).setView(R.layout.dialog_about).show() + val view = LayoutInflater.from(this).inflate(R.layout.dialog_about, window.decorView as ViewGroup?, false) + + view.findViewById(android.R.id.summary).text = packageManager.getPackageInfo(packageName, 0).let(PackageInfo::versionName) + + AlertDialog.Builder(this).setView(view).show() } override fun onErrorEvent(event: ErrorEvent?) { diff --git a/app/src/main/res/layout/dialog_about.xml b/app/src/main/res/layout/dialog_about.xml index e2601be..2db02de 100644 --- a/app/src/main/res/layout/dialog_about.xml +++ b/app/src/main/res/layout/dialog_about.xml @@ -19,9 +19,9 @@ android:layout_height="wrap_content" android:layout_toEndOf="@android:id/icon" /> \ No newline at end of file diff --git a/core/src/main/golang/bridge/init.go b/core/src/main/golang/bridge/init.go index f7baf7d..e1fa614 100644 --- a/core/src/main/golang/bridge/init.go +++ b/core/src/main/golang/bridge/init.go @@ -2,6 +2,15 @@ package bridge import "github.com/Dreamacro/clash/constant" +import "github.com/kr328/cfa/profile" + +import "github.com/Dreamacro/clash/tunnel" + func Init(home string) { constant.SetHomeDir(home) } + +func Reset() { + profile.LoadDefault() + tunnel.DefaultManager.Reset() +} diff --git a/core/src/main/golang/bridge/profiles.go b/core/src/main/golang/bridge/profiles.go index 1245bee..5decb71 100644 --- a/core/src/main/golang/bridge/profiles.go +++ b/core/src/main/golang/bridge/profiles.go @@ -6,10 +6,6 @@ func LoadProfileFile(path string) error { return profile.LoadFromFile(path) } -func LoadProfileDefault() { - profile.LoadDefault() -} - func CheckProfileValid(profileData string) error { return profile.CheckValid(profileData) } diff --git a/core/src/main/golang/clash b/core/src/main/golang/clash index c00e907..c351009 160000 --- a/core/src/main/golang/clash +++ b/core/src/main/golang/clash @@ -1 +1 @@ -Subproject commit c00e907d953d71c4c2abf603c1c7dbf82287b4b3 +Subproject commit c351009e6d5b208650e13ed2f9492568740211ee diff --git a/core/src/main/java/com/github/kr328/clash/core/Clash.kt b/core/src/main/java/com/github/kr328/clash/core/Clash.kt index 2aa0d1e..d524e29 100644 --- a/core/src/main/java/com/github/kr328/clash/core/Clash.kt +++ b/core/src/main/java/com/github/kr328/clash/core/Clash.kt @@ -62,7 +62,7 @@ class Clash( listener(currentProcess) - loadDefault() + Bridge.reset() } fun stop() { @@ -73,7 +73,7 @@ class Clash( listener(currentProcess) - loadDefault() + Bridge.reset() } fun startTunDevice( @@ -173,10 +173,6 @@ class Clash( }) } - private fun loadDefault() { - Bridge.loadProfileDefault() - } - private fun enforceStarted() { if ( currentProcess == ProcessEvent.STOPPED ) throw IllegalStateException("Clash Stopped") diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashEventPoll.kt b/service/src/main/java/com/github/kr328/clash/service/ClashEventPoll.kt index 3744244..0415b46 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashEventPoll.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashEventPoll.kt @@ -75,4 +75,10 @@ class ClashEventPoll(private val clash: Clash, private val master: Master) { logs = null } } + + fun shutdown() { + stopTrafficPoll() + stopBandwidthPoll() + stopLogPoll() + } } \ No newline at end of file diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashEventService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashEventService.kt index 70e4a7a..782a0b7 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashEventService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashEventService.kt @@ -20,79 +20,87 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub() ) private val observers = mutableMapOf() - private val handler = Executors.newSingleThreadExecutor() + private val executor = Executors.newSingleThreadExecutor() private var currentProcessEvent = ProcessEvent.STOPPED fun performProcessEvent(event: ProcessEvent) { - handler.submit { - currentProcessEvent = event + if (!executor.isShutdown) + executor.submit { + currentProcessEvent = event - observers.values.forEach { - it.observer.onProcessEvent(event) + observers.values.forEach { + it.observer.onProcessEvent(event) + } } - } } fun performLogEvent(event: LogEvent) { - handler.submit { - observers.values.forEach { - if (it.acquiredEvent.contains(Event.EVENT_LOG)) - it.observer.onLogEvent(event) + if (!executor.isShutdown) + executor.submit { + observers.values.forEach { + if (it.acquiredEvent.contains(Event.EVENT_LOG)) + it.observer.onLogEvent(event) + } } - } } fun performSpeedEvent(event: TrafficEvent) { - handler.submit { - observers.values.forEach { - if (it.acquiredEvent.contains(Event.EVENT_TRAFFIC)) - it.observer.onTrafficEvent(event) + if (!executor.isShutdown) + executor.submit { + observers.values.forEach { + if (it.acquiredEvent.contains(Event.EVENT_TRAFFIC)) + it.observer.onTrafficEvent(event) + } } - } } fun performBandwidthEvent(event: BandwidthEvent) { - handler.submit { - observers.values.forEach { - if ( it.acquiredEvent.contains(Event.EVENT_BANDWIDTH) ) - it.observer.onBandwidthEvent(event) + if (!executor.isShutdown) + executor.submit { + observers.values.forEach { + if (it.acquiredEvent.contains(Event.EVENT_BANDWIDTH)) + it.observer.onBandwidthEvent(event) + } } - } } fun performErrorEvent(event: ErrorEvent) { - handler.submit { - observers.values.forEach { - it.observer.onErrorEvent(event) + if (!executor.isShutdown) + executor.submit { + observers.values.forEach { + it.observer.onErrorEvent(event) + } } - } } fun performProfileChangedEvent(event: ProfileChangedEvent) { - handler.submit { - observers.values.forEach { - it.observer.onProfileChanged(event) + if (!executor.isShutdown) + executor.submit { + observers.values.forEach { + it.observer.onProfileChanged(event) + } } - } } fun performProfileReloadEvent(event: ProfileReloadEvent) { - handler.submit { - observers.values.forEach { - it.observer.onProfileReloaded(event) + if (!executor.isShutdown) + executor.submit { + observers.values.forEach { + it.observer.onProfileReloaded(event) + } } - } } override fun unregisterEventObserver(id: String?) { - handler.submit { - require(id != null) + if (!executor.isShutdown) + executor.submit { + require(id != null) - observers.remove(id) + observers.remove(id) - recastEventRequirement() - } + recastEventRequirement() + } } override fun registerEventObserver( @@ -100,38 +108,40 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub() observer: IClashEventObserver?, events: IntArray? ) { - handler.submit { - require(id != null && observer != null && events != null) + if (!executor.isShutdown) + executor.submit { + require(id != null && observer != null && events != null) - val initial = !observers.containsKey(id) + val initial = !observers.containsKey(id) - observers[id] = EventObserverRecord(observer, events.toSet()) + observers[id] = EventObserverRecord(observer, events.toSet()) - observer.asBinder().linkToDeath({ - unregisterEventObserver(id) - }, 0) + observer.asBinder().linkToDeath({ + unregisterEventObserver(id) + }, 0) - recastEventRequirement() + recastEventRequirement() - if (initial) { - observer.onProcessEvent(currentProcessEvent) + if (initial) { + observer.onProcessEvent(currentProcessEvent) + } } - } - } - - fun shutdown() { - handler.shutdown() } fun recastEventRequirement() { - handler.submit { - val req = observers.values.flatMap { - it.acquiredEvent - }.toSet() - val rel = EVENT_SET - req + if (!executor.isShutdown) + executor.submit { + val req = observers.values.flatMap { + it.acquiredEvent + }.toSet() + val rel = EVENT_SET - req - req.forEach(master::acquireEvent) - rel.forEach(master::releaseEvent) - } + req.forEach(master::acquireEvent) + rel.forEach(master::releaseEvent) + } + } + + fun shutdown() { + executor.shutdown() } } \ No newline at end of file diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashServiceImpl.kt b/service/src/main/java/com/github/kr328/clash/service/ClashServiceImpl.kt index eb53efb..df33882 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashServiceImpl.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashServiceImpl.kt @@ -113,6 +113,7 @@ class ClashServiceImpl(clashService: ClashService) : IClashService.Stub() { } fun shutdown() { + eventPoll.shutdown() eventService.shutdown() } } \ No newline at end of file diff --git a/service/src/main/java/com/github/kr328/clash/service/TunService.kt b/service/src/main/java/com/github/kr328/clash/service/TunService.kt index 5f4a348..9969faf 100644 --- a/service/src/main/java/com/github/kr328/clash/service/TunService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/TunService.kt @@ -10,13 +10,17 @@ import android.os.* import com.github.kr328.clash.core.event.* import com.github.kr328.clash.core.utils.Log import com.github.kr328.clash.service.net.DefaultNetworkObserver +import java.net.Inet4Address +import java.net.InetAddress class TunService : VpnService(), IClashEventObserver { companion object { // from https://github.com/shadowsocks/shadowsocks-android/blob/master/core/src/main/java/com/github/shadowsocks/bg/VpnService.kt private const val VPN_MTU = 1500 - private const val PRIVATE_VLAN_DNS = "172.19.0.2" - private const val PRIVATE_VLAN4_CLIENT = "172.19.0.1" + private const val PRIVATE_VLAN4_SUBNET = 30 + private const val PRIVATE_VLAN4_CLIENT = "172.255.255.253" + private const val PRIVATE_VLAN_DNS = "172.255.255.254" + private const val VLAN4_ANY = "0.0.0.0" } private var start = true @@ -114,7 +118,7 @@ class TunService : VpnService(), IClashEventObserver { if ( settings.isDnsHijackingEnabled ) { clash.clash.startTunDevice( fileDescriptor.fd, VPN_MTU, - "$PRIVATE_VLAN4_CLIENT/30", "0.0.0.0" + "$PRIVATE_VLAN4_CLIENT/$PRIVATE_VLAN4_SUBNET", VLAN4_ANY ) { protect(it.toInt()) } @@ -122,7 +126,7 @@ class TunService : VpnService(), IClashEventObserver { else { clash.clash.startTunDevice( fileDescriptor.fd, VPN_MTU, - "$PRIVATE_VLAN4_CLIENT/30", PRIVATE_VLAN_DNS + "$PRIVATE_VLAN4_CLIENT/$PRIVATE_VLAN4_SUBNET", PRIVATE_VLAN_DNS ) { protect(it.toInt()) } @@ -195,7 +199,7 @@ class TunService : VpnService(), IClashEventObserver { } private fun Builder.addAddress(): Builder { - addAddress(PRIVATE_VLAN4_CLIENT, 30) + addAddress(PRIVATE_VLAN4_CLIENT, PRIVATE_VLAN4_SUBNET) return this }