mirror of
https://github.com/netchx/netch.git
synced 2026-04-11 20:43:21 +08:00
Refactor clean temp file
Refactor Mode.GetRules() yield
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user