Skip to content

Commit 74a0a93

Browse files
committed
Improve and refactor the code
1 parent 522571f commit 74a0a93

File tree

9 files changed

+291
-97
lines changed

9 files changed

+291
-97
lines changed

v2rayN/v2rayN/Handler/ConfigHandler.cs

Lines changed: 36 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,49 +1184,33 @@ private static int AddBatchServers4Custom(Config config, string strData, string
11841184
{
11851185
return -1;
11861186
}
1187+
var subRemarks = LazyConfig.Instance.GetSubItem(subid)?.remarks;
11871188

1188-
//判断str是否包含s的任意一个字符串
1189-
static bool Contains(string str, params string[] s)
1189+
List<ProfileItem>? lstProfiles = null;
1190+
//Is sing-box array configuration
1191+
if (lstProfiles is null || lstProfiles.Count <= 0)
11901192
{
1191-
foreach (var item in s)
1192-
{
1193-
if (str.Contains(item, StringComparison.OrdinalIgnoreCase)) return true;
1194-
}
1195-
return false;
1193+
lstProfiles = SingboxFmt.ResolveFullArray(strData, subRemarks);
11961194
}
1197-
11981195
//Is v2ray array configuration
1199-
var configObjects = JsonUtils.Deserialize<Object[]>(strData);
1200-
if (configObjects != null && configObjects.Length > 0)
1196+
if (lstProfiles is null || lstProfiles.Count <= 0)
1197+
{
1198+
lstProfiles = V2rayFmt.ResolveFullArray(strData, subRemarks);
1199+
}
1200+
if (lstProfiles != null && lstProfiles.Count > 0)
12011201
{
12021202
if (isSub && !Utils.IsNullOrEmpty(subid))
12031203
{
12041204
RemoveServerViaSubid(config, subid, isSub);
12051205
}
1206-
12071206
int count = 0;
1208-
foreach (var configObject in configObjects)
1207+
foreach (var it in lstProfiles)
12091208
{
1210-
var objectString = JsonUtils.Serialize(configObject);
1211-
var v2rayCon = JsonUtils.Deserialize<V2rayConfig>(objectString);
1212-
if (v2rayCon?.inbounds?.Count > 0 && v2rayCon.outbounds?.Count > 0)
1209+
it.subid = subid;
1210+
it.isSub = isSub;
1211+
if (AddCustomServer(config, it, true) == 0)
12131212
{
1214-
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
1215-
File.WriteAllText(fileName, objectString);
1216-
1217-
var profileIt = new ProfileItem
1218-
{
1219-
coreType = ECoreType.Xray,
1220-
address = fileName,
1221-
remarks = v2rayCon.remarks ?? "v2ray_custom",
1222-
subid = subid,
1223-
isSub = isSub
1224-
};
1225-
1226-
if (AddCustomServer(config, profileIt, true) == 0)
1227-
{
1228-
count++;
1229-
}
1213+
count++;
12301214
}
12311215
}
12321216
if (count > 0)
@@ -1235,58 +1219,39 @@ static bool Contains(string str, params string[] s)
12351219
}
12361220
}
12371221

