This commit is contained in:
zmjack 2024-06-14 01:15:48 +08:00
parent 6bee3ea985
commit 8e36f87205
26 changed files with 1712 additions and 1727 deletions

View File

@ -1,7 +1,7 @@
namespace PortProxyGUI
namespace PortProxyGUI;
partial class About
{
partial class About
{
/// <summary>
/// Required designer variable.
/// </summary>
@ -81,5 +81,4 @@
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label_version;
private System.Windows.Forms.Label label_Star;
}
}

View File

@ -2,10 +2,10 @@
using System.Diagnostics;
using System.Windows.Forms;
namespace PortProxyGUI
namespace PortProxyGUI;
public partial class About : Form
{
public partial class About : Form
{
public readonly PortProxyGUI PortProxyGUI;
public About(PortProxyGUI portProxyGUI)
@ -28,5 +28,4 @@ namespace PortProxyGUI
{
PortProxyGUI.AboutForm = null;
}
}
}

View File

@ -3,12 +3,12 @@ using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
namespace PortProxyGUI.Data
namespace PortProxyGUI.Data;
public class AppConfig
{
public class AppConfig
{
public Size MainWindowSize = new(720, 500);
public int[] PortProxyColumnWidths = new int[] { 24, 64, 140, 100, 140, 100, 100 };
public int[] PortProxyColumnWidths = [24, 64, 140, 100, 140, 100, 100];
private readonly Regex _intArrayRegex = new(@"^\[\s*(\d+)(?:\s*,\s*(\d+))*\s*\]$");
@ -43,11 +43,10 @@ namespace PortProxyGUI.Data
#if NETCOREAPP3_0_OR_GREATER
PortProxyColumnWidths = Array.Empty<int>();
#else
PortProxyColumnWidths = new int[0];
PortProxyColumnWidths = [];
#endif
}
}
}
}
}

View File

@ -5,10 +5,10 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace PortProxyGUI.Data
namespace PortProxyGUI.Data;
public class ApplicationDbScope : SqliteScope<ApplicationDbScope>
{
public class ApplicationDbScope : SqliteScope<ApplicationDbScope>
{
public static readonly string AppDbDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PortProxyGUI");
public static readonly string AppDbFile = Path.Combine(AppDbDirectory, "config.db");
@ -109,5 +109,4 @@ namespace PortProxyGUI.Data
Sql($"UPDATE Configs SET Value = {s_portProxyColumnWidths} WHERE Item = 'PortProxy' AND `Key` = 'ColumnWidths';");
}
}
}

View File

@ -1,9 +1,8 @@
namespace PortProxyGUI.Data
namespace PortProxyGUI.Data;
public class Config
{
public class Config
{
public string Item { get; set; }
public string Key { get; set; }
public string Value { get; set; }
}
}

View File

@ -1,8 +1,7 @@
namespace PortProxyGUI.Data
namespace PortProxyGUI.Data;
public class Migration
{
public class Migration
{
public string MigrationId { get; set; }
public string ProductVersion { get; set; }
}
}

View File

@ -1,8 +1,7 @@
namespace PortProxyGUI.Data
namespace PortProxyGUI.Data;
public struct MigrationKey
{
public struct MigrationKey
{
public string MigrationId { get; set; }
public string ProductVersion { get; set; }
}
}

View File

