Skip to content

Commit c203806

Browse files
committed
Migrate self-hooking VPN hide module to libxposed API 101
1 parent e573ed7 commit c203806

15 files changed

Lines changed: 15 additions & 1505 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: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package io.nekohasekai.sfa.xposed
22

3-
import android.content.Context
4-
import io.github.libxposed.api.XposedInterface
53
import io.github.libxposed.api.XposedModule
6-
import io.github.libxposed.api.XposedModuleInterface
4+
import io.github.libxposed.api.XposedModuleInterface.ModuleLoadedParam
5+
import io.github.libxposed.api.XposedModuleInterface.SystemServerStartingParam
76
import io.nekohasekai.sfa.xposed.hooks.HookIConnectivityManagerOnTransact
87
import io.nekohasekai.sfa.xposed.hooks.hidevpn.ConnectivityServiceHookHelper
98
import io.nekohasekai.sfa.xposed.hooks.hidevpn.HookNetworkCapabilitiesWriteToParcel
109
import io.nekohasekai.sfa.xposed.hooks.hidevpn.HookNetworkInterfaceGetName
1110
import io.nekohasekai.sfa.xposed.hooks.hidevpnapp.HookPackageManagerGetInstalledPackages
1211

13-
class XposedInit(base: XposedInterface, param: XposedModuleInterface.ModuleLoadedParam) : XposedModule(base, param) {
12+
class XposedInit : XposedModule() {
1413

15-
private val activityThreadClass by lazy { Class.forName("android.app.ActivityThread") }
16-
private val currentActivityThreadMethod by lazy { activityThreadClass.getMethod("currentActivityThread") }
17-
private val getSystemContextMethod by lazy { activityThreadClass.getMethod("getSystemContext") }
14+
override fun onModuleLoaded(param: ModuleLoadedParam) {
15+
HookErrorStore.i("XposedInit", "onModuleLoaded process=${param.processName} system=${param.isSystemServer}")
16+
}
1817

19-
override fun onSystemServerLoaded(param: XposedModuleInterface.SystemServerLoadedParam) {
20-
val systemContext = resolveSystemContext()
21-
HookErrorStore.i("XposedInit", "handleSystemServerLoaded")
18+
override fun onSystemServerStarting(param: SystemServerStartingParam) {
19+
HookErrorStore.i("XposedInit", "handleSystemServerStarting")
2220
val hooks = arrayOf(
2321
ConnectivityServiceHookHelper(param.classLoader),
24-
HookIConnectivityManagerOnTransact(param.classLoader, systemContext),
22+
HookIConnectivityManagerOnTransact(param.classLoader),
2523
HookPackageManagerGetInstalledPackages(param.classLoader),
2624
HookNetworkCapabilitiesWriteToParcel(),
2725
HookNetworkInterfaceGetName(param.classLoader),
@@ -43,12 +41,4 @@ class XposedInit(base: XposedInterface, param: XposedModuleInterface.ModuleLoade
4341
companion object {
4442
const val TAG = "sing-box-lsposed"
4543
}
46-
47-
private fun resolveSystemContext(): Context? = try {
48-
val currentThread = currentActivityThreadMethod.invoke(null)
49-
getSystemContextMethod.invoke(currentThread) as? Context
50-
} catch (e: Throwable) {
51-
HookErrorStore.e("XposedInit", "resolveSystemContext failed", e)
52-
null
53-
}
5444
}

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
minApiVersion=100
2-
targetApiVersion=100
1+
minApiVersion=101
2+
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.

third_party/libxposed-api/src/main/AndroidManifest.xml

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

0 commit comments

Comments
 (0)