BUG: Redefine randomly-generated naiveproxy port to reconfigure inbounds for preventing vpn server external ip from detection
Not sure if it is actually a bug, so please correct me if i'm wrong, but nevertheless.
This question comes from the major issue - https://github.com/MatsuriDayo/NekoBoxForAndroid/issues/1153
To connect to naive server nekobox generates inbounds in this way:
{
"inbounds": [
{
"domain_strategy": "",
"endpoint_independent_nat": true,
"inet4_address": [
"172.19.0.1/28"
],
"mtu": 9000,
"sniff": true,
"sniff_override_destination": false,
"stack": "mixed",
"tag": "tun-in",
"type": "tun"
},
{
"domain_strategy": "",
"listen": "127.0.0.1",
"listen_port": 2080,
"sniff": true,
"sniff_override_destination": false,
"tag": "mixed-in",
"type": "mixed"
},
{
"listen": "127.0.0.1",
"listen_port": 42071,
"override_address": "naive.server.domain",
"override_port": 443,
"tag": "c-0-mapping-9",
"type": "direct"
}
]
}
And the mixed-in section causes detection by a spyware potentially exposing real vpn server ip. In case of vless, it was possible just to remove the entire mixed-in section replacing the whole inbounds with custom user json to prevent spyware from accessing things it does not have to see, but in case of naive redefining the inbouns section messes everything up, presumably because of the listen_port value in c-0-mapping-9 inbound, as it becomes strictly defined after the custom inbounds section is applied.
The point is in the following section with creds which lies below the singbox config in both log and connection dump and cant be modified manually:
{
"host-resolver-rules": "MAP naive.server.domain 127.0.0.1",
"listen": "socks://127.0.0.1:44477",
"proxy": "https://username:[email protected]:36539/"
}
Port in the proxy line is generated randomly every time a new connection is initiated. If the inbound section of the config remains unmodified, this port always matches one located in the listen_port field of the c-0-mapping-9 inbound, and everything works fine, but if inbounds are somehow modified to remove mixed-in, value in the listen_port field becomes static and does not match one generated in the proxy line, which causes getting read: connection reset by peer error.
Tried to replace inbounds in husi - it worked fine, the mixed-in section was removed, connection was establieshed and port 2080 was closed as expected.
How can it be done in Nekobox? Is there any way to set a custom proxy line, or at least set port in it to some user-defined value?
Yes, this port is defined in line 426 of ConfigBuilder.kt:
val mappingPort = mkPort()
bean.finalAddress = LOCALHOST
bean.finalPort = mappingPort
Bean bean.finalPort is defined in the same place, and used to create full proxy url in NaiveBean.toUri method in NaiveFmt.kt.
And only after that the whole config is merged with custom user json in line
Util.mergeJSON(configMap, proxy.requireBean().customConfigJson)
in ConfigBuilder.kt
So, looks like check for redefined listen_port from the custom user json is definitely required before doing val mappingPort = mkPort() or even in mkPort()
upd: looks like the similar thing is done in outbounds a bit higher in ConfigBuilder.kt in _hack_custom_config = bean.customOutboundJson, so something like that should be applied to inbounds too