|
| 1 | +// Copyright 2026, AsteriskNG contributors |
| 2 | +// SPDX-License-Identifier: GPL-3.0 |
| 3 | + |
| 4 | +package engine.proxy |
| 5 | + |
| 6 | +import app.AppState |
| 7 | +import app.effectiveFakeDnsEnabled |
| 8 | +import app.modes.RunModeTun2Socks |
| 9 | +import app.modes.RunModeTproxy |
| 10 | +import engine.network.NetworkDefaults |
| 11 | +import engine.network.toPortOrNull |
| 12 | +import engine.tproxy.DefaultTproxyPort |
| 13 | +import engine.tun2socks.DefaultTun2SocksProxyPort |
| 14 | +import engine.vpn.VpnDefaults |
| 15 | +import engine.xray.XrayProtocols |
| 16 | +import engine.xray.toJsonStringArray |
| 17 | +import engine.xray.xraySniffingDestOverrides |
| 18 | +import java.net.InetAddress |
| 19 | +import java.net.ServerSocket |
| 20 | +import java.util.concurrent.atomic.AtomicReference |
| 21 | +import kotlinx.serialization.json.JsonObject |
| 22 | +import kotlinx.serialization.json.buildJsonArray |
| 23 | +import kotlinx.serialization.json.buildJsonObject |
| 24 | +import kotlinx.serialization.json.put |
| 25 | + |
| 26 | +internal const val LocalProxyLoopbackAddress = NetworkDefaults.IPV4_LOOPBACK_ADDRESS |
| 27 | +private const val LocalProxyAllInterfacesAddress = NetworkDefaults.IPV4_ANY_ADDRESS |
| 28 | + |
| 29 | +internal data class LocalProxyOptions( |
| 30 | + val listenAddress: String, |
| 31 | + val port: Int, |
| 32 | + val username: String, |
| 33 | + val password: String, |
| 34 | +) |
| 35 | + |
| 36 | +internal object LocalProxyRuntime { |
| 37 | + private val currentOptions = AtomicReference<LocalProxyOptions?>() |
| 38 | + |
| 39 | + fun update(options: LocalProxyOptions) { |
| 40 | + currentOptions.set(options) |
| 41 | + } |
| 42 | + |
| 43 | + fun clear() { |
| 44 | + currentOptions.set(null) |
| 45 | + } |
| 46 | + |
| 47 | + fun current(): LocalProxyOptions? { |
| 48 | + return currentOptions.get() |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +internal fun AppState.withResolvedDynamicLocalProxyPort(): AppState { |
| 53 | + if (!enableDynamicLocalProxyPort) return this |
| 54 | + |
| 55 | + val configuredPort = localProxyPort.toPortOrNull() |
| 56 | + val listenAddress = localProxyListenAddress() |
| 57 | + val excludedPorts = localProxyExcludedPorts() |
| 58 | + val currentOptions = LocalProxyRuntime.current() |
| 59 | + val canKeepConfiguredPort = configuredPort != null && |
| 60 | + configuredPort !in excludedPorts && |
| 61 | + ( |
| 62 | + isPortAvailable(listenAddress, configuredPort) || |
| 63 | + currentOptions?.matches(listenAddress, configuredPort) == true |
| 64 | + ) |
| 65 | + val resolvedPort = when { |
| 66 | + canKeepConfiguredPort -> configuredPort |
| 67 | + else -> availablePort(listenAddress, excludedPorts) ?: configuredPort ?: VpnDefaults.LOCAL_PROXY_PORT |
| 68 | + } |
| 69 | + val resolvedPortText = resolvedPort.toString() |
| 70 | + return if (localProxyPort == resolvedPortText) this else copy(localProxyPort = resolvedPortText) |
| 71 | +} |
| 72 | + |
| 73 | +internal fun AppState.toLocalProxyOptions(): LocalProxyOptions { |
| 74 | + return LocalProxyOptions( |
| 75 | + listenAddress = localProxyListenAddress(), |
| 76 | + port = localProxyPort.toPortOrNull() ?: VpnDefaults.LOCAL_PROXY_PORT, |
| 77 | + username = localProxyUsername.trim(), |
| 78 | + password = localProxyPassword, |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +internal fun buildLocalSocksInbound( |
| 83 | + appState: AppState, |
| 84 | + tag: String, |
| 85 | + options: LocalProxyOptions, |
| 86 | +): JsonObject { |
| 87 | + return buildJsonObject { |
| 88 | + put("tag", tag) |
| 89 | + put("listen", options.listenAddress) |
| 90 | + put("port", options.port) |
| 91 | + put("protocol", XrayProtocols.SOCKS) |
| 92 | + put("settings", options.toSocksInboundSettings()) |
| 93 | + put( |
| 94 | + "sniffing", |
| 95 | + buildJsonObject { |
| 96 | + put("enabled", appState.enableSniffing) |
| 97 | + put("destOverride", xraySniffingDestOverrides(appState.effectiveFakeDnsEnabled).toJsonStringArray()) |
| 98 | + put("routeOnly", appState.enableSniffingRouteOnly) |
| 99 | + }, |
| 100 | + ) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +private fun AppState.localProxyListenAddress(): String { |
| 105 | + return if (localProxyListenAllInterfaces) { |
| 106 | + LocalProxyAllInterfacesAddress |
| 107 | + } else { |
| 108 | + LocalProxyLoopbackAddress |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +private fun AppState.localProxyExcludedPorts(): Set<Int> { |
| 113 | + return buildSet { |
| 114 | + if (runMode == RunModeTproxy) { |
| 115 | + add(transparentProxyPort.toPortOrNull() ?: DefaultTproxyPort) |
| 116 | + } |
| 117 | + if (runMode == RunModeTun2Socks) { |
| 118 | + add(socks5ProxyPort.toPortOrNull() ?: DefaultTun2SocksProxyPort) |
| 119 | + } |
| 120 | + if (enableHttpProxy) { |
| 121 | + httpProxyPort.toPortOrNull()?.let(::add) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +private fun LocalProxyOptions.matches(listenAddress: String, port: Int): Boolean { |
| 127 | + return this.port == port && |
| 128 | + ( |
| 129 | + this.listenAddress == listenAddress || |
| 130 | + this.listenAddress == LocalProxyAllInterfacesAddress && |
| 131 | + listenAddress == LocalProxyLoopbackAddress |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +private fun LocalProxyOptions.toSocksInboundSettings(): JsonObject { |
| 136 | + return buildJsonObject { |
| 137 | + put("auth", if (username.isBlank()) "noauth" else "password") |
| 138 | + put("udp", true) |
| 139 | + put("userLevel", 0) |
| 140 | + if (listenAddress == LocalProxyLoopbackAddress) { |
| 141 | + put("ip", LocalProxyLoopbackAddress) |
| 142 | + } |
| 143 | + if (username.isNotBlank()) { |
| 144 | + put( |
| 145 | + "users", |
| 146 | + buildJsonArray { |
| 147 | + add( |
| 148 | + buildJsonObject { |
| 149 | + put("user", username) |
| 150 | + put("pass", password) |
| 151 | + }, |
| 152 | + ) |
| 153 | + }, |
| 154 | + ) |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +internal fun availablePort( |
| 160 | + listenAddress: String, |
| 161 | + excludedPorts: Set<Int> = emptySet(), |
| 162 | +): Int? { |
| 163 | + return runCatching { |
| 164 | + repeat(10) { |
| 165 | + ServerSocket(0, 0, InetAddress.getByName(listenAddress)).use { socket -> |
| 166 | + if (socket.localPort !in excludedPorts) { |
| 167 | + return@runCatching socket.localPort |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + null |
| 172 | + }.getOrNull() |
| 173 | +} |
| 174 | + |
| 175 | +private fun isPortAvailable( |
| 176 | + listenAddress: String, |
| 177 | + port: Int, |
| 178 | +): Boolean { |
| 179 | + return runCatching { |
| 180 | + ServerSocket(port, 0, InetAddress.getByName(listenAddress)).use { } |
| 181 | + }.isSuccess |
| 182 | +} |
0 commit comments