@ -5,10 +5,10 @@ using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace PortProxyGUI.Data
namespace PortProxyGUI.Data;
public class MigrationUtil
{
public class MigrationUtil
{
public ApplicationDbScope DbScope { get; private set; }
public MigrationUtil(ApplicationDbScope context)
@ -67,8 +67,8 @@ Would you like to download it now?", "Upgrade", MessageBoxButtons.YesNo, Message
public Dictionary<MigrationKey, string[]> History = new Dictionary<MigrationKey, string[]>
{
[new MigrationKey { MigrationId = "202103021542", ProductVersion = "1.1.0" }] = new[]
{
[new MigrationKey { MigrationId = "202103021542", ProductVersion = "1.1.0" }] =
[
@"CREATE TABLE rules
(
Id text PRIMARY KEY,
@ -79,16 +79,16 @@ Would you like to download it now?", "Upgrade", MessageBoxButtons.YesNo, Message
ConnectPort integer
);",
"CREATE UNIQUE INDEX IX_Rules_Type_ListenOn_ListenPort ON Rules(Type, ListenOn, ListenPort);",
},
],
[new MigrationKey { MigrationId = "202201172103", ProductVersion = "1.2.0" }] = new[]
{
[new MigrationKey { MigrationId = "202201172103", ProductVersion = "1.2.0" }] =
[
"ALTER TABLE rules ADD Note text;",
"ALTER TABLE rules ADD `Group` text;",
},
],
[new MigrationKey { MigrationId = "202202221635", ProductVersion = "1.3.0" }] = new[]
{
[new MigrationKey { MigrationId = "202202221635", ProductVersion = "1.3.0" }] =
[
"ALTER TABLE rules RENAME TO rulesOld;",
"DROP INDEX IX_Rules_Type_ListenOn_ListenPort;",
@ -106,10 +106,10 @@ Would you like to download it now?", "Upgrade", MessageBoxButtons.YesNo, Message
"INSERT INTO rules SELECT Id, Type, ListenOn, ListenPort, ConnectTo, ConnectPort, Note, `Group` FROM rulesOld;",
"DROP TABLE rulesOld;",
},
],
[new MigrationKey { MigrationId = "202303092024", ProductVersion = "1.4.0" }] = new[]
{
[new MigrationKey { MigrationId = "202303092024", ProductVersion = "1.4.0" }] =
[
@"CREATE TABLE configs (
Item text,
`Key` text,
@ -121,7 +121,6 @@ Would you like to download it now?", "Upgrade", MessageBoxButtons.YesNo, Message
"INSERT INTO configs ( Item, `Key`, Value ) VALUES ( 'MainWindow', 'Width', '720' );",
"INSERT INTO configs ( Item, `Key`, Value ) VALUES ( 'MainWindow', 'Height', '500' );",
"INSERT INTO configs ( Item, `Key`, Value ) VALUES ( 'PortProxy', 'ColumnWidths', '[24, 64, 140, 100, 140, 100, 100]' );",
},
],
};
}
}

View File

@ -1,9 +1,9 @@
using System;
namespace PortProxyGUI.Data
namespace PortProxyGUI.Data;
public class Rule : IEquatable<Rule>
{
public class Rule : IEquatable<Rule>
{
public string Id { get; set; }
public string Type { get; set; }
@ -70,5 +70,4 @@ namespace PortProxyGUI.Data
{
return Equals(obj as Rule);
}
}
}

View File

@ -1,13 +1,12 @@
using System;
namespace PortProxyGUI.Native
namespace PortProxyGUI.Native;
[Flags]
internal enum GenericRights : uint
{
[Flags]
internal enum GenericRights : uint
{
GENERIC_READ = 0x80000000,
GENERIC_WRITE = 0x40000000,
GENERIC_EXECUTE = 0x20000000,
GENERIC_ALL = 0x10000000,
}
}

View File

@ -1,10 +1,10 @@
using System;
using System.Runtime.InteropServices;
namespace PortProxyGUI.Native
namespace PortProxyGUI.Native;
internal class NativeMethods
{
internal class NativeMethods
{
[DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);
@ -29,5 +29,4 @@ namespace PortProxyGUI.Native
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors);
}
}

View File

@ -1,7 +1,7 @@
namespace PortProxyGUI.Native
namespace PortProxyGUI.Native;
internal enum ScmRights : uint
{
internal enum ScmRights : uint
{
SC_MANAGER_CONNECT = 0x0001,
SC_MANAGER_CREATE_SERVICE = 0x0002,
SC_MANAGER_ENUMERATE_SERVICE = 0x0004,
@ -17,5 +17,4 @@
| SC_MANAGER_LOCK
| SC_MANAGER_QUERY_LOCK_STATUS
| SC_MANAGER_MODIFY_BOOT_CONFIG
}
}

View File

@ -1,10 +1,9 @@
using System;
namespace PortProxyGUI.Native
namespace PortProxyGUI.Native;
[Flags]
internal enum ServiceControls : uint
{
[Flags]
internal enum ServiceControls : uint
{
SERVICE_CONTROL_PARAMCHANGE = 0x00000006,
}
}

View File

