diff --git a/PortProxyGUI/About.cs b/PortProxyGUI/About.cs index 709bd99..f2c3cd7 100644 --- a/PortProxyGUI/About.cs +++ b/PortProxyGUI/About.cs @@ -1,7 +1,6 @@ using PortProxyGUI.Utils; using System; using System.Diagnostics; -using System.Drawing; using System.Windows.Forms; namespace PortProxyGUI diff --git a/PortProxyGUI/Native/GenericRights.cs b/PortProxyGUI/Native/GenericRights.cs index fdaad98..233acf6 100644 --- a/PortProxyGUI/Native/GenericRights.cs +++ b/PortProxyGUI/Native/GenericRights.cs @@ -3,8 +3,11 @@ namespace PortProxyGUI.Native { [Flags] - public enum GenericRights : uint + internal enum GenericRights : uint { GENERIC_READ = 0x80000000, + GENERIC_WRITE = 0x40000000, + GENERIC_EXECUTE = 0x20000000, + GENERIC_ALL = 0x10000000, } } diff --git a/PortProxyGUI/Native/NativeMethods.cs b/PortProxyGUI/Native/NativeMethods.cs index 7f442f1..bd1d6fd 100644 --- a/PortProxyGUI/Native/NativeMethods.cs +++ b/PortProxyGUI/Native/NativeMethods.cs @@ -6,17 +6,28 @@ namespace PortProxyGUI.Native internal class NativeMethods { [DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] - public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess); + internal static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess); [DllImport("advapi32.dll", EntryPoint = "OpenServiceW", SetLastError = true, CharSet = CharSet.Unicode)] - public static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, ServiceRights dwDesiredAccess); + internal static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, ServiceRights dwDesiredAccess); + + [DllImport("advapi32.dll", EntryPoint = "QueryServiceStatus", CharSet = CharSet.Auto)] + internal static extern bool QueryServiceStatus(IntPtr hService, ref ServiceStatus dwServiceStatus); [DllImport("advapi32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool ControlService(IntPtr hService, ServiceControls dwControl, ref ServiceStatus lpServiceStatus); + internal static extern bool ControlService(IntPtr hService, ServiceControls dwControl, ref ServiceStatus lpServiceStatus); [DllImport("advapi32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CloseServiceHandle(IntPtr hSCObject); + internal static extern bool CloseServiceHandle(IntPtr hSCObject); + + [DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCache")] + internal static extern uint DnsFlushResolverCache(); + + [DllImport("advapi32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool StartService(IntPtr hService, int dwNumServiceArgs, string[] lpServiceArgVectors); + } } diff --git a/PortProxyGUI/Native/ScmRights.cs b/PortProxyGUI/Native/ScmRights.cs new file mode 100644 index 0000000..6a21bc6 --- /dev/null +++ b/PortProxyGUI/Native/ScmRights.cs @@ -0,0 +1,21 @@ +namespace PortProxyGUI.Native +{ + internal enum ScmRights : uint + { + SC_MANAGER_CONNECT = 0x0001, + SC_MANAGER_CREATE_SERVICE = 0x0002, + SC_MANAGER_ENUMERATE_SERVICE = 0x0004, + SC_MANAGER_LOCK = 0x0008, + SC_MANAGER_QUERY_LOCK_STATUS = 0x0010, + SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020, + + SC_MANAGER_ALL_ACCESS = + StandardRights.STANDARD_RIGHTS_REQUIRED + | SC_MANAGER_CONNECT + | SC_MANAGER_CREATE_SERVICE + | SC_MANAGER_ENUMERATE_SERVICE + | SC_MANAGER_LOCK + | SC_MANAGER_QUERY_LOCK_STATUS + | SC_MANAGER_MODIFY_BOOT_CONFIG + } +} diff --git a/PortProxyGUI/Native/ServiceControls.cs b/PortProxyGUI/Native/ServiceControls.cs index a4b2b32..05c050d 100644 --- a/PortProxyGUI/Native/ServiceControls.cs +++ b/PortProxyGUI/Native/ServiceControls.cs @@ -3,7 +3,7 @@ namespace PortProxyGUI.Native { [Flags] - public enum ServiceControls : uint + internal enum ServiceControls : uint { SERVICE_CONTROL_PARAMCHANGE = 0x00000006, } diff --git a/PortProxyGUI/Native/ServiceRights.cs b/PortProxyGUI/Native/ServiceRights.cs index 104a09d..df0b56c 100644 --- a/PortProxyGUI/Native/ServiceRights.cs +++ b/PortProxyGUI/Native/ServiceRights.cs @@ -3,8 +3,27 @@ namespace PortProxyGUI.Native { [Flags] - public enum ServiceRights : uint + internal enum ServiceRights : uint { + SERVICE_QUERY_CONFIG = 0x0001, + SERVICE_CHANGE_CONFIG = 0x0002, + SERVICE_QUERY_STATUS = 0x0004, + SERVICE_ENUMERATE_DEPENDENTS = 0x0008, + SERVICE_START = 0x0010, + SERVICE_STOP = 0x0020, SERVICE_PAUSE_CONTINUE = 0x0040, + SERVICE_INTERROGATE = 0x0080, + SERVICE_USER_DEFINED_CONTROL = 0x0100, + + SERVICE_ALL_ACCESS = + SERVICE_QUERY_CONFIG + | SERVICE_CHANGE_CONFIG + | SERVICE_QUERY_STATUS + | SERVICE_ENUMERATE_DEPENDENTS + | SERVICE_START + | SERVICE_STOP + | SERVICE_PAUSE_CONTINUE + | SERVICE_INTERROGATE + | SERVICE_USER_DEFINED_CONTROL } } diff --git a/PortProxyGUI/Native/ServiceState.cs b/PortProxyGUI/Native/ServiceState.cs new file mode 100644 index 0000000..a7b0596 --- /dev/null +++ b/PortProxyGUI/Native/ServiceState.cs @@ -0,0 +1,13 @@ +namespace PortProxyGUI.Native +{ + internal enum ServiceState : int + { + SERVICE_STOPPED = 0x00000001, + SERVICE_START_PENDING = 0x00000002, + SERVICE_STOP_PENDING = 0x00000003, + SERVICE_RUNNING = 0x00000004, + SERVICE_CONTINUE_PENDING = 0x00000005, + SERVICE_PAUSE_PENDING = 0x00000006, + SERVICE_PAUSED = 0x00000007, + } +} diff --git a/PortProxyGUI/Native/ServiceStatus.cs b/PortProxyGUI/Native/ServiceStatus.cs index 3ad590f..f4803c1 100644 --- a/PortProxyGUI/Native/ServiceStatus.cs +++ b/PortProxyGUI/Native/ServiceStatus.cs @@ -3,10 +3,10 @@ namespace PortProxyGUI.Native { [StructLayout(LayoutKind.Sequential)] - public struct ServiceStatus + internal struct ServiceStatus { public uint dwServiceType; - public uint dwCurrentState; + public ServiceState dwCurrentState; public uint dwControlsAccepted; public uint dwWin32ExitCode; public uint dwServiceSpecificExitCode; diff --git a/PortProxyGUI/Native/StandardRights.cs b/PortProxyGUI/Native/StandardRights.cs new file mode 100644 index 0000000..9d17948 --- /dev/null +++ b/PortProxyGUI/Native/StandardRights.cs @@ -0,0 +1,7 @@ +namespace PortProxyGUI.Native +{ + internal enum StandardRights : uint + { + STANDARD_RIGHTS_REQUIRED = 0x000F0000, + } +} diff --git a/PortProxyGUI/PortProxyGUI.Designer.cs b/PortProxyGUI/PortProxyGUI.Designer.cs index b405866..b1c18ad 100644 --- a/PortProxyGUI/PortProxyGUI.Designer.cs +++ b/PortProxyGUI/PortProxyGUI.Designer.cs @@ -52,14 +52,18 @@ toolStripMenuItem_More = new System.Windows.Forms.ToolStripMenuItem(); toolStripMenuItem_Import = new System.Windows.Forms.ToolStripMenuItem(); toolStripMenuItem_Export = new System.Windows.Forms.ToolStripMenuItem(); + toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator(); + toolStripMenuItem_ResetWindowSize = new System.Windows.Forms.ToolStripMenuItem(); toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator(); toolStripMenuItem_About = new System.Windows.Forms.ToolStripMenuItem(); imageListProxies = new System.Windows.Forms.ImageList(components); saveFileDialog_Export = new System.Windows.Forms.SaveFileDialog(); openFileDialog_Import = new System.Windows.Forms.OpenFileDialog(); - toolStripMenuItem_ResetWindowSize = new System.Windows.Forms.ToolStripMenuItem(); - toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator(); + statusStrip_Footer = new System.Windows.Forms.StatusStrip(); + toolStripStatusLabel_Status = new System.Windows.Forms.ToolStripStatusLabel(); + toolStripStatusLabel_ServiceNotRunning = new System.Windows.Forms.ToolStripStatusLabel(); contextMenuStrip_RightClick.SuspendLayout(); + statusStrip_Footer.SuspendLayout(); SuspendLayout(); // // listViewProxies @@ -69,7 +73,6 @@ listViewProxies.ContextMenuStrip = contextMenuStrip_RightClick; resources.ApplyResources(listViewProxies, "listViewProxies"); listViewProxies.FullRowSelect = true; - listViewProxies.HideSelection = false; listViewProxies.Name = "listViewProxies"; listViewProxies.SmallImageList = imageListProxies; listViewProxies.UseCompatibleStateImageBehavior = false; @@ -185,6 +188,17 @@ resources.ApplyResources(toolStripMenuItem_Export, "toolStripMenuItem_Export"); toolStripMenuItem_Export.Click += toolStripMenuItem_Export_Click; // + // toolStripSeparator5 + // + toolStripSeparator5.Name = "toolStripSeparator5"; + resources.ApplyResources(toolStripSeparator5, "toolStripSeparator5"); + // + // toolStripMenuItem_ResetWindowSize + // + toolStripMenuItem_ResetWindowSize.Name = "toolStripMenuItem_ResetWindowSize"; + resources.ApplyResources(toolStripMenuItem_ResetWindowSize, "toolStripMenuItem_ResetWindowSize"); + toolStripMenuItem_ResetWindowSize.Click += toolStripMenuItem_ResetWindowSize_Click; + // // toolStripSeparator4 // toolStripSeparator4.Name = "toolStripSeparator4"; @@ -212,21 +226,30 @@ openFileDialog_Import.FileName = "openFileDialog1"; resources.ApplyResources(openFileDialog_Import, "openFileDialog_Import"); // - // toolStripMenuItem_ResetWindowSize + // statusStrip_Footer // - toolStripMenuItem_ResetWindowSize.Name = "toolStripMenuItem_ResetWindowSize"; - resources.ApplyResources(toolStripMenuItem_ResetWindowSize, "toolStripMenuItem_ResetWindowSize"); - toolStripMenuItem_ResetWindowSize.Click += toolStripMenuItem_ResetWindowSize_Click; + statusStrip_Footer.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { toolStripStatusLabel_Status, toolStripStatusLabel_ServiceNotRunning }); + resources.ApplyResources(statusStrip_Footer, "statusStrip_Footer"); + statusStrip_Footer.Name = "statusStrip_Footer"; // - // toolStripSeparator5 + // toolStripStatusLabel_Status // - toolStripSeparator5.Name = "toolStripSeparator5"; - resources.ApplyResources(toolStripSeparator5, "toolStripSeparator5"); + toolStripStatusLabel_Status.Name = "toolStripStatusLabel_Status"; + resources.ApplyResources(toolStripStatusLabel_Status, "toolStripStatusLabel_Status"); + // + // toolStripStatusLabel_ServiceNotRunning + // + toolStripStatusLabel_ServiceNotRunning.IsLink = true; + toolStripStatusLabel_ServiceNotRunning.LinkColor = System.Drawing.Color.Red; + toolStripStatusLabel_ServiceNotRunning.Name = "toolStripStatusLabel_ServiceNotRunning"; + resources.ApplyResources(toolStripStatusLabel_ServiceNotRunning, "toolStripStatusLabel_ServiceNotRunning"); + toolStripStatusLabel_ServiceNotRunning.Click += toolStripStatusLabel_ServiceNotRunning_Click; // // PortProxyGUI // resources.ApplyResources(this, "$this"); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + Controls.Add(statusStrip_Footer); Controls.Add(listViewProxies); Name = "PortProxyGUI"; FormClosing += PortProxyGUI_FormClosing; @@ -234,7 +257,10 @@ Shown += PortProxyGUI_Shown; Resize += PortProxyGUI_Resize; contextMenuStrip_RightClick.ResumeLayout(false); + statusStrip_Footer.ResumeLayout(false); + statusStrip_Footer.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -267,6 +293,9 @@ private System.Windows.Forms.OpenFileDialog openFileDialog_Import; private System.Windows.Forms.ToolStripSeparator toolStripSeparator5; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem_ResetWindowSize; + private System.Windows.Forms.StatusStrip statusStrip_Footer; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel_Status; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel_ServiceNotRunning; } } diff --git a/PortProxyGUI/PortProxyGUI.cs b/PortProxyGUI/PortProxyGUI.cs index d7a5e6a..eea743b 100644 --- a/PortProxyGUI/PortProxyGUI.cs +++ b/PortProxyGUI/PortProxyGUI.cs @@ -4,7 +4,6 @@ using PortProxyGUI.UI; using PortProxyGUI.Utils; using System; using System.Data; -using System.Drawing; using System.IO; using System.Linq; using System.Windows.Forms; @@ -224,6 +223,9 @@ namespace PortProxyGUI rules = Program.Database.Rules.ToArray(); InitProxyGroups(rules); InitProxyItems(rules, proxies); + + // CheckServiceStatus + toolStripStatusLabel_ServiceNotRunning.Visible = !PortPorxyUtil.IsServiceRunning(); } private void contextMenuStrip_RightClick_MouseClick(object sender, MouseEventArgs e) @@ -239,25 +241,30 @@ namespace PortProxyGUI case ToolStripMenuItem item when item == toolStripMenuItem_Disable: DisableSelectedProxies(); break; case ToolStripMenuItem item when item == toolStripMenuItem_New: - if (SetProxyForm == null) SetProxyForm = new SetProxy(this); + SetProxyForm ??= new SetProxy(this); SetProxyForm.UseNormalMode(); SetProxyForm.ShowDialog(); break; case ToolStripMenuItem item when item == toolStripMenuItem_Modify: - if (SetProxyForm == null) SetProxyForm = new SetProxy(this); + SetProxyForm ??= new SetProxy(this); SetProxyForUpdate(SetProxyForm); SetProxyForm.ShowDialog(); break; case ToolStripMenuItem item when item == toolStripMenuItem_Refresh: RefreshProxyList(); - break; - case ToolStripMenuItem item when item == toolStripMenuItem_FlushDnsCache: - DnsUtil.FlushCache(); + toolStripStatusLabel_Status.Text = $"{DateTime.Now} : Refreshed."; break; - case ToolStripMenuItem item when item == toolStripMenuItem_Delete: DeleteSelectedProxies(); break; + case ToolStripMenuItem item when item == toolStripMenuItem_FlushDnsCache: + DnsUtil.FlushCache(); + toolStripStatusLabel_Status.Text = $"{DateTime.Now} : DNS cache cleared."; + break; + + case ToolStripMenuItem item when item == toolStripMenuItem_Delete: + DeleteSelectedProxies(); + break; case ToolStripMenuItem item when item == toolStripMenuItem_About: if (AboutForm == null) @@ -290,7 +297,7 @@ namespace PortProxyGUI var selectAny = listView.SelectedItems.OfType().Any(); if (selectAny) { - if (SetProxyForm == null) SetProxyForm = new SetProxy(this); + SetProxyForm ??= new SetProxy(this); SetProxyForUpdate(SetProxyForm); SetProxyForm.ShowDialog(); } @@ -394,5 +401,11 @@ namespace PortProxyGUI AppConfig = new AppConfig(); ResetWindowSize(); } + + private void toolStripStatusLabel_ServiceNotRunning_Click(object sender, EventArgs e) + { + PortPorxyUtil.StartService(); + toolStripStatusLabel_ServiceNotRunning.Visible = false; + } } } diff --git a/PortProxyGUI/PortProxyGUI.csproj b/PortProxyGUI/PortProxyGUI.csproj index 9f69eb0..fda4406 100644 --- a/PortProxyGUI/PortProxyGUI.csproj +++ b/PortProxyGUI/PortProxyGUI.csproj @@ -14,7 +14,7 @@ portproxy TCP/IP redirector LICENSE.md Copyright © nstandard.net 2020 - 1.4.0 + 1.4.1 icon.ico Microsoft Sans Serif, 8pt PPGUI diff --git a/PortProxyGUI/PortProxyGUI.resx b/PortProxyGUI/PortProxyGUI.resx index 822a02e..a229e02 100644 --- a/PortProxyGUI/PortProxyGUI.resx +++ b/PortProxyGUI/PortProxyGUI.resx @@ -164,6 +164,94 @@ 17, 17 + + 175, 226 + + + contextMenuStrip_RightClick + + + System.Windows.Forms.ContextMenuStrip, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + Fill + + + Microsoft YaHei UI, 9pt + + + 0, 0 + + + 4, 3, 4, 3 + + + 704, 461 + + + 239, 17 + + + + AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w + LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 + ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAADc + CAAAAk1TRnQBSQFMAgEBAgEAAdABAQHYAQEBEAEAARABAAT/AQkBEAj/AUIBTQE2AQQGAAE2AQQCAAEo + AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA + AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEA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G6Af8iAAGUDBcBlAIAAboMlQG6IgAOFwIADpUiAAQXARYB9AL/AfQBFgQXAgAOlSIA + AxcBFgH/Ab0BFgGUAfQB/wEWAxcCAAWVArsHlSIAAxcB9AG9AhcBlAH/AvQDFwIABJUBugL/AcEGlSIA + AxcB/wEWARcBlAH/ApQB/wMXAgADlQG6Af8BwQG6Af8BwQWVIgADFwH/ApQB/wGUARcBFgH/AxcCAAOV + Af8BwQKVAboB/wHBBJUiAAMXAvQB/wGUAhcBvQH0AxcCAAiVAboB/wHBA5UiAAMXARYB/wH0AZQBFgG9 + Af8BFgMXAgAJlQG6Af8DlSIABBcBFgH0Av8B9AEWBBcB/wEADpUB/yEADhcB/wEADpUB/yEAAZQMFwGU + AgABugyVAboiAAH/AZQKFwGUAf8CAAH/AboKlQG6Af80AAH/CPQkAAFCAU0BPgcAAT4DAAEoAwABQAMA + ARADAAEBAQABAQUAAYAXAAP/AQAB4AEHAv8EAAGAAQEBgAEBBAABgAEBAYABAQQAAYABAQGAAQEEAAGA + AQEBgAEBBAABgAEBAYABAQQAAYABAQGAAQEEAAGAAQEBgAEBBAABgAEBAYABAQQAAYABAQGAAQEEAAGA + AQEBgAEBBAABgAEAAYAFAAGAAQABgAUAAYABAQGAAQEEAAGAAQEBgAEBBAAC/wHgAQ8aAAs= + + + + 0 + + + listViewProxies + + + System.Windows.Forms.ListView, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 2 + 174, 22 @@ -218,33 +306,33 @@ 171, 6 - - 186, 22 - - - Import - - - 186, 22 - - - Export - - - 183, 6 - - - 186, 22 - - - Reset Window - 174, 22 More + + 159, 22 + + + Import + + + 159, 22 + + + Export + + + 156, 6 + + + 159, 22 + + + Reset Window + 171, 6 @@ -254,94 +342,6 @@ About - - 175, 226 - - - contextMenuStrip_RightClick - - - System.Windows.Forms.ContextMenuStrip, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - - Fill - - - Microsoft YaHei U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AABgAEBAYABAQQAAYABAQGAAQEEAAGAAQEBgAEBBAABgAEBAYABAQQAAYABAQGAAQEEAAGA - AQEBgAEBBAABgAEAAYAFAAGAAQABgAUAAYABAQGAAQEEAAGAAQEBgAEBBAAC/wHgAQ8aAAs= - - - - 0 - - - listViewProxies - - - System.Windows.Forms.ListView, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - $this - - - 1 - 389, 17 @@ -354,6 +354,48 @@ Database File|*.db + + 755, 17 + + + 0, 439 + + + 704, 22 + + + 1 + + + FooterStrip + + + statusStrip_Footer + + + System.Windows.Forms.StatusStrip, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 1 + + + 171, 17 + + + Welcome to Port Proxy GUI ! + + + 278, 17 + + + IP Helper service is not running (Click to start) + + + False + True @@ -2650,6 +2692,18 @@ System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + toolStripSeparator5 + + + System.Windows.Forms.ToolStripSeparator, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + toolStripMenuItem_ResetWindowSize + + + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + toolStripSeparator4 @@ -2680,17 +2734,17 @@ System.Windows.Forms.OpenFileDialog, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - toolStripMenuItem_ResetWindowSize + + toolStripStatusLabel_Status - - System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + System.Windows.Forms.ToolStripStatusLabel, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - toolStripSeparator5 + + toolStripStatusLabel_ServiceNotRunning - - System.Windows.Forms.ToolStripSeparator, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + System.Windows.Forms.ToolStripStatusLabel, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 PortProxyGUI diff --git a/PortProxyGUI/PortProxyGUI.zh-CN.resx b/PortProxyGUI/PortProxyGUI.zh-CN.resx index e001a81..17fd5d7 100644 --- a/PortProxyGUI/PortProxyGUI.zh-CN.resx +++ b/PortProxyGUI/PortProxyGUI.zh-CN.resx @@ -197,7 +197,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAADc - CAAAAk1TRnQBSQFMAgEBAgEAASABAQEwAQEBEAEAARABAAT/AQkBEAj/AUIBTQE2AQQGAAE2AQQCAAEo + CAAAAk1TRnQBSQFMAgEBAgEAASABAQE4AQEBEAEAARABAAT/AQkBEAj/AUIBTQE2AQQGAAE2AQQCAAEo AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5 AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA @@ -2611,4 +2611,13 @@ 重置窗口 + + FooterStrip + + + IP Helper 服务未启动(点击启动) + + + 欢迎使用 Port Proxy GUI ! + \ No newline at end of file diff --git a/PortProxyGUI/Program.cs b/PortProxyGUI/Program.cs index a92bcbb..0cfff63 100644 --- a/PortProxyGUI/Program.cs +++ b/PortProxyGUI/Program.cs @@ -1,8 +1,6 @@ using PortProxyGUI.Data; using System; -using System.Globalization; using System.IO; -using System.Threading; using System.Windows.Forms; namespace PortProxyGUI diff --git a/PortProxyGUI/SetProxy.cs b/PortProxyGUI/SetProxy.cs index ff9dfcf..c69f456 100644 --- a/PortProxyGUI/SetProxy.cs +++ b/PortProxyGUI/SetProxy.cs @@ -2,7 +2,6 @@ using PortProxyGUI.Data; using PortProxyGUI.Utils; using System; -using System.Drawing; using System.Linq; using System.Text.RegularExpressions; using System.Windows.Forms; diff --git a/PortProxyGUI/Utils/DnsUtil.cs b/PortProxyGUI/Utils/DnsUtil.cs index 1ade0fc..b6e3574 100644 --- a/PortProxyGUI/Utils/DnsUtil.cs +++ b/PortProxyGUI/Utils/DnsUtil.cs @@ -1,16 +1,13 @@ -using System; -using System.Runtime.InteropServices; +using PortProxyGUI.Native; +using System; namespace PortProxyGUI.Utils { internal class DnsUtil { - [DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCache")] - static extern uint DnsFlushResolverCache(); - public static void FlushCache() { - var status = DnsFlushResolverCache(); + var status = NativeMethods.DnsFlushResolverCache(); if (status == 0) throw new InvalidOperationException("Flush DNS Cache failed."); } diff --git a/PortProxyGUI/Utils/InterfaceUtil.cs b/PortProxyGUI/Utils/InterfaceUtil.cs index f2b0122..d0b0c3b 100644 --- a/PortProxyGUI/Utils/InterfaceUtil.cs +++ b/PortProxyGUI/Utils/InterfaceUtil.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Text; +using System.Drawing; namespace PortProxyGUI.Utils { diff --git a/PortProxyGUI/Utils/PortPorxyUtil.cs b/PortProxyGUI/Utils/PortPorxyUtil.cs index 84717bc..3b541f8 100644 --- a/PortProxyGUI/Utils/PortPorxyUtil.cs +++ b/PortProxyGUI/Utils/PortPorxyUtil.cs @@ -4,12 +4,14 @@ using PortProxyGUI.Native; using System; using System.Collections.Generic; using System.Linq; -using System.Runtime.InteropServices; 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" }; @@ -85,29 +87,62 @@ namespace PortProxyGUI.Utils catch { } } - public static void ParamChange() + 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 serviceName = "iphlpsvc"; - var hService = NativeMethods.OpenService(hManager, serviceName, ServiceRights.SERVICE_PAUSE_CONTINUE); + var hService = NativeMethods.OpenService(hManager, ServiceName, ServiceRights.SERVICE_QUERY_STATUS); if (hService == IntPtr.Zero) { NativeMethods.CloseServiceHandle(hManager); - throw new InvalidOperationException($"Open Service ({serviceName}) failed."); + throw new InvalidOperationException($"Open Service ({ServiceName}) failed."); } - var serviceStatus = new ServiceStatus(); - var success = NativeMethods.ControlService(hService, ServiceControls.SERVICE_CONTROL_PARAMCHANGE, ref serviceStatus); + var status = new ServiceStatus(); + NativeMethods.QueryServiceStatus(hService, ref status); NativeMethods.CloseServiceHandle(hService); NativeMethods.CloseServiceHandle(hManager); - if (!success) + 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_PAUSE_CONTINUE | ServiceRights.SERVICE_START); + if (hService == IntPtr.Zero) { - throw new InvalidOperationException($"Control Service ({serviceName}) ParamChange failed."); + 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 | (uint)ScmRights.SC_MANAGER_CONNECT); + 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); } }