Skip to content

Commit 6f9fad1

Browse files
committed
add vless encroption
1 parent 58472e1 commit 6f9fad1

8 files changed

Lines changed: 44 additions & 3 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public abstract class StandardV2RayBean extends AbstractBean {
1111

1212
public String uuid;
1313
public String encryption; // or VLESS flow
14+
public String vlessEncryption; // VLESS ncryption
1415

1516
//////// End of VMess & VLESS ////////
1617

@@ -78,6 +79,7 @@ public void initializeDefaultValues() {
7879
if (JavaUtil.isNullOrBlank(uuid)) uuid = "";
7980

8081
if (JavaUtil.isNullOrBlank(encryption)) encryption = "";
82+
if (JavaUtil.isNullOrBlank(vlessEncryption)) vlessEncryption = "";
8183

8284
if (JavaUtil.isNullOrBlank(type)) type = "tcp";
8385
else if ("h2".equals(type)) type = "http";
@@ -122,10 +124,11 @@ public void initializeDefaultValues() {
122124

123125
@Override
124126
public void serialize(ByteBufferOutput output) {
125-
output.writeInt(4);
127+
output.writeInt(5);
126128
super.serialize(output);
127129
output.writeString(uuid);
128130
output.writeString(encryption);
131+
output.writeString(vlessEncryption);
129132
if (this instanceof VMessBean) {
130133
output.writeInt(((VMessBean) this).alterId);
131134
}
@@ -193,6 +196,9 @@ public void deserialize(ByteBufferInput input) {
193196
super.deserialize(input);
194197
uuid = input.readString();
195198
encryption = input.readString();
199+
if (version >= 5) {
200+
vlessEncryption = input.readString();
201+
}
196202
if (this instanceof VMessBean) {
197203
((VMessBean) this).alterId = input.readInt();
198204
}
@@ -284,6 +290,7 @@ public void deserialize(ByteBufferInput input) {
284290
muxConcurrency = input.readInt();
285291
}
286292

293+
// Note: xhttp fields are read in the switch case above when version >= 4
287294
}
288295

289296
@Override

app/src/main/java/io/nekohasekai/sagernet/fmt/v2ray/V2RayFmt.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,13 @@ fun StandardV2RayBean.parseDuckSoft(url: HttpUrl) {
279279
}
280280
}
281281

282+
// VLESS encryption (ML-KEM-768)
283+
url.queryParameter("encryption")?.let {
284+
if (isVLESS && it != "none") {
285+
vlessEncryption = it
286+
}
287+
}
288+
282289
url.queryParameter("fp")?.let {
283290
utlsFingerprint = it
284291
}
@@ -472,7 +479,13 @@ fun StandardV2RayBean.toUriVMessVLESSTrojan(isTrojan: Boolean): String {
472479
.addQueryParameter("type", type)
473480

474481
if (isVLESS) {
475-
builder.addQueryParameter("encryption", "none")
482+
// Add encryption if configured
483+
if (vlessEncryption.isNotBlank() && vlessEncryption != "none") {
484+
builder.addQueryParameter("encryption", vlessEncryption)
485+
} else {
486+
builder.addQueryParameter("encryption", "none")
487+
}
488+
476489
if (encryption != "auto") builder.addQueryParameter("flow", encryption)
477490
}
478491

@@ -642,11 +655,15 @@ fun buildSingBoxOutboundStreamSettings(bean: StandardV2RayBean): V2RayTransportO
642655
if (bean.xhttpExtra.isNotBlank()) {
643656
try {
644657
val gson = Gson()
658+
// Convert base config to JSON
645659
val baseJson = JSONObject(gson.toJson(baseConfig))
660+
// Parse extra config
646661
val extraJson = JSONObject(bean.xhttpExtra)
662+
// Merge extra fields into base config (only allow download field)
647663
if (extraJson.has("download")) {
648664
baseJson.put("download", extraJson.get("download"))
649665
}
666+
// Convert merged JSON back to object
650667
return gson.fromJson(baseJson.toString(), V2RayTransportOptions_XHTTPOptions::class.java)
651668
} catch (e: Exception) {
652669
// If parsing fails, return base config
@@ -726,6 +743,9 @@ fun buildSingBoxOutboundStandardV2RayBean(bean: StandardV2RayBean): Outbound {
726743
if (bean.encryption.isNotBlank() && bean.encryption != "auto") {
727744
flow = bean.encryption
728745
}
746+
if (bean.vlessEncryption.isNotBlank() && bean.vlessEncryption != "none") {
747+
encryption = bean.vlessEncryption
748+
}
729749
when (bean.packetEncoding) {
730750
0 -> packet_encoding = ""
731751
1 -> packet_encoding = "packetaddr"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ abstract class StandardV2RaySettingsActivity : ProfileSettingsActivity<StandardV
5454

5555
private val xhttpMode = pbm.add(PreferenceBinding(Type.Text, "xhttpMode"))
5656
private val xhttpExtra = pbm.add(PreferenceBinding(Type.Text, "xhttpExtra"))
57+
private val vlessEncryption = pbm.add(PreferenceBinding(Type.Text, "vlessEncryption"))
5758

5859
override fun StandardV2RayBean.init() {
5960
if (this is TrojanBean) {
@@ -110,6 +111,7 @@ abstract class StandardV2RaySettingsActivity : ProfileSettingsActivity<StandardV
110111
packetEncoding.preference.isVisible = isVmess || isVless
111112
alterId.preference.isVisible = isVmess
112113
encryption.preference.isVisible = isVmess || isVless
114+
vlessEncryption.preference.isVisible = isVless
113115
username.preference.isVisible = isHttp
114116
password.preference.isVisible = isHttp
115117

app/src/main/java/moe/matsuri/nb4a/SingBoxOptions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4243,6 +4243,8 @@ public static class Outbound_VLESSOptions extends Outbound {
42434243

42444244
public String flow;
42454245

4246+
public String encryption;
4247+
42464248
public String network;
42474249

42484250
public OutboundTLSOptions tls;

app/src/main/res/values-zh-rCN/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,6 @@
553553
<string name="xhttp_mode">XHTTP 模式</string>
554554
<string name="xhttp_extra">XHTTP Extra</string>
555555
<string name="xhttp_settings">XHTTP 设置</string>
556+
<string name="vless_encryption">加密方式</string>
557+
<string name="vless_encryption_hint">mlkem768x25519plus.native.0rtt.KEY1.KEY2...</string>
556558
</resources>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,6 @@
637637
<string name="xhttp_mode">XHTTP Mode</string>
638638
<string name="xhttp_extra">XHTTP Extra</string>
639639
<string name="xhttp_settings">XHTTP Settings</string>
640+
<string name="vless_encryption">Encryption</string>
641+
<string name="vless_encryption_hint">mlkem768x25519plus.native.0rtt.KEY1.KEY2...</string>
640642
</resources>

app/src/main/res/xml/standard_v2ray_preferences.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
app:key="encryption"
4646
app:title="@string/encryption"
4747
app:useSimpleSummaryProvider="true" />
48+
<EditTextPreference
49+
app:icon="@drawable/ic_notification_enhanced_encryption"
50+
app:key="vlessEncryption"
51+
app:title="@string/vless_encryption"
52+
app:dialogMessage="@string/vless_encryption_hint"
53+
app:useSimpleSummaryProvider="true" />
4854
<moe.matsuri.nb4a.ui.SimpleMenuPreference
4955
app:entries="@array/packet_encoding_entry"
5056
app:entryValues="@array/int_array_3"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export COMMIT_SING_BOX="e5069355c397b9c78903a82987d8e8fd26fa3e8a"
1+
export COMMIT_SING_BOX="87e3315a31656bba7547df99ac14273cf31c4a47"
22
export COMMIT_LIBNEKO="1c47a3af71990a7b2192e03292b4d246c308ef0b"

0 commit comments

Comments
 (0)