Skip to content

Commit 1e9a6cb

Browse files
committed
Refactor code
1 parent 31748aa commit 1e9a6cb

24 files changed

+1170
-1056
lines changed

v2rayN/v2rayN/Common/QRCodeHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using QRCoder;
22
using QRCoder.Xaml;
33
using System.Drawing;
4-
using System.Windows.Interop;
54
using System.Windows;
5+
using System.Windows.Interop;
66
using System.Windows.Media;
7+
using ZXing;
78
using ZXing.Common;
89
using ZXing.QrCode;
910
using ZXing.Windows.Compatibility;
10-
using ZXing;
1111

1212
namespace v2rayN
1313
{

v2rayN/v2rayN/Handler/ConfigHandler.cs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Text.RegularExpressions;
44
using v2rayN.Enums;
5+
using v2rayN.Handler.Fmt;
56
using v2rayN.Models;
67

78
namespace v2rayN.Handler
@@ -1070,12 +1071,12 @@ private static int RemoveProfileItem(Config config, string indexId)
10701071
/// 批量添加服务器
10711072
/// </summary>
10721073
/// <param name="config"></param>
1073-
/// <param name="clipboardData"></param>
1074+
/// <param name="strData"></param>
10741075
/// <param name="subid"></param>
10751076
/// <returns>成功导入的数量</returns>
1076-
private static int AddBatchServers(Config config, string clipboardData, string subid, bool isSub, List<ProfileItem> lstOriSub)
1077+
private static int AddBatchServers(Config config, string strData, string subid, bool isSub, List<ProfileItem> lstOriSub)
10771078
{
1078-
if (Utils.IsNullOrEmpty(clipboardData))
1079+
if (Utils.IsNullOrEmpty(strData))
10791080
{
10801081
return -1;
10811082
}
@@ -1092,7 +1093,7 @@ private static int AddBatchServers(Config config, string clipboardData, string s
10921093
//Check for duplicate indexId
10931094
List<string>? lstDbIndexId = null;
10941095
List<ProfileItem> lstAdd = new();
1095-
var arrData = clipboardData.Split(Environment.NewLine.ToCharArray()).Where(t => !t.IsNullOrEmpty());
1096+
var arrData = strData.Split(Environment.NewLine.ToCharArray()).Where(t => !t.IsNullOrEmpty());
10961097
if (isSub)
10971098
{
10981099
arrData = arrData.Distinct();
@@ -1108,7 +1109,7 @@ private static int AddBatchServers(Config config, string clipboardData, string s
11081109
}
11091110
continue;
11101111
}
1111-
var profileItem = ShareHandler.ImportFromClipboardConfig(str, out string msg);
1112+
var profileItem = FmtHandler.ResolveConfig(str, out string msg);
11121113
if (profileItem is null)
11131114
{
11141115
continue;
@@ -1177,9 +1178,9 @@ private static int AddBatchServers(Config config, string clipboardData, string s
11771178
return countServers;
11781179
}
11791180

1180-
private static int AddBatchServers4Custom(Config config, string clipboardData, string subid, bool isSub, List<ProfileItem> lstOriSub)
1181+
private static int AddBatchServers4Custom(Config config, string strData, string subid, bool isSub, List<ProfileItem> lstOriSub)
11811182
{
1182-
if (Utils.IsNullOrEmpty(clipboardData))
1183+
if (Utils.IsNullOrEmpty(strData))
11831184
{
11841185
return -1;
11851186
}
@@ -1195,7 +1196,7 @@ static bool Contains(string str, params string[] s)
11951196
}
11961197

11971198
//Is v2ray array configuration
1198-
var configObjects = JsonUtils.Deserialize<Object[]>(clipboardData);
1199+
var configObjects = JsonUtils.Deserialize<Object[]>(strData);
11991200
if (configObjects != null && configObjects.Length > 0)
12001201
{
12011202
if (isSub && !Utils.IsNullOrEmpty(subid))
@@ -1236,42 +1237,42 @@ static bool Contains(string str, params string[] s)
12361237

12371238
ProfileItem profileItem = new();
12381239
//Is v2ray configuration
1239-
var v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(clipboardData);
1240+
var v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(strData);
12401241
if (v2rayConfig?.inbounds?.Count > 0
12411242
&& v2rayConfig.outbounds?.Count > 0)
12421243
{
12431244
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
1244-
File.WriteAllText(fileName, clipboardData);
1245+
File.WriteAllText(fileName, strData);
12451246

12461247
profileItem.coreType = ECoreType.Xray;
12471248
profileItem.address = fileName;
12481249
profileItem.remarks = v2rayConfig.remarks ?? "v2ray_custom";
12491250
}
12501251
//Is Clash configuration
1251-
else if (Contains(clipboardData, "port", "socks-port", "proxies"))
1252+
else if (Contains(strData, "port", "socks-port", "proxies"))
12521253
{
12531254
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.yaml");
1254-
File.WriteAllText(fileName, clipboardData);
1255+
File.WriteAllText(fileName, strData);
12551256

12561257
profileItem.coreType = ECoreType.mihomo;
12571258
profileItem.address = fileName;
12581259
profileItem.remarks = "clash_custom";
12591260
}
12601261
//Is hysteria configuration
1261-
else if (Contains(clipboardData, "server", "up", "down", "listen", "<html>", "<body>"))
1262+
else if (Contains(strData, "server", "up", "down", "listen", "<html>", "<body>"))
12621263
{
12631264
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
1264-
File.WriteAllText(fileName, clipboardData);
1265+
File.WriteAllText(fileName, strData);
12651266

12661267
profileItem.coreType = ECoreType.hysteria;
12671268
profileItem.address = fileName;
12681269
profileItem.remarks = "hysteria_custom";
12691270
}
12701271
//Is naiveproxy configuration
1271-
else if (Contains(clipboardData, "listen", "proxy", "<html>", "<body>"))
1272+
else if (Contains(strData, "listen", "proxy", "<html>", "<body>"))
12721273
{
12731274
var fileName = Utils.GetTempPath($"{Utils.GetGUID(false)}.json");
1274-
File.WriteAllText(fileName, clipboardData);
1275+
File.WriteAllText(fileName, strData);
12751276

12761277
profileItem.coreType = ECoreType.naiveproxy;
12771278
profileItem.address = fileName;
@@ -1282,7 +1283,7 @@ static bool Contains(string str, params string[] s)
12821283
{
12831284
return -1;
12841285
//var fileName = Utile.GetTempPath($"{Utile.GetGUID(false)}.txt");
1285-
//File.WriteAllText(fileName, clipboardData);
1286+
//File.WriteAllText(fileName, strData);
12861287

12871288
//profileItem.address = fileName;
12881289
//profileItem.remarks = "other_custom";
@@ -1314,9 +1315,9 @@ static bool Contains(string str, params string[] s)
13141315
}
13151316
}
13161317

1317-
private static int AddBatchServers4SsSIP008(Config config, string clipboardData, string subid, bool isSub, List<ProfileItem> lstOriSub)
1318+
private static int AddBatchServers4SsSIP008(Config config, string strData, string subid, bool isSub, List<ProfileItem> lstOriSub)
13181319
{
1319-
if (Utils.IsNullOrEmpty(clipboardData))
1320+
if (Utils.IsNullOrEmpty(strData))
13201321
{
13211322
return -1;
13221323
}
@@ -1327,10 +1328,10 @@ private static int AddBatchServers4SsSIP008(Config config, string clipboardData,
13271328
}
13281329

13291330
//SsSIP008
1330-
var lstSsServer = JsonUtils.Deserialize<List<SsServer>>(clipboardData);
1331+
var lstSsServer = JsonUtils.Deserialize<List<SsServer>>(strData);
13311332
if (lstSsServer?.Count <= 0)
13321333
{
1333-
var ssSIP008 = JsonUtils.Deserialize<SsSIP008>(clipboardData);
1334+
var ssSIP008 = JsonUtils.Deserialize<SsSIP008>(strData);
13341335
if (ssSIP008?.servers?.Count > 0)
13351336
{
13361337
lstSsServer = ssSIP008.servers;
@@ -1365,7 +1366,7 @@ private static int AddBatchServers4SsSIP008(Config config, string clipboardData,
13651366
return -1;
13661367
}
13671368

1368-
public static int AddBatchServers(Config config, string clipboardData, string subid, bool isSub)
1369+
public static int AddBatchServers(Config config, string strData, string subid, bool isSub)
13691370
{
13701371
List<ProfileItem>? lstOriSub = null;
13711372
if (isSub && !Utils.IsNullOrEmpty(subid))
@@ -1374,28 +1375,28 @@ public static int AddBatchServers(Config config, string clipboardData, string su
13741375
}
13751376

13761377
var counter = 0;
1377-
if (Utils.IsBase64String(clipboardData))
1378+
if (Utils.IsBase64String(strData))
13781379
{
1379-
counter = AddBatchServers(config, Utils.Base64Decode(clipboardData), subid, isSub, lstOriSub);
1380+
counter = AddBatchServers(config, Utils.Base64Decode(strData), subid, isSub, lstOriSub);
13801381
}
13811382
if (counter < 1)
13821383
{
1383-
counter = AddBatchServers(config, clipboardData, subid, isSub, lstOriSub);
1384+
counter = AddBatchServers(config, strData, subid, isSub, lstOriSub);
13841385
}
13851386
if (counter < 1)
13861387
{
1387-
counter = AddBatchServers(config, Utils.Base64Decode(clipboardData), subid, isSub, lstOriSub);
1388+
counter = AddBatchServers(config, Utils.Base64Decode(strData), subid, isSub, lstOriSub);
13881389
}
13891390

13901391
if (counter < 1)
13911392
{
1392-
counter = AddBatchServers4SsSIP008(config, clipboardData, subid, isSub, lstOriSub);
1393+
counter = AddBatchServers4SsSIP008(config, strData, subid, isSub, lstOriSub);
13931394
}
13941395

13951396
//maybe other sub
13961397
if (counter < 1)
13971398
{
1398-
counter = AddBatchServers4Custom(config, clipboardData, subid, isSub, lstOriSub);
1399+
counter = AddBatchServers4Custom(config, strData, subid, isSub, lstOriSub);
13991400
}
14001401

14011402
return counter;
@@ -1533,16 +1534,16 @@ public static int SaveRoutingItem(Config config, RoutingItem item)
15331534
/// AddBatchRoutingRules
15341535
/// </summary>
15351536
/// <param name="config"></param>
1536-
/// <param name="clipboardData"></param>
1537+
/// <param name="strData"></param>
15371538
/// <returns></returns>
1538-
public static int AddBatchRoutingRules(ref RoutingItem routingItem, string clipboardData)
1539+
public static int AddBatchRoutingRules(ref RoutingItem routingItem, string strData)
15391540
{
1540-
if (Utils.IsNullOrEmpty(clipboardData))
1541+
if (Utils.IsNullOrEmpty(strData))
15411542
{
15421543
return -1;
15431544
}
15441545

1545-
var lstRules = JsonUtils.Deserialize<List<RulesItem>>(clipboardData);
1546+
var lstRules = JsonUtils.Deserialize<List<RulesItem>>(strData);
15461547
if (lstRules == null)
15471548
{
15481549
return -1;

v2rayN/v2rayN/Handler/CoreConfigHandler.cs renamed to v2rayN/v2rayN/Handler/CoreConfig/CoreConfigHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using v2rayN.Models;
44
using v2rayN.Resx;
55

6-
namespace v2rayN.Handler
6+
namespace v2rayN.Handler.CoreConfig
77
{
88
/// <summary>
99
/// Core configuration file processing class
@@ -155,15 +155,15 @@ public static int GenerateClientSpeedtestConfig(Config config, string fileName,
155155
{
156156
if (coreType == ECoreType.sing_box)
157157
{
158-
if ((new CoreConfigSingbox(config)).GenerateClientSpeedtestConfig(selecteds, out SingboxConfig? singboxConfig, out msg) != 0)
158+
if (new CoreConfigSingbox(config).GenerateClientSpeedtestConfig(selecteds, out SingboxConfig? singboxConfig, out msg) != 0)
159159
{
160160
return -1;
161161
}
162162
JsonUtils.ToFile(singboxConfig, fileName, false);
163163
}
164164
else
165165
{
166-
if ((new CoreConfigV2ray(config)).GenerateClientSpeedtestConfig(selecteds, out V2rayConfig? v2rayConfig, out msg) != 0)
166+
if (new CoreConfigV2ray(config).GenerateClientSpeedtestConfig(selecteds, out V2rayConfig? v2rayConfig, out msg) != 0)
167167
{
168168
return -1;
169169
}

v2rayN/v2rayN/Handler/CoreConfigSingbox.cs renamed to v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using v2rayN.Models;
55
using v2rayN.Resx;
66

7-
namespace v2rayN.Handler
7+
namespace v2rayN.Handler.CoreConfig
88
{
99
internal class CoreConfigSingbox
1010
{
@@ -120,7 +120,7 @@ private int GenInbounds(SingboxConfig singboxConfig)
120120
var listen = "::";
121121
singboxConfig.inbounds = [];
122122

123-
if (!_config.tunModeItem.enableTun || (_config.tunModeItem.enableTun && _config.tunModeItem.enableExInbound))
123+
if (!_config.tunModeItem.enableTun || _config.tunModeItem.enableTun && _config.tunModeItem.enableExInbound)
124124
{
125125
var inbound = new Inbound4Sbox()
126126
{

v2rayN/v2rayN/Handler/CoreConfigV2ray.cs renamed to v2rayN/v2rayN/Handler/CoreConfig/CoreConfigV2ray.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using v2rayN.Models;
55
using v2rayN.Resx;
66

7-
namespace v2rayN.Handler
7+
namespace v2rayN.Handler.CoreConfig
88
{
99
internal class CoreConfigV2ray
1010
{
@@ -277,8 +277,8 @@ private int GenRoutingUserRule(RulesItem4Ray? rules, V2rayConfig v2rayConfig)
277277
if (!hasDomainIp)
278278
{
279279
if (!Utils.IsNullOrEmpty(rules.port)
280-
|| (rules.protocol?.Count > 0)
281-
|| (rules.inboundTag?.Count > 0)
280+
|| rules.protocol?.Count > 0
281+
|| rules.inboundTag?.Count > 0
282282
)
283283
{
284284
var it = JsonUtils.DeepCopy(rules);
@@ -674,7 +674,7 @@ private int GenBoundStreamSettings(ProfileItem node, StreamSettings4Ray streamSe
674674
{
675675
authority = Utils.IsNullOrEmpty(host) ? null : host,
676676
serviceName = node.path,
677-
multiMode = (node.headerType == Global.GrpcMultiMode),
677+
multiMode = node.headerType == Global.GrpcMultiMode,
678678
idle_timeout = _config.grpcItem.idle_timeout,
679679
health_check_timeout = _config.grpcItem.health_check_timeout,
680680
permit_without_stream = _config.grpcItem.permit_without_stream,

v2rayN/v2rayN/Handler/CoreHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Reactive.Linq;
44
using System.Text;
55
using v2rayN.Enums;
6+
using v2rayN.Handler.CoreConfig;
67
using v2rayN.Models;
78
using v2rayN.Resx;
89

@@ -39,7 +40,7 @@ public void LoadCore()
3940
string fileName = Utils.GetConfigPath(Global.CoreConfigFileName);
4041
if (CoreConfigHandler.GenerateClientConfig(node, fileName, out string msg, out string content) != 0)
4142
{
42-
ShowMsg(false, msg);
43+
ShowMsg(false, msg);
4344
return;
4445
}
4546
else

0 commit comments

Comments
 (0)