-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_config.cpp
More file actions
296 lines (279 loc) · 12.6 KB
/
Copy pathclient_config.cpp
File metadata and controls
296 lines (279 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "client_config.h"
#include <algorithm>
#include <cctype>
#include <fstream>
#include <utility>
#include <json/json.h>
#include "nat_protocol.h"
#include "util.h"
namespace {
// Mirrors the previous hand-rolled parser's leniency: a missing key or a key
// whose JSON type does not match keeps the struct's current value instead of
// failing the whole file.
void ReadString(const Json::Value& root, const char* key, std::string* value) {
if (root.isMember(key) && root[key].isString()) *value = root[key].asString();
}
void ReadInt(const Json::Value& root, const char* key, int* value) {
if (root.isMember(key) && root[key].isInt()) *value = root[key].asInt();
}
void ReadBool(const Json::Value& root, const char* key, bool* value) {
if (root.isMember(key) && root[key].isBool()) *value = root[key].asBool();
}
bool ReadStunServers(const Json::Value& root,
std::vector<StunServerConfig>* servers,
std::string* error) {
if (!root.isMember("stun_servers")) return true;
const Json::Value& value = root["stun_servers"];
if (!value.isArray()) {
*error = "stun_servers must be an array";
return false;
}
std::vector<StunServerConfig> parsed;
// An empty host is an unconfigured slot, which is also what the defaults
// save. ValidateClientConfig rejects it only when NAT Punch needs STUN.
for (const auto& item : value) {
if (!item.isObject() || !item.isMember("host")
|| !item["host"].isString()
|| !item.isMember("port") || !item["port"].isInt()) {
*error = "Each STUN server must contain host and port";
return false;
}
const int port = item["port"].asInt();
if (port < 1 || port > 65535) {
*error = "STUN server port must be between 1 and 65535";
return false;
}
parsed.push_back({item["host"].asString(),
static_cast<uint16_t>(port)});
}
if (parsed.size() != 2) {
*error = "Exactly two STUN servers are required";
return false;
}
*servers = std::move(parsed);
return true;
}
bool IsValidStunHost(const std::string& host) {
return !host.empty() && host.size() <= 253
&& std::none_of(host.begin(), host.end(), [](unsigned char ch) {
return std::isspace(ch) != 0;
});
}
} // namespace
bool LoadClientConfig(const std::string& path, ClientConfig* config,
bool* existed, std::string* error) {
std::ifstream input(path, std::ios::binary);
if (!input.is_open()) {
*existed = false;
return true;
}
*existed = true;
Json::Value root;
Json::CharReaderBuilder builder;
std::string parseErrors;
if (!Json::parseFromStream(builder, input, &root, &parseErrors)
|| !root.isObject()) {
*error = "Invalid JSON object: " + path;
return false;
}
ReadString(root, "rendezvous_address", &config->rendezvousAddress);
ReadInt(root, "rendezvous_port", &config->rendezvousPort);
ReadString(root, "room_id", &config->roomId);
ReadString(root, "peer_id", &config->peerId);
ReadString(root, "auth_token", &config->authToken);
ReadString(root, "adapter_name", &config->adapterName);
ReadString(root, "local_tun_ipv4", &config->localTunIpv4);
ReadInt(root, "tun_prefix", &config->tunPrefix);
ReadInt(root, "tun_mtu", &config->tunMtu);
ReadBool(root, "auto_config_ipv4", &config->autoConfigIpv4);
ReadInt(root, "keepalive_interval", &config->keepaliveInterval);
ReadInt(root, "peer_timeout", &config->peerTimeout);
ReadBool(root, "dummy_traffic_enabled", &config->dummyTrafficEnabled);
ReadInt(root, "punch_timeout", &config->punchTimeout);
ReadInt(root, "nat_punch_attempt_limit", &config->natPunchAttemptLimit);
if (root.isMember("nat_punch_profile")) {
if (!root["nat_punch_profile"].isString()
|| !ParseNatPunchProfile(root["nat_punch_profile"].asString(),
&config->natPunchProfile)) {
*error = "nat_punch_profile must be balanced or aggressive";
return false;
}
}
if (!ReadStunServers(root, &config->stunServers, error)) return false;
if (root.isMember("traversal_modes") && root["traversal_modes"].isString()
&& !ParseTraversalModes(root["traversal_modes"].asString(),
&config->traversalModes, error)) {
*error = "Invalid traversal_modes: " + *error;
return false;
}
ReadBool(root, "ipv6_accept_inbound", &config->ipv6AcceptInbound);
ReadInt(root, "ipv6_listen_port", &config->ipv6ListenPort);
ReadString(root, "ipv6_probe_host", &config->ipv6ProbeHost);
ReadInt(root, "ipv6_probe_port", &config->ipv6ProbePort);
ReadInt(root, "ipv6_fallback_timeout", &config->ipv6FallbackTimeout);
ReadInt(root, "log_level", &config->logLevel);
ReadInt(root, "rendezvous_retry_delay_seconds",
&config->rendezvousRetryDelaySeconds);
ReadBool(root, "auto_wait_for_peer", &config->autoWaitForPeer);
ReadBool(root, "close_to_minimize", &config->closeToMinimize);
config->rendezvousPort = std::clamp(config->rendezvousPort, 1, 65535);
config->tunPrefix = std::clamp(config->tunPrefix, 0, 32);
config->tunMtu = std::clamp(config->tunMtu, 576, 9000);
config->keepaliveInterval = std::clamp(config->keepaliveInterval, 1, 300);
config->peerTimeout = std::clamp(config->peerTimeout,
config->keepaliveInterval + 1, 3600);
config->punchTimeout = std::clamp(config->punchTimeout, 1, 600);
config->natPunchAttemptLimit = std::clamp(
config->natPunchAttemptLimit, 1, 10);
config->ipv6ListenPort = std::clamp(config->ipv6ListenPort, 0, 65535);
config->ipv6ProbePort = std::clamp(config->ipv6ProbePort, 1, 65535);
config->ipv6FallbackTimeout = std::clamp(config->ipv6FallbackTimeout, 1, 120);
config->logLevel = std::clamp(config->logLevel, 0, 3);
config->rendezvousRetryDelaySeconds = std::clamp(
config->rendezvousRetryDelaySeconds, 1, 3600);
return true;
}
bool SaveClientConfig(const std::string& path, const ClientConfig& config,
std::string* error) {
Json::Value root;
root["rendezvous_address"] = config.rendezvousAddress;
root["rendezvous_port"] = config.rendezvousPort;
root["room_id"] = config.roomId;
root["peer_id"] = config.peerId;
root["auth_token"] = config.authToken;
root["adapter_name"] = config.adapterName;
root["local_tun_ipv4"] = config.localTunIpv4;
root["tun_prefix"] = config.tunPrefix;
root["tun_mtu"] = config.tunMtu;
root["auto_config_ipv4"] = config.autoConfigIpv4;
root["keepalive_interval"] = config.keepaliveInterval;
root["peer_timeout"] = config.peerTimeout;
root["dummy_traffic_enabled"] = config.dummyTrafficEnabled;
root["punch_timeout"] = config.punchTimeout;
root["nat_punch_attempt_limit"] = config.natPunchAttemptLimit;
root["nat_punch_profile"] = NatPunchProfileName(config.natPunchProfile);
Json::Value stunServers(Json::arrayValue);
for (const auto& server : config.stunServers) {
Json::Value item(Json::objectValue);
item["host"] = server.host;
item["port"] = server.port;
stunServers.append(std::move(item));
}
root["stun_servers"] = std::move(stunServers);
root["traversal_modes"] = SerializeTraversalModes(config.traversalModes);
root["ipv6_accept_inbound"] = config.ipv6AcceptInbound;
root["ipv6_listen_port"] = config.ipv6ListenPort;
root["ipv6_probe_host"] = config.ipv6ProbeHost;
root["ipv6_probe_port"] = config.ipv6ProbePort;
root["ipv6_fallback_timeout"] = config.ipv6FallbackTimeout;
root["log_level"] = config.logLevel;
root["rendezvous_retry_delay_seconds"] = config.rendezvousRetryDelaySeconds;
root["auto_wait_for_peer"] = config.autoWaitForPeer;
root["close_to_minimize"] = config.closeToMinimize;
std::ofstream output(path, std::ios::binary | std::ios::trunc);
if (!output.is_open()) {
*error = "Cannot save configuration: " + path;
return false;
}
Json::StreamWriterBuilder writerBuilder;
writerBuilder["indentation"] = " ";
output << Json::writeString(writerBuilder, root) << '\n';
output.flush();
if (!output.good()) {
*error = "Cannot write configuration: " + path;
return false;
}
return true;
}
bool ValidateClientConfig(const ClientConfig& config, std::string* error) {
if (config.rendezvousAddress.empty()) {
*error = "Rendezvous server is required";
return false;
}
if (!IsSafeControlField(config.roomId)) { *error = "Invalid room ID"; return false; }
if (!IsSafeControlField(config.peerId)) { *error = "Invalid peer ID"; return false; }
if (!config.authToken.empty() && !IsSafeControlField(config.authToken)) {
*error = "Invalid auth token";
return false;
}
in_addr address{};
if (!ParseIpv4(config.localTunIpv4, &address)) {
*error = "Invalid local TUN IPv4";
return false;
}
const bool anyModeEnabled = std::any_of(
config.traversalModes.begin(), config.traversalModes.end(),
[](const auto& setting) { return setting.enabled; });
if (!anyModeEnabled) {
*error = "Enable at least one traversal mode";
return false;
}
const auto natPunchMode = std::find_if(
config.traversalModes.begin(), config.traversalModes.end(),
[](const auto& setting) {
return setting.mode == TraversalMode::NatPunch;
});
if (natPunchMode != config.traversalModes.end()
&& natPunchMode->enabled) {
if (config.stunServers.size() != 2
|| !IsValidStunHost(config.stunServers[0].host)
|| !IsValidStunHost(config.stunServers[1].host)) {
*error = "Adaptive NAT Punch requires two valid STUN servers";
return false;
}
if (config.stunServers[0].host == config.stunServers[1].host
&& config.stunServers[0].port == config.stunServers[1].port) {
*error = "STUN A and STUN B must be different servers";
return false;
}
}
const auto ipv6Mode = std::find_if(
config.traversalModes.begin(), config.traversalModes.end(),
[](const auto& setting) { return setting.mode == TraversalMode::Ipv6; });
if (ipv6Mode != config.traversalModes.end() && ipv6Mode->enabled
&& config.ipv6ProbeHost.empty()) {
*error = "IPv6 probe host is required";
return false;
}
return true;
}
// The clamps mirror LoadClientConfig, so a config that came through the loader
// converts without further checks; UI-edited values are clamped here again.
Config ToEngineConfig(const ClientConfig& config,
const std::string& targetPeerId) {
Config output;
output.rendezvous_addr = config.rendezvousAddress;
output.rendezvous_port = static_cast<uint16_t>(
std::clamp(config.rendezvousPort, 1, 65535));
output.room_id = config.roomId;
output.peer_id = config.peerId;
output.target_peer_id = targetPeerId;
output.auth_token = config.authToken;
output.adapter_name = config.adapterName;
output.local_tun_ipv4 = config.localTunIpv4;
output.tun_prefix = static_cast<uint8_t>(std::clamp(config.tunPrefix, 0, 32));
output.tun_mtu = static_cast<uint16_t>(std::clamp(config.tunMtu, 576, 9000));
output.auto_config_ipv4 = config.autoConfigIpv4;
output.keepalive_interval = static_cast<uint16_t>(
std::clamp(config.keepaliveInterval, 1, 300));
output.peer_timeout = static_cast<uint16_t>(
std::clamp(config.peerTimeout, output.keepalive_interval + 1, 3600));
output.dummy_traffic_enabled = config.dummyTrafficEnabled;
output.punch_timeout = static_cast<uint16_t>(
std::clamp(config.punchTimeout, 1, 600));
output.nat_punch_attempt_limit = static_cast<uint16_t>(
std::clamp(config.natPunchAttemptLimit, 1, 10));
output.nat_punch_profile = config.natPunchProfile;
output.stun_servers = config.stunServers;
output.traversal_modes = config.traversalModes;
output.ipv6_accept_inbound = config.ipv6AcceptInbound;
output.ipv6_listen_port = static_cast<uint16_t>(
std::clamp(config.ipv6ListenPort, 0, 65535));
output.ipv6_probe_host = config.ipv6ProbeHost;
output.ipv6_probe_port = static_cast<uint16_t>(
std::clamp(config.ipv6ProbePort, 1, 65535));
output.ipv6_fallback_timeout = static_cast<uint16_t>(
std::clamp(config.ipv6FallbackTimeout, 1, 120));
output.log_level = static_cast<LogLevel>(std::clamp(config.logLevel, 0, 3));
return output;
}