Skip to content

Commit

Permalink
Add port hopping for hy2
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 31, 2024
1 parent 81ed321 commit 2ec691f
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 15 deletions.
11 changes: 11 additions & 0 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/Hysteria2Bean.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ data class Hysteria2Bean(
val socks5: Socks5Bean? = null,
val http: Socks5Bean? = null,
val tls: TlsBean? = null,
val transport: TransportBean? = null,
) {
data class ObfsBean(
val type: String?,
Expand All @@ -25,5 +26,15 @@ data class Hysteria2Bean(
data class TlsBean(
val sni: String?,
val insecure: Boolean?,
val pinSHA256: String?,
)

data class TransportBean(
val type: String?,
val udp: TransportUdpBean?
) {
data class TransportUdpBean(
val hopInterval: String?,
)
}
}
3 changes: 3 additions & 0 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ProfileItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ data class ProfileItem(
var mtu: Int? = null,

var obfsPassword: String? = null,
var portHopping: String? = null,
var portHoppingInterval: String? = null,
var pinSHA256: String? = null,

) {
companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
holder.itemMainBinding.layoutEdit.setOnClickListener {
val intent = Intent().putExtra("guid", guid)
.putExtra("isRunning", isRunning)
.putExtra("createConfigType", profile.configType.value)
if (profile.configType == EConfigType.CUSTOM) {
mActivity.startActivity(intent.setClass(mActivity, ServerCustomConfigActivity::class.java))
} else {
Expand Down
29 changes: 19 additions & 10 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.v2ray.ang.AppConfig
import com.v2ray.ang.AppConfig.ANG_PACKAGE
import com.v2ray.ang.AppConfig.DEFAULT_PORT
import com.v2ray.ang.AppConfig.PREF_ALLOW_INSECURE
import com.v2ray.ang.AppConfig.REALITY
import com.v2ray.ang.AppConfig.TLS
import com.v2ray.ang.AppConfig.WIREGUARD_LOCAL_ADDRESS_V4
import com.v2ray.ang.AppConfig.WIREGUARD_LOCAL_ADDRESS_V6
import com.v2ray.ang.AppConfig.WIREGUARD_LOCAL_MTU
import com.v2ray.ang.R
import com.v2ray.ang.dto.EConfigType
import com.v2ray.ang.dto.ProfileItem
import com.v2ray.ang.AppConfig.DEFAULT_PORT
import com.v2ray.ang.AppConfig.REALITY
import com.v2ray.ang.AppConfig.TLS
import com.v2ray.ang.extension.toast
import com.v2ray.ang.util.JsonUtil
import com.v2ray.ang.util.MmkvManager
Expand Down Expand Up @@ -117,6 +117,9 @@ class ServerActivity : BaseActivity() {
private val et_local_address: EditText? by lazy { findViewById(R.id.et_local_address) }
private val et_local_mtu: EditText? by lazy { findViewById(R.id.et_local_mtu) }
private val et_obfs_password: EditText? by lazy { findViewById(R.id.et_obfs_password) }
private val et_port_hop: EditText? by lazy { findViewById(R.id.et_port_hop) }
private val et_port_hop_interval: EditText? by lazy { findViewById(R.id.et_port_hop_interval) }
private val et_pinsha256: EditText? by lazy { findViewById(R.id.et_pinsha256) }


override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -295,6 +298,9 @@ class ServerActivity : BaseActivity() {
}
} else if (config.configType == EConfigType.HYSTERIA2) {
et_obfs_password?.text = Utils.getEditable(config.obfsPassword)
et_port_hop?.text = Utils.getEditable(config.portHopping)
et_port_hop_interval?.text = Utils.getEditable(config.portHoppingInterval)
et_pinsha256?.text = Utils.getEditable(config.pinSHA256)
}

val securityEncryptions = if (config.configType == EConfigType.SHADOWSOCKS) shadowsocksSecuritys else securitys
Expand Down Expand Up @@ -396,13 +402,13 @@ class ServerActivity : BaseActivity() {
toast(R.string.server_lab_address)
return false
}
val port = Utils.parseInt(et_port.text.toString())
if (port <= 0) {
toast(R.string.server_lab_port)
return false
if (createConfigType != EConfigType.HYSTERIA2) {
if (Utils.parseInt(et_port.text.toString()) <= 0) {
toast(R.string.server_lab_port)
return false
}
}
val config =
MmkvManager.decodeServerConfig(editGuid) ?: ProfileItem.create(createConfigType)
val config = MmkvManager.decodeServerConfig(editGuid) ?: ProfileItem.create(createConfigType)
if (config.configType != EConfigType.SOCKS
&& config.configType != EConfigType.HTTP
&& TextUtils.isEmpty(et_id.text.toString())
Expand Down Expand Up @@ -431,7 +437,7 @@ class ServerActivity : BaseActivity() {
if (config.subscriptionId.isEmpty() && !subscriptionId.isNullOrEmpty()) {
config.subscriptionId = subscriptionId.orEmpty()
}
Log.d(ANG_PACKAGE, JsonUtil.toJsonPretty(config))
Log.d(ANG_PACKAGE, JsonUtil.toJsonPretty(config))
MmkvManager.encodeServerConfig(editGuid, config)
toast(R.string.toast_success)
finish()
Expand Down Expand Up @@ -464,6 +470,9 @@ class ServerActivity : BaseActivity() {
config.mtu = Utils.parseInt(et_local_mtu?.text.toString())
} else if (config.configType == EConfigType.HYSTERIA2) {
config.obfsPassword = et_obfs_password?.text?.toString()
config.portHopping = et_port_hop?.text?.toString()
config.portHoppingInterval = et_port_hop_interval?.text?.toString()
config.pinSHA256 = et_pinsha256?.text?.toString()
}
}

Expand Down
31 changes: 28 additions & 3 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/Hysteria2Fmt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import java.net.URI

object Hysteria2Fmt : FmtBase() {
fun parse(str: String): ProfileItem? {
var allowInsecure = settingsStorage.decodeBool(AppConfig.PREF_ALLOW_INSECURE,false)
var allowInsecure = settingsStorage.decodeBool(AppConfig.PREF_ALLOW_INSECURE, false)
val config = ProfileItem.create(EConfigType.HYSTERIA2)

val uri = URI(Utils.fixIllegalUrl(str))
Expand All @@ -37,6 +37,9 @@ object Hysteria2Fmt : FmtBase() {
config.alpn = queryParam["alpn"]

config.obfsPassword = queryParam["obfs-password"]
config.portHopping = queryParam["mport"]
config.pinSHA256 = queryParam["pinSHA256"]

}

return config
Expand All @@ -54,6 +57,12 @@ object Hysteria2Fmt : FmtBase() {
dicQuery["obfs"] = "salamander"
dicQuery["obfs-password"] = config.obfsPassword.orEmpty()
}
if (config.portHopping.isNotNullEmpty()) {
dicQuery["mport"] = config.portHopping.orEmpty()
}
if (config.pinSHA256.isNotNullEmpty()) {
dicQuery["pinSHA256"] = config.pinSHA256.orEmpty()
}

return toUri(config, config.password, dicQuery)
}
Expand All @@ -68,10 +77,25 @@ object Hysteria2Fmt : FmtBase() {
)
)

val transport = if (config.portHopping.isNullOrEmpty()) null else
Hysteria2Bean.TransportBean(
type = "udp",
udp = Hysteria2Bean.TransportBean.TransportUdpBean(
hopInterval = (config.portHoppingInterval ?: "30") + "s"
)
)

val server =
if (config.portHopping.isNullOrEmpty())
config.getServerAddressAndPort()
else
Utils.getIpv6Address(config.server) + ":" + config.portHopping

val bean = Hysteria2Bean(
server = config.getServerAddressAndPort(),
server = server,
auth = config.password,
obfs = obfs,
transport = transport,
socks5 = Hysteria2Bean.Socks5Bean(
listen = "$LOOPBACK:${socksPort}",
),
Expand All @@ -80,7 +104,8 @@ object Hysteria2Fmt : FmtBase() {
),
tls = Hysteria2Bean.TlsBean(
sni = config.sni ?: config.server,
insecure = config.insecure
insecure = config.insecure,
pinSHA256 = if (config.pinSHA256.isNullOrEmpty()) null else config.pinSHA256
)
)
return bean
Expand Down
42 changes: 40 additions & 2 deletions V2rayNG/app/src/main/res/layout/activity_server_hysteria2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@

<include layout="@layout/layout_address_port" />

<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="wrap_content"
android:layout_height="wrap_content"
android:text="@string/server_lab_id3" />

<EditText
android:id="@+id/et_id"
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"
Expand Down Expand Up @@ -41,16 +60,35 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/server_lab_id3" />
android:text="@string/server_lab_port_hop" />

<EditText
android:id="@+id/et_id"
android:id="@+id/et_port_hop"
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="wrap_content"
android:layout_height="wrap_content"
android:text="@string/server_lab_port_hop_interval" />

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

</LinearLayout>

<include layout="@layout/layout_tls_hysteria2" />

<LinearLayout
Expand Down
21 changes: 21 additions & 0 deletions V2rayNG/app/src/main/res/layout/layout_tls_hysteria2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@
android:layout_width="match_parent"
android:layout_height="@dimen/edit_height"
android:entries="@array/allowinsecures" />


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

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

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

</LinearLayout>
</LinearLayout>


</LinearLayout>
3 changes: 3 additions & 0 deletions V2rayNG/app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
<string name="msg_remark_is_duplicate">الملاحظات موجودة بالفعل</string>
<string name="toast_action_not_allowed">الإجراء غير مسموح به</string>
<string name="server_obfs_password">Obfs password</string>
<string name="server_lab_port_hop">Port Hopping</string>
<string name="server_lab_port_hop_interval">Port Hopping Interval</string>
<string name="server_lab_stream_pinsha256">pinSHA256</string>

<!-- PerAppProxyActivity -->
<string name="msg_dialog_progress">جار التحميل</string>
Expand Down
3 changes: 3 additions & 0 deletions V2rayNG/app/src/main/res/values-bn/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
<string name="msg_remark_is_duplicate">মন্তব্য ইতিমধ্যে বিদ্যমান</string>
<string name="toast_action_not_allowed">অ্যাকশন অনুমোদিত নয়</string>
<string name="server_obfs_password">Obfs password</string>
<string name="server_lab_port_hop">Port Hopping</string>
<string name="server_lab_port_hop_interval">Port Hopping Interval</string>
<string name="server_lab_stream_pinsha256">pinSHA256</string>

<!-- PerAppProxyActivity -->
<string name="msg_dialog_progress">লোড হচ্ছে</string>
Expand Down
3 changes: 3 additions & 0 deletions V2rayNG/app/src/main/res/values-fa/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@
<string name="menu_item_download_file">دانلود فایل‌ها</string>
<string name="toast_action_not_allowed">این عمل ممنوع است</string>
<string name="server_obfs_password">Obfs password</string>
<string name="server_lab_port_hop">Port Hopping</string>
<string name="server_lab_port_hop_interval">Port Hopping Interval</string>
<string name="server_lab_stream_pinsha256">pinSHA256</string>

<!-- PerAppProxyActivity -->
<string name="title_user_asset_add_url">URL را اضافه کنید</string>
Expand Down
3 changes: 3 additions & 0 deletions V2rayNG/app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
<string name="msg_remark_is_duplicate">Название уже существует</string>
<string name="toast_action_not_allowed">Это действие запрещено</string>
<string name="server_obfs_password">Пароль obfs</string>
<string name="server_lab_port_hop">Port Hopping</string>
<string name="server_lab_port_hop_interval">Port Hopping Interval</string>
<string name="server_lab_stream_pinsha256">pinSHA256</string>

<!-- PerAppProxyActivity -->
<string name="msg_dialog_progress">Загрузка…</string>
Expand Down
3 changes: 3 additions & 0 deletions V2rayNG/app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@
<string name="menu_item_download_file">Tải xuống tệp tin</string>
<string name="toast_action_not_allowed">Hành động này bị cấm!</string>
<string name="server_obfs_password">Obfs password</string>
<string name="server_lab_port_hop">Port Hopping</string>
<string name="server_lab_port_hop_interval">Port Hopping Interval</string>
<string name="server_lab_stream_pinsha256">pinSHA256</string>

<!-- PerAppProxyActivity -->
<string name="title_user_asset_add_url">Thêm URL nội dung</string>
Expand Down
3 changes: 3 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 @@ -107,6 +107,9 @@
<string name="menu_item_download_file">下载文件</string>
<string name="toast_action_not_allowed">禁止此项操作</string>
<string name="server_obfs_password">混淆密码</string>
<string name="server_lab_port_hop">跳跃端口(会覆盖服务器端口)</string>
<string name="server_lab_port_hop_interval">端口跳跃间隔(秒)</string>
<string name="server_lab_stream_pinsha256">SHA256证书指纹</string>

<!-- PerAppProxyActivity -->
<string name="title_user_asset_add_url">添加资产网址</string>
Expand Down
3 changes: 3 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 @@ -107,6 +107,9 @@
<string name="menu_item_download_file">下載檔案</string>
<string name="toast_action_not_allowed">禁止此項操作</string>
<string name="server_obfs_password">混淆密碼</string>
<string name="server_lab_port_hop">跳躍連接埠(會覆蓋伺服器連接埠)</string>
<string name="server_lab_port_hop_interval">連接埠跳躍間隔(秒)</string>
<string name="server_lab_stream_pinsha256">SHA256憑證指紋</string>

<!-- PerAppProxyActivity -->
<string name="title_user_asset_add_url">新增資產網址</string>
Expand Down
3 changes: 3 additions & 0 deletions V2rayNG/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
<string name="msg_remark_is_duplicate">The remarks already exists</string>
<string name="toast_action_not_allowed">Action not allowed</string>
<string name="server_obfs_password">Obfs password</string>
<string name="server_lab_port_hop">Port Hopping(will override the port)</string>
<string name="server_lab_port_hop_interval">Port Hopping Interval</string>
<string name="server_lab_stream_pinsha256">pinSHA256</string>

<!-- PerAppProxyActivity -->
<string name="msg_dialog_progress">Loading</string>
Expand Down

0 comments on commit 2ec691f

Please sign in to comment.