1238-
ProfileItem profileItem = new();
1222+
ProfileItem? profileItem = null;
1223+
//Is sing-box configuration
1224+
if (profileItem is null)
1225+
{
1226+
profileItem = SingboxFmt.ResolveFull(strData, subRemarks);
1227+
}
12391228
//Is v2ray configuration
1240-
var v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(strData);
1241-
if (v2rayConfig?.inbounds?.Count > 0
1242-
&& v2rayConfig.outbounds?.Count > 0)
1229+
if (profileItem is null)
12431230
{
1244-
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
1245-
File.WriteAllText(fileName, strData);
1246-
1247-
profileItem.coreType = ECoreType.Xray;
1248-
profileItem.address = fileName;
1249-
profileItem.remarks = v2rayConfig.remarks ?? "v2ray_custom";
1231+
profileItem = V2rayFmt.ResolveFull(strData, subRemarks);
12501232
}
12511233
//Is Clash configuration
1252-
else if (Contains(strData, "port", "socks-port", "proxies"))
1234+
if (profileItem is null)
12531235
{
1254-
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.yaml");
1255-
File.WriteAllText(fileName, strData);
1256-
1257-
profileItem.coreType = ECoreType.mihomo;
1258-
profileItem.address = fileName;
1259-
profileItem.remarks = "clash_custom";
1236+
profileItem = ClashFmt.ResolveFull(strData, subRemarks);
12601237
}
12611238
//Is hysteria configuration
1262-
else if (Contains(strData, "server", "up", "down", "listen", "<html>", "<body>"))
1239+
if (profileItem is null)
12631240
{
1264-
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
1265-
File.WriteAllText(fileName, strData);
1266-
1267-
profileItem.coreType = ECoreType.hysteria;
1268-
profileItem.address = fileName;
1269-
profileItem.remarks = "hysteria_custom";
1241+
profileItem = Hysteria2Fmt.ResolveFull2(strData, subRemarks);
1242+
}
1243+
if (profileItem is null)
1244+
{
1245+
profileItem = Hysteria2Fmt.ResolveFull(strData, subRemarks);
12701246
}
12711247
//Is naiveproxy configuration
1272-
else if (Contains(strData, "listen", "proxy", "<html>", "<body>"))
1248+
if (profileItem is null)
12731249
{
1274-
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
1275-
File.WriteAllText(fileName, strData);
1276-
1277-
profileItem.coreType = ECoreType.naiveproxy;
1278-
profileItem.address = fileName;
1279-
profileItem.remarks = "naiveproxy_custom";
1250+
profileItem = NaiveproxyFmt.ResolveFull(strData, subRemarks);
12801251
}
1281-
//Is Other configuration
1282-
else
1252+
if (profileItem is null || Utils.IsNullOrEmpty(profileItem.address))
12831253
{
12841254
return -1;
1285-
//var fileName = Utile.GetTempPath($"{Utile.GetGUID(false)}.txt");
1286-
//File.WriteAllText(fileName, strData);
1287-
1288-
//profileItem.address = fileName;
1289-
//profileItem.remarks = "other_custom";
12901255
}
12911256

12921257
if (isSub && !Utils.IsNullOrEmpty(subid))
@@ -1299,12 +1264,6 @@ static bool Contains(string str, params string[] s)
12991264
}
13001265
profileItem.subid = subid;
13011266
profileItem.isSub = isSub;
1302-
1303-
if (Utils.IsNullOrEmpty(profileItem.address))
1304-
{
1305-
return -1;
1306-
}
1307-
13081267
if (AddCustomServer(config, profileItem, true) == 0)
13091268
{
13101269
return 1;
@@ -1327,31 +1286,12 @@ private static int AddBatchServers4SsSIP008(Config config, string strData, strin
13271286
RemoveServerViaSubid(config, subid, isSub);
13281287
}
13291288

1330-
//SsSIP008
1331-
var lstSsServer = JsonUtils.Deserialize<List<SsServer>>(strData);
1332-
if (lstSsServer?.Count <= 0)
1333-
{
1334-
var ssSIP008 = JsonUtils.Deserialize<SsSIP008>(strData);
1335-
if (ssSIP008?.servers?.Count > 0)
1336-
{
1337-
lstSsServer = ssSIP008.servers;
1338-
}
1339-
}
1340-
1289+
var lstSsServer = ShadowsocksFmt.ResolveSip008(strData);
13411290
if (lstSsServer?.Count > 0)
13421291
{
13431292
int counter = 0;
1344-
foreach (var it in lstSsServer)
1293+
foreach (var ssItem in lstSsServer)
13451294
{
1346-
var ssItem = new ProfileItem()
1347-
{
1348-
subid = subid,
1349-
remarks = it.remarks,
1350-
security = it.method,
1351-
id = it.password,
1352-
address = it.server,
1353-
port = Utils.ToInt(it.server_port)
1354-
};
13551295
ssItem.subid = subid;
13561296
ssItem.isSub = isSub;
13571297
if (AddShadowsocksServer(config, ssItem) == 0)

v2rayN/v2rayN/Handler/Fmt/BaseFmt.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Specialized;
2+
using System.IO;
23
using v2rayN.Enums;
34
using v2rayN.Models;
45

@@ -182,5 +183,21 @@ protected static int ResolveStdTransport(NameValueCollection query, ref ProfileI
182183
}
183184
return 0;
184185
}
186+
187+
protected static bool Contains(string str, params string[] s)
188+
{
189+
foreach (var item in s)
190+
{
191+
if (str.Contains(item, StringComparison.OrdinalIgnoreCase)) return true;
192+
}
193+
return false;
194+
}
195+
196+
protected static string WriteAllText(string strData, string ext = "json")
197+
{
198+
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.{ext}");
199+
File.WriteAllText(fileName, strData);
200+
return fileName;
201+
}
185202
}
186203
}

