Add system proxy toggle

This commit is contained in:
世界
2023-09-03 22:55:19 +08:00
parent 1c8cc6b3cf
commit 03f9dc4883
8 changed files with 95 additions and 9 deletions

View File

@@ -19,6 +19,7 @@ import io.nekohasekai.libbox.CommandServerHandler
import io.nekohasekai.libbox.Libbox
import io.nekohasekai.libbox.PProfServer
import io.nekohasekai.libbox.PlatformInterface
import io.nekohasekai.libbox.SystemProxyStatus
import io.nekohasekai.sfa.Application
import io.nekohasekai.sfa.constant.Action
import io.nekohasekai.sfa.constant.Alert
@@ -164,6 +165,7 @@ class BoxService(
}
override fun serviceReload() {
status.postValue(Status.Starting)
GlobalScope.launch(Dispatchers.IO) {
val pfd = fileDescriptor
if (pfd != null) {
@@ -184,6 +186,19 @@ class BoxService(
}
}
override fun getSystemProxyStatus(): SystemProxyStatus {
val status = SystemProxyStatus()
if (service is VPNService) {
status.available = service.systemProxyAvailable
status.enabled = service.systemProxyEnabled
}
return status
}
override fun setSystemProxyEnabled(isEnabled: Boolean) {
serviceReload()
}
@RequiresApi(Build.VERSION_CODES.M)
private fun serviceUpdateIdleMode() {
if (Application.powerManager.isDeviceIdleMode) {

View File

@@ -32,6 +32,9 @@ class VPNService : VpnService(), PlatformInterfaceWrapper {
protect(fd)
}
var systemProxyAvailable = false
var systemProxyEnabled = false
override fun openTun(options: TunOptions): Int {
if (prepare(this) != null) error("android: missing vpn permission")
@@ -124,8 +127,10 @@ class VPNService : VpnService(), PlatformInterfaceWrapper {
}
if (options.isHTTPProxyEnabled) {
systemProxyAvailable = true
systemProxyEnabled = Settings.systemProxyEnabled
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
builder.setHttpProxy(
if (systemProxyEnabled) builder.setHttpProxy(
ProxyInfo.buildDirectProxy(
options.httpProxyServer,
options.httpProxyServerPort
@@ -134,6 +139,9 @@ class VPNService : VpnService(), PlatformInterfaceWrapper {
} else {
error("android: tun.platform.http_proxy requires android 10 or higher")
}
} else {
systemProxyAvailable = false
systemProxyEnabled = false
}
val pfd =