Skip to content

Commit

Permalink
Merge pull request #2718 from yuhan6665/wireguard
Browse files Browse the repository at this point in the history
Wireguard can configure tun address
  • Loading branch information
2dust authored Jan 2, 2024
2 parents 834766e + 73fad43 commit 41b2251
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ data class ServerConfig(
fun getV2rayPointDomainAndPort(): String {
val address = getProxyOutbound()?.getServerAddress().orEmpty()
val port = getProxyOutbound()?.getServerPort()
return if (Utils.isIpv6Address(address)) {
String.format("[%s]:%s", address, port)
} else {
String.format("%s:%s", address, port)
}
return Utils.getIpv6Address(address) + ":" + port
}
}
18 changes: 17 additions & 1 deletion V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.v2ray.ang.util.MmkvManager
import com.v2ray.ang.util.MmkvManager.ID_MAIN
import com.v2ray.ang.util.MmkvManager.KEY_SELECTED_SERVER
import com.v2ray.ang.util.Utils
import com.v2ray.ang.util.Utils.getIpv6Address

class ServerActivity : BaseActivity() {

Expand Down Expand Up @@ -106,6 +107,9 @@ class ServerActivity : BaseActivity() {
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) }
private val et_local_v4_address: EditText? by lazy { findViewById(R.id.et_local_v4_address) }
private val et_local_v6_address: EditText? by lazy { findViewById(R.id.et_local_v6_address) }


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -210,6 +214,14 @@ class ServerActivity : BaseActivity() {
et_reserved2?.text = Utils.getEditable(outbound.settings?.reserved?.get(1).toString())
et_reserved3?.text = Utils.getEditable(outbound.settings?.reserved?.get(2).toString())
}
if (outbound.settings?.address == null) {
et_local_v4_address?.text = Utils.getEditable("172.16.0.2/32")
et_local_v6_address?.text = Utils.getEditable("2606:4700:110:8f81:d551:a0:532e:a2b3/128")
} else {
val list = outbound.settings?.address as List<*>
et_local_v4_address?.text = Utils.getEditable(list.get(0).toString())
et_local_v6_address?.text = Utils.getEditable(list.get(1).toString())
}
}
val securityEncryptions = if (config.configType == EConfigType.SHADOWSOCKS) shadowsocksSecuritys else securitys
val security = Utils.arrayFind(securityEncryptions, outbound.getSecurityEncryption().orEmpty())
Expand Down Expand Up @@ -295,6 +307,8 @@ class ServerActivity : BaseActivity() {
et_reserved1?.text = Utils.getEditable("0")
et_reserved2?.text = Utils.getEditable("0")
et_reserved3?.text = Utils.getEditable("0")
et_local_v4_address?.text = Utils.getEditable("172.16.0.2/32")
et_local_v6_address?.text = Utils.getEditable("2606:4700:110:8f81:d551:a0:532e:a2b3/128")
return true
}

Expand Down Expand Up @@ -398,7 +412,7 @@ 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
wireguard.peers?.get(0)?.endpoint = getIpv6Address(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())
Expand All @@ -407,6 +421,8 @@ class ServerActivity : BaseActivity() {
}else {
wireguard.reserved = null
}
wireguard.address = listOf(et_local_v4_address?.text.toString().trim(),
et_local_v6_address?.text.toString().trim())
}