v2rayN/v2rayN/Handler/Fmt/ClashFmt.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using v2rayN.Enums;
2+
using v2rayN.Models;
3+
4+
namespace v2rayN.Handler.Fmt
5+
{
6+
internal class ClashFmt : BaseFmt
7+
{
8+
public static ProfileItem? ResolveFull(string strData, string? subRemarks)
9+
{
10+
if (Contains(strData, "port", "socks-port", "proxies"))
11+
{
12+
var fileName = WriteAllText(strData, "yaml");
13+
14+
var profileItem = new ProfileItem
15+
{
16+
coreType = ECoreType.mihomo,
17+
address = fileName,
18+
remarks = subRemarks ?? "clash_custom"
19+
};
20+
return profileItem;
21+
}
22+
23+
return null;
24+
}
25+
}
26+
}

v2rayN/v2rayN/Handler/Fmt/FmtHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ internal class FmtHandler
3232
}
3333
}
3434

35-
3635
public static ProfileItem? ResolveConfig(string config, out string msg)
3736
{
3837
msg = ResUI.ConfigurationFormatIncorrect;

v2rayN/v2rayN/Handler/Fmt/Hysteria2Fmt.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,41 @@ internal class Hysteria2Fmt : BaseFmt
6464
url = $"{Global.ProtocolShares[EConfigType.Hysteria2]}{url}/{query}{remark}";
6565
return url;
6666
}
67+
68+
public static ProfileItem? ResolveFull(string strData, string? subRemarks)
69+
{
70+
if (Contains(strData, "server", "up", "down", "listen", "<html>", "<body>"))
71+
{
72+
var fileName = WriteAllText(strData);
73+
74+
var profileItem = new ProfileItem
75+
{
76+
coreType = ECoreType.hysteria,
77+
address = fileName,
78+
remarks = subRemarks ?? "hysteria_custom"
79+
};
80+
return profileItem;
81+
}
82+
83+
return null;
84+
}
85+
86+
public static ProfileItem? ResolveFull2(string strData, string? subRemarks)
87+
{
88+
if (Contains(strData, "server", "auth", "up", "down", "listen"))
89+
{
90+
var fileName = WriteAllText(strData);
91+
92+
var profileItem = new ProfileItem
93+
{
94+
coreType = ECoreType.hysteria2,
95+
address = fileName,
96+
remarks = subRemarks ?? "hysteria2_custom"
97+
};
98+
return profileItem;
99+
}
100+
101+
return null;
102+
}
67103
}
68104
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using v2rayN.Enums;
2+
using v2rayN.Models;
3+
4+
namespace v2rayN.Handler.Fmt
5+
{
6+
internal class NaiveproxyFmt : BaseFmt
7+
{
8+
public static ProfileItem? ResolveFull(string strData, string? subRemarks)
9+
{
10+
if (Contains(strData, "listen", "proxy", "<html>", "<body>"))
11+
{
12+
var fileName = WriteAllText(strData);
13+
14+
var profileItem = new ProfileItem
15+
{
16+
coreType = ECoreType.naiveproxy,
17+
address = fileName,
18+
remarks = subRemarks ?? "naiveproxy_custom"
19+
};
20+
return profileItem;
21+
}
22+
23+
return null;
24+
}
25+
}
26+
}

v2rayN/v2rayN/Handler/Fmt/ShadowsocksFmt.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,38 @@ internal class ShadowsocksFmt : BaseFmt
146146

147147
return item;
148148
}
149+
150+
public static List<ProfileItem>? ResolveSip008(string result)
151+
{
152+
//SsSIP008
153+
var lstSsServer = JsonUtils.Deserialize<List<SsServer>>(result);
154+
if (lstSsServer?.Count <= 0)
155+
{
156+
var ssSIP008 = JsonUtils.Deserialize<SsSIP008>(result);
157+
if (ssSIP008?.servers?.Count > 0)
158+
{
159+
lstSsServer = ssSIP008.servers;
160+
}
161+
}
162+
163+
if (lstSsServer?.Count > 0)
164+
{
165+
List<ProfileItem> lst = [];
166+
foreach (var it in lstSsServer)
167+
{
168+
var ssItem = new ProfileItem()
169+
{
170+
remarks = it.remarks,
171+
security = it.method,
172+
id = it.password,
173+
address = it.server,
174+
port = Utils.ToInt(it.server_port)
175+
};
176+
lst.Add(ssItem);
177+
}
178+
return lst;
179+
}
180+
return null;
181+
}
149182
}
150183
}

0 commit comments

Comments
 (0)