Skip to content

Commit

Permalink
Refactor Mode Load
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Dec 19, 2020
1 parent b31bedb commit 9d08633
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Netch/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static async Task<bool> Start(Server server, Mode mode)
throw new StartFailedException();
}

if (mode.TestNatRequired)
if (mode.TestNatRequired())
NatTest();

return true;
Expand Down
18 changes: 10 additions & 8 deletions Netch/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,21 +366,23 @@ private void EditModePictureBox_Click(object sender, EventArgs e)
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;
}
}
}

Expand Down
48 changes: 8 additions & 40 deletions Netch/Models/Mode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ public class Mode
/// </summary>
public int Type = 0;

/// 是否会转发 UDP
public bool TestNatRequired => Type is 0 or 1 or 2;

/// <summary>
/// 绕过中国(0. 不绕过 1. 绕过)
/// </summary>
Expand Down Expand Up @@ -120,44 +117,15 @@ public override string ToString()
/// <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();
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;

public string TypeToString()
{
return Type switch
{
0 => "Process",
1 => "TUNTAP",
2 => "TUNTAP",
3 => "SYSTEM",
4 => "S5",
5 => "S5+HTTP",
_ => "ERROR",
};
}
/// Socks5 分流是否能被有效实施
public static bool ClientRouting(this Mode mode) => mode.Type is not 1 or 2;
}
}
1 change: 1 addition & 0 deletions Netch/Servers/VMess/Utils/V2rayConfigUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private static void routing(Server server, Mode mode, ref V2rayConfig v2rayConfi
switch (mode.Type)
{
case 0:
directRuleObject.ip.Add("geoip:cn");
break;
case 1:
case 2:
Expand Down
13 changes: 8 additions & 5 deletions Netch/Utils/ModeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,19 @@ private static void LoadModeFile(string fullName)

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
{
Expand Down Expand Up @@ -137,6 +139,7 @@ public static void Delete(Mode mode)
Global.MainForm.InitMode();
}


public static bool SkipServerController(Server server, Mode mode)
{
return mode.Type switch
Expand Down

0 comments on commit 9d08633

Please sign in to comment.