WinFWHelper.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "WinFWHelper.hpp"
  2. namespace ZeroTier {
  3. void ZeroTier::WinFWHelper::newICMPRule(const InetAddress& ip, uint64_t nwid)
  4. {
  5. char nwString[32] = { 0 };
  6. char ipbuf[64];
  7. sprintf(nwString, "%.16llx", nwid);
  8. std::string nwString2 = { nwString };
  9. ip.toString(ipbuf);
  10. if (ip.isV4()) {
  11. WinFWHelper::newICMPv4Rule(ipbuf, nwid);
  12. }
  13. else {
  14. WinFWHelper::newICMPv6Rule(ipbuf, nwid);
  15. }
  16. }
  17. void ZeroTier::WinFWHelper::removeICMPRule(const InetAddress& ip, uint64_t nwid)
  18. {
  19. char nwString[32] = { 0 };
  20. char ipbuf[64];
  21. sprintf(nwString, "%.16llx", nwid);
  22. std::string nwString2 = { nwString };
  23. ip.toString(ipbuf);
  24. if (ip.isV4()) {
  25. WinFWHelper::removeICMPv4Rule(ipbuf, nwid);
  26. }
  27. else {
  28. WinFWHelper::removeICMPv6Rule(ipbuf, nwid);
  29. }
  30. }
  31. void WinFWHelper::newICMPv4Rule(std::string address, uint64_t nwid)
  32. {
  33. // allows icmp, scoped to a specific ip address and interface name
  34. char nwString[32] = { 0 };
  35. sprintf(nwString, "%.16llx", nwid);
  36. std::string nwString2 = { nwString };
  37. std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "New-NetFirewallRule -DisplayName zerotier-icmpv4-)" + nwString2 + address + R"( -InterfaceAlias 'ZeroTier One `[)" + nwString2 + R"(`]')"
  38. + " -Protocol ICMPv4 -Action Allow" + " -LocalAddress " + address + "\"\r\n";
  39. _run(cmd);
  40. }
  41. void WinFWHelper::newICMPv6Rule(std::string address, uint64_t nwid)
  42. {
  43. // allows icmp, scoped to a specific ip address and interface name
  44. char nwString[32] = { 0 };
  45. sprintf(nwString, "%.16llx", nwid);
  46. std::string nwString2 = { nwString };
  47. std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "New-NetFirewallRule -DisplayName zerotier-icmpv6-)" + nwString2 + address + R"( -InterfaceAlias 'ZeroTier One `[)" + nwString2 + R"(`]')"
  48. + " -Protocol ICMPv6 -Action Allow" + " -LocalAddress " + address + "\"\r\n";
  49. _run(cmd);
  50. }
  51. void WinFWHelper::removeICMPv4Rule(std::string addr, uint64_t nwid)
  52. {
  53. // removes 1 icmp firewall rule
  54. char nwString[32] = { 0 };
  55. sprintf(nwString, "%.16llx", nwid);
  56. std::string nwString2 = { nwString };
  57. std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "Remove-NetFirewallRule -DisplayName zerotier-icmpv4-)" + nwString2 + addr + "\"\r\n";
  58. _run(cmd);
  59. }
  60. void WinFWHelper::removeICMPv6Rule(std::string addr, uint64_t nwid)
  61. {
  62. // removes 1 icmp firewall rule
  63. char nwString[32] = { 0 };
  64. sprintf(nwString, "%.16llx", nwid);
  65. std::string nwString2 = { nwString };
  66. std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "Remove-NetFirewallRule -DisplayName zerotier-icmpv6-)" + nwString2 + addr + "\"\r\n";
  67. _run(cmd);
  68. }
  69. void WinFWHelper::removeICMPv4Rules(uint64_t nwid)
  70. {
  71. // removes all icmp firewall rules for this network id
  72. char nwString[32] = { 0 };
  73. sprintf(nwString, "%.16llx", nwid);
  74. std::string nwString2 = { nwString };
  75. std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "Remove-NetFirewallRule -DisplayName zerotier-icmpv4-)" + nwString2 + "*\" \r\n";
  76. _run(cmd);
  77. }
  78. void WinFWHelper::removeICMPv6Rules(uint64_t nwid)
  79. {
  80. // removes all icmp firewall rules for this network id
  81. char nwString[32] = { 0 };
  82. sprintf(nwString, "%.16llx", nwid);
  83. std::string nwString2 = { nwString };
  84. std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "Remove-NetFirewallRule -DisplayName zerotier-icmpv6-)" + nwString2 + "*\" \r\n";
  85. _run(cmd);
  86. }
  87. void WinFWHelper::removeICMPRules()
  88. {
  89. // removes all icmp firewall rules for all networks
  90. std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "Remove-NetFirewallRule -DisplayName zerotier-icmp*)" + std::string("\r\n");
  91. _run(cmd);
  92. }
  93. void WinFWHelper::removeICMPRules(uint64_t nwid)
  94. {
  95. // removes all icmp firewall rules for this network
  96. WinFWHelper::removeICMPv4Rules(nwid);
  97. WinFWHelper::removeICMPv6Rules(nwid);
  98. }
  99. void WinFWHelper::_run(std::string cmd)
  100. {
  101. #ifdef ZT_DEBUG
  102. fprintf(stderr, cmd.c_str());
  103. #endif
  104. STARTUPINFOA startupInfo;
  105. PROCESS_INFORMATION processInfo;
  106. startupInfo.cb = sizeof(startupInfo);
  107. memset(&startupInfo, 0, sizeof(STARTUPINFOA));
  108. memset(&processInfo, 0, sizeof(PROCESS_INFORMATION));
  109. if (CreateProcessA(NULL, (LPSTR)cmd.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &startupInfo, &processInfo)) {
  110. WaitForSingleObject(processInfo.hProcess, INFINITE);
  111. CloseHandle(processInfo.hProcess);
  112. CloseHandle(processInfo.hThread);
  113. }
  114. }
  115. } // namespace ZeroTier