diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index a8a248bb..c2a28571 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -1184,49 +1184,33 @@ namespace v2rayN.Handler { return -1; } + var subRemarks = LazyConfig.Instance.GetSubItem(subid)?.remarks; - //判断str是否包含s的任意一个字符串 - static bool Contains(string str, params string[] s) + List? lstProfiles = null; + //Is sing-box array configuration + if (lstProfiles is null || lstProfiles.Count <= 0) { - foreach (var item in s) - { - if (str.Contains(item, StringComparison.OrdinalIgnoreCase)) return true; - } - return false; + lstProfiles = SingboxFmt.ResolveFullArray(strData, subRemarks); } - //Is v2ray array configuration - var configObjects = JsonUtils.Deserialize(strData); - if (configObjects != null && configObjects.Length > 0) + if (lstProfiles is null || lstProfiles.Count <= 0) + { + lstProfiles = V2rayFmt.ResolveFullArray(strData, subRemarks); + } + if (lstProfiles != null && lstProfiles.Count > 0) { if (isSub && !Utils.IsNullOrEmpty(subid)) { RemoveServerViaSubid(config, subid, isSub); } - int count = 0; - foreach (var configObject in configObjects) + foreach (var it in lstProfiles) { - var objectString = JsonUtils.Serialize(configObject); - var v2rayCon = JsonUtils.Deserialize(objectString); - if (v2rayCon?.inbounds?.Count > 0 && v2rayCon.outbounds?.Count > 0) + it.subid = subid; + it.isSub = isSub; + if (AddCustomServer(config, it, true) == 0) { - var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json"); - File.WriteAllText(fileName, objectString); - - var profileIt = new ProfileItem - { - coreType = ECoreType.Xray, - address = fileName, - remarks = v2rayCon.remarks ?? "v2ray_custom", - subid = subid, - isSub = isSub - }; - - if (AddCustomServer(config, profileIt, true) == 0) - { - count++; - } + count++; } } if (count > 0) @@ -1235,58 +1219,39 @@ namespace v2rayN.Handler } } - ProfileItem profileItem = new(); + ProfileItem? profileItem = null; + //Is sing-box configuration + if (profileItem is null) + { + profileItem = SingboxFmt.ResolveFull(strData, subRemarks); + } //Is v2ray configuration - var v2rayConfig = JsonUtils.Deserialize(strData); - if (v2rayConfig?.inbounds?.Count > 0 - && v2rayConfig.outbounds?.Count > 0) + if (profileItem is null) { - var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json"); - File.WriteAllText(fileName, strData); - - profileItem.coreType = ECoreType.Xray; - profileItem.address = fileName; - profileItem.remarks = v2rayConfig.remarks ?? "v2ray_custom"; + profileItem = V2rayFmt.ResolveFull(strData, subRemarks); } //Is Clash configuration - else if (Contains(strData, "port", "socks-port", "proxies")) + if (profileItem is null) { - var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.yaml"); - File.WriteAllText(fileName, strData); - - profileItem.coreType = ECoreType.mihomo; - profileItem.address = fileName; - profileItem.remarks = "clash_custom"; + profileItem = ClashFmt.ResolveFull(strData, subRemarks); } //Is hysteria configuration - else if (Contains(strData, "server", "up", "down", "listen", "", "")) + if (profileItem is null) { - var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json"); - File.WriteAllText(fileName, strData); - - profileItem.coreType = ECoreType.hysteria; - profileItem.address = fileName; - profileItem.remarks = "hysteria_custom"; + profileItem = Hysteria2Fmt.ResolveFull2(strData, subRemarks); + } + if (profileItem is null) + { + profileItem = Hysteria2Fmt.ResolveFull(strData, subRemarks); } //Is naiveproxy configuration - else if (Contains(strData, "listen", "proxy", "", "")) + if (profileItem is null) { - var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json"); - File.WriteAllText(fileName, strData); - - profileItem.coreType = ECoreType.naiveproxy; - profileItem.address = fileName; - profileItem.remarks = "naiveproxy_custom"; + profileItem = NaiveproxyFmt.ResolveFull(strData, subRemarks); } - //Is Other configuration - else + if (profileItem is null || Utils.IsNullOrEmpty(profileItem.address)) { return -1; - //var fileName = Utile.GetTempPath($"{Utile.GetGUID(false)}.txt"); - //File.WriteAllText(fileName, strData); - - //profileItem.address = fileName; - //profileItem.remarks = "other_custom"; } if (isSub && !Utils.IsNullOrEmpty(subid)) @@ -1299,12 +1264,6 @@ namespace v2rayN.Handler } profileItem.subid = subid; profileItem.isSub = isSub; - - if (Utils.IsNullOrEmpty(profileItem.address)) - { - return -1; - } - if (AddCustomServer(config, profileItem, true) == 0) { return 1; @@ -1327,31 +1286,12 @@ namespace v2rayN.Handler RemoveServerViaSubid(config, subid, isSub); } - //SsSIP008 - var lstSsServer = JsonUtils.Deserialize>(strData); - if (lstSsServer?.Count <= 0) - { - var ssSIP008 = JsonUtils.Deserialize(strData); - if (ssSIP008?.servers?.Count > 0) - { - lstSsServer = ssSIP008.servers; - } - } - + var lstSsServer = ShadowsocksFmt.ResolveSip008(strData); if (lstSsServer?.Count > 0) { int counter = 0; - foreach (var it in lstSsServer) + foreach (var ssItem in lstSsServer) { - var ssItem = new ProfileItem() - { - subid = subid, - remarks = it.remarks, - security = it.method, - id = it.password, - address = it.server, - port = Utils.ToInt(it.server_port) - }; ssItem.subid = subid; ssItem.isSub = isSub; if (AddShadowsocksServer(config, ssItem) == 0) diff --git a/v2rayN/v2rayN/Handler/Fmt/BaseFmt.cs b/v2rayN/v2rayN/Handler/Fmt/BaseFmt.cs index 42b76252..cc867c36 100644 --- a/v2rayN/v2rayN/Handler/Fmt/BaseFmt.cs +++ b/v2rayN/v2rayN/Handler/Fmt/BaseFmt.cs @@ -1,4 +1,5 @@ using System.Collections.Specialized; +using System.IO; using v2rayN.Enums; using v2rayN.Models; @@ -182,5 +183,21 @@ namespace v2rayN.Handler.Fmt } return 0; } + + protected static bool Contains(string str, params string[] s) + { + foreach (var item in s) + { + if (str.Contains(item, StringComparison.OrdinalIgnoreCase)) return true; + } + return false; + } + + protected static string WriteAllText(string strData, string ext = "json") + { + var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.{ext}"); + File.WriteAllText(fileName, strData); + return fileName; + } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Handler/Fmt/ClashFmt.cs b/v2rayN/v2rayN/Handler/Fmt/ClashFmt.cs new file mode 100644 index 00000000..90d96749 --- /dev/null +++ b/v2rayN/v2rayN/Handler/Fmt/ClashFmt.cs @@ -0,0 +1,26 @@ +using v2rayN.Enums; +using v2rayN.Models; + +namespace v2rayN.Handler.Fmt +{ + internal class ClashFmt : BaseFmt + { + public static ProfileItem? ResolveFull(string strData, string? subRemarks) + { + if (Contains(strData, "port", "socks-port", "proxies")) + { + var fileName = WriteAllText(strData, "yaml"); + + var profileItem = new ProfileItem + { + coreType = ECoreType.mihomo, + address = fileName, + remarks = subRemarks ?? "clash_custom" + }; + return profileItem; + } + + return null; + } + } +} \ No newline at end of file diff --git a/v2rayN/v2rayN/Handler/Fmt/FmtHandler.cs b/v2rayN/v2rayN/Handler/Fmt/FmtHandler.cs index 34ec25a3..af0cffe7 100644 --- a/v2rayN/v2rayN/Handler/Fmt/FmtHandler.cs +++ b/v2rayN/v2rayN/Handler/Fmt/FmtHandler.cs @@ -32,7 +32,6 @@ namespace v2rayN.Handler.Fmt } } - public static ProfileItem? ResolveConfig(string config, out string msg) { msg = ResUI.ConfigurationFormatIncorrect; diff --git a/v2rayN/v2rayN/Handler/Fmt/Hysteria2Fmt.cs b/v2rayN/v2rayN/Handler/Fmt/Hysteria2Fmt.cs index 6565e935..f205861f 100644 --- a/v2rayN/v2rayN/Handler/Fmt/Hysteria2Fmt.cs +++ b/v2rayN/v2rayN/Handler/Fmt/Hysteria2Fmt.cs @@ -64,5 +64,41 @@ namespace v2rayN.Handler.Fmt url = $"{Global.ProtocolShares[EConfigType.Hysteria2]}{url}/{query}{remark}"; return url; } + + public static ProfileItem? ResolveFull(string strData, string? subRemarks) + { + if (Contains(strData, "server", "up", "down", "listen", "", "")) + { + var fileName = WriteAllText(strData); + + var profileItem = new ProfileItem + { + coreType = ECoreType.hysteria, + address = fileName, + remarks = subRemarks ?? "hysteria_custom" + }; + return profileItem; + } + + return null; + } + + public static ProfileItem? ResolveFull2(string strData, string? subRemarks) + { + if (Contains(strData, "server", "auth", "up", "down", "listen")) + { + var fileName = WriteAllText(strData); + + var profileItem = new ProfileItem + { + coreType = ECoreType.hysteria2, + address = fileName, + remarks = subRemarks ?? "hysteria2_custom" + }; + return profileItem; + } + + return null; + } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Handler/Fmt/NaiveproxyFmt.cs b/v2rayN/v2rayN/Handler/Fmt/NaiveproxyFmt.cs new file mode 100644 index 00000000..3e523170 --- /dev/null +++ b/v2rayN/v2rayN/Handler/Fmt/NaiveproxyFmt.cs @@ -0,0 +1,26 @@ +using v2rayN.Enums; +using v2rayN.Models; + +namespace v2rayN.Handler.Fmt +{ + internal class NaiveproxyFmt : BaseFmt + { + public static ProfileItem? ResolveFull(string strData, string? subRemarks) + { + if (Contains(strData, "listen", "proxy", "", "")) + { + var fileName = WriteAllText(strData); + + var profileItem = new ProfileItem + { + coreType = ECoreType.naiveproxy, + address = fileName, + remarks = subRemarks ?? "naiveproxy_custom" + }; + return profileItem; + } + + return null; + } + } +} \ No newline at end of file diff --git a/v2rayN/v2rayN/Handler/Fmt/ShadowsocksFmt.cs b/v2rayN/v2rayN/Handler/Fmt/ShadowsocksFmt.cs index 92f51400..495f0d50 100644 --- a/v2rayN/v2rayN/Handler/Fmt/ShadowsocksFmt.cs +++ b/v2rayN/v2rayN/Handler/Fmt/ShadowsocksFmt.cs @@ -146,5 +146,38 @@ namespace v2rayN.Handler.Fmt return item; } + + public static List? ResolveSip008(string result) + { + //SsSIP008 + var lstSsServer = JsonUtils.Deserialize>(result); + if (lstSsServer?.Count <= 0) + { + var ssSIP008 = JsonUtils.Deserialize(result); + if (ssSIP008?.servers?.Count > 0) + { + lstSsServer = ssSIP008.servers; + } + } + + if (lstSsServer?.Count > 0) + { + List lst = []; + foreach (var it in lstSsServer) + { + var ssItem = new ProfileItem() + { + remarks = it.remarks, + security = it.method, + id = it.password, + address = it.server, + port = Utils.ToInt(it.server_port) + }; + lst.Add(ssItem); + } + return lst; + } + return null; + } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Handler/Fmt/SingboxFmt.cs b/v2rayN/v2rayN/Handler/Fmt/SingboxFmt.cs new file mode 100644 index 00000000..c8221afe --- /dev/null +++ b/v2rayN/v2rayN/Handler/Fmt/SingboxFmt.cs @@ -0,0 +1,58 @@ +using v2rayN.Enums; +using v2rayN.Models; + +namespace v2rayN.Handler.Fmt +{ + internal class SingboxFmt : BaseFmt + { + public static List? ResolveFullArray(string strData, string? subRemarks) + { + var configObjects = JsonUtils.Deserialize(strData); + if (configObjects != null && configObjects.Length > 0) + { + List lstResult = []; + foreach (var configObject in configObjects) + { + var objectString = JsonUtils.Serialize(configObject); + var singboxCon = JsonUtils.Deserialize(objectString); + if (singboxCon?.inbounds?.Count > 0 + && singboxCon.outbounds?.Count > 0 + && singboxCon.route != null) + { + var fileName = WriteAllText(objectString); + + var profileIt = new ProfileItem + { + coreType = ECoreType.sing_box, + address = fileName, + remarks = subRemarks ?? "singbox_custom", + }; + lstResult.Add(profileIt); + } + } + return lstResult; + } + return null; + } + + public static ProfileItem? ResolveFull(string strData, string? subRemarks) + { + var singboxConfig = JsonUtils.Deserialize(strData); + if (singboxConfig?.inbounds?.Count > 0 + && singboxConfig.outbounds?.Count > 0 + && singboxConfig.route != null) + { + var fileName = WriteAllText(strData); + var profileItem = new ProfileItem + { + coreType = ECoreType.sing_box, + address = fileName, + remarks = subRemarks ?? "singbox_custom" + }; + + return profileItem; + } + return null; + } + } +} \ No newline at end of file diff --git a/v2rayN/v2rayN/Handler/Fmt/V2rayFmt.cs b/v2rayN/v2rayN/Handler/Fmt/V2rayFmt.cs new file mode 100644 index 00000000..ed34c9a2 --- /dev/null +++ b/v2rayN/v2rayN/Handler/Fmt/V2rayFmt.cs @@ -0,0 +1,59 @@ +using v2rayN.Enums; +using v2rayN.Models; + +namespace v2rayN.Handler.Fmt +{ + internal class V2rayFmt : BaseFmt + { + public static List? ResolveFullArray(string strData, string? subRemarks) + { + var configObjects = JsonUtils.Deserialize(strData); + if (configObjects != null && configObjects.Length > 0) + { + List lstResult = []; + foreach (var configObject in configObjects) + { + var objectString = JsonUtils.Serialize(configObject); + var v2rayCon = JsonUtils.Deserialize(objectString); + if (v2rayCon?.inbounds?.Count > 0 + && v2rayCon.outbounds?.Count > 0 + && v2rayCon.routing != null) + { + var fileName = WriteAllText(objectString); + + var profileIt = new ProfileItem + { + coreType = ECoreType.Xray, + address = fileName, + remarks = v2rayCon.remarks ?? subRemarks ?? "v2ray_custom", + }; + lstResult.Add(profileIt); + } + } + return lstResult; + } + return null; + } + + public static ProfileItem? ResolveFull(string strData, string? subRemarks) + { + var v2rayConfig = JsonUtils.Deserialize(strData); + if (v2rayConfig?.inbounds?.Count > 0 + && v2rayConfig.outbounds?.Count > 0 + && v2rayConfig.routing != null) + { + var fileName = WriteAllText(strData); + + var profileItem = new ProfileItem + { + coreType = ECoreType.Xray, + address = fileName, + remarks = v2rayConfig.remarks ?? subRemarks ?? "v2ray_custom" + }; + + return profileItem; + } + return null; + } + } +} \ No newline at end of file