4#include "Stroika/Frameworks/StroikaPreComp.h"
7#if qStroika_Foundation_Common_Platform_Windows
11#if qStroika_Foundation_Common_Platform_Windows
12#include "Stroika/Foundation/Characters/Platform/Windows/SmartBSTR.h"
15#include "Stroika/Foundation/Containers/Collection.h"
18#if qStroika_Foundation_Common_Platform_Windows
19#include "Stroika/Foundation/Execution/Platform/Windows/COM.h"
20#include "Stroika/Foundation/Execution/Platform/Windows/HRESULTErrorException.h"
23#include "SystemFirewall.h"
29#if qStroika_Foundation_Common_Platform_Windows
33#if qStroika_Foundation_Common_Platform_Windows
37using namespace Stroika::Foundation::IO::Network::SystemFirewall;
42#if qStroika_Foundation_Common_Platform_Windows
43#pragma comment(lib, "ole32.lib")
44#pragma comment(lib, "oleaut32.lib")
56 sb <<
"Name: "sv << fName <<
","sv;
57 sb <<
"Description: "sv << fDescription <<
","sv;
58 sb <<
"Application: "sv << fApplication <<
","sv;
59#if qStroika_Foundation_Common_Platform_Windows
60 sb <<
"ProfileMask: "sv << (int)fProfileMask <<
","sv;
61 sb <<
"Direction: "sv << (int)fDirection <<
","sv;
62 sb <<
"Protocol: "sv << (int)fProtocol <<
","sv;
64 sb <<
"LocalPorts: "sv << fLocalPorts <<
","sv;
65 sb <<
"RemotePorts: "sv << fRemotePorts <<
","sv;
66#if qStroika_Foundation_Common_Platform_Windows
67 sb <<
"Action: "sv << (int)fAction <<
","sv;
69 sb <<
"Enabled: "sv << fEnabled <<
","sv;
79#if qStroika_Foundation_Common_Platform_Windows
81 Rule ReadRule_ (INetFwRule* pFwRule)
84#if USE_NOISY_TRACE_IN_THIS_MODULE_
90 BSTR application =
nullptr;
91 BSTR localPorts =
nullptr;
92 BSTR remotePorts =
nullptr;
94 if (name !=
nullptr) {
95 ::SysFreeString (name);
97 if (desc !=
nullptr) {
98 ::SysFreeString (desc);
100 if (group !=
nullptr) {
101 ::SysFreeString (group);
103 if (application !=
nullptr) {
104 ::SysFreeString (application);
106 if (localPorts !=
nullptr) {
107 ::SysFreeString (localPorts);
109 if (remotePorts !=
nullptr) {
110 ::SysFreeString (remotePorts);
117 long profileMask = 0;
119 NET_FW_RULE_DIRECTION direction = NET_FW_RULE_DIR_MAX;
125 NET_FW_ACTION action = NET_FW_ACTION_MAX;
127 VARIANT_BOOL enabled = VARIANT_FALSE;
129 return Rule{name ==
nullptr ? wstring{} : wstring{name},
130 desc ==
nullptr ? wstring{} : wstring{desc},
131 group ==
nullptr ? wstring{} : wstring{group},
132 application ==
nullptr ? wstring{} : wstring{application},
133 (NET_FW_PROFILE_TYPE2)(profileMask),
135 (NET_FW_IP_PROTOCOL_)(protocol),
136 localPorts ==
nullptr ? wstring{} : wstring{localPorts},
137 remotePorts ==
nullptr ? wstring{} : wstring{remotePorts},
139 enabled != VARIANT_FALSE};
141 optional<Rule> ReadRule_ (INetFwRules* pFwRules,
const String& ruleName)
144#if USE_NOISY_TRACE_IN_THIS_MODULE_
147 INetFwRule* pFwRule =
nullptr;
149 if (pFwRule !=
nullptr) {
153 HRESULT hr = pFwRules->Item (
SmartBSTR{ruleName.
As<wstring> ().c_str ()}, &pFwRule);
154 if (hr == S_OK and pFwRule !=
nullptr) {
155 return ReadRule_ (pFwRule);
166 for (
const auto& r : LookupByGroup (rule.fGroup)) {
168 DbgTrace (
"run unchanged, so returning false"_f);
173#if qStroika_Foundation_Common_Platform_Windows
174 COMInitializer comInitializeContext{COINIT_APARTMENTTHREADED};
175 INetFwPolicy2* pNetFwPolicy2 =
nullptr;
176 INetFwRules* pFwRules =
nullptr;
177 INetFwRule* pFwRule =
nullptr;
179 if (pFwRule !=
nullptr) {
182 if (pFwRules !=
nullptr) {
183 pFwRules->Release ();
185 if (pNetFwPolicy2 !=
nullptr) {
186 pNetFwPolicy2->Release ();
191 ThrowIfErrorHRESULT (::CoCreateInstance (__uuidof (NetFwPolicy2),
nullptr, CLSCTX_INPROC_SERVER, __uuidof (INetFwPolicy2), (
void**)&pNetFwPolicy2));
194 ThrowIfErrorHRESULT (pNetFwPolicy2->get_Rules (&pFwRules));
197 ThrowIfErrorHRESULT (::CoCreateInstance (__uuidof (NetFwRule), NULL, CLSCTX_INPROC_SERVER, __uuidof (INetFwRule), (
void**)&pFwRule));
200 ThrowIfErrorHRESULT (pFwRule->put_Name (
SmartBSTR{rule.fName.As<wstring> ().c_str ()}));
201 ThrowIfErrorHRESULT (pFwRule->put_Description (
SmartBSTR{rule.fDescription.As<wstring> ().c_str ()}));
202 ThrowIfErrorHRESULT (pFwRule->put_ApplicationName (
SmartBSTR{rule.fApplication.c_str ()}));
203 ThrowIfErrorHRESULT (pFwRule->put_Protocol (rule.fProtocol));
204 ThrowIfErrorHRESULT (pFwRule->put_LocalPorts (
SmartBSTR{rule.fLocalPorts.As<wstring> ().c_str ()}));
205 ThrowIfErrorHRESULT (pFwRule->put_RemotePorts (
SmartBSTR{rule.fRemotePorts.As<wstring> ().c_str ()}));
206 ThrowIfErrorHRESULT (pFwRule->put_Direction (rule.fDirection));
207 ThrowIfErrorHRESULT (pFwRule->put_Grouping (
SmartBSTR{rule.fGroup.As<wstring> ().c_str ()}));
208 ThrowIfErrorHRESULT (pFwRule->put_Profiles (rule.fProfileMask));
209 ThrowIfErrorHRESULT (pFwRule->put_Action (rule.fAction));
210 ThrowIfErrorHRESULT (pFwRule->put_Enabled (rule.fEnabled ? VARIANT_TRUE : VARIANT_FALSE));
213 DbgTrace (
"Updating firewall rule"_f);
214 ThrowIfErrorHRESULT (pFwRules->Add (pFwRule));
219optional<Rule> SystemFirewall::Manager::Lookup (
const String& ruleName)
const
222#if qStroika_Foundation_Common_Platform_Windows
223 COMInitializer comInitializeContext{COINIT_APARTMENTTHREADED};
224 INetFwPolicy2* pNetFwPolicy2 =
nullptr;
225 INetFwRules* pFwRules =
nullptr;
227 if (pFwRules !=
nullptr) {
228 pFwRules->Release ();
230 if (pNetFwPolicy2 !=
nullptr) {
231 pNetFwPolicy2->Release ();
236 ThrowIfErrorHRESULT (::CoCreateInstance (__uuidof (NetFwPolicy2),
nullptr, CLSCTX_INPROC_SERVER, __uuidof (INetFwPolicy2), (
void**)&pNetFwPolicy2));
239 ThrowIfErrorHRESULT (pNetFwPolicy2->get_Rules (&pFwRules));
240 return ReadRule_ (pFwRules, ruleName);
249 for (
const Rule& r : LookupAll ()) {
250 if (r.fGroup == groupName) {
261#if qStroika_Foundation_Common_Platform_Windows
262 COMInitializer comInitializeContext{COINIT_APARTMENTTHREADED};
263 INetFwPolicy2* pNetFwPolicy2 =
nullptr;
264 INetFwRules* pFwRules =
nullptr;
265 IEnumVARIANT* pEnum =
nullptr;
267 if (pEnum !=
nullptr) {
270 if (pFwRules !=
nullptr) {
271 pFwRules->Release ();
273 if (pNetFwPolicy2 !=
nullptr) {
274 pNetFwPolicy2->Release ();
279 ThrowIfErrorHRESULT (::CoCreateInstance (__uuidof (NetFwPolicy2),
nullptr, CLSCTX_INPROC_SERVER, __uuidof (INetFwPolicy2), (
void**)&pNetFwPolicy2));
288 ::VariantInit (&nextElt);
289 for (; SUCCEEDED (pEnum->Next (1, &nextElt, &nRead)) and nRead == 1;) {
290 INetFwRule* r =
nullptr;
292 rules += ReadRule_ (r);
#define Stroika_Foundation_Debug_OptionalizeTraceArgs(...)
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
A Collection<T> is a container to manage an un-ordered collection of items, without equality defined ...
nonvirtual bool Register(const Rule &rule)
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
auto Finally(FUNCTION &&f) -> Private_::FinallySentry< FUNCTION >
nonvirtual String ToString() const