Skip to content

Commit 4c9a1bd

Browse files
committed
refactor: unify JSON config builders
1 parent 8ecb293 commit 4c9a1bd

14 files changed

Lines changed: 811 additions & 598 deletions

app/src/main/kotlin/engine/root/RootConfigSupport.kt

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import engine.xray.buildXrayOutboundPlan
1818
import engine.xray.prepareXrayCoreLogPaths
1919
import features.resources.runtime.XrayResourceFilePaths
2020
import features.resources.runtime.prepareXrayResourceFilePaths
21-
import org.json.JSONObject
21+
import kotlinx.serialization.json.JsonObject
22+
import kotlinx.serialization.json.buildJsonObject
23+
import kotlinx.serialization.json.put
2224

2325
internal class RootConfigBuildContext(
2426
private val androidContext: Context,
@@ -29,7 +31,7 @@ internal class RootConfigBuildContext(
2931
private val dnsHosts: List<String>,
3032
) {
3133
fun buildRootStartConfig(
32-
inbounds: List<JSONObject>,
34+
inbounds: List<JsonObject>,
3335
dnsHijackInboundTags: List<String>,
3436
): RootStartConfig {
3537
val xrayConfigJson = XrayConfigFactory.buildXrayConfig(
@@ -98,7 +100,7 @@ internal fun AppState.buildRootSharedProxyInbounds(
98100
socksInboundTag: String,
99101
httpInboundTag: String,
100102
includeSocks5Proxy: Boolean = true,
101-
): List<JSONObject> {
103+
): List<JsonObject> {
102104
return buildList {
103105
socks5ProxyPort.toPortOrNull()
104106
?.takeIf { includeSocks5Proxy && enableSocks5Proxy }
@@ -116,35 +118,39 @@ internal fun AppState.rootSocks5ProxyPortValue(): Int {
116118
private fun buildRootSocksProxyInbound(
117119
tag: String,
118120
port: Int,
119-
): JSONObject {
120-
return JSONObject()
121-
.put("tag", tag)
122-
.put("listen", RootSharedProxyListenAddress)
123-
.put("port", port)
124-
.put("protocol", XrayProtocols.SOCKS)
125-
.put(
121+
): JsonObject {
122+
return buildJsonObject {
123+
put("tag", tag)
124+
put("listen", RootSharedProxyListenAddress)
125+
put("port", port)
126+
put("protocol", XrayProtocols.SOCKS)
127+
put(
126128
"settings",
127-
JSONObject()
128-
.put("auth", "noauth")
129-
.put("udp", true)
130-
.put("ip", RootSharedProxyListenAddress)
131-
.put("userLevel", 0),
129+
buildJsonObject {
130+
put("auth", "noauth")
131+
put("udp", true)
132+
put("ip", RootSharedProxyListenAddress)
133+
put("userLevel", 0)
134+
},
132135
)
136+
}
133137
}
134138

135139
private fun buildRootHttpProxyInbound(
136140
tag: String,
137141
port: Int,
138-
): JSONObject {
139-
return JSONObject()
140-
.put("tag", tag)
141-
.put("listen", RootSharedProxyListenAddress)
142-
.put("port", port)
143-
.put("protocol", XrayProtocols.HTTP)
144-
.put(
142+
): JsonObject {
143+
return buildJsonObject {
144+
put("tag", tag)
145+
put("listen", RootSharedProxyListenAddress)
146+
put("port", port)
147+
put("protocol", XrayProtocols.HTTP)
148+
put(
145149
"settings",
146-
JSONObject()
147-
.put("allowTransparent", false)
148-
.put("userLevel", 0),
150+
buildJsonObject {
151+
put("allowTransparent", false)
152+
put("userLevel", 0)
153+
},
149154
)
155+
}
150156
}

app/src/main/kotlin/engine/tproxy/TproxyConfig.kt

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import engine.xray.XrayProtocols
1515
import engine.xray.XrayTags
1616
import engine.xray.toJsonStringArray
1717
import engine.xray.xraySniffingDestOverrides
18-
import org.json.JSONObject
18+
import kotlinx.serialization.json.JsonObject
19+
import kotlinx.serialization.json.buildJsonObject
20+
import kotlinx.serialization.json.put
1921

2022
internal data class TproxyStartConfig(
2123
override val root: RootStartConfig,
@@ -45,7 +47,7 @@ internal fun RootConfigBuildContext.buildTproxyStartConfig(): TproxyStartConfig
4547
)
4648
}
4749

48-
private fun AppState.buildTproxyInbounds(tproxyPort: Int): List<JSONObject> {
50+
private fun AppState.buildTproxyInbounds(tproxyPort: Int): List<JsonObject> {
4951
return buildList {
5052
add(buildTproxyTunnelInbound(this@buildTproxyInbounds, tproxyPort))
5153
addAll(
@@ -60,38 +62,41 @@ private fun AppState.buildTproxyInbounds(tproxyPort: Int): List<JSONObject> {
6062
private fun buildTproxyTunnelInbound(
6163
appState: AppState,
6264
port: Int,
63-
): JSONObject {
64-
return JSONObject()
65-
.put("tag", XrayTags.TPROXY_INBOUND)
66-
.put("port", port)
67-
.put("protocol", XrayProtocols.DOKODEMO_DOOR)
68-
.put(
65+
): JsonObject {
66+
return buildJsonObject {
67+
put("tag", XrayTags.TPROXY_INBOUND)
68+
put("port", port)
69+
put("protocol", XrayProtocols.TUNNEL)
70+
put(
6971
"settings",
70-
JSONObject()
71-
.put("allowedNetwork", "tcp,udp")
72-
.put("followRedirect", true)
73-
.put("userLevel", 0),
72+
buildJsonObject {
73+
put("allowedNetwork", "tcp,udp")
74+
put("followRedirect", true)
75+
put("userLevel", 0)
76+
},
7477
)
75-
.put(
78+
put(
7679
"streamSettings",
77-
JSONObject()
78-
.put(
79-
"sockopt",
80-
JSONObject()
81-
.put("tproxy", "tproxy"),
82-
),
83-
)
84-
.apply {
85-
if (appState.enableSniffing) {
80+
buildJsonObject {
8681
put(
87-
"sniffing",
88-
JSONObject()
89-
.put("enabled", true)
90-
.put("destOverride", xraySniffingDestOverrides(appState.effectiveFakeDnsEnabled).toJsonStringArray())
91-
.put("routeOnly", appState.enableSniffingRouteOnly),
82+
"sockopt",
83+
buildJsonObject {
84+
put("tproxy", "tproxy")
85+
},
9286
)
93-
}
87+
},
88+
)
89+
if (appState.enableSniffing) {
90+
put(
91+
"sniffing",
92+
buildJsonObject {
93+
put("enabled", true)
94+
put("destOverride", xraySniffingDestOverrides(appState.effectiveFakeDnsEnabled).toJsonStringArray())
95+
put("routeOnly", appState.enableSniffingRouteOnly)
96+
},
97+
)
9498
}
99+
}
95100
}
96101

97102
private fun AppState.tproxyPortValue(): Int {

app/src/main/kotlin/engine/tun2socks/Tun2SocksConfig.kt

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import engine.xray.prepareXrayCoreLogPaths
2323
import engine.xray.toJsonStringArray
2424
import engine.xray.xraySniffingDestOverrides
2525
import features.resources.runtime.prepareXrayResourceFilePaths
26-
import org.json.JSONObject
26+
import kotlinx.serialization.json.JsonObject
27+
import kotlinx.serialization.json.buildJsonObject
28+
import kotlinx.serialization.json.put
2729
import java.io.File
2830

2931
internal data class Tun2SocksStartConfig(
@@ -84,7 +86,7 @@ internal fun Context.prepareHevSocks5TunnelConfig(appState: AppState): HevSocks5
8486
)
8587
}
8688

87-
private fun AppState.buildTun2SocksInbounds(socks5ProxyPort: Int): List<JSONObject> {
89+
private fun AppState.buildTun2SocksInbounds(socks5ProxyPort: Int): List<JsonObject> {
8890
return buildList {
8991
add(buildTun2SocksInbound(this@buildTun2SocksInbounds, socks5ProxyPort))
9092
addAll(
@@ -100,27 +102,30 @@ private fun AppState.buildTun2SocksInbounds(socks5ProxyPort: Int): List<JSONObje
100102
private fun buildTun2SocksInbound(
101103
appState: AppState,
102104
port: Int,
103-
): JSONObject {
104-
return JSONObject()
105-
.put("tag", XrayTags.TUN2SOCKS_INBOUND)
106-
.put("listen", Tun2SocksListenAddress)
107-
.put("port", port)
108-
.put("protocol", XrayProtocols.SOCKS)
109-
.put(
105+
): JsonObject {
106+
return buildJsonObject {
107+
put("tag", XrayTags.TUN2SOCKS_INBOUND)
108+
put("listen", Tun2SocksListenAddress)
109+
put("port", port)
110+
put("protocol", XrayProtocols.SOCKS)
111+
put(
110112
"settings",
111-
JSONObject()
112-
.put("auth", "noauth")
113-
.put("udp", true)
114-
.put("ip", Tun2SocksListenAddress)
115-
.put("userLevel", 0),
113+
buildJsonObject {
114+
put("auth", "noauth")
115+
put("udp", true)
116+
put("ip", Tun2SocksListenAddress)
117+
put("userLevel", 0)
118+
},
116119
)
117-
.put(
120+
put(
118121
"sniffing",
119-
JSONObject()
120-
.put("enabled", appState.enableSniffing)
121-
.put("destOverride", xraySniffingDestOverrides(appState.effectiveFakeDnsEnabled).toJsonStringArray())
122-
.put("routeOnly", appState.enableSniffingRouteOnly),
122+
buildJsonObject {
123+
put("enabled", appState.enableSniffing)
124+
put("destOverride", xraySniffingDestOverrides(appState.effectiveFakeDnsEnabled).toJsonStringArray())
125+
put("routeOnly", appState.enableSniffingRouteOnly)
126+
},
123127
)
128+
}
124129
}
125130

126131
private fun buildHevSocks5TunnelConfig(

app/src/main/kotlin/engine/vpn/VpnLocalProxyConfig.kt

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import engine.xray.toJsonStringArray
1111
import engine.xray.xraySniffingDestOverrides
1212
import engine.network.NetworkDefaults
1313
import engine.network.toPortOrNull
14-
import org.json.JSONArray
15-
import org.json.JSONObject
14+
import kotlinx.serialization.json.JsonObject
15+
import kotlinx.serialization.json.buildJsonArray
16+
import kotlinx.serialization.json.buildJsonObject
17+
import kotlinx.serialization.json.put
1618
import java.net.InetAddress
1719
import java.net.ServerSocket
1820
import java.util.concurrent.atomic.AtomicReference
@@ -86,56 +88,62 @@ internal fun AppState.toVpnAppendHttpProxyOptions(localProxyOptions: VpnLocalPro
8688
internal fun buildVpnLocalSocksInbound(
8789
appState: AppState,
8890
options: VpnLocalProxyOptions,
89-
): JSONObject {
90-
return JSONObject()
91-
.put("tag", XrayTags.LOCAL_SOCKS_INBOUND)
92-
.put("listen", options.listenAddress)
93-
.put("port", options.port)
94-
.put("protocol", XrayProtocols.SOCKS)
95-
.put("settings", options.toSocksInboundSettings())
96-
.put(
91+
): JsonObject {
92+
return buildJsonObject {
93+
put("tag", XrayTags.LOCAL_SOCKS_INBOUND)
94+
put("listen", options.listenAddress)
95+
put("port", options.port)
96+
put("protocol", XrayProtocols.SOCKS)
97+
put("settings", options.toSocksInboundSettings())
98+
put(
9799
"sniffing",
98-
JSONObject()
99-
.put("enabled", appState.enableSniffing)
100-
.put("destOverride", xraySniffingDestOverrides(appState.effectiveFakeDnsEnabled).toJsonStringArray())
101-
.put("routeOnly", appState.enableSniffingRouteOnly),
100+
buildJsonObject {
101+
put("enabled", appState.enableSniffing)
102+
put("destOverride", xraySniffingDestOverrides(appState.effectiveFakeDnsEnabled).toJsonStringArray())
103+
put("routeOnly", appState.enableSniffingRouteOnly)
104+
},
102105
)
106+
}
103107
}
104108

105-
internal fun buildVpnAppendHttpInbound(options: VpnAppendHttpProxyOptions): JSONObject {
106-
return JSONObject()
107-
.put("tag", XrayTags.VPN_APPEND_HTTP_INBOUND)
108-
.put("listen", LocalProxyLoopbackAddress)
109-
.put("port", options.port)
110-
.put("protocol", XrayProtocols.HTTP)
111-
.put(
109+
internal fun buildVpnAppendHttpInbound(options: VpnAppendHttpProxyOptions): JsonObject {
110+
return buildJsonObject {
111+
put("tag", XrayTags.VPN_APPEND_HTTP_INBOUND)
112+
put("listen", LocalProxyLoopbackAddress)
113+
put("port", options.port)
114+
put("protocol", XrayProtocols.HTTP)
115+
put(
112116
"settings",
113-
JSONObject()
114-
.put("allowTransparent", false)
115-
.put("userLevel", 0),
117+
buildJsonObject {
118+
put("allowTransparent", false)
119+
put("userLevel", 0)
120+
},
116121
)
122+
}
117123
}
118124

119-
private fun VpnLocalProxyOptions.toSocksInboundSettings(): JSONObject {
120-
return JSONObject()
121-
.put("auth", if (username.isBlank()) "noauth" else "password")
122-
.put("udp", true)
123-
.put("userLevel", 0)
124-
.apply {
125-
if (listenAddress == LocalProxyLoopbackAddress) {
126-
put("ip", LocalProxyLoopbackAddress)
127-
}
128-
if (username.isNotBlank()) {
129-
put(
130-
"users",
131-
JSONArray().put(
132-
JSONObject()
133-
.put("user", username)
134-
.put("pass", password),
135-
),
136-
)
137-
}
125+
private fun VpnLocalProxyOptions.toSocksInboundSettings(): JsonObject {
126+
return buildJsonObject {
127+
put("auth", if (username.isBlank()) "noauth" else "password")
128+
put("udp", true)
129+
put("userLevel", 0)
130+
if (listenAddress == LocalProxyLoopbackAddress) {
131+
put("ip", LocalProxyLoopbackAddress)
132+
}
133+
if (username.isNotBlank()) {
134+
put(
135+
"users",
136+
buildJsonArray {
137+
add(
138+
buildJsonObject {
139+
put("user", username)
140+
put("pass", password)
141+
},
142+
)
143+
},
144+
)
138145
}
146+
}
139147
}
140148

141149
private fun availablePort(

0 commit comments

Comments
 (0)