@ -1,10 +1,10 @@
using System;
namespace PortProxyGUI.Native
namespace PortProxyGUI.Native;
[Flags]
internal enum ServiceRights : uint
{
[Flags]
internal enum ServiceRights : uint
{
SERVICE_QUERY_CONFIG = 0x0001,
SERVICE_CHANGE_CONFIG = 0x0002,
SERVICE_QUERY_STATUS = 0x0004,
@ -25,5 +25,4 @@ namespace PortProxyGUI.Native
| SERVICE_PAUSE_CONTINUE
| SERVICE_INTERROGATE
| SERVICE_USER_DEFINED_CONTROL
}
}

View File

@ -1,7 +1,7 @@
namespace PortProxyGUI.Native
namespace PortProxyGUI.Native;
internal enum ServiceState : int
{
internal enum ServiceState : int
{
SERVICE_STOPPED = 0x00000001,
SERVICE_START_PENDING = 0x00000002,
SERVICE_STOP_PENDING = 0x00000003,
@ -9,5 +9,4 @@
SERVICE_CONTINUE_PENDING = 0x00000005,
SERVICE_PAUSE_PENDING = 0x00000006,
SERVICE_PAUSED = 0x00000007,
}
}

View File

@ -1,10 +1,10 @@
using System.Runtime.InteropServices;
namespace PortProxyGUI.Native
namespace PortProxyGUI.Native;
[StructLayout(LayoutKind.Sequential)]
internal struct ServiceStatus
{
[StructLayout(LayoutKind.Sequential)]
internal struct ServiceStatus
{
public uint dwServiceType;
public ServiceState dwCurrentState;
public uint dwControlsAccepted;
@ -12,5 +12,4 @@ namespace PortProxyGUI.Native
public uint dwServiceSpecificExitCode;
public uint dwCheckPoint;
public uint dwWaitHint;
}
}

View File

@ -1,7 +1,6 @@
namespace PortProxyGUI.Native
namespace PortProxyGUI.Native;
internal enum StandardRights : uint
{
internal enum StandardRights : uint
{
STANDARD_RIGHTS_REQUIRED = 0x000F0000,
}
}

View File

@ -1,7 +1,7 @@
namespace PortProxyGUI
namespace PortProxyGUI;
partial class PortProxyGUI
{
partial class PortProxyGUI
{
/// <summary>
/// Required designer variable.
/// </summary>
@ -296,6 +296,5 @@
private System.Windows.Forms.StatusStrip statusStrip_Footer;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel_Status;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel_ServiceNotRunning;
}
}

View File

@ -9,10 +9,10 @@ using System.Linq;
using System.Windows.Forms;
using static System.Windows.Forms.ListViewItem;
namespace PortProxyGUI
namespace PortProxyGUI;
public partial class PortProxyGUI : Form
{
public partial class PortProxyGUI : Form
{
private readonly ListViewColumnSorter lvwColumnSorter = new ListViewColumnSorter();
public SetProxy SetProxyForm;
@ -87,7 +87,7 @@ namespace PortProxyGUI
try
{
var rule = ParseRule(item);
PortPorxyUtil.AddOrUpdateProxy(rule);
Util.AddOrUpdateProxy(rule);
}
catch (NotSupportedException ex)
{
@ -95,7 +95,7 @@ namespace PortProxyGUI
return;
}
}
PortPorxyUtil.ParamChange();
Util.ParamChange();
}
private void DisableSelectedProxies()
@ -108,7 +108,7 @@ namespace PortProxyGUI
try
{
var rule = ParseRule(item);
PortPorxyUtil.DeleteProxy(rule);
Util.DeleteProxy(rule);
}
catch (NotSupportedException ex)
{
@ -116,7 +116,7 @@ namespace PortProxyGUI
return;
}
}
PortPorxyUtil.ParamChange();
Util.ParamChange();
}
private void DeleteSelectedProxies()
@ -199,7 +199,7 @@ namespace PortProxyGUI
public void RefreshProxyList()
{
var proxies = PortPorxyUtil.GetProxies();
var proxies = Util.GetProxies();
var rules = Program.Database.Rules.ToArray();
foreach (var proxy in proxies)
{
@ -223,7 +223,7 @@ namespace PortProxyGUI
InitProxyItems(rules, proxies);
// CheckServiceStatus
toolStripStatusLabel_ServiceNotRunning.Visible = !PortPorxyUtil.IsServiceRunning();
toolStripStatusLabel_ServiceNotRunning.Visible = !Util.IsServiceRunning();
}
private void contextMenuStrip_RightClick_MouseClick(object sender, MouseEventArgs e)
@ -402,8 +402,7 @@ namespace PortProxyGUI
private void toolStripStatusLabel_ServiceNotRunning_Click(object sender, EventArgs e)
{
PortPorxyUtil.StartService();
Util.StartService();
toolStripStatusLabel_ServiceNotRunning.Visible = false;
}
}
}

