diff --git a/PortProxyGUI/PortProxyGUI.cs b/PortProxyGUI/PortProxyGUI.cs index 2722ab1..6e797b1 100644 --- a/PortProxyGUI/PortProxyGUI.cs +++ b/PortProxyGUI/PortProxyGUI.cs @@ -63,7 +63,7 @@ namespace PortProxyGUI try { var rule = ParseRule(item); - CmdUtil.AddOrUpdateProxy(rule); + PortProxyUtil.AddOrUpdateProxy(rule); } catch (NotSupportedException ex) { @@ -83,7 +83,7 @@ namespace PortProxyGUI try { var rule = ParseRule(item); - CmdUtil.DeleteProxy(rule); + PortProxyUtil.DeleteProxy(rule); } catch (NotSupportedException ex) { @@ -173,7 +173,7 @@ namespace PortProxyGUI public void RefreshProxyList() { - var proxies = CmdUtil.GetProxies(); + var proxies = PortProxyUtil.GetProxies(); var rules = Program.SqliteDbScope.Rules.ToArray(); foreach (var proxy in proxies) { diff --git a/PortProxyGUI/PortProxyGUI.csproj b/PortProxyGUI/PortProxyGUI.csproj index abb7977..5147f69 100644 --- a/PortProxyGUI/PortProxyGUI.csproj +++ b/PortProxyGUI/PortProxyGUI.csproj @@ -42,4 +42,4 @@ - \ No newline at end of file + diff --git a/PortProxyGUI/SetProxy.cs b/PortProxyGUI/SetProxy.cs index 8a32394..1020ae2 100644 --- a/PortProxyGUI/SetProxy.cs +++ b/PortProxyGUI/SetProxy.cs @@ -116,17 +116,17 @@ namespace PortProxyGUI if (_updateMode) { var oldRule = Program.SqliteDbScope.GetRule(_itemRule.Type, _itemRule.ListenOn, _itemRule.ListenPort); - CmdUtil.DeleteProxy(oldRule); + PortProxyUtil.DeleteProxy(oldRule); Program.SqliteDbScope.Remove(oldRule); - CmdUtil.AddOrUpdateProxy(rule); + PortProxyUtil.AddOrUpdateProxy(rule); Program.SqliteDbScope.Add(rule); ParentWindow.UpdateListViewItem(_listViewItem, rule, 1); } else { - CmdUtil.AddOrUpdateProxy(rule); + PortProxyUtil.AddOrUpdateProxy(rule); Program.SqliteDbScope.Add(rule); ParentWindow.RefreshProxyList(); diff --git a/PortProxyGUI/~DS/CmdRunner.cs b/PortProxyGUI/~DS/CmdRunner.cs index 5d3c62e..bb75e0c 100644 --- a/PortProxyGUI/~DS/CmdRunner.cs +++ b/PortProxyGUI/~DS/CmdRunner.cs @@ -1,7 +1,9 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; namespace PortProxyGUI { + [Obsolete("The method of creating a new process is no longer used.")] public static class CmdRunner { public static string Execute(string cmd) diff --git a/PortProxyGUI/~DS/CmdUtil.cs b/PortProxyGUI/~DS/CmdUtil.cs index fbab17d..6f85802 100644 --- a/PortProxyGUI/~DS/CmdUtil.cs +++ b/PortProxyGUI/~DS/CmdUtil.cs @@ -7,6 +7,7 @@ using System.Text.RegularExpressions; namespace PortProxyGUI { + [Obsolete("The method of creating a new process is no longer used.", true)] public static class CmdUtil { private static Regex GetRegex(string fromType, string toType) diff --git a/PortProxyGUI/~DS/PortProxyUtil.cs b/PortProxyGUI/~DS/PortProxyUtil.cs new file mode 100644 index 0000000..b7fd22a --- /dev/null +++ b/PortProxyGUI/~DS/PortProxyUtil.cs @@ -0,0 +1,71 @@ +using Microsoft.Win32; +using PortProxyGUI.Data; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace PortProxyGUI +{ + public static class PortProxyUtil + { + private static InvalidOperationException InvalidPortProxyType(string type) => new($"Invalid port proxy type ({type})."); + private static readonly string[] ProxyTypes = new[] { "v4tov4", "v4tov6", "v6tov4", "v6tov6" }; + + public static Rule[] GetProxies() + { + var ruleList = new List(); + foreach (var type in ProxyTypes) + { + var keyName = $@"SYSTEM\ControlSet001\Services\PortProxy\{type}\tcp"; + var key = Registry.LocalMachine.OpenSubKey(keyName); + + if (key is not null) + { + foreach (var name in key.GetValueNames()) + { + var listenParts = name.Split('/'); + var listenOn = listenParts[0]; + if (!int.TryParse(listenParts[1], out var listenPort)) continue; + + var connectParts = key.GetValue(name).ToString().Split('/'); + var connectTo = connectParts[0]; + if (!int.TryParse(connectParts[1], out var connectPort)) continue; + + ruleList.Add(new Rule + { + Type = type, + ListenOn = listenOn, + ListenPort = listenPort, + ConnectTo = connectTo, + ConnectPort = connectPort, + }); + } + } + } + return ruleList.ToArray(); + } + + public static void AddOrUpdateProxy(Rule rule) + { + if (!ProxyTypes.Contains(rule.Type)) throw InvalidPortProxyType(rule.Type); + + var keyName = $@"SYSTEM\ControlSet001\Services\PortProxy\{rule.Type}\tcp"; + var key = Registry.LocalMachine.OpenSubKey(keyName, true); + var valueName = $"{rule.ListenOn}/{rule.ListenPort}"; + var value = $"{rule.ConnectTo}/{rule.ConnectPort}"; + + key.SetValue(valueName, value); + } + + public static void DeleteProxy(Rule rule) + { + if (!ProxyTypes.Contains(rule.Type)) throw InvalidPortProxyType(rule.Type); + + var keyName = $@"SYSTEM\ControlSet001\Services\PortProxy\{rule.Type}\tcp"; + var key = Registry.LocalMachine.OpenSubKey(keyName, true); + var valueName = $"{rule.ListenOn}/{rule.ListenPort}"; + + key.DeleteValue(valueName); + } + } +}