@@ -11,8 +11,10 @@ import engine.xray.toJsonStringArray
1111import engine.xray.xraySniffingDestOverrides
1212import engine.network.NetworkDefaults
1313import 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
1618import java.net.InetAddress
1719import java.net.ServerSocket
1820import java.util.concurrent.atomic.AtomicReference
@@ -86,56 +88,62 @@ internal fun AppState.toVpnAppendHttpProxyOptions(localProxyOptions: VpnLocalPro
8688internal 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
141149private fun availablePort (
0 commit comments