From e287ac88c8ea5cd0c5611065095536d2d59ae78c Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 30 Dec 2025 20:03:17 +0800 Subject: [PATCH] Remove legacy tun2socks support and related settings Deleted Tun2SocksService and all code paths, preferences, and UI related to toggling between HevSocks5Tunnel and tun2socks. The app now always uses HevSocks5Tunnel for VPN traffic, simplifying configuration and maintenance. --- .../v2ray/ang/handler/V2rayConfigManager.kt | 54 ++------ .../com/v2ray/ang/service/TProxyService.kt | 8 +- .../com/v2ray/ang/service/Tun2SocksService.kt | 128 ------------------ .../com/v2ray/ang/service/V2RayVpnService.kt | 22 +-- .../java/com/v2ray/ang/ui/LogcatActivity.kt | 2 +- .../java/com/v2ray/ang/ui/SettingsActivity.kt | 12 +- .../app/src/main/res/xml/pref_settings.xml | 10 +- 7 files changed, 32 insertions(+), 204 deletions(-) delete mode 100644 V2rayNG/app/src/main/java/com/v2ray/ang/service/Tun2SocksService.kt diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/handler/V2rayConfigManager.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/handler/V2rayConfigManager.kt index 2f3b7030..5b09dabe 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/handler/V2rayConfigManager.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/handler/V2rayConfigManager.kt @@ -480,52 +480,16 @@ object V2rayConfigManager { ) } - if (MmkvManager.decodeSettingsBool(AppConfig.PREF_USE_HEV_TUNNEL, true) == false) { - - // DNS inbound - val remoteDns = SettingsManager.getRemoteDnsServers() - if (v2rayConfig.inbounds.none { e -> e.protocol == "dokodemo-door" && e.tag == "dns-in" }) { - val dnsInboundSettings = V2rayConfig.InboundBean.InSettingsBean( - address = if (Utils.isPureIpAddress(remoteDns.first())) remoteDns.first() else AppConfig.DNS_PROXY, - port = 53, - network = "tcp,udp" - ) - - val localDnsPort = Utils.parseInt( - MmkvManager.decodeSettingsString(AppConfig.PREF_LOCAL_DNS_PORT), - AppConfig.PORT_LOCAL_DNS.toInt() - ) - v2rayConfig.inbounds.add( - V2rayConfig.InboundBean( - tag = "dns-in", - port = localDnsPort, - listen = AppConfig.LOOPBACK, - protocol = "dokodemo-door", - settings = dnsInboundSettings, - sniffing = null - ) - ) - } - - // DNS routing tag - v2rayConfig.routing.rules.add( - 0, RulesBean( - inboundTag = arrayListOf("dns-in"), - outboundTag = "dns-out", - domain = null - ) + // if (MmkvManager.decodeSettingsBool(AppConfig.PREF_USE_HEV_TUNNEL, true)) { + //hev-socks5-tunnel dns routing + v2rayConfig.routing.rules.add( + 0, RulesBean( + inboundTag = arrayListOf("socks"), + outboundTag = "dns-out", + port = "53", + type = "field" ) - } else { - //hev-socks5-tunnel dns routing - v2rayConfig.routing.rules.add( - 0, RulesBean( - inboundTag = arrayListOf("socks"), - outboundTag = "dns-out", - port = "53", - type = "field" - ) - ) - } + ) // DNS outbound if (v2rayConfig.outbounds.none { e -> e.protocol == "dns" && e.tag == "dns-out" }) { diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/TProxyService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/TProxyService.kt index fd33cc71..1050a4f4 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/TProxyService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/TProxyService.kt @@ -37,17 +37,17 @@ class TProxyService( * Starts the tun2socks process with the appropriate parameters. */ override fun startTun2Socks() { - Log.i(AppConfig.TAG, "Starting HevSocks5Tunnel via JNI") +// Log.i(AppConfig.TAG, "Starting HevSocks5Tunnel via JNI") val configContent = buildConfig() val configFile = File(context.filesDir, "hev-socks5-tunnel.yaml").apply { writeText(configContent) } - Log.i(AppConfig.TAG, "Config file created: ${configFile.absolutePath}") - Log.d(AppConfig.TAG, "Config content:\n$configContent") +// Log.i(AppConfig.TAG, "Config file created: ${configFile.absolutePath}") + Log.d(AppConfig.TAG, "HevSocks5Tunnel Config content:\n$configContent") try { - Log.i(AppConfig.TAG, "TProxyStartService...") +// Log.i(AppConfig.TAG, "TProxyStartService...") TProxyStartService(configFile.absolutePath, vpnInterface.fd) } catch (e: Exception) { Log.e(AppConfig.TAG, "HevSocks5Tunnel exception: ${e.message}") diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/Tun2SocksService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/Tun2SocksService.kt deleted file mode 100644 index eb33f5b7..00000000 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/Tun2SocksService.kt +++ /dev/null @@ -1,128 +0,0 @@ -package com.v2ray.ang.service - -import android.content.Context -import android.net.LocalSocket -import android.net.LocalSocketAddress -import android.os.ParcelFileDescriptor -import android.util.Log -import com.v2ray.ang.AppConfig -import com.v2ray.ang.handler.MmkvManager -import com.v2ray.ang.handler.SettingsManager -import com.v2ray.ang.util.Utils -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import java.io.File - -/** - * Manages the tun2socks process that handles VPN traffic - */ -class Tun2SocksService( - private val context: Context, - private val vpnInterface: ParcelFileDescriptor, - private val isRunningProvider: () -> Boolean, - private val restartCallback: () -> Unit -) : Tun2SocksControl { - companion object { - private const val TUN2SOCKS = "libtun2socks.so" - } - - private lateinit var process: Process - - /** - * Starts the tun2socks process with the appropriate parameters. - */ - override fun startTun2Socks() { - Log.i(AppConfig.TAG, "Start run $TUN2SOCKS") - val socksPort = SettingsManager.getSocksPort() - val vpnConfig = SettingsManager.getCurrentVpnInterfaceAddressConfig() - val cmd = arrayListOf( - File(context.applicationInfo.nativeLibraryDir, TUN2SOCKS).absolutePath, - "--netif-ipaddr", vpnConfig.ipv4Router, - "--netif-netmask", "255.255.255.252", - "--socks-server-addr", "${AppConfig.LOOPBACK}:${socksPort}", - "--tunmtu", SettingsManager.getVpnMtu().toString(), - "--sock-path", "sock_path", - "--enable-udprelay", - "--loglevel", "notice" - ) - - if (MmkvManager.decodeSettingsBool(AppConfig.PREF_PREFER_IPV6)) { - cmd.add("--netif-ip6addr") - cmd.add(vpnConfig.ipv6Router) - } - if (MmkvManager.decodeSettingsBool(AppConfig.PREF_LOCAL_DNS_ENABLED)) { - val localDnsPort = Utils.parseInt( - MmkvManager.decodeSettingsString(AppConfig.PREF_LOCAL_DNS_PORT), - AppConfig.PORT_LOCAL_DNS.toInt() - ) - cmd.add("--dnsgw") - cmd.add("${AppConfig.LOOPBACK}:${localDnsPort}") - } - Log.i(AppConfig.TAG, cmd.toString()) - - try { - val proBuilder = ProcessBuilder(cmd) - proBuilder.redirectErrorStream(true) - process = proBuilder - .directory(context.filesDir) - .start() - Thread { - Log.i(AppConfig.TAG, "$TUN2SOCKS check") - process.waitFor() - Log.i(AppConfig.TAG, "$TUN2SOCKS exited") - if (isRunningProvider()) { - Log.i(AppConfig.TAG, "$TUN2SOCKS restart") - restartCallback() - } - }.start() - Log.i(AppConfig.TAG, "$TUN2SOCKS process info: $process") - - sendFd() - } catch (e: Exception) { - Log.e(AppConfig.TAG, "Failed to start $TUN2SOCKS process", e) - } - } - - /** - * Sends the file descriptor to the tun2socks process. - * Attempts to send the file descriptor multiple times if necessary. - */ - private fun sendFd() { - val fd = vpnInterface.fileDescriptor - val path = File(context.filesDir, "sock_path").absolutePath - Log.i(AppConfig.TAG, "LocalSocket path: $path") - - CoroutineScope(Dispatchers.IO).launch { - var tries = 0 - while (true) try { - Thread.sleep(50L shl tries) - Log.i(AppConfig.TAG, "LocalSocket sendFd tries: $tries") - LocalSocket().use { localSocket -> - localSocket.connect(LocalSocketAddress(path, LocalSocketAddress.Namespace.FILESYSTEM)) - localSocket.setFileDescriptorsForSend(arrayOf(fd)) - localSocket.outputStream.write(42) - } - break - } catch (e: Exception) { - Log.e(AppConfig.TAG, "Failed to send file descriptor, try: $tries", e) - if (tries > 5) break - tries += 1 - } - } - } - - /** - * Stops the tun2socks process - */ - override fun stopTun2Socks() { - try { - Log.i(AppConfig.TAG, "$TUN2SOCKS destroy") - if (::process.isInitialized) { - process.destroy() - } - } catch (e: Exception) { - Log.e(AppConfig.TAG, "Failed to destroy $TUN2SOCKS process", e) - } - } -} diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt index a85d2f79..190e38ed 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt @@ -294,21 +294,13 @@ class V2RayVpnService : VpnService(), ServiceControl { * Starts the tun2socks process with the appropriate parameters. */ private fun runTun2socks() { - if (MmkvManager.decodeSettingsBool(AppConfig.PREF_USE_HEV_TUNNEL, true) == true) { - tun2SocksService = TProxyService( - context = applicationContext, - vpnInterface = mInterface, - isRunningProvider = { isRunning }, - restartCallback = { runTun2socks() } - ) - } else { - tun2SocksService = Tun2SocksService( - context = applicationContext, - vpnInterface = mInterface, - isRunningProvider = { isRunning }, - restartCallback = { runTun2socks() } - ) - } + //if (MmkvManager.decodeSettingsBool(AppConfig.PREF_USE_HEV_TUNNEL, true)) { + tun2SocksService = TProxyService( + context = applicationContext, + vpnInterface = mInterface, + isRunningProvider = { isRunning }, + restartCallback = { runTun2socks() } + ) tun2SocksService?.startTun2Socks() } diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt index 436f5913..8b214a1b 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt @@ -56,7 +56,7 @@ class LogcatActivity : BaseActivity(), SwipeRefreshLayout.OnRefreshListener { lst.add("-v") lst.add("time") lst.add("-s") - lst.add("GoLog,tun2socks,${ANG_PACKAGE},AndroidRuntime,System.err") + lst.add("GoLog,${ANG_PACKAGE},AndroidRuntime,System.err") val process = withContext(Dispatchers.IO) { Runtime.getRuntime().exec(lst.toTypedArray()) } diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/SettingsActivity.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/SettingsActivity.kt index 385e5632..616a477f 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/SettingsActivity.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/SettingsActivity.kt @@ -68,7 +68,7 @@ class SettingsActivity : BaseActivity() { private val hevTunLogLevel by lazy { findPreference(AppConfig.PREF_HEV_TUNNEL_LOGLEVEL) } private val hevTunRwTimeout by lazy { findPreference(AppConfig.PREF_HEV_TUNNEL_RW_TIMEOUT) } - private val useHevTun by lazy { findPreference(AppConfig.PREF_USE_HEV_TUNNEL) } +// private val useHevTun by lazy { findPreference(AppConfig.PREF_USE_HEV_TUNNEL) } override fun onCreatePreferences(bundle: Bundle?, s: String?) { addPreferencesFromResource(R.xml.pref_settings) @@ -182,10 +182,10 @@ class SettingsActivity : BaseActivity() { mode?.dialogLayoutResource = R.layout.preference_with_help_link //loglevel.summary = "LogLevel" - useHevTun?.setOnPreferenceChangeListener { _, newValue -> - updateHevTunSettings(newValue as Boolean) - true - } +// useHevTun?.setOnPreferenceChangeListener { _, newValue -> +// updateHevTunSettings(newValue as Boolean) +// true +// } hevTunRwTimeout?.setOnPreferenceChangeListener { _, any -> val nval = any as String @@ -226,7 +226,7 @@ class SettingsActivity : BaseActivity() { dnsHosts?.summary = MmkvManager.decodeSettingsString(AppConfig.PREF_DNS_HOSTS) delayTestUrl?.summary = MmkvManager.decodeSettingsString(AppConfig.PREF_DELAY_TEST_URL, AppConfig.DELAY_TEST_URL) - updateHevTunSettings(MmkvManager.decodeSettingsBool(AppConfig.PREF_USE_HEV_TUNNEL, true)) + //updateHevTunSettings(MmkvManager.decodeSettingsBool(AppConfig.PREF_USE_HEV_TUNNEL, true)) hevTunRwTimeout?.summary = MmkvManager.decodeSettingsString(AppConfig.PREF_HEV_TUNNEL_RW_TIMEOUT, AppConfig.HEVTUN_RW_TIMEOUT) initSharedPreference() diff --git a/V2rayNG/app/src/main/res/xml/pref_settings.xml b/V2rayNG/app/src/main/res/xml/pref_settings.xml index 7267234a..3c03bd59 100644 --- a/V2rayNG/app/src/main/res/xml/pref_settings.xml +++ b/V2rayNG/app/src/main/res/xml/pref_settings.xml @@ -78,11 +78,11 @@ android:summary="1500" android:title="@string/title_pref_vpn_mtu" /> - + + + + +