Skip to content

Commit c7ef42d

Browse files
committed
Refactoring mux settings
1 parent 607afa8 commit c7ef42d

14 files changed

Lines changed: 138 additions & 112 deletions

File tree

app/src/main/java/io/nekohasekai/sagernet/Constants.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ object Key {
5252
const val ALWAYS_SHOW_ADDRESS = "alwaysShowAddress"
5353

5454
// Protocol Settings
55-
const val MUX_TYPE = "muxType"
56-
const val MUX_PROTOCOLS = "mux"
57-
const val MUX_CONCURRENCY = "muxConcurrency"
5855
const val GLOBAL_ALLOW_INSECURE = "globalAllowInsecure"
5956

6057
const val ACQUIRE_WAKE_LOCK = "acquireWakeLock"

app/src/main/java/io/nekohasekai/sagernet/database/DataStore.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@ object DataStore : OnPreferenceDataStoreChangeListener {
169169

170170
// protocol
171171

172-
var muxType by configurationStore.stringToInt(Key.MUX_TYPE)
173-
var muxProtocols by configurationStore.stringSet(Key.MUX_PROTOCOLS)
174-
var muxConcurrency by configurationStore.stringToInt(Key.MUX_CONCURRENCY) { 8 }
175172
var globalAllowInsecure by configurationStore.boolean(Key.GLOBAL_ALLOW_INSECURE) { false }
176173

177174
// old cache, DO NOT ADD

app/src/main/java/io/nekohasekai/sagernet/database/ProxyEntity.kt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import io.nekohasekai.sagernet.ktx.app
3333
import io.nekohasekai.sagernet.ktx.applyDefaultValues
3434
import io.nekohasekai.sagernet.ui.profile.*
3535
import moe.matsuri.nb4a.Protocols
36+
import moe.matsuri.nb4a.SingBoxOptions.MultiplexOptions
3637
import moe.matsuri.nb4a.proxy.config.ConfigBean
3738
import moe.matsuri.nb4a.proxy.config.ConfigSettingActivity
3839
import moe.matsuri.nb4a.proxy.neko.*
@@ -309,19 +310,31 @@ data class ProxyEntity(
309310
}
310311
}
311312

312-
fun needCoreMux(): Boolean {
313+
fun singMux(): MultiplexOptions? {
313314
return when (type) {
314-
TYPE_VMESS -> if (vmessBean!!.isVLESS) {
315-
Protocols.isProfileNeedMux(vmessBean!!) && Protocols.shouldEnableMux("vless")
316-
} else {
317-
Protocols.isProfileNeedMux(vmessBean!!) && Protocols.shouldEnableMux("vmess")
315+
TYPE_VMESS -> MultiplexOptions().apply {
316+
enabled = vmessBean!!.enableMux
317+
padding = vmessBean!!.muxPadding
318+
max_streams = vmessBean!!.muxConcurrency
319+
protocol = when (vmessBean!!.muxType) {
320+
1 -> "smux"
321+
2 -> "yamux"
322+
else -> "h2mux"
323+
}
318324
}
319325

320-
TYPE_TROJAN -> Protocols.isProfileNeedMux(trojanBean!!)
321-
&& Protocols.shouldEnableMux("trojan")
326+
TYPE_TROJAN -> MultiplexOptions().apply {
327+
enabled = trojanBean!!.enableMux
328+
padding = trojanBean!!.muxPadding
329+
max_streams = trojanBean!!.muxConcurrency
330+
protocol = when (trojanBean!!.muxType) {
331+
1 -> "smux"
332+
2 -> "yamux"
333+
else -> "h2mux"
334+
}
335+
}
322336

323-
TYPE_SS -> !ssBean!!.sUoT && Protocols.shouldEnableMux("shadowsocks")
324-
else -> false
337+
else -> null
325338
}
326339
}
327340

app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -383,18 +383,12 @@ fun buildConfig(
383383
// val keepAliveInterval = DataStore.tcpKeepAliveInterval
384384
// val needKeepAliveInterval = keepAliveInterval !in intArrayOf(0, 15)
385385

386-
if (!muxApplied && proxyEntity.needCoreMux()) {
387-
muxApplied = true
388-
currentOutbound["multiplex"] = MultiplexOptions().apply {
389-
enabled = true
390-
padding = Protocols.shouldEnableMux("padding")
391-
max_streams = DataStore.muxConcurrency
392-
protocol = when (DataStore.muxType) {
393-
1 -> "smux"
394-
2 -> "yamux"
395-
else -> "h2mux"
396-
}
397-
}.asMap()
386+
if (!muxApplied) {
387+
val muxObj = proxyEntity.singMux()
388+
if (muxObj != null && muxObj.enabled) {
389+
muxApplied = true
390+
currentOutbound["multiplex"] = muxObj.asMap()
391+
}
398392
}
399393
}
400394
}

app/src/main/java/io/nekohasekai/sagernet/fmt/trojan_go/TrojanGoFmt.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ fun TrojanGoBean.buildTrojanGoConfig(port: Int): String {
9292
put(password)
9393
})
9494
put("log_level", if (DataStore.logLevel > 0) 0 else 2)
95-
if (Protocols.shouldEnableMux("trojan-go")) put("mux", JSONObject().apply {
96-
put("enabled", true)
97-
put("concurrency", DataStore.muxConcurrency)
98-
})
95+
// if (Protocols.shouldEnableMux("trojan-go")) put("mux", JSONObject().apply {
96+
// put("enabled", true)
97+
// put("concurrency", DataStore.muxConcurrency)
98+
// })
9999
put("tcp", JSONObject().apply {
100100
put("prefer_ipv4", DataStore.ipv6Mode <= IPv6Mode.ENABLE)
101101
})

app/src/main/java/io/nekohasekai/sagernet/fmt/v2ray/StandardV2RayBean.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,20 @@ public abstract class StandardV2RayBean extends AbstractBean {
5252

5353
public Boolean enableECH;
5454

55+
public String echConfig;
56+
57+
// sing-box 不再使用
5558
public Boolean enablePqSignature;
5659

5760
public Boolean disabledDRS;
5861

59-
public String echConfig;
62+
// --------------------------------------- Mux
63+
64+
public Boolean enableMux;
65+
public Boolean muxPadding;
66+
public Integer muxType;
67+
public Integer muxConcurrency;
68+
6069

6170
// --------------------------------------- //
6271

@@ -101,11 +110,16 @@ public void initializeDefaultValues() {
101110
if (JavaUtil.isNullOrBlank(echConfig)) echConfig = "";
102111
if (enablePqSignature == null) enablePqSignature = false;
103112
if (disabledDRS == null) disabledDRS = false;
113+
114+
if (enableMux == null) enableMux = false;
115+
if (muxPadding == null) muxPadding = false;
116+
if (muxType == null) muxType = 0;
117+
if (muxConcurrency == null) muxConcurrency = 1;
104118
}
105119

106120
@Override
107121
public void serialize(ByteBufferOutput output) {
108-
output.writeInt(1);
122+
output.writeInt(2);
109123
super.serialize(output);
110124
output.writeString(uuid);
111125
output.writeString(encryption);
@@ -160,6 +174,11 @@ public void serialize(ByteBufferOutput output) {
160174
}
161175

162176
output.writeInt(packetEncoding);
177+
178+
output.writeBoolean(enableMux);
179+
output.writeBoolean(muxPadding);
180+
output.writeInt(muxType);
181+
output.writeInt(muxConcurrency);
163182
}
164183

165184
@Override
@@ -239,6 +258,13 @@ public void deserialize(ByteBufferInput input) {
239258
}
240259

241260
packetEncoding = input.readInt();
261+
262+
if (version >= 2) {
263+
enableMux = input.readBoolean();
264+
muxPadding = input.readBoolean();
265+
muxType = input.readInt();
266+
muxConcurrency = input.readInt();
267+
}
242268
}
243269

244270
@Override
@@ -251,6 +277,10 @@ public void applyFeatureSettings(AbstractBean other) {
251277
bean.enableECH = enableECH;
252278
bean.disabledDRS = disabledDRS;
253279
bean.echConfig = echConfig;
280+
bean.enableMux = enableMux;
281+
bean.muxPadding = muxPadding;
282+
bean.muxType = muxType;
283+
bean.muxConcurrency = muxConcurrency;
254284
}
255285

256286
public boolean isVLESS() {

app/src/main/java/io/nekohasekai/sagernet/group/RawUpdater.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,20 @@ object RawUpdater : GroupUpdater() {
416416
realityOpt.value.toString()
417417
}
418418
}
419+
420+
"smux" -> for (smuxOpt in (opt.value as Map<String, Any>)) {
421+
when (smuxOpt.key.lowercase()) {
422+
"enabled" -> bean.enableMux =
423+
smuxOpt.value.toString() == "true"
424+
425+
"max-streams" -> bean.muxConcurrency =
426+
smuxOpt.value.toString().toInt()
427+
428+
"padding" -> bean.muxPadding =
429+
smuxOpt.value.toString() == "true"
430+
}
431+
}
432+
419433
}
420434
}
421435
if (isHttpUpgrade) {
@@ -475,6 +489,19 @@ object RawUpdater : GroupUpdater() {
475489
grpcOpt.value.toString()
476490
}
477491
}
492+
493+
"smux" -> for (smuxOpt in (opt.value as Map<String, Any>)) {
494+
when (smuxOpt.key.lowercase()) {
495+
"enabled" -> bean.enableMux =
496+
smuxOpt.value.toString() == "true"
497+
498+
"max-streams" -> bean.muxConcurrency =
499+
smuxOpt.value.toString().toInt()
500+
501+
"padding" -> bean.muxPadding =
502+
smuxOpt.value.toString() == "true"
503+
}
504+
}
478505
}
479506
}
480507
if (isHttpUpgrade) {

app/src/main/java/io/nekohasekai/sagernet/ui/SettingsPreferenceFragment.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import io.nekohasekai.sagernet.database.preference.EditTextPreferenceModifiers
1717
import io.nekohasekai.sagernet.ktx.*
1818
import io.nekohasekai.sagernet.utils.Theme
1919
import io.nekohasekai.sagernet.widget.AppListPreference
20-
import moe.matsuri.nb4a.Protocols
2120
import moe.matsuri.nb4a.ui.*
2221

2322
class SettingsPreferenceFragment : PreferenceFragmentCompat() {
@@ -81,7 +80,6 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
8180
val ipv6Mode = findPreference<Preference>(Key.IPV6_MODE)!!
8281
val trafficSniffing = findPreference<Preference>(Key.TRAFFIC_SNIFFING)!!
8382

84-
val muxConcurrency = findPreference<EditTextPreference>(Key.MUX_CONCURRENCY)!!
8583
val tcpKeepAliveInterval = findPreference<EditTextPreference>(Key.TCP_KEEP_ALIVE_INTERVAL)!!
8684
tcpKeepAliveInterval.isVisible = false
8785

@@ -123,16 +121,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
123121
true
124122
}
125123

126-
val muxProtocols = findPreference<MultiSelectListPreference>(Key.MUX_PROTOCOLS)!!
127-
128-
muxProtocols.apply {
129-
val e = Protocols.getCanMuxList().toTypedArray()
130-
entries = e
131-
entryValues = e
132-
}
133-
134124
portLocalDns.setOnBindEditTextListener(EditTextPreferenceModifiers.Port)
135-
muxConcurrency.setOnBindEditTextListener(EditTextPreferenceModifiers.Port)
136125
mixedPort.setOnBindEditTextListener(EditTextPreferenceModifiers.Port)
137126

138127
val metedNetwork = findPreference<Preference>(Key.METERED_NETWORK)!!
@@ -175,7 +164,6 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
175164
appendHttpProxy.onPreferenceChangeListener = reloadListener
176165
showDirectSpeed.onPreferenceChangeListener = reloadListener
177166
trafficSniffing.onPreferenceChangeListener = reloadListener
178-
muxConcurrency.onPreferenceChangeListener = reloadListener
179167
tcpKeepAliveInterval.onPreferenceChangeListener = reloadListener
180168
bypassLan.onPreferenceChangeListener = reloadListener
181169
bypassLanInCore.onPreferenceChangeListener = reloadListener

app/src/main/java/io/nekohasekai/sagernet/ui/profile/StandardV2RaySettingsActivity.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ abstract class StandardV2RaySettingsActivity : ProfileSettingsActivity<StandardV
4747
private val enableECH = pbm.add(PreferenceBinding(Type.Bool, "enableECH"))
4848
private val echConfig = pbm.add(PreferenceBinding(Type.Text, "echConfig"))
4949

50+
private val enableMux = pbm.add(PreferenceBinding(Type.Bool, "enableMux"))
51+
private val muxPadding = pbm.add(PreferenceBinding(Type.Bool, "muxPadding"))
52+
private val muxType = pbm.add(PreferenceBinding(Type.TextToInt, "muxType"))
53+
private val muxConcurrency = pbm.add(PreferenceBinding(Type.TextToInt, "muxConcurrency"))
54+
5055
override fun StandardV2RayBean.init() {
5156
if (this is TrojanBean) {
5257
this@StandardV2RaySettingsActivity.uuid.fieldName = "password"
@@ -158,6 +163,7 @@ abstract class StandardV2RaySettingsActivity : ProfileSettingsActivity<StandardV
158163
host.preference.isVisible = true
159164
path.preference.isVisible = true
160165
}
166+
161167
"ws" -> {
162168
host.preference.setTitle(R.string.ws_host)
163169
path.preference.setTitle(R.string.ws_path)

app/src/main/java/moe/matsuri/nb4a/Protocols.kt

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,13 @@ package moe.matsuri.nb4a
22

33
import android.content.Context
44
import io.nekohasekai.sagernet.R
5-
import io.nekohasekai.sagernet.database.DataStore
65
import io.nekohasekai.sagernet.database.ProxyEntity.Companion.TYPE_NEKO
76
import io.nekohasekai.sagernet.fmt.AbstractBean
8-
import io.nekohasekai.sagernet.fmt.v2ray.StandardV2RayBean
9-
import io.nekohasekai.sagernet.fmt.v2ray.isTLS
107
import io.nekohasekai.sagernet.ktx.app
118
import io.nekohasekai.sagernet.ktx.getColorAttr
12-
import moe.matsuri.nb4a.plugin.NekoPluginManager
139

1410
// Settings for all protocols, built-in or plugin
1511
object Protocols {
16-
// Mux
17-
18-
fun isProfileNeedMux(bean: StandardV2RayBean): Boolean {
19-
return when (bean.type) {
20-
"tcp", "ws" -> true
21-
"http" -> !bean.isTLS()
22-
else -> false
23-
}
24-
}
25-
26-
fun shouldEnableMux(protocol: String): Boolean {
27-
return DataStore.muxProtocols.contains(protocol)
28-
}
29-
30-
fun getCanMuxList(): List<String> {
31-
// built-in and support mux
32-
val list = mutableListOf("vmess", "trojan", "trojan-go", "shadowsocks", "vless", "padding")
33-
34-
NekoPluginManager.getProtocols().forEach {
35-
if (it.protocolConfig.optBoolean("canMux")) {
36-
list.add(it.protocolId)
37-
}
38-
}
39-
40-
return list
41-
}
4212

4313
// Deduplication
4414

0 commit comments

Comments
 (0)