mirror of https://github.com/2dust/v2rayN
Improve and refactor the code
parent
522571f0b3
commit
74a0a93201
|
@ -1184,49 +1184,33 @@ namespace v2rayN.Handler
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
var subRemarks = LazyConfig.Instance.GetSubItem(subid)?.remarks;
|
||||||
|
|
||||||
//判断str是否包含s的任意一个字符串
|
List<ProfileItem>? lstProfiles = null;
|
||||||
static bool Contains(string str, params string[] s)
|
//Is sing-box array configuration
|
||||||
|
if (lstProfiles is null || lstProfiles.Count <= 0)
|
||||||
{
|
{
|
||||||
foreach (var item in s)
|
lstProfiles = SingboxFmt.ResolveFullArray(strData, subRemarks);
|
||||||
{
|
|
||||||
if (str.Contains(item, StringComparison.OrdinalIgnoreCase)) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Is v2ray array configuration
|
//Is v2ray array configuration
|
||||||
var configObjects = JsonUtils.Deserialize<Object[]>(strData);
|
if (lstProfiles is null || lstProfiles.Count <= 0)
|
||||||
if (configObjects != null && configObjects.Length > 0)
|
{
|
||||||
|
lstProfiles = V2rayFmt.ResolveFullArray(strData, subRemarks);
|
||||||
|
}
|
||||||
|
if (lstProfiles != null && lstProfiles.Count > 0)
|
||||||
{
|
{
|
||||||
if (isSub && !Utils.IsNullOrEmpty(subid))
|
if (isSub && !Utils.IsNullOrEmpty(subid))
|
||||||
{
|
{
|
||||||
RemoveServerViaSubid(config, subid, isSub);
|
RemoveServerViaSubid(config, subid, isSub);
|
||||||
}
|
}
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
foreach (var configObject in configObjects)
|
foreach (var it in lstProfiles)
|
||||||
{
|
{
|
||||||
var objectString = JsonUtils.Serialize(configObject);
|
it.subid = subid;
|
||||||
var v2rayCon = JsonUtils.Deserialize<V2rayConfig>(objectString);
|
it.isSub = isSub;
|
||||||
if (v2rayCon?.inbounds?.Count > 0 && v2rayCon.outbounds?.Count > 0)
|
if (AddCustomServer(config, it, true) == 0)
|
||||||
{
|
{
|
||||||
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
|
count++;
|
||||||
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++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (count > 0)
|
if (count > 0)
|
||||||
|
@ -1235,58 +1219,39 @@ namespace v2rayN.Handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ProfileItem profileItem = new();
|
ProfileItem? profileItem = null;
|
||||||
//Is v2ray configuration
|
//Is sing-box configuration
|
||||||
var v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(strData);
|
if (profileItem is null)
|
||||||
if (v2rayConfig?.inbounds?.Count > 0
|
|
||||||
&& v2rayConfig.outbounds?.Count > 0)
|
|
||||||
{
|
{
|
||||||
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
|
profileItem = SingboxFmt.ResolveFull(strData, subRemarks);
|
||||||
File.WriteAllText(fileName, strData);
|
}
|
||||||
|
//Is v2ray configuration
|
||||||
profileItem.coreType = ECoreType.Xray;
|
if (profileItem is null)
|
||||||
profileItem.address = fileName;
|
{
|
||||||
profileItem.remarks = v2rayConfig.remarks ?? "v2ray_custom";
|
profileItem = V2rayFmt.ResolveFull(strData, subRemarks);
|
||||||
}
|
}
|
||||||
//Is Clash configuration
|
//Is Clash configuration
|
||||||
else if (Contains(strData, "port", "socks-port", "proxies"))
|
if (profileItem is null)
|
||||||
{
|
{
|
||||||
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.yaml");
|
profileItem = ClashFmt.ResolveFull(strData, subRemarks);
|
||||||
File.WriteAllText(fileName, strData);
|
|
||||||
|
|
||||||
profileItem.coreType = ECoreType.mihomo;
|
|
||||||
profileItem.address = fileName;
|
|
||||||
profileItem.remarks = "clash_custom";
|
|
||||||
}
|
}
|
||||||
//Is hysteria configuration
|
//Is hysteria configuration
|
||||||
else if (Contains(strData, "server", "up", "down", "listen", "<html>", "<body>"))
|
if (profileItem is null)
|
||||||
{
|
{
|
||||||
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
|
profileItem = Hysteria2Fmt.ResolveFull2(strData, subRemarks);
|
||||||
File.WriteAllText(fileName, strData);
|
}
|
||||||
|
if (profileItem is null)
|
||||||
profileItem.coreType = ECoreType.hysteria;
|
{
|
||||||
profileItem.address = fileName;
|
profileItem = Hysteria2Fmt.ResolveFull(strData, subRemarks);
|
||||||
profileItem.remarks = "hysteria_custom";
|
|
||||||
}
|
}
|
||||||
//Is naiveproxy configuration
|
//Is naiveproxy configuration
|
||||||
else if (Contains(strData, "listen", "proxy", "<html>", "<body>"))
|
if (profileItem is null)
|
||||||
{
|
{
|
||||||
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
|
profileItem = NaiveproxyFmt.ResolveFull(strData, subRemarks);
|
||||||
File.WriteAllText(fileName, strData);
|
|
||||||
|
|
||||||
profileItem.coreType = ECoreType.naiveproxy;
|
|
||||||
profileItem.address = fileName;
|
|
||||||
profileItem.remarks = "naiveproxy_custom";
|
|
||||||
}
|
}
|
||||||
//Is Other configuration
|
if (profileItem is null || Utils.IsNullOrEmpty(profileItem.address))
|
||||||
else
|
|
||||||
{
|
{
|
||||||
return -1;
|
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))
|
if (isSub && !Utils.IsNullOrEmpty(subid))
|
||||||
|
@ -1299,12 +1264,6 @@ namespace v2rayN.Handler
|
||||||
}
|
}
|
||||||
profileItem.subid = subid;
|
profileItem.subid = subid;
|
||||||
profileItem.isSub = isSub;
|
profileItem.isSub = isSub;
|
||||||
|
|
||||||
if (Utils.IsNullOrEmpty(profileItem.address))
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (AddCustomServer(config, profileItem, true) == 0)
|
if (AddCustomServer(config, profileItem, true) == 0)
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1327,31 +1286,12 @@ namespace v2rayN.Handler
|
||||||
RemoveServerViaSubid(config, subid, isSub);
|
RemoveServerViaSubid(config, subid, isSub);
|
||||||
}
|
}
|
||||||
|
|
||||||
//SsSIP008
|
var lstSsServer = ShadowsocksFmt.ResolveSip008(strData);
|
||||||
var lstSsServer = JsonUtils.Deserialize<List<SsServer>>(strData);
|
|
||||||
if (lstSsServer?.Count <= 0)
|
|
||||||
{
|
|
||||||
var ssSIP008 = JsonUtils.Deserialize<SsSIP008>(strData);
|
|
||||||
if (ssSIP008?.servers?.Count > 0)
|
|
||||||
{
|
|
||||||
lstSsServer = ssSIP008.servers;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lstSsServer?.Count > 0)
|
if (lstSsServer?.Count > 0)
|
||||||
{
|
{
|
||||||
int counter = 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.subid = subid;
|
||||||
ssItem.isSub = isSub;
|
ssItem.isSub = isSub;
|
||||||
if (AddShadowsocksServer(config, ssItem) == 0)
|
if (AddShadowsocksServer(config, ssItem) == 0)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
|
using System.IO;
|
||||||
using v2rayN.Enums;
|
using v2rayN.Enums;
|
||||||
using v2rayN.Models;
|
using v2rayN.Models;
|
||||||
|
|
||||||
|
@ -182,5 +183,21 @@ namespace v2rayN.Handler.Fmt
|
||||||
}
|
}
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -32,7 +32,6 @@ namespace v2rayN.Handler.Fmt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static ProfileItem? ResolveConfig(string config, out string msg)
|
public static ProfileItem? ResolveConfig(string config, out string msg)
|
||||||
{
|
{
|
||||||
msg = ResUI.ConfigurationFormatIncorrect;
|
msg = ResUI.ConfigurationFormatIncorrect;
|
||||||
|
|
|
@ -64,5 +64,41 @@ namespace v2rayN.Handler.Fmt
|
||||||
url = $"{Global.ProtocolShares[EConfigType.Hysteria2]}{url}/{query}{remark}";
|
url = $"{Global.ProtocolShares[EConfigType.Hysteria2]}{url}/{query}{remark}";
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ProfileItem? ResolveFull(string strData, string? subRemarks)
|
||||||
|
{
|
||||||
|
if (Contains(strData, "server", "up", "down", "listen", "<html>", "<body>"))
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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", "<html>", "<body>"))
|
||||||
|
{
|
||||||
|
var fileName = WriteAllText(strData);
|
||||||
|
|
||||||
|
var profileItem = new ProfileItem
|
||||||
|
{
|
||||||
|
coreType = ECoreType.naiveproxy,
|
||||||
|
address = fileName,
|
||||||
|
remarks = subRemarks ?? "naiveproxy_custom"
|
||||||
|
};
|
||||||
|
return profileItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -146,5 +146,38 @@ namespace v2rayN.Handler.Fmt
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<ProfileItem>? ResolveSip008(string result)
|
||||||
|
{
|
||||||
|
//SsSIP008
|
||||||
|
var lstSsServer = JsonUtils.Deserialize<List<SsServer>>(result);
|
||||||
|
if (lstSsServer?.Count <= 0)
|
||||||
|
{
|
||||||
|
var ssSIP008 = JsonUtils.Deserialize<SsSIP008>(result);
|
||||||
|
if (ssSIP008?.servers?.Count > 0)
|
||||||
|
{
|
||||||
|
lstSsServer = ssSIP008.servers;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lstSsServer?.Count > 0)
|
||||||
|
{
|
||||||
|
List<ProfileItem> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
using v2rayN.Enums;
|
||||||
|
using v2rayN.Models;
|
||||||
|
|
||||||
|
namespace v2rayN.Handler.Fmt
|
||||||
|
{
|
||||||
|
internal class SingboxFmt : BaseFmt
|
||||||
|
{
|
||||||
|
public static List<ProfileItem>? ResolveFullArray(string strData, string? subRemarks)
|
||||||
|
{
|
||||||
|
var configObjects = JsonUtils.Deserialize<Object[]>(strData);
|
||||||
|
if (configObjects != null && configObjects.Length > 0)
|
||||||
|
{
|
||||||
|
List<ProfileItem> lstResult = [];
|
||||||
|
foreach (var configObject in configObjects)
|
||||||
|
{
|
||||||
|
var objectString = JsonUtils.Serialize(configObject);
|
||||||
|
var singboxCon = JsonUtils.Deserialize<SingboxConfig>(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<SingboxConfig>(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
using v2rayN.Enums;
|
||||||
|
using v2rayN.Models;
|
||||||
|
|
||||||
|
namespace v2rayN.Handler.Fmt
|
||||||
|
{
|
||||||
|
internal class V2rayFmt : BaseFmt
|
||||||
|
{
|
||||||
|
public static List<ProfileItem>? ResolveFullArray(string strData, string? subRemarks)
|
||||||
|
{
|
||||||
|
var configObjects = JsonUtils.Deserialize<Object[]>(strData);
|
||||||
|
if (configObjects != null && configObjects.Length > 0)
|
||||||
|
{
|
||||||
|
List<ProfileItem> lstResult = [];
|
||||||
|
foreach (var configObject in configObjects)
|
||||||
|
{
|
||||||
|
var objectString = JsonUtils.Serialize(configObject);
|
||||||
|
var v2rayCon = JsonUtils.Deserialize<V2rayConfig>(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<V2rayConfig>(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue