Skip to content

Commit 0db4e00

Browse files
committed
Migrate self-hooking VPN hide module to libxposed API 101
1 parent a5f9769 commit 0db4e00

18 files changed

Lines changed: 15 additions & 1571 deletions

File tree

app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ dependencies {
323323

324324
// Xposed API for self-hooking VPN hide module
325325
compileOnly("de.robv.android.xposed:api:82")
326-
compileOnly(project(":libxposed-api"))
326+
compileOnly("io.github.libxposed:api:101.0.1")
327327
}
328328

329329
val playCredentialsJSON = rootProject.file("service-account-credentials.json")
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.nekohasekai.sfa.xposed
22

3-
import android.content.Context
43
import io.nekohasekai.sfa.xposed.hooks.HookIConnectivityManagerOnTransact
54
import io.nekohasekai.sfa.xposed.hooks.hidevpn.ConnectivityServiceHookHelper
65
import io.nekohasekai.sfa.xposed.hooks.hidevpn.HookNetworkCapabilitiesWriteToParcel
@@ -11,16 +10,11 @@ object HookInstaller {
1110

1211
private const val TAG = "XposedInit"
1312

14-
private val activityThreadClass by lazy { Class.forName("android.app.ActivityThread") }
15-
private val currentActivityThreadMethod by lazy { activityThreadClass.getMethod("currentActivityThread") }
16-
private val getSystemContextMethod by lazy { activityThreadClass.getMethod("getSystemContext") }
17-
1813
fun install(classLoader: ClassLoader) {
19-
val systemContext = resolveSystemContext()
20-
HookErrorStore.i(TAG, "handleSystemServerLoaded")
14+
HookErrorStore.i(TAG, "handleSystemServerStarting")
2115
val hooks = arrayOf(
2216
ConnectivityServiceHookHelper(classLoader),
23-
HookIConnectivityManagerOnTransact(classLoader, systemContext),
17+
HookIConnectivityManagerOnTransact(classLoader),
2418
HookPackageManagerGetInstalledPackages(classLoader),
2519
HookNetworkCapabilitiesWriteToParcel(),
2620
HookNetworkInterfaceGetName(classLoader),
@@ -38,12 +32,4 @@ object HookInstaller {
3832
}
3933
}
4034
}
41-
42-
private fun resolveSystemContext(): Context? = try {
43-
val currentThread = currentActivityThreadMethod.invoke(null)
44-
getSystemContextMethod.invoke(currentThread) as? Context
45-
} catch (e: Throwable) {
46-
HookErrorStore.e(TAG, "resolveSystemContext failed", e)
47-
null
48-
}
4935
}

app/src/main/java/io/nekohasekai/sfa/xposed/XposedInit.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package io.nekohasekai.sfa.xposed
22

3-
import io.github.libxposed.api.XposedInterface
43
import io.github.libxposed.api.XposedModule
5-
import io.github.libxposed.api.XposedModuleInterface
4+
import io.github.libxposed.api.XposedModuleInterface.ModuleLoadedParam
5+
import io.github.libxposed.api.XposedModuleInterface.SystemServerStartingParam
66

7-
class XposedInit(base: XposedInterface, param: XposedModuleInterface.ModuleLoadedParam) : XposedModule(base, param) {
7+
class XposedInit : XposedModule() {
88

9-
override fun onSystemServerLoaded(param: XposedModuleInterface.SystemServerLoadedParam) {
9+
override fun onModuleLoaded(param: ModuleLoadedParam) {
10+
HookErrorStore.i("XposedInit", "onModuleLoaded process=${param.processName} system=${param.isSystemServer}")
11+
}
12+
13+
override fun onSystemServerStarting(param: SystemServerStartingParam) {
1014
HookInstaller.install(param.classLoader)
1115
}
1216

app/src/main/java/io/nekohasekai/sfa/xposed/XposedInit101.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

app/src/main/java/io/nekohasekai/sfa/xposed/hooks/IConnectivityManager+onTransact.kt

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.nekohasekai.sfa.xposed.hooks
22

3-
import android.content.Context
43
import android.content.pm.PackageInfo
54
import android.os.Binder
65
import android.os.Parcel
@@ -12,8 +11,9 @@ import io.nekohasekai.sfa.xposed.HookErrorStore
1211
import io.nekohasekai.sfa.xposed.HookStatusKeys
1312
import io.nekohasekai.sfa.xposed.HookStatusStore
1413
import io.nekohasekai.sfa.xposed.PrivilegeSettingsStore
14+
import io.nekohasekai.sfa.xposed.VpnAppStore
1515

16-
class HookIConnectivityManagerOnTransact(private val classLoader: ClassLoader, private val context: Context?) : XHook {
16+
class HookIConnectivityManagerOnTransact(private val classLoader: ClassLoader) : XHook {
1717
private companion object {
1818
private const val SOURCE = "HookIConnectivityManagerOnTransact"
1919
}
@@ -109,17 +109,8 @@ class HookIConnectivityManagerOnTransact(private val classLoader: ClassLoader, p
109109
private fun isCallerAllowed(): Boolean {
110110
val uid = Binder.getCallingUid()
111111
if (uid == 0) return true
112-
val pm = context?.packageManager
113-
if (pm == null) {
114-
HookErrorStore.e(SOURCE, "isCallerAllowed: context or packageManager is null, uid=$uid")
115-
return false
116-
}
117112
return try {
118-
val packages = pm.getPackagesForUid(uid)
119-
if (packages == null) {
120-
HookErrorStore.w(SOURCE, "isCallerAllowed: getPackagesForUid returned null for uid=$uid")
121-
return false
122-
}
113+
val packages = VpnAppStore.getPackagesForUid(uid)
123114
packages.any { it == BuildConfig.APPLICATION_ID }
124115
} catch (e: Throwable) {
125116
HookErrorStore.e(SOURCE, "isCallerAllowed failed for uid=$uid", e)
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
io.nekohasekai.sfa.xposed.XposedInit
2-
io.nekohasekai.sfa.xposed.XposedInit101
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
minApiVersion=100
1+
minApiVersion=101
22
targetApiVersion=101
33
staticScope=true

settings.gradle.kts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@ dependencyResolutionManagement {
1616
}
1717
rootProject.name = "sing-box"
1818
include(":app")
19-
include(":libxposed-api")
20-
project(":libxposed-api").projectDir = file("third_party/libxposed-api")

third_party/libxposed-api/LICENSE

Lines changed: 0 additions & 201 deletions
This file was deleted.

third_party/libxposed-api/build.gradle.kts

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)