View File

@ -1,18 +1,28 @@
using PortProxyGUI.Data;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace PortProxyGUI
namespace PortProxyGUI;
static class Program
{
static class Program
private static string GetPath(params string[] pathes)
{
public static readonly ApplicationDbScope Database = ApplicationDbScope.FromFile(
Path.Combine(
Path.Combine(
if (!pathes.Any()) return string.Empty;
#if NET6_0_OR_GREATER || NET451_OR_GREATER
return Path.Combine(pathes);
#else
return pathes.Aggregate(Path.Combine);
#endif
}
public static ApplicationDbScope Database { get; } = ApplicationDbScope.FromFile(GetPath(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"PortProxyGUI"
), "config.db"
"PortProxyGUI",
"config.db"
));
/// <summary>
@ -37,5 +47,4 @@ namespace PortProxyGUI
Application.Run(new PortProxyGUI());
}
}
}

View File

@ -1,7 +1,7 @@
namespace PortProxyGUI
namespace PortProxyGUI;
partial class SetProxy
{
partial class SetProxy
{
/// <summary>
/// Required designer variable.
/// </summary>
@ -180,5 +180,4 @@
private System.Windows.Forms.TextBox textBox_Comment;
private System.Windows.Forms.Label label_Group;
private System.Windows.Forms.ComboBox comboBox_Group;
}
}

View File

@ -6,10 +6,10 @@ using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace PortProxyGUI
namespace PortProxyGUI;
public partial class SetProxy : Form
{
public partial class SetProxy : Form
{
public readonly PortProxyGUI ParentWindow;
private string AutoTypeString { get; }
@ -115,22 +115,22 @@ namespace PortProxyGUI
if (_updateMode)
{
var oldRule = Program.Database.GetRule(_itemRule.Type, _itemRule.ListenOn, _itemRule.ListenPort);
PortPorxyUtil.DeleteProxy(oldRule);
Util.DeleteProxy(oldRule);
Program.Database.Remove(oldRule);
PortPorxyUtil.AddOrUpdateProxy(rule);
Util.AddOrUpdateProxy(rule);
Program.Database.Add(rule);
ParentWindow.UpdateListViewItem(_listViewItem, rule, 1);
}
else
{
PortPorxyUtil.AddOrUpdateProxy(rule);
Util.AddOrUpdateProxy(rule);
Program.Database.Add(rule);
ParentWindow.RefreshProxyList();
}
PortPorxyUtil.ParamChange();
Util.ParamChange();
Close();
}
@ -146,5 +146,4 @@ namespace PortProxyGUI
ParentWindow.SetProxyForm = null;
}
}
}

View File

@ -1,10 +1,10 @@
using System.Collections;
using System.Windows.Forms;
namespace PortProxyGUI.UI
namespace PortProxyGUI.UI;
public class ListViewColumnSorter : IComparer
{
public class ListViewColumnSorter : IComparer
{
/// <summary>
/// Specifies the column to be sorted
/// </summary>
@ -107,5 +107,4 @@ namespace PortProxyGUI.UI
}
}
}
}

View File

@ -1,15 +1,14 @@
using PortProxyGUI.Native;
using System;
namespace PortProxyGUI.Utils
namespace PortProxyGUI.Utils;
internal class DnsUtil
{
internal class DnsUtil
{
public static void FlushCache()
{
var status = NativeMethods.DnsFlushResolverCache();
if (status == 0) throw new InvalidOperationException("Flush DNS Cache failed.");
}
}
}

View File

