From 002283899f0bf975f20cb0734cae55278efb398c Mon Sep 17 00:00:00 2001
From: 2dust <31833384+2dust@users.noreply.github.com>
Date: Fri, 28 Jan 2022 21:02:25 +0800
Subject: [PATCH] add VmessItem indexId
---
v2rayN/v2rayN/Handler/ConfigHandler.cs | 91 ++++++++++++--------------
v2rayN/v2rayN/Mode/Config.cs | 14 ++++
v2rayN/v2rayN/Tool/Utils.cs | 11 +++-
3 files changed, 64 insertions(+), 52 deletions(-)
diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs
index 728ef5bb..06daf839 100644
--- a/v2rayN/v2rayN/Handler/ConfigHandler.cs
+++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs
@@ -172,6 +172,11 @@ namespace v2rayN.Handler
{
VmessItem vmessItem = config.vmess[i];
UpgradeServerVersion(ref vmessItem);
+
+ if (string.IsNullOrEmpty(vmessItem.indexId))
+ {
+ vmessItem.indexId = Utils.GetGUID(false);
+ }
}
}
@@ -191,7 +196,6 @@ namespace v2rayN.Handler
///
public static int AddServer(ref Config config, VmessItem vmessItem, int index, bool toFile = true)
{
- vmessItem.configVersion = 2;
vmessItem.configType = (int)EConfigType.Vmess;
vmessItem.address = vmessItem.address.TrimEx();
@@ -203,6 +207,11 @@ namespace v2rayN.Handler
vmessItem.path = vmessItem.path.TrimEx();
vmessItem.streamSecurity = vmessItem.streamSecurity.TrimEx();
+ if (!Global.vmessSecuritys.Contains(vmessItem.security))
+ {
+ return -1;
+ }
+
if (index >= 0)
{
//修改
@@ -214,17 +223,7 @@ namespace v2rayN.Handler
}
else
{
- //添加
- if (Utils.IsNullOrEmpty(vmessItem.allowInsecure))
- {
- vmessItem.allowInsecure = config.defAllowInsecure.ToString();
- }
- config.vmess.Add(vmessItem);
- if (config.vmess.Count == 1)
- {
- config.index = 0;
- Global.reloadV2ray = true;
- }
+ AddServerCommon(ref config, vmessItem);
}
if (toFile)
@@ -545,7 +544,6 @@ namespace v2rayN.Handler
///
public static int AddShadowsocksServer(ref Config config, VmessItem vmessItem, int index, bool toFile = true)
{
- vmessItem.configVersion = 2;
vmessItem.configType = (int)EConfigType.Shadowsocks;
vmessItem.address = vmessItem.address.TrimEx();
@@ -568,13 +566,7 @@ namespace v2rayN.Handler
}
else
{
- //添加
- config.vmess.Add(vmessItem);
- if (config.vmess.Count == 1)
- {
- config.index = 0;
- Global.reloadV2ray = true;
- }
+ AddServerCommon(ref config, vmessItem);
}
if (toFile)
@@ -594,7 +586,6 @@ namespace v2rayN.Handler
///
public static int AddSocksServer(ref Config config, VmessItem vmessItem, int index, bool toFile = true)
{
- vmessItem.configVersion = 2;
vmessItem.configType = (int)EConfigType.Socks;
vmessItem.address = vmessItem.address.TrimEx();
@@ -610,13 +601,7 @@ namespace v2rayN.Handler
}
else
{
- //添加
- config.vmess.Add(vmessItem);
- if (config.vmess.Count == 1)
- {
- config.index = 0;
- Global.reloadV2ray = true;
- }
+ AddServerCommon(ref config, vmessItem);
}
if (toFile)
@@ -637,7 +622,6 @@ namespace v2rayN.Handler
///
public static int AddTrojanServer(ref Config config, VmessItem vmessItem, int index, bool toFile = true)
{
- vmessItem.configVersion = 2;
vmessItem.configType = (int)EConfigType.Trojan;
vmessItem.address = vmessItem.address.TrimEx();
@@ -662,13 +646,7 @@ namespace v2rayN.Handler
}
else
{
- //添加
- config.vmess.Add(vmessItem);
- if (config.vmess.Count == 1)
- {
- config.index = 0;
- Global.reloadV2ray = true;
- }
+ AddServerCommon(ref config, vmessItem);
}
if (toFile)
@@ -866,7 +844,7 @@ namespace v2rayN.Handler
{
if (Utils.IsNullOrEmpty(sub.id))
{
- sub.id = Utils.GetGUID();
+ sub.id = Utils.GetGUID(false);
}
}
@@ -954,7 +932,6 @@ namespace v2rayN.Handler
///
public static int AddVlessServer(ref Config config, VmessItem vmessItem, int index, bool toFile = true)
{
- vmessItem.configVersion = 2;
vmessItem.configType = (int)EConfigType.VLESS;
vmessItem.address = vmessItem.address.TrimEx();
@@ -976,18 +953,8 @@ namespace v2rayN.Handler
}
}
else
- {
- //添加
- if (Utils.IsNullOrEmpty(vmessItem.allowInsecure))
- {
- vmessItem.allowInsecure = config.defAllowInsecure.ToString();
- }
- config.vmess.Add(vmessItem);
- if (config.vmess.Count == 1)
- {
- config.index = 0;
- Global.reloadV2ray = true;
- }
+ {
+ AddServerCommon(ref config, vmessItem);
}
if (toFile)
@@ -1054,6 +1021,23 @@ namespace v2rayN.Handler
return 0;
}
+ public static int AddServerCommon(ref Config config, VmessItem vmessItem)
+ {
+ vmessItem.indexId = Utils.GetGUID(false);
+ vmessItem.configVersion = 2;
+ if (Utils.IsNullOrEmpty(vmessItem.allowInsecure))
+ {
+ vmessItem.allowInsecure = config.defAllowInsecure.ToString();
+ }
+
+ config.vmess.Add(vmessItem);
+ if (config.vmess.Count == 1)
+ {
+ config.index = 0;
+ Global.reloadV2ray = true;
+ }
+ return 0;
+ }
#endregion
#region UI
@@ -1133,6 +1117,13 @@ namespace v2rayN.Handler
else
{
config.routings.Add(item);
+ int indexLocked = config.routings.FindIndex(it => it.locked == true);
+ if (indexLocked != -1)
+ {
+ var itemLocked = Utils.DeepCopy(config.routings[indexLocked]);
+ config.routings.RemoveAt(indexLocked);
+ config.routings.Add(itemLocked);
+ }
}
ToJsonFile(config);
diff --git a/v2rayN/v2rayN/Mode/Config.cs b/v2rayN/v2rayN/Mode/Config.cs
index 7945c0b3..7d3eb8bf 100644
--- a/v2rayN/v2rayN/Mode/Config.cs
+++ b/v2rayN/v2rayN/Mode/Config.cs
@@ -366,6 +366,15 @@ namespace v2rayN.Mode
return null;
}
}
+
+ public int FindIndexId(string indexId)
+ {
+ if (string.IsNullOrEmpty(indexId))
+ {
+ return -1;
+ }
+ return vmess.FindIndex(it => it.indexId == indexId);
+ }
#endregion
}
@@ -375,6 +384,7 @@ namespace v2rayN.Mode
{
public VmessItem()
{
+ indexId = string.Empty;
configVersion = 1;
address = string.Empty;
port = 0;
@@ -460,6 +470,10 @@ namespace v2rayN.Mode
itemId = Utils.Base64Encode(itemId);
return itemId;
}
+ public string indexId
+ {
+ get; set;
+ }
///
/// 版本(现在=2)
diff --git a/v2rayN/v2rayN/Tool/Utils.cs b/v2rayN/v2rayN/Tool/Utils.cs
index 6fa98069..54c9638d 100644
--- a/v2rayN/v2rayN/Tool/Utils.cs
+++ b/v2rayN/v2rayN/Tool/Utils.cs
@@ -844,11 +844,18 @@ namespace v2rayN
/// 取得GUID
///
///
- public static string GetGUID()
+ public static string GetGUID(bool full = true)
{
try
{
- return Guid.NewGuid().ToString("D");
+ if (full)
+ {
+ return Guid.NewGuid().ToString("D");
+ }
+ else
+ {
+ return BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0).ToString();
+ }
}
catch (Exception ex)
{