diff --git a/V2rayNG/app/libs/arm64-v8a/libhev-socks5-tunnel.so b/V2rayNG/app/libs/arm64-v8a/libhev-socks5-tunnel.so new file mode 100644 index 00000000..5dcaea02 Binary files /dev/null and b/V2rayNG/app/libs/arm64-v8a/libhev-socks5-tunnel.so differ diff --git a/V2rayNG/app/libs/armeabi-v7a/libhev-socks5-tunnel.so b/V2rayNG/app/libs/armeabi-v7a/libhev-socks5-tunnel.so new file mode 100644 index 00000000..7b9326de Binary files /dev/null and b/V2rayNG/app/libs/armeabi-v7a/libhev-socks5-tunnel.so differ diff --git a/V2rayNG/app/libs/x86/libhev-socks5-tunnel.so b/V2rayNG/app/libs/x86/libhev-socks5-tunnel.so new file mode 100644 index 00000000..9bf50e71 Binary files /dev/null and b/V2rayNG/app/libs/x86/libhev-socks5-tunnel.so differ diff --git a/V2rayNG/app/libs/x86_64/libhev-socks5-tunnel.so b/V2rayNG/app/libs/x86_64/libhev-socks5-tunnel.so new file mode 100644 index 00000000..82b08152 Binary files /dev/null and b/V2rayNG/app/libs/x86_64/libhev-socks5-tunnel.so differ diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt index 18364096..f7adb61a 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt @@ -62,6 +62,7 @@ object AppConfig { const val PREF_IS_BOOTED = "pref_is_booted" const val PREF_CHECK_UPDATE_PRE_RELEASE = "pref_check_update_pre_release" const val PREF_GEO_FILES_SOURCES = "pref_geo_files_sources" + const val PREF_USE_HEV_TUNNEL = "pref_use_hev_tunnel" /** Cache keys. */ const val CACHE_SUBSCRIPTION_ID = "cache_subscription_id" 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 new file mode 100644 index 00000000..66370b8a --- /dev/null +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/TProxyService.kt @@ -0,0 +1,96 @@ +package com.v2ray.ang.service + +import android.content.Context +import android.os.ParcelFileDescriptor +import android.util.Log +import com.v2ray.ang.AppConfig +import com.v2ray.ang.AppConfig.VPN_MTU +import com.v2ray.ang.handler.MmkvManager +import com.v2ray.ang.handler.SettingsManager +import java.io.File + +/** + * Manages the tun2socks process that handles VPN traffic + */ +class TProxyService( + private val context: Context, + private val vpnInterface: ParcelFileDescriptor, + private val isRunningProvider: () -> Boolean, + private val restartCallback: () -> Unit +) : Tun2SocksControl { + companion object { + @JvmStatic + @Suppress("FunctionName") + private external fun TProxyStartService(configPath: String, fd: Int) + @JvmStatic + @Suppress("FunctionName") + private external fun TProxyStopService() + @JvmStatic + @Suppress("FunctionName") + private external fun TProxyGetStats(): LongArray? + + init { + System.loadLibrary("hev-socks5-tunnel") + } + } + + /** + * Starts the tun2socks process with the appropriate parameters. + */ + override fun startTun2Socks() { + 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") + + try { + Log.i(AppConfig.TAG, "TProxyStartService...") + TProxyStartService(configFile.absolutePath, vpnInterface.fd) + } catch (e: Exception) { + Log.e(AppConfig.TAG, "HevSocks5Tunnel exception: ${e.message}") + } + } + + private fun buildConfig(): String { + val socksPort = SettingsManager.getSocksPort() + val vpnConfig = SettingsManager.getCurrentVpnInterfaceAddressConfig() + return buildString { + appendLine("tunnel:") + appendLine(" mtu: $VPN_MTU") + appendLine(" ipv4: ${vpnConfig.ipv4Client}") + + if (MmkvManager.decodeSettingsBool(AppConfig.PREF_PREFER_IPV6) == true) { + appendLine(" ipv6: '${vpnConfig.ipv6Client}'") + } + + appendLine("socks5:") + appendLine(" port: ${socksPort}") + appendLine(" address: ${AppConfig.LOOPBACK}") + appendLine(" udp: 'udp'") + + MmkvManager.decodeSettingsString(AppConfig.PREF_LOGLEVEL)?.let { logPref -> + if (logPref != "none") { + val logLevel = if (logPref == "warning") "warn" else logPref + appendLine("misc:") + appendLine(" log-level: $logLevel") + } + } + } + } + + /** + * Stops the tun2socks process + */ + override fun stopTun2Socks() { + try { + Log.i(AppConfig.TAG, "TProxyStopService...") + TProxyStopService() + } catch (e: Exception) { + Log.e(AppConfig.TAG, "Failed to stop hev-socks5-tunnel", e) + } + } +} diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/Tun2SocksControl.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/Tun2SocksControl.kt new file mode 100644 index 00000000..f478249a --- /dev/null +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/Tun2SocksControl.kt @@ -0,0 +1,19 @@ +package com.v2ray.ang.service + +/** + * Interface that defines the control operations for tun2socks implementations. + * + * This interface is implemented by different tunnel solutions like: + */ +interface Tun2SocksControl { + /** + * Starts the tun2socks process with the appropriate parameters. + * This initializes the VPN tunnel and connects it to the SOCKS proxy. + */ + fun startTun2Socks() + + /** + * Stops the tun2socks process and cleans up resources. + */ + fun stopTun2Socks() +} 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 index e1fd52db..37a26fd2 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/Tun2SocksService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/Tun2SocksService.kt @@ -23,7 +23,7 @@ class Tun2SocksService( private val vpnInterface: ParcelFileDescriptor, private val isRunningProvider: () -> Boolean, private val restartCallback: () -> Unit -) { +) : Tun2SocksControl { companion object { private const val TUN2SOCKS = "libtun2socks.so" } @@ -33,7 +33,7 @@ class Tun2SocksService( /** * Starts the tun2socks process with the appropriate parameters. */ - fun startTun2Socks() { + override fun startTun2Socks() { Log.i(AppConfig.TAG, "Start run $TUN2SOCKS") val socksPort = SettingsManager.getSocksPort() val vpnConfig = SettingsManager.getCurrentVpnInterfaceAddressConfig() @@ -116,7 +116,7 @@ class Tun2SocksService( /** * Stops the tun2socks process */ - fun stopTun2Socks() { + override fun stopTun2Socks() { try { Log.i(AppConfig.TAG, "$TUN2SOCKS destroy") if (::process.isInitialized) { 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 93e8b583..db4cf04b 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 @@ -30,7 +30,7 @@ import java.lang.ref.SoftReference class V2RayVpnService : VpnService(), ServiceControl { private lateinit var mInterface: ParcelFileDescriptor private var isRunning = false - private var tun2SocksService: Tun2SocksService? = null + private var tun2SocksService: Tun2SocksControl? = null /**destroy * Unfortunately registerDefaultNetworkCallback is going to return our VPN interface: https://android.googlesource.com/platform/frameworks/base/+/dda156ab0c5d66ad82bdcf76cda07cbc0a9c8a2e @@ -145,20 +145,20 @@ class V2RayVpnService : VpnService(), ServiceControl { */ private fun configureVpnService(): Boolean { val builder = Builder() - + // Configure network settings (addresses, routing and DNS) configureNetworkSettings(builder) // Configure app-specific settings (session name and per-app proxy) configurePerAppProxy(builder) - + // Close the old interface since the parameters have been changed try { mInterface.close() } catch (ignored: Exception) { // ignored } - + // Configure platform-specific features configurePlatformFeatures(builder) @@ -173,21 +173,21 @@ class V2RayVpnService : VpnService(), ServiceControl { } return false } - + /** * Configures the basic network settings for the VPN. * This includes IP addresses, routing rules, and DNS servers. - * + * * @param builder The VPN Builder to configure */ private fun configureNetworkSettings(builder: Builder) { val vpnConfig = SettingsManager.getCurrentVpnInterfaceAddressConfig() val bypassLan = SettingsManager.routingRulesetsBypassLan() - + // Configure IPv4 settings builder.setMtu(VPN_MTU) builder.addAddress(vpnConfig.ipv4Client, 30) - + // Configure routing rules if (bypassLan) { AppConfig.ROUTED_IP_LIST.forEach { @@ -197,7 +197,7 @@ class V2RayVpnService : VpnService(), ServiceControl { } else { builder.addRoute("0.0.0.0", 0) } - + // Configure IPv6 if enabled if (MmkvManager.decodeSettingsBool(AppConfig.PREF_PREFER_IPV6) == true) { builder.addAddress(vpnConfig.ipv6Client, 126) @@ -208,7 +208,7 @@ class V2RayVpnService : VpnService(), ServiceControl { builder.addRoute("::", 0) } } - + // Configure DNS servers //if (MmkvManager.decodeSettingsBool(AppConfig.PREF_LOCAL_DNS_ENABLED) == true) { // builder.addDnsServer(PRIVATE_VLAN4_ROUTER) @@ -218,13 +218,13 @@ class V2RayVpnService : VpnService(), ServiceControl { builder.addDnsServer(it) } } - + builder.setSession(V2RayServiceManager.getRunningServerName()) } - + /** * Configures platform-specific VPN features for different Android versions. - * + * * @param builder The VPN Builder to configure */ private fun configurePlatformFeatures(builder: Builder) { @@ -236,7 +236,7 @@ class V2RayVpnService : VpnService(), ServiceControl { Log.e(AppConfig.TAG, "Failed to request default network", e) } } - + // Android Q (API 29) and above: Configure metering and HTTP proxy if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { builder.setMetered(false) @@ -296,14 +296,23 @@ class V2RayVpnService : VpnService(), ServiceControl { * Starts the tun2socks process with the appropriate parameters. */ private fun runTun2socks() { - tun2SocksService = Tun2SocksService( - context = applicationContext, - vpnInterface = mInterface, - isRunningProvider = { isRunning }, - restartCallback = { runTun2socks() } - ).also { - it.startTun2Socks() + if (MmkvManager.decodeSettingsBool(AppConfig.PREF_USE_HEV_TUNNEL) == true) { + tun2SocksService = TProxyService( + context = applicationContext, + vpnInterface = mInterface, + isRunningProvider = { isRunning }, + restartCallback = { runTun2socks() } + ) + } else { + tun2SocksService = Tun2SocksService( + context = applicationContext, + vpnInterface = mInterface, + isRunningProvider = { isRunning }, + restartCallback = { runTun2socks() } + ) } + + tun2SocksService?.startTun2Socks() } /** 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 ad270c33..f0317b6f 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 @@ -242,7 +242,8 @@ class SettingsActivity : BaseActivity() { AppConfig.PREF_DOUBLE_COLUMN_DISPLAY, AppConfig.PREF_PREFER_IPV6, AppConfig.PREF_PROXY_SHARING, - AppConfig.PREF_ALLOW_INSECURE + AppConfig.PREF_ALLOW_INSECURE, + AppConfig.PREF_USE_HEV_TUNNEL ).forEach { key -> findPreference(key)?.isChecked = MmkvManager.decodeSettingsBool(key, false) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/viewmodel/SettingsViewModel.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/viewmodel/SettingsViewModel.kt index fe108ea6..0001ad05 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/viewmodel/SettingsViewModel.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/viewmodel/SettingsViewModel.kt @@ -80,6 +80,7 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application AppConfig.SUBSCRIPTION_AUTO_UPDATE, AppConfig.PREF_FRAGMENT_ENABLED, AppConfig.PREF_MUX_ENABLED, + AppConfig.PREF_USE_HEV_TUNNEL -> { MmkvManager.encodeSettings(key, sharedPreferences.getBoolean(key, false)) } diff --git a/V2rayNG/app/src/main/res/values-ar/strings.xml b/V2rayNG/app/src/main/res/values-ar/strings.xml index ace91940..278ee738 100644 --- a/V2rayNG/app/src/main/res/values-ar/strings.xml +++ b/V2rayNG/app/src/main/res/values-ar/strings.xml @@ -247,6 +247,8 @@ اللغة إعدادات واجهة المستخدم إعدادات وضع واجهة المستخدم ليلاً + Enable New TUN Feature + When enabled, TUN will use hev-socks5-tunnel; otherwise, it will use badvpn-tun2socks. Logcat نسخ diff --git a/V2rayNG/app/src/main/res/values-bn/strings.xml b/V2rayNG/app/src/main/res/values-bn/strings.xml index 152cac9b..c717b5f7 100644 --- a/V2rayNG/app/src/main/res/values-bn/strings.xml +++ b/V2rayNG/app/src/main/res/values-bn/strings.xml @@ -248,6 +248,8 @@ ভাষা ইউআই সেটিংস ইউআই মোড সেটিংস + Enable New TUN Feature + When enabled, TUN will use hev-socks5-tunnel; otherwise, it will use badvpn-tun2socks. লগক্যাট কপি করুন diff --git a/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml b/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml index 3c1fa302..481373d5 100644 --- a/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml +++ b/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml @@ -248,6 +248,8 @@ زووݩ سامووا رابت منتوری سامووا هالت رابت منتوری + Enable New TUN Feature + When enabled, TUN will use hev-socks5-tunnel; otherwise, it will use badvpn-tun2socks. داسووا لف گیری diff --git a/V2rayNG/app/src/main/res/values-fa/strings.xml b/V2rayNG/app/src/main/res/values-fa/strings.xml index 1324a872..8ea92a8d 100644 --- a/V2rayNG/app/src/main/res/values-fa/strings.xml +++ b/V2rayNG/app/src/main/res/values-fa/strings.xml @@ -245,6 +245,8 @@ زبان تنظیمات رابط کاربری تنظیمات حالت رابط کاربری + Enable New TUN Feature + When enabled, TUN will use hev-socks5-tunnel; otherwise, it will use badvpn-tun2socks. گزارشات کپی diff --git a/V2rayNG/app/src/main/res/values-ru/strings.xml b/V2rayNG/app/src/main/res/values-ru/strings.xml index 126901e6..2c10ad1d 100644 --- a/V2rayNG/app/src/main/res/values-ru/strings.xml +++ b/V2rayNG/app/src/main/res/values-ru/strings.xml @@ -247,6 +247,8 @@ Язык Настройки интерфейса Тема интерфейса + Enable New TUN Feature + When enabled, TUN will use hev-socks5-tunnel; otherwise, it will use badvpn-tun2socks. Журнал Копировать diff --git a/V2rayNG/app/src/main/res/values-vi/strings.xml b/V2rayNG/app/src/main/res/values-vi/strings.xml index 9b545d20..62d9515f 100644 --- a/V2rayNG/app/src/main/res/values-vi/strings.xml +++ b/V2rayNG/app/src/main/res/values-vi/strings.xml @@ -248,6 +248,8 @@ Ngôn ngữ Cài đặt UI Cài đặt chế độ UI + Enable New TUN Feature + When enabled, TUN will use hev-socks5-tunnel; otherwise, it will use badvpn-tun2socks. Logcat Sao chép diff --git a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml index b52e0435..5a4e5121 100644 --- a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml @@ -245,6 +245,8 @@ 语言 用户界面设置 界面颜色设置 + 启用新的 TUN 功能 + 选择启用后 TUN 将使用 hev-socks5-tunnel 否则使用 badvpn-tun2socks Logcat 复制 diff --git a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml index 8fda1da6..72cf283a 100644 --- a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml @@ -246,6 +246,8 @@ 語言 介面顏色設定 介面顯示模式 + 啟用新 TUN 功能 + 選擇啟用後,TUN 將使用 hev-socks5-tunnel,否則使用 badvpn-tun2socks。 Logcat 複製 diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml index 5d70d51d..4a93fa95 100644 --- a/V2rayNG/app/src/main/res/values/strings.xml +++ b/V2rayNG/app/src/main/res/values/strings.xml @@ -249,6 +249,8 @@ Language UI settings UI mode settings + Enable New TUN Feature + When enabled, TUN will use hev-socks5-tunnel; otherwise, it will use badvpn-tun2socks. Logcat Copy diff --git a/V2rayNG/app/src/main/res/xml/pref_settings.xml b/V2rayNG/app/src/main/res/xml/pref_settings.xml index a2f179d7..c51c8281 100644 --- a/V2rayNG/app/src/main/res/xml/pref_settings.xml +++ b/V2rayNG/app/src/main/res/xml/pref_settings.xml @@ -13,6 +13,7 @@ android:key="pref_route_only_enabled" android:summary="@string/summary_pref_route_only_enabled" android:title="@string/title_pref_route_only_enabled" /> + + +