@ -1,149 +0,0 @@
using Microsoft.Win32;
using PortProxyGUI.Data;
using PortProxyGUI.Native;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PortProxyGUI.Utils
{
public static class PortPorxyUtil
{
internal static readonly string ServiceName = "iphlpsvc";
internal static readonly string ServiceFriendlyName = "IP Helper";
private static InvalidOperationException InvalidPortProxyType(string type) => new($"Invalid port proxy type ({type}).");
private static readonly string[] ProxyTypes = new[] { "v4tov4", "v4tov6", "v6tov4", "v6tov6" };
private static string GetKeyName(string type)
{
return $@"SYSTEM\CurrentControlSet\Services\PortProxy\{type}\tcp";
}
public static Rule[] GetProxies()
{
var ruleList = new List<Rule>();
foreach (var type in ProxyTypes)
{
var keyName = GetKeyName(type);
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)
{
// $"netsh interface portproxy add {rule.Type} listenaddress={rule.ListenOn} listenport={rule.ListenPort} connectaddress={rule.ConnectTo} connectport={rule.ConnectPort}"
if (!ProxyTypes.Contains(rule.Type)) throw InvalidPortProxyType(rule.Type);
var keyName = GetKeyName(rule.Type);
var key = Registry.LocalMachine.OpenSubKey(keyName, true);
var name = $"{rule.ListenOn}/{rule.ListenPort}";
var value = $"{rule.ConnectTo}/{rule.ConnectPort}";
if (key is null) Registry.LocalMachine.CreateSubKey(keyName);
key = Registry.LocalMachine.OpenSubKey(keyName, true);
key?.SetValue(name, value);
}
public static void DeleteProxy(Rule rule)
{
// $"netsh interface portproxy delete {rule.Type} listenaddress={rule.ListenOn} listenport={rule.ListenPort}"
if (!ProxyTypes.Contains(rule.Type)) throw InvalidPortProxyType(rule.Type);
var keyName = GetKeyName(rule.Type);
var key = Registry.LocalMachine.OpenSubKey(keyName, true);
var name = $"{rule.ListenOn}/{rule.ListenPort}";
try
{
key?.DeleteValue(name);
}
catch { }
}
public static bool IsServiceRunning()
{
var hManager = NativeMethods.OpenSCManager(null, null, (uint)GenericRights.GENERIC_READ);
if (hManager == IntPtr.Zero) throw new InvalidOperationException("Open SC Manager failed.");
var hService = NativeMethods.OpenService(hManager, ServiceName, ServiceRights.SERVICE_QUERY_STATUS);
if (hService == IntPtr.Zero)
{
NativeMethods.CloseServiceHandle(hManager);
throw new InvalidOperationException($"Open Service ({ServiceName}) failed.");
}
var status = new ServiceStatus();
NativeMethods.QueryServiceStatus(hService, ref status);
NativeMethods.CloseServiceHandle(hService);
NativeMethods.CloseServiceHandle(hManager);
return status.dwCurrentState == ServiceState.SERVICE_RUNNING;
}
public static void StartService()
{
var hManager = NativeMethods.OpenSCManager(null, null, (uint)GenericRights.GENERIC_READ | (uint)ScmRights.SC_MANAGER_CONNECT);
if (hManager == IntPtr.Zero) throw new InvalidOperationException("Open SC Manager failed.");
var hService = NativeMethods.OpenService(hManager, ServiceName, ServiceRights.SERVICE_START);
if (hService == IntPtr.Zero)
{
NativeMethods.CloseServiceHandle(hManager);
throw new InvalidOperationException($"Open Service ({ServiceName}) failed.");
}
NativeMethods.StartService(hService, 0, null);
NativeMethods.CloseServiceHandle(hService);
NativeMethods.CloseServiceHandle(hManager);
}
public static void ParamChange()
{
var hManager = NativeMethods.OpenSCManager(null, null, (uint)GenericRights.GENERIC_READ);
if (hManager == IntPtr.Zero) throw new InvalidOperationException("Open SC Manager failed.");
var hService = NativeMethods.OpenService(hManager, ServiceName, ServiceRights.SERVICE_PAUSE_CONTINUE);
if (hService == IntPtr.Zero)
{
NativeMethods.CloseServiceHandle(hManager);
throw new InvalidOperationException($"Open Service ({ServiceName}) failed.");
}
var status = new ServiceStatus();
NativeMethods.ControlService(hService, ServiceControls.SERVICE_CONTROL_PARAMCHANGE, ref status);
NativeMethods.CloseServiceHandle(hService);
NativeMethods.CloseServiceHandle(hManager);
}
}
}

148
PortProxyGUI/Utils/Util.cs Normal file
View File

@ -0,0 +1,148 @@
using Microsoft.Win32;
using PortProxyGUI.Data;
using PortProxyGUI.Native;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PortProxyGUI.Utils;
public static class Util
{
internal static readonly string ServiceName = "iphlpsvc";
internal static readonly string ServiceFriendlyName = "IP Helper";
private static InvalidOperationException InvalidPortProxyType(string type) => new($"Invalid port proxy type ({type}).");
private static readonly string[] ProxyTypes = ["v4tov4", "v4tov6", "v6tov4", "v6tov6"];
private static string GetKeyName(string type)
{
return $@"SYSTEM\CurrentControlSet\Services\PortProxy\{type}\tcp";
}
public static Rule[] GetProxies()
{
var ruleList = new List<Rule>();
foreach (var type in ProxyTypes)
{
var keyName = GetKeyName(type);
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];
}
public static void AddOrUpdateProxy(Rule rule)
{
// $"netsh interface portproxy add {rule.Type} listenaddress={rule.ListenOn} listenport={rule.ListenPort} connectaddress={rule.ConnectTo} connectport={rule.ConnectPort}"
if (!ProxyTypes.Contains(rule.Type)) throw InvalidPortProxyType(rule.Type);
var keyName = GetKeyName(rule.Type);
var key = Registry.LocalMachine.OpenSubKey(keyName, true);
var name = $"{rule.ListenOn}/{rule.ListenPort}";
var value = $"{rule.ConnectTo}/{rule.ConnectPort}";
if (key is null) Registry.LocalMachine.CreateSubKey(keyName);
key = Registry.LocalMachine.OpenSubKey(keyName, true);
key?.SetValue(name, value);
}
public static void DeleteProxy(Rule rule)
{
// $"netsh interface portproxy delete {rule.Type} listenaddress={rule.ListenOn} listenport={rule.ListenPort}"
if (!ProxyTypes.Contains(rule.Type)) throw InvalidPortProxyType(rule.Type);
var keyName = GetKeyName(rule.Type);
var key = Registry.LocalMachine.OpenSubKey(keyName, true);
var name = $"{rule.ListenOn}/{rule.ListenPort}";
try
{
key?.DeleteValue(name);
}
catch { }
}
public static bool IsServiceRunning()
{
var hManager = NativeMethods.OpenSCManager(null, null, (uint)GenericRights.GENERIC_READ);
if (hManager == IntPtr.Zero) throw new InvalidOperationException("Open SC Manager failed.");
var hService = NativeMethods.OpenService(hManager, ServiceName, ServiceRights.SERVICE_QUERY_STATUS);
if (hService == IntPtr.Zero)
{
NativeMethods.CloseServiceHandle(hManager);
throw new InvalidOperationException($"Open Service ({ServiceName}) failed.");
}
var status = new ServiceStatus();
NativeMethods.QueryServiceStatus(hService, ref status);
NativeMethods.CloseServiceHandle(hService);
NativeMethods.CloseServiceHandle(hManager);
return status.dwCurrentState == ServiceState.SERVICE_RUNNING;
}
public static void StartService()
{
var hManager = NativeMethods.OpenSCManager(null, null, (uint)GenericRights.GENERIC_READ | (uint)ScmRights.SC_MANAGER_CONNECT);
if (hManager == IntPtr.Zero) throw new InvalidOperationException("Open SC Manager failed.");
var hService = NativeMethods.OpenService(hManager, ServiceName, ServiceRights.SERVICE_START);
if (hService == IntPtr.Zero)
{
NativeMethods.CloseServiceHandle(hManager);
throw new InvalidOperationException($"Open Service ({ServiceName}) failed.");
}
NativeMethods.StartService(hService, 0, null);
NativeMethods.CloseServiceHandle(hService);
NativeMethods.CloseServiceHandle(hManager);
}
public static void ParamChange()
{
var hManager = NativeMethods.OpenSCManager(null, null, (uint)GenericRights.GENERIC_READ);
if (hManager == IntPtr.Zero) throw new InvalidOperationException("Open SC Manager failed.");
var hService = NativeMethods.OpenService(hManager, ServiceName, ServiceRights.SERVICE_PAUSE_CONTINUE);
if (hService == IntPtr.Zero)
{
NativeMethods.CloseServiceHandle(hManager);
throw new InvalidOperationException($"Open Service ({ServiceName}) failed.");
}
var status = new ServiceStatus();
NativeMethods.ControlService(hService, ServiceControls.SERVICE_CONTROL_PARAMCHANGE, ref status);
NativeMethods.CloseServiceHandle(hService);
NativeMethods.CloseServiceHandle(hManager);
}
}