mirror of
				https://github.com/snltty/linker.git
				synced 2025-10-31 04:26:45 +08:00 
			
		
		
		
	添加防火墙规则
This commit is contained in:
		| @@ -53,6 +53,7 @@ namespace cmonitor.viewer.server.win | ||||
|             this.Visible = false; | ||||
| #endif | ||||
|  | ||||
|             FireWallHelper.Write(Path.GetFileNameWithoutExtension(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)); | ||||
|             CheckRunning(); | ||||
|  | ||||
|             if (shareMode == Mode.Client) | ||||
| @@ -119,25 +120,32 @@ namespace cmonitor.viewer.server.win | ||||
|         } | ||||
|  | ||||
|  | ||||
|         RDPSession session; | ||||
|         private RDPSession session; | ||||
|         private NotifyIcon notifyIcon; | ||||
|         private string invitationString; | ||||
|         private void OpenShareDesktop() | ||||
|         { | ||||
|             notifyIcon = new NotifyIcon(); | ||||
|             notifyIcon.Visible = true; | ||||
|             notifyIcon.ContextMenuStrip = new ContextMenuStrip(); | ||||
|             notifyIcon.ContextMenuStrip.Items.Add("ˢ<>¹<EFBFBD><C2B9><EFBFBD>"); | ||||
|             notifyIcon.ContextMenuStrip.Items.Add("<EFBFBD>˳<EFBFBD>"); | ||||
|             //notifyIcon.ContextMenuStrip.Items.Add("连接串"); | ||||
|             notifyIcon.ContextMenuStrip.Items.Add("刷新共享"); | ||||
|             notifyIcon.ContextMenuStrip.Items.Add("退出"); | ||||
|             notifyIcon.ContextMenuStrip.ItemClicked += (object sender, ToolStripItemClickedEventArgs e) => | ||||
|             { | ||||
|                 if (e.ClickedItem.Text == "<22>˳<EFBFBD>") | ||||
|  | ||||
|                 if (e.ClickedItem.Text == "退出") | ||||
|                 { | ||||
|                     CloseServer(); | ||||
|                 } | ||||
|                 else if (e.ClickedItem.Text == "ˢ<EFBFBD>¹<EFBFBD><EFBFBD><EFBFBD>") | ||||
|                 else if (e.ClickedItem.Text == "刷新共享") | ||||
|                 { | ||||
|                     NewShare(); | ||||
|                 } | ||||
|                 else if (e.ClickedItem.Text == "连接串") | ||||
|                 { | ||||
|                     //MessageBox.Show(invitationString); | ||||
|                 } | ||||
|             }; | ||||
|             NewShare(); | ||||
|         } | ||||
| @@ -154,7 +162,7 @@ namespace cmonitor.viewer.server.win | ||||
|                     session.OnAttendeeConnected += Session_OnAttendeeConnected; | ||||
|                     session.Open(); | ||||
|                     IRDPSRAPIInvitation invitation = session.Invitations.CreateInvitation(guid, "snltty", "snltty", 1024); | ||||
|                     string invitationString = invitation.ConnectionString; | ||||
|                     invitationString = invitation.ConnectionString; | ||||
|  | ||||
|                     /* | ||||
|                     XmlDocument xmlDoc = new XmlDocument(); | ||||
| @@ -169,13 +177,14 @@ namespace cmonitor.viewer.server.win | ||||
|                     Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Cmonitor", "viewerConnectStr", invitationString); | ||||
|  | ||||
|                     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) | ||||
|                 { | ||||
|                     Debug.WriteLine(ex + ""); | ||||
|                     //MessageBox.Show(ex.Message); | ||||
|                     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> | ||||
|     <Configuration>Release</Configuration> | ||||
|     <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> | ||||
|     <_TargetId>Folder</_TargetId> | ||||
|     <TargetFramework>net8.0-windows</TargetFramework> | ||||
|   | ||||
| @@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. | ||||
| --> | ||||
| <Project> | ||||
|   <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 /> | ||||
|   </PropertyGroup> | ||||
| </Project> | ||||
| @@ -34,6 +34,8 @@ namespace cmonitor | ||||
|             StartupTransfer.Use(serviceProvider, config, assemblies); | ||||
|  | ||||
|  | ||||
|             FireWallHelper.Write(Path.GetFileNameWithoutExtension(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)); | ||||
|  | ||||
|             GCHelper.FlushMemory(); | ||||
|             await Helper.Await(); | ||||
|         } | ||||
|   | ||||
| @@ -28,6 +28,7 @@ namespace cmonitor.plugins.wlan | ||||
|  | ||||
|         public void UseClient(ServiceProvider serviceProvider, Config config, Assembly[] assemblies) | ||||
|         { | ||||
|             serviceProvider.GetServices<WlanReport>(); | ||||
|         } | ||||
|  | ||||
|         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