Skip to content

Commit

Permalink
fix(hysteria): share "standard" port range
Browse files Browse the repository at this point in the history
  • Loading branch information
xchacha20-poly1305 committed Jan 20, 2025
1 parent d7297ed commit ba64613
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package io.nekohasekai.sagernet.fmt.hysteria

import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.fmt.LOCALHOST4
import io.nekohasekai.sagernet.ktx.*
import io.nekohasekai.sagernet.ktx.getBool
import io.nekohasekai.sagernet.ktx.getIntOrNull
import io.nekohasekai.sagernet.ktx.getStr
import io.nekohasekai.sagernet.ktx.isIpAddress
import io.nekohasekai.sagernet.ktx.linkBoolean
import io.nekohasekai.sagernet.ktx.toStringPretty
import io.nekohasekai.sagernet.ktx.wrapIPV6Host
import libcore.Libcore
import moe.matsuri.nb4a.SingBoxOptions
import moe.matsuri.nb4a.SingBoxOptions.OutboundECHOptions
Expand All @@ -24,9 +30,7 @@ fun parseHysteria1(link: String): HysteriaBean {
authPayloadType = HysteriaBean.TYPE_STRING
authPayload = it
}
url.queryParameterNotBlank("insecure").also {
allowInsecure = it == "1" || it == "true"
}
allowInsecure = url.queryParameterNotBlank("insecure").linkBoolean()
alpn = url.queryParameterNotBlank("alpn")
obfuscation = url.queryParameterNotBlank("obfsParam")
protocol = when (url.queryParameterNotBlank("protocol")) {
Expand Down Expand Up @@ -61,13 +65,11 @@ fun parseHysteria2(link: String): HysteriaBean {
name = url.fragment

sni = url.queryParameterNotBlank("sni")
url.queryParameterNotBlank("insecure").also {
allowInsecure = it == "1" || it == "true"
}
allowInsecure = url.queryParameterNotBlank("insecure").linkBoolean()
obfuscation = url.queryParameterNotBlank("obfs-password")
url.queryParameterNotBlank("pinSHA256").also {
/*url.queryParameterNotBlank("pinSHA256").also {
// TODO your box do not support it
}
}*/
}
}

Expand All @@ -79,7 +81,11 @@ fun HysteriaBean.toUri(): String {
}
).apply {
host = serverAddress
ports = serverPorts
ports = when (val ports = HopPort.from(serverPorts)) {
is HopPort.Single -> serverPorts
// URL just support Hysteria style.
is HopPort.Ports -> ports.hyStyle().joinToString(HopPort.SPLIT_FLAG)
}
username = authPayload
}

Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/io/nekohasekai/sagernet/ktx/Formats.kt
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,10 @@ suspend fun parseProxies(text: String): List<AbstractBean> {
fun <T : Serializable> T.applyDefaultValues(): T {
initializeDefaultValues()
return this
}
}

/**
* Due to the lack of standards, the different share links use different style to
* store boolean.
*/
fun String.linkBoolean(): Boolean = this.lowercase().let { it == "1" || it == "true" }

0 comments on commit ba64613

Please sign in to comment.