Refactor Mode Load

This commit is contained in:
ChsBuffer
2020-12-19 18:31:25 +08:00
parent b31bedbc82
commit 9d086336ea
5 changed files with 29 additions and 55 deletions

View File

@@ -82,7 +82,7 @@ namespace Netch.Controllers
throw new StartFailedException();
}
if (mode.TestNatRequired)
if (mode.TestNatRequired())
NatTest();
return true;

View File

@@ -366,21 +366,23 @@ namespace Netch.Forms
return;
}
var selectedMode = (Models.Mode) ModeComboBox.SelectedItem;
switch (selectedMode.Type)
var mode = (Models.Mode) ModeComboBox.SelectedItem;
if (ModifierKeys == Keys.Control)
{
Utils.Utils.Open(ModeHelper.GetFullPath(mode.RelativePath));
return;
}
switch (mode.Type)
{
case 0:
{
Hide();
new Process(selectedMode).ShowDialog();
new Process(mode).ShowDialog();
Show();
break;
}
default:
{
MessageBoxX.Show($"Current not support editing {selectedMode.TypeToString()} Mode");
Utils.Utils.Open(ModeHelper.GetFullPath(mode.RelativePath));
break;
}
}
}

View File

@@ -33,9 +33,6 @@ namespace Netch.Models
/// </summary>
public int Type = 0;
/// 是否会转发 UDP
public bool TestNatRequired => Type is 0 or 1 or 2;
/// <summary>
/// 绕过中国0. 不绕过 1. 绕过)
/// </summary>
@@ -120,44 +117,15 @@ namespace Netch.Models
/// <returns>模式文件字符串</returns>
public string ToFileString()
{
StringBuilder fileString = new StringBuilder();
switch (Type)
{
case 0:
// 进程模式
fileString.Append($"# {Remark}");
break;
case 1:
// TUN/TAP 规则内 IP CIDR无 Bypass China 设置
fileString.Append($"# {Remark}, {Type}, 0");
break;
default:
fileString.Append($"# {Remark}, {Type}, {(BypassChina ? 1 : 0)}");
break;
}
if (Rule.Any())
{
fileString.Append(Global.EOF);
fileString.Append(string.Join(Global.EOF, Rule));
}
return fileString.ToString();
}
public string TypeToString()
{
return Type switch
{
0 => "Process",
1 => "TUNTAP",
2 => "TUNTAP",
3 => "SYSTEM",
4 => "S5",
5 => "S5+HTTP",
_ => "ERROR",
};
return $"# {Remark}, {Type}, {(BypassChina ? 1 : 0)}{Global.EOF}{string.Join(Global.EOF, Rule)}";
}
}
public static class ModeExtension
{
/// 是否会转发 UDP
public static bool TestNatRequired(this Mode mode) => mode.Type is 0 or 1 or 2;
/// Socks5 分流是否能被有效实施
public static bool ClientRouting(this Mode mode) => mode.Type is not 1 or 2;
}
}

View File

@@ -80,6 +80,7 @@ namespace Netch.Servers.VMess.Utils
switch (mode.Type)
{
case 0:
directRuleObject.ip.Add("geoip:cn");
break;
case 1:
case 2:

View File

@@ -68,17 +68,19 @@ namespace Netch.Utils
if (i == 0)
{
if (text.First() != '#')
return;
try
{
var splited = text.Substring(text.IndexOf('#') + 1).Split(',').Select(s => s.Trim()).ToArray();
var splited = text.Substring(1).Split(',').Select(s => s.Trim()).ToArray();
mode.Remark = splited[0];
var result = int.TryParse(splited.ElementAtOrDefault(1), out var type);
mode.Type = result ? type : 0;
var typeResult = int.TryParse(splited.ElementAtOrDefault(1), out var type);
mode.Type = typeResult ? type : 0;
var result1 = int.TryParse(splited.ElementAtOrDefault(2), out var bypassChina);
mode.BypassChina = result1 && bypassChina == 1;
var bypassChinaResult = int.TryParse(splited.ElementAtOrDefault(2), out var bypassChina);
mode.BypassChina = mode.ClientRouting() && bypassChinaResult && bypassChina == 1;
}
catch
{
@@ -137,6 +139,7 @@ namespace Netch.Utils
Global.MainForm.InitMode();
}
public static bool SkipServerController(Server server, Mode mode)
{
return mode.Type switch