mirror of
				https://github.com/snltty/linker.git
				synced 2025-10-31 12:36:48 +08:00 
			
		
		
		
	添加防火墙规则
This commit is contained in:
		| @@ -53,6 +53,7 @@ namespace cmonitor.viewer.server.win | |||||||
|             this.Visible = false; |             this.Visible = false; | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |             FireWallHelper.Write(Path.GetFileNameWithoutExtension(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)); | ||||||
|             CheckRunning(); |             CheckRunning(); | ||||||
|  |  | ||||||
|             if (shareMode == Mode.Client) |             if (shareMode == Mode.Client) | ||||||
| @@ -119,25 +120,32 @@ namespace cmonitor.viewer.server.win | |||||||
|         } |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|         RDPSession session; |         private RDPSession session; | ||||||
|         private NotifyIcon notifyIcon; |         private NotifyIcon notifyIcon; | ||||||
|  |         private string invitationString; | ||||||
|         private void OpenShareDesktop() |         private void OpenShareDesktop() | ||||||
|         { |         { | ||||||
|             notifyIcon = new NotifyIcon(); |             notifyIcon = new NotifyIcon(); | ||||||
|             notifyIcon.Visible = true; |             notifyIcon.Visible = true; | ||||||
|             notifyIcon.ContextMenuStrip = new ContextMenuStrip(); |             notifyIcon.ContextMenuStrip = new ContextMenuStrip(); | ||||||
|             notifyIcon.ContextMenuStrip.Items.Add("ˢ<>¹<EFBFBD><C2B9><EFBFBD>"); |             //notifyIcon.ContextMenuStrip.Items.Add("连接串"); | ||||||
|             notifyIcon.ContextMenuStrip.Items.Add("<EFBFBD>˳<EFBFBD>"); |             notifyIcon.ContextMenuStrip.Items.Add("刷新共享"); | ||||||
|  |             notifyIcon.ContextMenuStrip.Items.Add("退出"); | ||||||
|             notifyIcon.ContextMenuStrip.ItemClicked += (object sender, ToolStripItemClickedEventArgs e) => |             notifyIcon.ContextMenuStrip.ItemClicked += (object sender, ToolStripItemClickedEventArgs e) => | ||||||
|             { |             { | ||||||
|                 if (e.ClickedItem.Text == "<22>˳<EFBFBD>") |  | ||||||
|  |                 if (e.ClickedItem.Text == "退出") | ||||||
|                 { |                 { | ||||||
|                     CloseServer(); |                     CloseServer(); | ||||||
|                 } |                 } | ||||||
|                 else if (e.ClickedItem.Text == "ˢ<EFBFBD>¹<EFBFBD><EFBFBD><EFBFBD>") |                 else if (e.ClickedItem.Text == "刷新共享") | ||||||
|                 { |                 { | ||||||
|                     NewShare(); |                     NewShare(); | ||||||
|                 } |                 } | ||||||
|  |                 else if (e.ClickedItem.Text == "连接串") | ||||||
|  |                 { | ||||||
|  |                     //MessageBox.Show(invitationString); | ||||||
|  |                 } | ||||||
|             }; |             }; | ||||||
|             NewShare(); |             NewShare(); | ||||||
|         } |         } | ||||||
| @@ -154,7 +162,7 @@ namespace cmonitor.viewer.server.win | |||||||
|                     session.OnAttendeeConnected += Session_OnAttendeeConnected; |                     session.OnAttendeeConnected += Session_OnAttendeeConnected; | ||||||
|                     session.Open(); |                     session.Open(); | ||||||
|                     IRDPSRAPIInvitation invitation = session.Invitations.CreateInvitation(guid, "snltty", "snltty", 1024); |                     IRDPSRAPIInvitation invitation = session.Invitations.CreateInvitation(guid, "snltty", "snltty", 1024); | ||||||
|                     string invitationString = invitation.ConnectionString; |                     invitationString = invitation.ConnectionString; | ||||||
|  |  | ||||||
|                     /* |                     /* | ||||||
|                     XmlDocument xmlDoc = new XmlDocument(); |                     XmlDocument xmlDoc = new XmlDocument(); | ||||||
| @@ -169,13 +177,14 @@ namespace cmonitor.viewer.server.win | |||||||
|                     Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Cmonitor", "viewerConnectStr", invitationString); |                     Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Cmonitor", "viewerConnectStr", invitationString); | ||||||
|  |  | ||||||
|                     notifyIcon.Icon = Icon.FromHandle(Resources.logo_share_green.GetHicon()); |                     notifyIcon.Icon = Icon.FromHandle(Resources.logo_share_green.GetHicon()); | ||||||
|                     notifyIcon.Text = "<EFBFBD><EFBFBD><EFBFBD>ڹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"; |                     notifyIcon.Text = "正在共享桌面"; | ||||||
|                 } |                 } | ||||||
|                 catch (Exception ex) |                 catch (Exception ex) | ||||||
|                 { |                 { | ||||||
|                     Debug.WriteLine(ex + ""); |                     Debug.WriteLine(ex + ""); | ||||||
|  |                     //MessageBox.Show(ex.Message); | ||||||
|                     notifyIcon.Icon = Icon.FromHandle(Resources.logo_share_gray.GetHicon()); |                     notifyIcon.Icon = Icon.FromHandle(Resources.logo_share_gray.GetHicon()); | ||||||
|                     notifyIcon.Text = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>"; |                     notifyIcon.Text = "共享失败"; | ||||||
|                 } |                 } | ||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. | |||||||
|   <PropertyGroup> |   <PropertyGroup> | ||||||
|     <Configuration>Release</Configuration> |     <Configuration>Release</Configuration> | ||||||
|     <Platform>Any CPU</Platform> |     <Platform>Any CPU</Platform> | ||||||
|     <PublishDir>..\cmonitor.viewer.client.win\dist</PublishDir> |     <PublishDir>D:\Desktop\cmonitor\cmonitor.viewer.server.win\bin\Release</PublishDir> | ||||||
|     <PublishProtocol>FileSystem</PublishProtocol> |     <PublishProtocol>FileSystem</PublishProtocol> | ||||||
|     <_TargetId>Folder</_TargetId> |     <_TargetId>Folder</_TargetId> | ||||||
|     <TargetFramework>net8.0-windows</TargetFramework> |     <TargetFramework>net8.0-windows</TargetFramework> | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. | |||||||
| --> | --> | ||||||
| <Project> | <Project> | ||||||
|   <PropertyGroup> |   <PropertyGroup> | ||||||
|     <History>True|2024-04-01T06:40:33.3491753Z;True|2024-04-01T14:34:23.8751731+08:00;True|2024-04-01T11:18:12.7770691+08:00;True|2024-04-01T11:15:15.7084207+08:00;True|2024-03-21T09:26:40.1376911+08:00;True|2024-03-21T09:24:13.2312644+08:00;True|2024-03-21T09:22:59.0517639+08:00;</History> |     <History>True|2024-04-07T02:23:56.7276437Z;True|2024-04-07T10:01:21.9803124+08:00;True|2024-04-07T09:30:28.6448429+08:00;True|2024-04-01T14:40:33.3491753+08:00;True|2024-04-01T14:34:23.8751731+08:00;True|2024-04-01T11:18:12.7770691+08:00;True|2024-04-01T11:15:15.7084207+08:00;True|2024-03-21T09:26:40.1376911+08:00;True|2024-03-21T09:24:13.2312644+08:00;True|2024-03-21T09:22:59.0517639+08:00;</History> | ||||||
|     <LastFailureDetails /> |     <LastFailureDetails /> | ||||||
|   </PropertyGroup> |   </PropertyGroup> | ||||||
| </Project> | </Project> | ||||||
| @@ -34,6 +34,8 @@ namespace cmonitor | |||||||
|             StartupTransfer.Use(serviceProvider, config, assemblies); |             StartupTransfer.Use(serviceProvider, config, assemblies); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |             FireWallHelper.Write(Path.GetFileNameWithoutExtension(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)); | ||||||
|  |  | ||||||
|             GCHelper.FlushMemory(); |             GCHelper.FlushMemory(); | ||||||
|             await Helper.Await(); |             await Helper.Await(); | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -28,6 +28,7 @@ namespace cmonitor.plugins.wlan | |||||||
|  |  | ||||||
|         public void UseClient(ServiceProvider serviceProvider, Config config, Assembly[] assemblies) |         public void UseClient(ServiceProvider serviceProvider, Config config, Assembly[] assemblies) | ||||||
|         { |         { | ||||||
|  |             serviceProvider.GetServices<WlanReport>(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         public void UseServer(ServiceProvider serviceProvider, Config config, Assembly[] assemblies) |         public void UseServer(ServiceProvider serviceProvider, Config config, Assembly[] assemblies) | ||||||
|   | |||||||
							
								
								
									
										57
									
								
								common.libs/FireWallHelper.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								common.libs/FireWallHelper.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | |||||||
|  | using System; | ||||||
|  |  | ||||||
|  | namespace common.libs | ||||||
|  | { | ||||||
|  |     public static class FireWallHelper | ||||||
|  |     { | ||||||
|  |         public static void Write(string fileName) | ||||||
|  |         { | ||||||
|  |             if (OperatingSystem.IsWindows()) | ||||||
|  |             { | ||||||
|  |                 Windows(fileName); | ||||||
|  |             } | ||||||
|  |             else if (OperatingSystem.IsLinux()) | ||||||
|  |             { | ||||||
|  |                 Linux(fileName); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         private static void Linux(string fileName) | ||||||
|  |         { | ||||||
|  |             CommandHelper.Linux(string.Empty,new string[] { | ||||||
|  |                 $"firewall-cmd --permanent --new-service={fileName}", | ||||||
|  |                 $"firewall-cmd --permanent --service={fileName} --set-short=\"My Application {fileName}\"", | ||||||
|  |                 $"firewall-cmd --permanent --service={fileName} --set-description=\"Allow all ports for My Application {fileName}\"", | ||||||
|  |                 $"firewall-cmd --permanent --service={fileName} --add-port=0-65535/tcp", | ||||||
|  |                 $"firewall-cmd --permanent --service={fileName} --add-port=0-65535/udp", | ||||||
|  |                 $"firewall-cmd --permanent --add-service={fileName}", | ||||||
|  |                 $"firewall-cmd --reload", | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         private static void Windows(string fileName) | ||||||
|  |         { | ||||||
|  |             string content = $@"@echo off | ||||||
|  | cd  ""%CD%"" | ||||||
|  | for /f ""tokens=4,5 delims=. "" %%a in ('ver') do if %%a%%b geq 60 goto new | ||||||
|  |  | ||||||
|  | :old | ||||||
|  | cmd /c netsh firewall delete allowedprogram program=""%CD%\{fileName}.exe"" profile=ALL | ||||||
|  | cmd /c netsh firewall add allowedprogram program=""%CD%\{fileName}.exe"" name=""{fileName}"" ENABLE | ||||||
|  | cmd /c netsh firewall add allowedprogram program=""%CD%\{fileName}.exe"" name=""{fileName}"" ENABLE profile=ALL | ||||||
|  | goto end | ||||||
|  | :new | ||||||
|  | cmd /c netsh advfirewall firewall delete rule name=""{fileName}"" | ||||||
|  | cmd /c netsh advfirewall firewall add rule name=""{fileName}"" dir=in action=allow program=""%CD%\{fileName}.exe"" protocol=tcp enable=yes profile=public | ||||||
|  | cmd /c netsh advfirewall firewall add rule name=""{fileName}"" dir=in action=allow program=""%CD%\{fileName}.exe"" protocol=udp enable=yes profile=public | ||||||
|  | cmd /c netsh advfirewall firewall add rule name=""{fileName}"" dir=in action=allow program=""%CD%\{fileName}.exe"" protocol=tcp enable=yes profile=domain | ||||||
|  | cmd /c netsh advfirewall firewall add rule name=""{fileName}"" dir=in action=allow program=""%CD%\{fileName}.exe"" protocol=udp enable=yes profile=domain | ||||||
|  | cmd /c netsh advfirewall firewall add rule name=""{fileName}"" dir=in action=allow program=""%CD%\{fileName}.exe"" protocol=tcp enable=yes profile=private | ||||||
|  | cmd /c netsh advfirewall firewall add rule name=""{fileName}"" dir=in action=allow program=""%CD%\{fileName}.exe"" protocol=udp enable=yes profile=private | ||||||
|  | :end"; | ||||||
|  |             System.IO.File.WriteAllText("firewall.bat", content); | ||||||
|  |             CommandHelper.Execute("firewall.bat", string.Empty, new string[0]); | ||||||
|  |             System.IO.File.Delete("firewall.bat"); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 snltty
					snltty