mirror of
https://github.com/snltty/linker.git
synced 2025-10-08 18:40:07 +08:00
75 lines
3.7 KiB
C#
75 lines
3.7 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace linker.libs
|
|
{
|
|
public static class FireWallHelper
|
|
{
|
|
public static void Write(string fileName, string distPatt)
|
|
{
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
Windows(fileName, distPatt);
|
|
}
|
|
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 --service={fileName} --add-port=0-65535/icmp",
|
|
$"firewall-cmd --permanent --add-service={fileName}",
|
|
$"firewall-cmd --reload",
|
|
});
|
|
}
|
|
|
|
private static void Windows(string fileName, string distPath)
|
|
{
|
|
try
|
|
{
|
|
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=icmp 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=icmp 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
|
|
cmd /c netsh advfirewall firewall add rule name=""{fileName}"" dir=in action=allow program=""%CD%\{fileName}.exe"" protocol=icmp enable=yes profile=private
|
|
:end";
|
|
if (Directory.Exists(distPath) == false)
|
|
{
|
|
Directory.CreateDirectory(distPath);
|
|
}
|
|
string firewall = Path.Join(distPath, "firewall.bat");
|
|
|
|
File.WriteAllText(firewall, content);
|
|
CommandHelper.Execute(firewall, string.Empty, new string[0]);
|
|
File.Delete(firewall);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|