Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
browser_policy.cpp
Go to the documentation of this file.
2#include <slick/logger.hpp>
3#include <algorithm>
4#include <cctype>
5#include <system_error>
6#include <vector>
7
8#ifdef _WIN32
9 #include <windows.h>
10#endif
11
12namespace slick::trader {
13
14namespace {
15
16constexpr size_t MAX_HOST = 253;
17
18bool host_char(unsigned char c) {
19 return std::isalnum(c) || c == '.' || c == '-';
20}
21
22bool ipv6_char(unsigned char c) {
23 return std::isxdigit(c) || c == ':' || c == '.';
24}
25
26#ifdef _WIN32
27std::wstring widen(std::string_view s) {
28 if (s.empty()) {
29 return {};
30 }
31 std::wstring out(MultiByteToWideChar(CP_UTF8, 0, s.data(), static_cast<int>(s.size()), nullptr, 0), L'\0');
32 MultiByteToWideChar(CP_UTF8, 0, s.data(), static_cast<int>(s.size()), out.data(), static_cast<int>(out.size()));
33 return out;
34}
35
36// The entry number a value name holds: "1", "2", ...; 0 for any other name, "01" too.
37unsigned long entry_number(const wchar_t* name) {
38 if (*name == L'0') {
39 return 0;
40 }
41 unsigned long n = 0;
42 for (const wchar_t* c = name; *c; ++c) {
43 if (*c < L'0' || *c > L'9' || n > 1'000'000) {
44 return 0;
45 }
46 n = n * 10 + static_cast<unsigned long>(*c - L'0');
47 }
48 return n;
49}
50#endif
51
52} // namespace
53
54std::string dashboard_origin(std::string_view ip, int64_t port) {
55 if (ip.empty() || ip.size() > MAX_HOST || port < 1 || port > 65535) {
56 return {};
57 }
58 const bool v6 = ip.find(':') != std::string_view::npos;
59 if (!std::all_of(ip.begin(), ip.end(), v6 ? ipv6_char : host_char)) {
60 return {};
61 }
62 std::string origin = "http://";
63 if (v6) {
64 origin.append("[").append(ip).append("]");
65 } else {
66 origin.append(ip);
67 }
68 return origin.append(":").append(std::to_string(port));
69}
70
71PolicyUrl add_policy_url(std::string_view key, std::string_view url, std::string& error) {
72#ifdef _WIN32
73 HKEY hkey = nullptr;
74 LSTATUS rc = RegCreateKeyExW(HKEY_CURRENT_USER, widen(key).c_str(), 0, nullptr, 0,
75 KEY_QUERY_VALUE | KEY_SET_VALUE, nullptr, &hkey, nullptr);
76 if (rc != ERROR_SUCCESS) {
77 error = std::system_category().message(static_cast<int>(rc));
78 return PolicyUrl::FAILED;
79 }
80 const std::wstring wanted = widen(url);
81 std::vector<unsigned long> taken; // entry numbers
82 std::vector<unsigned long> holding; // those of them that hold `url`
83 wchar_t name[256];
84 wchar_t data[1024];
85 for (DWORD i = 0;; ++i) {
86 DWORD name_len = static_cast<DWORD>(std::size(name));
87 rc = RegEnumValueW(hkey, i, name, &name_len, nullptr, nullptr, nullptr, nullptr);
88 if (rc == ERROR_NO_MORE_ITEMS) {
89 break;
90 }
91 const auto n = rc == ERROR_SUCCESS ? entry_number(name) : 0; // a longer name is no entry's
92 if (n == 0) {
93 continue;
94 }
95 taken.push_back(n);
96 DWORD size = sizeof(data); // a longer value is no URL of ours
97 if (RegGetValueW(hkey, nullptr, name, RRF_RT_REG_SZ, nullptr, data, &size) == ERROR_SUCCESS &&
98 std::wstring_view(data) == wanted) {
99 holding.push_back(n);
100 }
101 }
102 // Browsers read 1, 2, ... up to the first number missing: an entry past it doesn't count, and
103 // a new one goes there.
104 std::sort(taken.begin(), taken.end());
105 unsigned long next = 1;
106 for (const auto t : taken) {
107 next += t == next;
108 }
110 if (std::none_of(holding.begin(), holding.end(), [next](unsigned long h) { return h < next; })) {
111 const std::wstring value = std::to_wstring(next);
112 rc = RegSetValueExW(hkey, value.c_str(), 0, REG_SZ, reinterpret_cast<const BYTE*>(wanted.c_str()),
113 static_cast<DWORD>((wanted.size() + 1) * sizeof(wchar_t)));
114 result = rc == ERROR_SUCCESS ? PolicyUrl::ADDED : PolicyUrl::FAILED;
115 if (rc != ERROR_SUCCESS) {
116 error = std::system_category().message(static_cast<int>(rc));
117 }
118 }
119 RegCloseKey(hkey);
120 return result;
121#else
122 (void)key;
123 (void)url;
124 error = "browser policies are set in the Windows registry only";
125 return PolicyUrl::FAILED;
126#endif
127}
128
129void list_origin(const nlohmann::json& config, std::string_view setting, int64_t default_port,
130 std::span<const std::string_view> keys) {
131 const auto found = config.is_object() ? config.find(std::string(setting)) : config.end();
132 if (found == config.end()) {
133 return;
134 }
135 const auto& at = *found;
136 const auto ip = at.is_object() ? at.find("ip") : at.end();
137 const auto port = at.is_object() ? at.find("port") : at.end();
138 std::string origin;
139 if (at.is_object() && (ip == at.end() || ip->is_string()) && (port == at.end() || port->is_number_integer())) {
140 origin = dashboard_origin(ip == at.end() ? "localhost" : ip->get_ref<const std::string&>(),
141 port == at.end() ? default_port : port->get<int64_t>());
142 }
143 if (origin.empty()) {
144 LOG_WARN("WebServer: {} wants {{\"ip\": <address>, \"port\": <1..65535>}}, not {}", setting, at.dump());
145 return;
146 }
147 for (const auto key : keys) {
148 std::string error;
149 switch (add_policy_url(key, origin, error)) {
150 case PolicyUrl::ADDED:
151 LOG_INFO("WebServer: {}: added {} to HKCU\\{}", setting, origin, key);
152 break;
154 LOG_INFO("WebServer: {}: HKCU\\{} lists {}", setting, key, origin);
155 break;
157 LOG_WARN("WebServer: could not list {} in HKCU\\{}: {}", origin, key, error);
158 break;
159 }
160 }
161}
162
163} // namespace slick::trader
std::string dashboard_origin(std::string_view ip, int64_t port)
PolicyUrl add_policy_url(std::string_view key, std::string_view url, std::string &error)
void list_origin(const nlohmann::json &config, std::string_view setting, int64_t default_port, std::span< const std::string_view > keys)