From e304dce347c6dcead59c9a04d016bfe6954abe3d Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 23 Dec 2023 22:13:42 -0500 Subject: [PATCH] Add Wireguard maunal config No support for export and import via links yet --- .../kotlin/com/v2ray/ang/dto/ServerConfig.kt | 37 ++-- .../kotlin/com/v2ray/ang/dto/V2rayConfig.kt | 5 +- .../kotlin/com/v2ray/ang/ui/MainActivity.kt | 4 + .../kotlin/com/v2ray/ang/ui/ServerActivity.kt | 43 ++++- .../com/v2ray/ang/util/V2rayConfigUtil.kt | 10 +- .../res/layout/activity_server_wireguard.xml | 163 ++++++++++++++++++ V2rayNG/app/src/main/res/menu/menu_main.xml | 4 + .../app/src/main/res/values-ar/strings.xml | 2 + .../app/src/main/res/values-fa/strings.xml | 2 + .../app/src/main/res/values-ru/strings.xml | 2 + .../app/src/main/res/values-vi/strings.xml | 2 + .../src/main/res/values-zh-rCN/strings.xml | 2 + .../src/main/res/values-zh-rTW/strings.xml | 2 + V2rayNG/app/src/main/res/values/strings.xml | 3 + 14 files changed, 257 insertions(+), 24 deletions(-) create mode 100644 V2rayNG/app/src/main/res/layout/activity_server_wireguard.xml diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ServerConfig.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ServerConfig.kt index 88a9a7ec..915bf23b 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ServerConfig.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ServerConfig.kt @@ -19,23 +19,32 @@ data class ServerConfig( when(configType) { EConfigType.VMESS, EConfigType.VLESS -> return ServerConfig( - configType = configType, - outboundBean = V2rayConfig.OutboundBean( - protocol = configType.name.lowercase(), - settings = V2rayConfig.OutboundBean.OutSettingsBean( - vnext = listOf(V2rayConfig.OutboundBean.OutSettingsBean.VnextBean( - users = listOf(V2rayConfig.OutboundBean.OutSettingsBean.VnextBean.UsersBean())))), - streamSettings = V2rayConfig.OutboundBean.StreamSettingsBean())) - EConfigType.CUSTOM, EConfigType.WIREGUARD -> + configType = configType, + outboundBean = V2rayConfig.OutboundBean( + protocol = configType.name.lowercase(), + settings = V2rayConfig.OutboundBean.OutSettingsBean( + vnext = listOf(V2rayConfig.OutboundBean.OutSettingsBean.VnextBean( + users = listOf(V2rayConfig.OutboundBean.OutSettingsBean.VnextBean.UsersBean())))), + streamSettings = V2rayConfig.OutboundBean.StreamSettingsBean())) + EConfigType.CUSTOM -> return ServerConfig(configType = configType) EConfigType.SHADOWSOCKS, EConfigType.SOCKS, EConfigType.TROJAN -> return ServerConfig( - configType = configType, - outboundBean = V2rayConfig.OutboundBean( - protocol = configType.name.lowercase(), - settings = V2rayConfig.OutboundBean.OutSettingsBean( - servers = listOf(V2rayConfig.OutboundBean.OutSettingsBean.ServersBean())), - streamSettings = V2rayConfig.OutboundBean.StreamSettingsBean())) + configType = configType, + outboundBean = V2rayConfig.OutboundBean( + protocol = configType.name.lowercase(), + settings = V2rayConfig.OutboundBean.OutSettingsBean( + servers = listOf(V2rayConfig.OutboundBean.OutSettingsBean.ServersBean())), + streamSettings = V2rayConfig.OutboundBean.StreamSettingsBean())) + EConfigType.WIREGUARD -> + return ServerConfig( + configType = configType, + outboundBean = V2rayConfig.OutboundBean( + protocol = configType.name.lowercase(), + settings = V2rayConfig.OutboundBean.OutSettingsBean( + secretKey = "", + peers = listOf(V2rayConfig.OutboundBean.OutSettingsBean.WireGuardBean()) + ))) } } } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt index a97ee012..1def39a1 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt @@ -74,7 +74,7 @@ data class V2rayConfig( var response: Response? = null, /*DNS*/ val network: String? = null, - val address: Any? = null, + var address: Any? = null, val port: Int? = null, /*Freedom*/ var domainStrategy: String? = null, @@ -83,8 +83,9 @@ data class V2rayConfig( /*Loopback*/ val inboundTag: String? = null, /*Wireguard*/ - val secretKey: String? = null, + var secretKey: String? = null, val peers: List? = null, + var reserved: List? = null, ) { data class VnextBean(var address: String = "", diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index 201599b6..4c575d0b 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -253,6 +253,10 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList importManually(EConfigType.TROJAN.value) true } + R.id.import_manually_wireguard -> { + importManually(EConfigType.WIREGUARD.value) + true + } R.id.import_config_custom_clipboard -> { importConfigCustomClipboard() true diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt index ddfa4985..2f38b7e2 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt @@ -103,6 +103,9 @@ class ServerActivity : BaseActivity() { private val container_short_id: LinearLayout? by lazy { findViewById(R.id.l7) } private val et_spider_x: EditText? by lazy { findViewById(R.id.et_spider_x) } private val container_spider_x: LinearLayout? by lazy { findViewById(R.id.l8) } + private val et_reserved1: EditText? by lazy { findViewById(R.id.et_reserved1) } + private val et_reserved2: EditText? by lazy { findViewById(R.id.et_reserved2) } + private val et_reserved3: EditText? by lazy { findViewById(R.id.et_reserved3) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -116,7 +119,7 @@ class ServerActivity : BaseActivity() { EConfigType.SOCKS -> setContentView(R.layout.activity_server_socks) EConfigType.VLESS -> setContentView(R.layout.activity_server_vless) EConfigType.TROJAN -> setContentView(R.layout.activity_server_trojan) - else -> setContentView(R.layout.activity_server_vmess) + EConfigType.WIREGUARD -> setContentView(R.layout.activity_server_wireguard) } sp_network?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { @@ -182,7 +185,6 @@ class ServerActivity : BaseActivity() { */ private fun bindingServer(config: ServerConfig): Boolean { val outbound = config.getProxyOutbound() ?: return false - val streamSetting = config.outboundBean?.streamSettings ?: return false et_remarks.text = Utils.getEditable(config.remarks) et_address.text = Utils.getEditable(outbound.getServerAddress().orEmpty()) @@ -197,10 +199,16 @@ class ServerActivity : BaseActivity() { if (flow >= 0) { sp_flow?.setSelection(flow) } - } else if (config.configType == EConfigType.TROJAN) { - val flow = Utils.arrayFind(flows, outbound.settings?.servers?.get(0)?.flow.orEmpty()) - if (flow >= 0) { - sp_flow?.setSelection(flow) + } else if (config.configType == EConfigType.WIREGUARD) { + et_public_key?.text = Utils.getEditable(outbound.settings?.peers?.get(0)?.publicKey.orEmpty()) + if (outbound.settings?.reserved == null) { + et_reserved1?.text = Utils.getEditable("0") + et_reserved2?.text = Utils.getEditable("0") + et_reserved3?.text = Utils.getEditable("0") + } else { + et_reserved1?.text = Utils.getEditable(outbound.settings?.reserved?.get(0).toString()) + et_reserved2?.text = Utils.getEditable(outbound.settings?.reserved?.get(1).toString()) + et_reserved3?.text = Utils.getEditable(outbound.settings?.reserved?.get(2).toString()) } } val securityEncryptions = if (config.configType == EConfigType.SHADOWSOCKS) shadowsocksSecuritys else securitys @@ -209,6 +217,7 @@ class ServerActivity : BaseActivity() { sp_security?.setSelection(security) } + val streamSetting = config.outboundBean?.streamSettings ?: return true val streamSecurity = Utils.arrayFind(streamSecuritys, streamSetting.security) if (streamSecurity >= 0) { sp_stream_security?.setSelection(streamSecurity) @@ -282,6 +291,10 @@ class ServerActivity : BaseActivity() { //et_security.text = null sp_flow?.setSelection(0) + et_public_key?.text = null + et_reserved1?.text = Utils.getEditable("0") + et_reserved2?.text = Utils.getEditable("0") + et_reserved3?.text = Utils.getEditable("0") return true } @@ -332,6 +345,10 @@ class ServerActivity : BaseActivity() { config.outboundBean?.settings?.servers?.get(0)?.let { server -> saveServers(server, port, config) } + val wireguard = config.outboundBean?.settings + wireguard?.peers?.get(0)?.let { _ -> + savePeer(wireguard, port) + } config.outboundBean?.streamSettings?.let { saveStreamSettings(it) } @@ -378,6 +395,20 @@ class ServerActivity : BaseActivity() { } } + private fun savePeer(wireguard: V2rayConfig.OutboundBean.OutSettingsBean, port: Int) { + wireguard.secretKey = et_id.text.toString().trim() + wireguard.peers?.get(0)?.publicKey = et_public_key?.text.toString().trim() + wireguard.peers?.get(0)?.endpoint = et_address.text.toString().trim() + ":" + port + val reserved1 = Utils.parseInt(et_reserved1?.text.toString()) + val reserved2 = Utils.parseInt(et_reserved2?.text.toString()) + val reserved3 = Utils.parseInt(et_reserved3?.text.toString()) + if (reserved1 > 0 || reserved2 > 0 || reserved3 > 0) { + wireguard.reserved = listOf(reserved1, reserved2, reserved3) + }else { + wireguard.reserved = null + } + } + private fun saveStreamSettings(streamSetting: V2rayConfig.OutboundBean.StreamSettingsBean) { val network = sp_network?.selectedItemPosition ?: return val type = sp_header_type?.selectedItemPosition ?: return diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt index 9a785c16..2a80162c 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt @@ -402,7 +402,6 @@ object V2rayConfigUtil { private fun updateOutboundWithGlobalSettings(outbound: V2rayConfig.OutboundBean): Boolean { try { var muxEnabled = settingsStorage?.decodeBool(AppConfig.PREF_MUX_ENABLED, false) - val protocol = outbound.protocol if (protocol.equals(EConfigType.SHADOWSOCKS.name, true) || protocol.equals(EConfigType.SOCKS.name, true) @@ -414,7 +413,6 @@ object V2rayConfigUtil { ) { muxEnabled = false } - if (muxEnabled == true) { outbound.mux?.enabled = true outbound.mux?.concurrency = @@ -428,6 +426,14 @@ object V2rayConfigUtil { outbound.mux?.concurrency = -1 } + if (protocol.equals(EConfigType.WIREGUARD.name, true)) { + val localTunAddr = mutableListOf("172.16.0.2/32") + if (settingsStorage?.decodeBool(AppConfig.PREF_PREFER_IPV6) == true) { + localTunAddr.add("2606:4700:110:8f81:d551:a0:532e:a2b3/128") + } + outbound.settings?.address = localTunAddr + } + if (outbound.streamSettings?.network == DEFAULT_NETWORK && outbound.streamSettings?.tcpSettings?.header?.type == HTTP ) { diff --git a/V2rayNG/app/src/main/res/layout/activity_server_wireguard.xml b/V2rayNG/app/src/main/res/layout/activity_server_wireguard.xml new file mode 100644 index 00000000..284a8b23 --- /dev/null +++ b/V2rayNG/app/src/main/res/layout/activity_server_wireguard.xml @@ -0,0 +1,163 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/V2rayNG/app/src/main/res/menu/menu_main.xml b/V2rayNG/app/src/main/res/menu/menu_main.xml index b771b294..ec25a4a7 100644 --- a/V2rayNG/app/src/main/res/menu/menu_main.xml +++ b/V2rayNG/app/src/main/res/menu/menu_main.xml @@ -34,6 +34,10 @@ android:id="@+id/import_manually_trojan" android:title="@string/menu_item_import_config_manually_trojan" app:showAsAction="never" /> + الكتابة يدويا [Shadowsocks] الكتابة يدويا [Socks] الكتابة يدويا [Trojan] + [Wireguard] الكتابة يدويا تكوين مخصص استيراد التكوين المخصص من الحافظة استيراد التكوين المخصص من ملف محلي @@ -62,6 +63,7 @@ مفتاح عام ShortId SpiderX + Reserved (اختياري) نجاح فشل لا يوجد شيء diff --git a/V2rayNG/app/src/main/res/values-fa/strings.xml b/V2rayNG/app/src/main/res/values-fa/strings.xml index 5b1331a8..96b2cbe2 100644 --- a/V2rayNG/app/src/main/res/values-fa/strings.xml +++ b/V2rayNG/app/src/main/res/values-fa/strings.xml @@ -30,6 +30,7 @@ تایپ دستی[Shadowsocks] تایپ دستی[Socks] تایپ دستی[Trojan] + [Wireguard]تایپ دستی پیکربندی سفارشی پیکربندی سفارشی را از کلیپ‌بورد وارد کنید پیکربندی سفارشی را به صورت محلی وارد کنید @@ -59,6 +60,7 @@ نام‌کاربری (اختیاری) رمزگذاری جریان + Reserved (اختیاری) موفقیت شکست چیزی نیست diff --git a/V2rayNG/app/src/main/res/values-ru/strings.xml b/V2rayNG/app/src/main/res/values-ru/strings.xml index 832e2023..e2c4e548 100644 --- a/V2rayNG/app/src/main/res/values-ru/strings.xml +++ b/V2rayNG/app/src/main/res/values-ru/strings.xml @@ -30,6 +30,7 @@ Ручной ввод профиля Shadowsocks Ручной ввод профиля Socks Ручной ввод профиля Trojan + Ручной ввод профиля Wireguard Пользовательский профиль Импорт из буфера обмена Импорт из файла @@ -64,6 +65,7 @@ PublicKey ShortId SpiderX + Reserved (необязательно) Успешно Ошибка Ничего нет diff --git a/V2rayNG/app/src/main/res/values-vi/strings.xml b/V2rayNG/app/src/main/res/values-vi/strings.xml index 274dadd5..fab44ac3 100644 --- a/V2rayNG/app/src/main/res/values-vi/strings.xml +++ b/V2rayNG/app/src/main/res/values-vi/strings.xml @@ -29,6 +29,7 @@ Nhập thủ công [Shadowsocks] Nhập thủ công [Socks] Nhập thủ công [Trojan] + Nhập thủ công [Wireguard] Nâng cao / Cấu hình tùy chỉnh Nhập cấu hình tùy chỉnh từ Clipboard Nhập cấu hình tùy chỉnh từ Tệp @@ -58,6 +59,7 @@ Tên người dùng (Bổ sung) Mã hoá Kiểm soát lưu lượng + Reserved (Bổ sung) Thành công! Đã xảy ra lỗi, vui lòng thử lại! Không có gì ở đây 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 70da38bd..483fbe1f 100644 --- a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml @@ -29,6 +29,7 @@ 手动输入[Shadowsocks] 手动输入[Socks] 手动输入[Trojan] + 手动输入[Wireguard] 自定义配置 从剪贴板导入自定义配置 从本地导入自定义配置 @@ -58,6 +59,7 @@ 用户名(可选) 加密方式(encryption) 流控(flow) + Reserved(可选) 成功 失败 没有数据 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 6f8c36b8..de2a4862 100644 --- a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml @@ -29,6 +29,7 @@ 手動鍵入 [Shadowsocks] 手動鍵入 [Socks] 手動鍵入 [Trojan] + 手動鍵入 [Wireguard] 自訂組態 從剪貼簿匯入自訂組態 從本地匯入自訂組態 @@ -58,6 +59,7 @@ 使用者名稱 (可選) 加密 (encryption) 流程 (flow) + Reserved (可選) 成功 失敗 無資料 diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml index 2805ae07..532d9fc8 100644 --- a/V2rayNG/app/src/main/res/values/strings.xml +++ b/V2rayNG/app/src/main/res/values/strings.xml @@ -30,6 +30,7 @@ Type manually[Shadowsocks] Type manually[Socks] Type manually[Trojan] + Type manually[Wireguard] Custom config Import custom config from Clipboard Import custom config from locally @@ -64,6 +65,8 @@ PublicKey ShortId SpiderX + SecretKey + Reserved(Optional) Success Failure There is nothing