Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Jan 30, 2024
1 parent 4054369 commit c466ea1
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 101 deletions.
47 changes: 13 additions & 34 deletions v2rayN/v2rayN/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,8 +1089,8 @@ private static int AddBatchServers(Config config, string clipboardData, string s
}
continue;
}
ProfileItem profileItem = ShareHandler.ImportFromClipboardConfig(str, out string msg);
if (profileItem == null)
var profileItem = ShareHandler.ImportFromClipboardConfig(str, out string msg);
if (profileItem is null)
{
continue;
}
Expand Down Expand Up @@ -1127,40 +1127,19 @@ private static int AddBatchServers(Config config, string clipboardData, string s
}
profileItem.subid = subid;
profileItem.isSub = isSub;
var addStatus = -1;

if (profileItem.configType == EConfigType.VMess)
var addStatus = profileItem.configType switch
{
addStatus = AddServer(config, profileItem, false);
}
else if (profileItem.configType == EConfigType.Shadowsocks)
{
addStatus = AddShadowsocksServer(config, profileItem, false);
}
else if (profileItem.configType == EConfigType.Socks)
{
addStatus = AddSocksServer(config, profileItem, false);
}
else if (profileItem.configType == EConfigType.Trojan)
{
addStatus = AddTrojanServer(config, profileItem, false);
}
else if (profileItem.configType == EConfigType.VLESS)
{
addStatus = AddVlessServer(config, profileItem, false);
}
else if (profileItem.configType == EConfigType.Hysteria2)
{
addStatus = AddHysteria2Server(config, profileItem, false);
}
else if (profileItem.configType == EConfigType.Tuic)
{
addStatus = AddTuicServer(config, profileItem, false);
}
else if (profileItem.configType == EConfigType.Wireguard)
{
addStatus = AddWireguardServer(config, profileItem, false);
}
EConfigType.VMess => AddServer(config, profileItem, false),
EConfigType.Shadowsocks => AddShadowsocksServer(config, profileItem, false),
EConfigType.Socks => AddSocksServer(config, profileItem, false),
EConfigType.Trojan => AddTrojanServer(config, profileItem, false),
EConfigType.VLESS => AddVlessServer(config, profileItem, false),
EConfigType.Hysteria2 => AddHysteria2Server(config, profileItem, false),
EConfigType.Tuic => AddTuicServer(config, profileItem, false),
EConfigType.Wireguard => AddWireguardServer(config, profileItem, false),
_ => -1,
};

