PortProxyGUI.1.3.3

This commit is contained in:
zmjack 2023-03-08 21:07:56 +08:00
parent 1756d32c79
commit e8da3c5055
6 changed files with 82 additions and 8 deletions

View File

@ -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)
{

View File

@ -42,4 +42,4 @@
<PackageReference Include="SQLib.Sqlite" Version="0.8.6" />
</ItemGroup>
</Project>
</Project>

View File

@ -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();

View File

@ -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)

View File

@ -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)

View File

@ -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<Rule>();
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);
}
}
}