Refactor clean temp file

Refactor Mode.GetRules() yield
This commit is contained in:
ChsBuffer
2021-06-02 14:01:15 +08:00
parent 4fba66fab8
commit 5bbef8db12
7 changed files with 13 additions and 16 deletions

View File

@@ -2,6 +2,8 @@
{
public static class Constants
{
public const string TempConfig = "data\\last.json";
public const string TempRouteFile = "data\\route.txt";
public const string EOF = "\r\n";
public const string OutputTemplate = @"[{Timestamp:yyyy-MM-dd HH:mm:ss}][{Level}] {Message:lj}{NewLine}{Exception}";
public const string LogFile = "logging\\application.log";

View File

@@ -36,8 +36,7 @@ namespace Netch.Controllers
var server = MainController.Server!;
_serverAddresses = DnsUtils.Lookup(server.Hostname)!; // server address have been cached when MainController.Start
IPAddress address;
(_outbound, address) = NetRoute.GetBestRouteTemplate();
_outbound = NetRoute.GetBestRouteTemplate(out var address);
CheckDriver();
Dial(NameList.TYPE_ADAPMTU, "1500");
@@ -202,7 +201,7 @@ namespace Netch.Controllers
}
catch (Exception e)
{
Log.Error(e,"复制 wintun.dll 异常");
Log.Error(e, "复制 wintun.dll 异常");
throw new MessageException($"Failed to copy wintun.dll to system directory: {e.Message}");
}
}

View File

@@ -1225,11 +1225,9 @@ namespace Netch.Forms
Hide();
if (saveConfiguration)
{
Configuration.Save();
}
foreach (var file in new[] { "data\\last.json", "data\\privoxy.conf" })
foreach (var file in new[] { Constants.TempConfig, Constants.TempRouteFile })
if (File.Exists(file))
File.Delete(file);

View File

@@ -48,7 +48,6 @@ namespace Netch.Models
public IEnumerable<string> GetRules()
{
var result = new List<string>();
foreach (var s in Content)
{
if (string.IsNullOrWhiteSpace(s))
@@ -76,15 +75,14 @@ namespace Netch.Models
if (mode.Content.Any(rule => rule.StartsWith(include)))
throw new Exception("Cannot reference mode that reference other mode");
result.AddRange(mode.GetRules());
foreach (var rule in mode.GetRules())
yield return rule;
}
else
{
result.Add(s);
yield return s;
}
}
return result;
}
private static (string, ModeType) ReadHead(string fileName)

View File

@@ -16,14 +16,14 @@ namespace Netch.Models
};
}
public static (NetRoute, IPAddress address) GetBestRouteTemplate()
public static NetRoute GetBestRouteTemplate(out IPAddress address)
{
if (IpHlpApi.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var route) != 0)
throw new MessageException("GetBestRoute 搜索失败");
var address = new IPAddress(route.dwForwardNextHop.S_addr);
address = new IPAddress(route.dwForwardNextHop.S_addr);
var gateway = new IPAddress(route.dwForwardNextHop.S_un_b);
return (TemplateBuilder(gateway.ToString(), (int)route.dwForwardIfIndex), address);
return TemplateBuilder(gateway.ToString(), (int)route.dwForwardIfIndex);
}
public int InterfaceIndex;

View File

@@ -40,7 +40,7 @@ namespace Netch.Servers.Trojan
if (!string.IsNullOrWhiteSpace(server.Host))
trojanConfig.ssl.sni = server.Host;
File.WriteAllBytes("data\\last.json", JsonSerializer.SerializeToUtf8Bytes(trojanConfig, Global.NewDefaultJsonSerializerOptions));
File.WriteAllBytes(Constants.TempConfig, JsonSerializer.SerializeToUtf8Bytes(trojanConfig, Global.NewDefaultJsonSerializerOptions));
StartInstanceAuto("-c ..\\data\\last.json");
}

View File

@@ -23,7 +23,7 @@ namespace Netch.Servers.V2ray
public virtual void Start(in Server s, in Mode mode)
{
File.WriteAllText("data\\last.json", V2rayConfigUtils.GenerateClientConfig(s, mode));
File.WriteAllText(Constants.TempConfig, V2rayConfigUtils.GenerateClientConfig(s, mode));
StartInstanceAuto("-config ..\\data\\last.json");
}