private fun saveStreamSettings(streamSetting: V2rayConfig.OutboundBean.StreamSettingsBean) {
Expand Down
2 changes: 1 addition & 1 deletion V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ object Utils {
}

fun getIpv6Address(address: String): String {
return if (isIpv6Address(address)) {
return if (isIpv6Address(address) && !address.contains('[') && !address.contains(']')) {
String.format("[%s]", address)
} else {
address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,13 @@ object V2rayConfigUtil {
}

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")
var localTunAddr = if (outbound.settings?.address == null) {
listOf("172.16.0.2/32", "2606:4700:110:8f81:d551:a0:532e:a2b3/128")
} else {
outbound.settings?.address as List<*>
}
if (settingsStorage?.decodeBool(AppConfig.PREF_PREFER_IPV6) != true) {
localTunAddr = listOf(localTunAddr.first())
}
outbound.settings?.address = localTunAddr
}
Expand Down
37 changes: 37 additions & 0 deletions V2rayNG/app/src/main/res/layout/activity_server_wireguard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,43 @@
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_margin_top_height"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/server_lab_local_v4_address" />

<EditText
android:id="@+id/et_local_v4_address"
android:layout_width="match_parent"
android:layout_height="@dimen/edit_height"
android:inputType="text" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_margin_top_height"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/server_lab_local_v6_address" />

<EditText
android:id="@+id/et_local_v6_address"
android:layout_width="match_parent"
android:layout_height="@dimen/edit_height"
android:inputType="text" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
Expand Down
2 changes: 2 additions & 0 deletions V2rayNG/app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
<string name="server_lab_short_id" translatable="false">ShortId</string>
<string name="server_lab_spider_x" translatable="false">SpiderX</string>
<string name="server_lab_reserved">Reserved (اختياري)</string>
<string name="server_lab_local_v4_address">العنوان المحلي IPv4(اختياري)</string>
<string name="server_lab_local_v6_address">العنوان المحلي IPv6(اختياري, يستخدم فقط عندما تفضل IPv6)</string>
<string name="toast_success">نجاح</string>
<string name="toast_failure">فشل</string>
<string name="toast_none_data">لا يوجد شيء</string>
Expand Down
2 changes: 2 additions & 0 deletions V2rayNG/app/src/main/res/values-fa/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
<string name="server_lab_encryption">رمزگذاری</string>
<string name="server_lab_flow">جریان</string>
<string name="server_lab_reserved">Reserved (اختیاری)</string>
<string name="server_lab_local_v4_address">آدرس محلی IPv4(اختیاری)</string>
<string name="server_lab_local_v6_address">آدرس محلی IPv6(اختیاری, فقط زمانی استفاده می شود که IPv6 را ترجیح می دهد)</string>
<string name="toast_success">موفقیت</string>
<string name="toast_failure">شکست</string>
<string name="toast_none_data">چیزی نیست</string>
Expand Down
2 changes: 2 additions & 0 deletions V2rayNG/app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
<string name="server_lab_short_id" translatable="false">ShortId</string>
<string name="server_lab_spider_x" translatable="false">SpiderX</string>
<string name="server_lab_reserved">Reserved (необязательно)</string>
<string name="server_lab_local_v4_address">локальный адрес IPv4(необязательно)</string>
<string name="server_lab_local_v6_address">локальный адрес IPv6(необязательно, используется только в том случае если предпочитаете IPv6)</string>
<string name="toast_success">Успешно</string>
<string name="toast_failure">Ошибка</string>
<string name="toast_none_data">Ничего нет</string>
Expand Down
2 changes: 2 additions & 0 deletions V2rayNG/app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
<string name="server_lab_encryption">Mã hoá</string>
<string name="server_lab_flow">Kiểm soát lưu lượng</string>
<string name="server_lab_reserved">Reserved (Bổ sung)</string>
<string name="server_lab_local_v4_address">địa chỉ cục bộ IPv4(Bổ sung)</string>
<string name="server_lab_local_v6_address">địa chỉ cục bộ IPv6(Bổ sung, chỉ được sử dụng khi thích IPv6)</string>
<string name="toast_success">Thành công!</string>
<string name="toast_failure">Đã xảy ra lỗi, vui lòng thử lại!</string>
<string name="toast_none_data">Không có gì ở đây</string>
Expand Down
2 changes: 2 additions & 0 deletions V2rayNG/app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
<string name="server_lab_encryption">加密方式(encryption)</string>
<string name="server_lab_flow">流控(flow)</string>
<string name="server_lab_reserved">Reserved(可选)</string>
<string name="server_lab_local_v4_address">本地 IPv4 地址(可选)</string>
<string name="server_lab_local_v6_address">本地 IPv6 地址(可选, 仅 IPv6 优先时使用)</string>
<string name="toast_success">成功</string>
<string name="toast_failure">失败</string>
<string name="toast_none_data">没有数据</string>
Expand Down
2 changes: 2 additions & 0 deletions V2rayNG/app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
<string name="server_lab_encryption">加密 (encryption)</string>
<string name="server_lab_flow">流程 (flow)</string>
<string name="server_lab_reserved">Reserved (可選)</string>
<string name="server_lab_local_v4_address">本機 IPv4 位址(可選)</string>
<string name="server_lab_local_v6_address">本機 IPv6 位址(可選, 僅偏好 IPv6 時使用)</string>
<string name="toast_success">成功</string>
<string name="toast_failure">失敗</string>
<string name="toast_none_data">無資料</string>
Expand Down
2 changes: 2 additions & 0 deletions V2rayNG/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
<string name="server_lab_spider_x" translatable="false">SpiderX</string>
<string name="server_lab_secret_key" translatable="false">SecretKey</string>
<string name="server_lab_reserved">Reserved(Optional)</string>
<string name="server_lab_local_v4_address">local address IPv4(Optional)</string>
<string name="server_lab_local_v6_address">local address IPv6(Optional, only used when prefer IPv6)</string>
<string name="toast_success">Success</string>
<string name="toast_failure">Failure</string>
<string name="toast_none_data">There is nothing</string>
Expand Down

0 comments on commit 41b2251

Please sign in to comment.