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()));
55 if (ip.empty() || ip.size() > MAX_HOST || port < 1 || port > 65535) {
58 const bool v6 = ip.find(
':') != std::string_view::npos;
59 if (!std::all_of(ip.begin(), ip.end(), v6 ? ipv6_char : host_char)) {
62 std::string origin =
"http://";
64 origin.append(
"[").append(ip).append(
"]");
68 return origin.append(
":").append(std::to_string(port));
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));
80 const std::wstring wanted = widen(url);
81 std::vector<unsigned long> taken;
82 std::vector<unsigned long> holding;
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) {
91 const auto n = rc == ERROR_SUCCESS ? entry_number(name) : 0;
96 DWORD size =
sizeof(data);
97 if (RegGetValueW(hkey,
nullptr, name, RRF_RT_REG_SZ,
nullptr, data, &size) == ERROR_SUCCESS &&
98 std::wstring_view(data) == wanted) {
104 std::sort(taken.begin(), taken.end());
105 unsigned long next = 1;
106 for (
const auto t : taken) {
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)));
115 if (rc != ERROR_SUCCESS) {
116 error = std::system_category().message(
static_cast<int>(rc));
124 error =
"browser policies are set in the Windows registry only";
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()) {
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();
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>());
143 if (origin.empty()) {
144 LOG_WARN(
"WebServer: {} wants {{\"ip\": <address>, \"port\": <1..65535>}}, not {}", setting, at.dump());
147 for (
const auto key : keys) {
151 LOG_INFO(
"WebServer: {}: added {} to HKCU\\{}", setting, origin, key);
154 LOG_INFO(
"WebServer: {}: HKCU\\{} lists {}", setting, key, origin);
157 LOG_WARN(
"WebServer: could not list {} in HKCU\\{}: {}", origin, key, error);