if (addStatus == 0)
{
Expand Down
14 changes: 2 additions & 12 deletions v2rayN/v2rayN/Handler/ShareHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,12 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
/// <returns></returns>
public static ProfileItem? ImportFromClipboardConfig(string clipboardData, out string msg)
{
msg = string.Empty;

msg = ResUI.ConfigurationFormatIncorrect;
ProfileItem? profileItem;

try
{
//载入配置文件
string result = clipboardData.TrimEx();// Utils.GetClipboardData();
string result = clipboardData.TrimEx();
if (Utils.IsNullOrEmpty(result))
{
msg = ResUI.FailedReadConfiguration;
Expand All @@ -413,8 +411,6 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
}
else if (result.StartsWith(Global.ProtocolShares[EConfigType.Shadowsocks]))
{
msg = ResUI.ConfigurationFormatIncorrect;

profileItem = ResolveSSLegacy(result) ?? ResolveSip002(result);
if (profileItem == null)
{
Expand All @@ -429,8 +425,6 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
}
else if (result.StartsWith(Global.ProtocolShares[EConfigType.Socks]))
{
msg = ResUI.ConfigurationFormatIncorrect;

profileItem = ResolveSocksNew(result) ?? ResolveSocks(result);
if (profileItem == null)
{
Expand All @@ -445,8 +439,6 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
}
else if (result.StartsWith(Global.ProtocolShares[EConfigType.Trojan]))
{
msg = ResUI.ConfigurationFormatIncorrect;

profileItem = ResolveTrojan(result);
}
else if (result.StartsWith(Global.ProtocolShares[EConfigType.VLESS]))
Expand All @@ -455,8 +447,6 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
}
else if (result.StartsWith(Global.ProtocolShares[EConfigType.Hysteria2]) || result.StartsWith(Global.Hysteria2ProtocolShare))
{
msg = ResUI.ConfigurationFormatIncorrect;

profileItem = ResolveHysteria2(result);
}
else if (result.StartsWith(Global.ProtocolShares[EConfigType.Tuic]))
Expand Down
16 changes: 11 additions & 5 deletions v2rayN/v2rayN/Handler/UpdateHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ public void CheckUpdateGuiN(Config config, Action<bool, string> update, bool pre
_updateFunc(false, args.Msg);
url = args.Url;
_ = askToDownload(downloadHandle, url, true);
AskToDownload(downloadHandle, url, true).ContinueWith(task =>
{
_updateFunc(false, url);
});
}
else
{
Expand Down Expand Up @@ -139,7 +142,10 @@ public void CheckUpdateCore(ECoreType type, Config config, Action<bool, string>
_updateFunc(false, args.Msg);
url = args.Url;
_ = askToDownload(downloadHandle, url, true);
AskToDownload(downloadHandle, url, true).ContinueWith(task =>
{
_updateFunc(false, url);
});
}
else
{
Expand Down Expand Up @@ -527,7 +533,7 @@ private void responseHandler(ECoreType type, string gitHubReleaseApi, bool preRe
}
}

private async Task askToDownload(DownloadHandle downloadHandle, string url, bool blAsk)
private async Task AskToDownload(DownloadHandle downloadHandle, string url, bool blAsk)
{
bool blDownload = false;
if (blAsk)
Expand Down Expand Up @@ -591,7 +597,7 @@ private async Task UpdateGeoFile(string geoName, Config config, Action<bool, str
{
_updateFunc(false, args.GetException().Message);
};
await askToDownload(downloadHandle, url, false);
await AskToDownload(downloadHandle, url, false);
}

private async Task UpdateGeoFile4Singbox(string geoName, Config config, bool needStop, Action<bool, string> update)
Expand Down Expand Up @@ -639,7 +645,7 @@ private async Task UpdateGeoFile4Singbox(string geoName, Config config, bool nee
{
_updateFunc(false, args.GetException().Message);
};
await askToDownload(downloadHandle, url, false);
await AskToDownload(downloadHandle, url, false);
}

#endregion private
Expand Down
45 changes: 11 additions & 34 deletions v2rayN/v2rayN/ViewModels/AddServerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,18 @@ private void SaveServer()
item.spiderX = SelectedSource.spiderX;
}

int ret = -1;
switch (item.configType)
var ret = item.configType switch
{
case EConfigType.VMess:
ret = ConfigHandler.AddServer(_config, item);
break;

case EConfigType.Shadowsocks:
ret = ConfigHandler.AddShadowsocksServer(_config, item);
break;

case EConfigType.Socks:
ret = ConfigHandler.AddSocksServer(_config, item);
break;

case EConfigType.VLESS:
ret = ConfigHandler.AddVlessServer(_config, item);
break;

case EConfigType.Trojan:
ret = ConfigHandler.AddTrojanServer(_config, item);
break;

case EConfigType.Hysteria2:
ret = ConfigHandler.AddHysteria2Server(_config, item);
break;

case EConfigType.Tuic:
ret = ConfigHandler.AddTuicServer(_config, item);
break;

case EConfigType.Wireguard:
ret = ConfigHandler.AddWireguardServer(_config, item);
break;
}
EConfigType.VMess => ConfigHandler.AddServer(_config, item),
EConfigType.Shadowsocks => ConfigHandler.AddShadowsocksServer(_config, item),
EConfigType.Socks => ConfigHandler.AddSocksServer(_config, item),
EConfigType.Trojan => ConfigHandler.AddTrojanServer(_config, item),
EConfigType.VLESS => ConfigHandler.AddVlessServer(_config, item),
EConfigType.Hysteria2 => ConfigHandler.AddHysteria2Server(_config, item),
EConfigType.Tuic => ConfigHandler.AddTuicServer(_config, item),
EConfigType.Wireguard => ConfigHandler.AddWireguardServer(_config, item),
_ => -1,
};

if (ret == 0)
{
Expand Down
24 changes: 11 additions & 13 deletions v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,16 +1482,21 @@ private void CheckUpdateGeo()

public void Reload()
{
_ = LoadV2ray();
BlReloadEnabled = false;

LoadV2ray().ContinueWith(task =>
{
TestServerAvailability();
Application.Current.Dispatcher.Invoke((Action)(() =>
{
BlReloadEnabled = true;
}));
});
}

private async Task LoadV2ray()
{
Application.Current.Dispatcher.Invoke((Action)(() =>
{
BlReloadEnabled = false;
}));

await Task.Run(() =>
{
_coreHandler.LoadCore();
Expand All @@ -1500,13 +1505,6 @@ await Task.Run(() =>
ChangeSystemProxyStatus(_config.sysProxyType, false);
});

TestServerAvailability();

Application.Current.Dispatcher.Invoke((Action)(() =>
{
BlReloadEnabled = true;
}));
}

private void CloseV2ray()
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private async Task ImportRulesFromUrl()
}

DownloadHandle downloadHandle = new DownloadHandle();
string result = await downloadHandle.TryDownloadString(url, true, "");
var result = await downloadHandle.TryDownloadString(url, true, "");
if (AddBatchRoutingRules(SelectedRouting, result) == 0)
{
Application.Current.Dispatcher.Invoke((Action)(() =>
Expand All @@ -317,7 +317,7 @@ private async Task ImportRulesFromUrl()
}
}

private int AddBatchRoutingRules(RoutingItem routingItem, string clipboardData)
private int AddBatchRoutingRules(RoutingItem routingItem, string? clipboardData)
{
bool blReplace = false;
if (UI.ShowYesNo(ResUI.AddBatchRoutingRulesYesNo) == MessageBoxResult.No)
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
break;

case Key.S:
_ = ViewModel?.ScanScreenTaskAsync();
ViewModel?.ScanScreenTaskAsync().ContinueWith(_ => { });
break;

case Key.T:
Expand Down

0 comments on commit c466ea1

Please sign in to comment.