0%

安卓9 设置热点固定IP (Xposed)

更新

安卓12 固定Wifi热点IP (Xposed)

安卓10、11 固定Wifi热点IP (Xposed)

项目地址

SoftApHelper

概述

手机系统升级到安卓9之后,开热点的主机IP地址是随机的。其他设备设置代理很不方便,于是考虑固定热点IP。

Hook点

安卓9

com.android.server.connectivity.tethering.TetherInterfaceStateMachinegetRandomWifiIPv4Address函数。

TetherInterfaceStateMachine.java#259

1
private String getRandomWifiIPv4Address()

安卓8热点开启流程


图源:android8.0热点打开流程,从Setting到framework

跟踪源码

最终定位到com.android.server.connectivity.tethering.TetherInterfaceStateMachine类(源代码

函数调用链:startIPv4-configureIPv4-getRandomWifiIPv4Address

1
2
3
4
5
6
7
8
private boolean configureIPv4(boolean enabled) {
// ...
else if (mInterfaceType == ConnectivityManager.TETHERING_WIFI) {
ipAsString = getRandomWifiIPv4Address();
prefixLen = WIFI_HOST_IFACE_PREFIX_LENGTH;
}
// ...
}

调用getRandomWifiIPv4Address函数生成主机IP地址后,转换为LinkAddress,最终调用ifcg.setLinkAddress设置IP:

1
2
3
4
5
6
7
8
9
10
11
12
13
final LinkAddress linkAddr;
try {
final InterfaceConfiguration ifcg = mNMService.getInterfaceConfig(mIfaceName);
if (ifcg == null) {
mLog.e("Received null interface config");
return false;
}

InetAddress addr = NetworkUtils.numericToInetAddress(ipAsString);
linkAddr = new LinkAddress(addr, prefixLen);
ifcg.setLinkAddress(linkAddr);
// ...
}

查找getRandomWifiIPv4Address的交叉引用,发现只有这一处。直接修改返回值,即可固定IP。

解决方案

使用Xposed框架Hook该函数(过滤包名android),设置返回值为192.168.43.1即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Hook implements IXposedHookLoadPackage {

private static final String classname = "com.android.server.connectivity.tethering.TetherInterfaceStateMachine";
private static final String methodname = "getRandomWifiIPv4Address";
private static final String WIFI_HOST_IFACE_ADDR = "192.168.43.1";

@Override
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
if (lpparam.packageName.equals("android")) {
Class<?> clz = XposedHelpers.findClass(classname, lpparam.classLoader);

findAndHookMethod(clz, methodname, new XC_MethodReplacement() {
@Override
protected Object replaceHookedMethod(MethodHookParam param) {
return WIFI_HOST_IFACE_ADDR;
}
});
}
}

}

参考

How can I permanently change my hotspot tethering IP address?