123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- #include "WinFWHelper.hpp"
- namespace ZeroTier {
- void ZeroTier::WinFWHelper::newICMPRule(const InetAddress& ip, uint64_t nwid)
- {
- char nwString[32] = { 0 };
- char ipbuf[64];
- sprintf(nwString, "%.16llx", nwid);
- std::string nwString2 = { nwString };
-
- ip.toString(ipbuf);
- if (ip.isV4()) {
- WinFWHelper::newICMPv4Rule(ipbuf, nwid);
- }
- else {
- WinFWHelper::newICMPv6Rule(ipbuf, nwid);
- }
- }
- void ZeroTier::WinFWHelper::removeICMPRule(const InetAddress& ip, uint64_t nwid)
- {
- char nwString[32] = { 0 };
- char ipbuf[64];
- sprintf(nwString, "%.16llx", nwid);
- std::string nwString2 = { nwString };
- ip.toString(ipbuf);
- if (ip.isV4()) {
- WinFWHelper::removeICMPv4Rule(ipbuf, nwid);
- }
- else {
- WinFWHelper::removeICMPv6Rule(ipbuf, nwid);
- }
- }
- void WinFWHelper::newICMPv4Rule(std::string address, uint64_t nwid)
- {
- // allows icmp, scoped to a specific ip address and interface name
- char nwString[32] = { 0 };
- sprintf(nwString, "%.16llx", nwid);
- std::string nwString2 = { nwString };
- 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"(`]')" +
- " -Protocol ICMPv4 -Action Allow" +
- " -LocalAddress " + address + "\"\r\n";
-
- _run(cmd);
- }
- void WinFWHelper::newICMPv6Rule(std::string address, uint64_t nwid)
- {
- // allows icmp, scoped to a specific ip address and interface name
- char nwString[32] = { 0 };
- sprintf(nwString, "%.16llx", nwid);
- std::string nwString2 = { nwString };
- 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"(`]')" +
- " -Protocol ICMPv6 -Action Allow" +
- " -LocalAddress " + address + "\"\r\n";
- _run(cmd);
- }
- void WinFWHelper::removeICMPv4Rule(std::string addr, uint64_t nwid)
- {
- // removes 1 icmp firewall rule
- char nwString[32] = { 0 };
- sprintf(nwString, "%.16llx", nwid);
- std::string nwString2 = { nwString };
- std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "Remove-NetFirewallRule -DisplayName zerotier-icmpv4-)" + nwString2 + addr +
- "\"\r\n";
- _run(cmd);
- }
- void WinFWHelper::removeICMPv6Rule(std::string addr, uint64_t nwid)
- {
- // removes 1 icmp firewall rule
- char nwString[32] = { 0 };
- sprintf(nwString, "%.16llx", nwid);
- std::string nwString2 = { nwString };
- std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "Remove-NetFirewallRule -DisplayName zerotier-icmpv6-)" + nwString2 + addr +
- "\"\r\n";
- _run(cmd);
- }
- void WinFWHelper::removeICMPv4Rules(uint64_t nwid)
- {
- // removes all icmp firewall rules for this network id
- char nwString[32] = { 0 };
- sprintf(nwString, "%.16llx", nwid);
- std::string nwString2 = { nwString };
- std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "Remove-NetFirewallRule -DisplayName zerotier-icmpv4-)" + nwString2 + "*\" \r\n";
-
- _run(cmd);
- }
- void WinFWHelper::removeICMPv6Rules(uint64_t nwid)
- {
- // removes all icmp firewall rules for this network id
- char nwString[32] = { 0 };
- sprintf(nwString, "%.16llx", nwid);
- std::string nwString2 = { nwString };
- std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "Remove-NetFirewallRule -DisplayName zerotier-icmpv6-)" + nwString2 + "*\" \r\n";
- _run(cmd);
- }
- void WinFWHelper::removeICMPRules()
- {
- // removes all icmp firewall rules for all networks
- std::string cmd = R"(C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe "Remove-NetFirewallRule -DisplayName zerotier-icmp*)" + std::string("\r\n");
- _run(cmd);
- }
- void WinFWHelper::removeICMPRules(uint64_t nwid)
- {
- // removes all icmp firewall rules for this network
- WinFWHelper::removeICMPv4Rules(nwid);
- WinFWHelper::removeICMPv6Rules(nwid);
- }
- void WinFWHelper::_run(std::string cmd)
- {
-
- #ifdef ZT_DEBUG
- fprintf(stderr, cmd.c_str());
- #endif
- STARTUPINFOA startupInfo;
- PROCESS_INFORMATION processInfo;
- startupInfo.cb = sizeof(startupInfo);
- memset(&startupInfo, 0, sizeof(STARTUPINFOA));
- memset(&processInfo, 0, sizeof(PROCESS_INFORMATION));
- if (CreateProcessA(NULL, (LPSTR)cmd.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &startupInfo, &processInfo)) {
- WaitForSingleObject(processInfo.hProcess, INFINITE);
- CloseHandle(processInfo.hProcess);
- CloseHandle(processInfo.hThread);
- }
- }
- } // namespace ZeroTier
|