This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
221 lines (188 loc) · 8.5 KB
/
main.cpp
File metadata and controls
221 lines (188 loc) · 8.5 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
#include <deque>
#include <glib.h>
#include <libwebsockets.h>
#include "CxxPtr/libconfigDestroy.h"
#include "Common/ConfigHelpers.h"
#include "Common/LwsLog.h"
#include "Http/Log.h"
#include "Http/Config.h"
#include "Signalling/Log.h"
#include "Log.h"
#include "SignalingServer.h"
static const auto Log = SignalingServerLog;
static bool LoadConfig(Config* config)
{
const std::deque<std::string> configDirs = ::ConfigDirs();
if(configDirs.empty())
return false;
Config loadedConfig = *config;
bool someConfigFound = false;
for(const std::string& configDir: configDirs) {
const std::string configFile = configDir + "/signaling-server.conf";
if(!g_file_test(configFile.c_str(), G_FILE_TEST_IS_REGULAR)) {
Log()->info("Config \"{}\" not found", configFile);
continue;
}
someConfigFound = true;
config_t config;
config_init(&config);
ConfigDestroy ConfigDestroy(&config);
Log()->info("Loading config \"{}\"", configFile);
if(!config_read_file(&config, configFile.c_str())) {
Log()->error("Fail load config. {}. {}:{}",
config_error_text(&config),
configFile,
config_error_line(&config));
return false;
}
config_setting_t* serverConfig = config_lookup(&config, "server");
if(serverConfig && CONFIG_TRUE == config_setting_is_group(serverConfig)) {
int frontPort = 0;
if(CONFIG_TRUE == config_setting_lookup_int(serverConfig, "front-port", &frontPort)) {
loadedConfig.frontPort = static_cast<unsigned short>(frontPort);
}
int backPort = 0;
if(CONFIG_TRUE == config_setting_lookup_int(serverConfig, "back-port", &backPort)) {
loadedConfig.backPort = static_cast<unsigned short>(backPort);
}
int httpPort = 0;
if(CONFIG_TRUE == config_setting_lookup_int(serverConfig, "http-port", &httpPort)) {
loadedConfig.httpConfig.port = static_cast<unsigned short>(httpPort);
}
int loopbackOnly = false;
if(CONFIG_TRUE == config_setting_lookup_bool(serverConfig, "loopback-only", &loopbackOnly)) {
loadedConfig.httpConfig.bindToLoopbackOnly = loopbackOnly != false;
loadedConfig.bindToLoopbackOnly = loopbackOnly != false;
}
}
config_setting_t* stunServerConfig = config_lookup(&config, "stun");
if(stunServerConfig && CONFIG_TRUE == config_setting_is_group(stunServerConfig)) {
const char* server = nullptr;
if(CONFIG_TRUE == config_setting_lookup_string(stunServerConfig, "server", &server)) {
loadedConfig.stunServer = server;
}
}
config_setting_t* turnServerConfig = config_lookup(&config, "turn");
if(turnServerConfig && CONFIG_TRUE == config_setting_is_group(turnServerConfig)) {
const char* server = nullptr;
if(CONFIG_TRUE == config_setting_lookup_string(turnServerConfig, "server", &server)) {
loadedConfig.turnServer = server;
}
const char* username = nullptr;
if(CONFIG_TRUE == config_setting_lookup_string(turnServerConfig, "username", &username)) {
loadedConfig.turnUsername = username;
}
const char* credential = nullptr;
if(CONFIG_TRUE == config_setting_lookup_string(turnServerConfig, "credential", &credential)) {
loadedConfig.turnCredential = credential;
}
const char* secret = nullptr;
if(CONFIG_TRUE == config_setting_lookup_string(turnServerConfig, "static-auth-secret", &secret)) {
loadedConfig.turnStaticAuthSecret = secret;
}
int passwordTTL = 0;
if(CONFIG_TRUE == config_setting_lookup_int(turnServerConfig, "password-ttl", &passwordTTL)) {
if(passwordTTL > 0)
loadedConfig.turnPasswordTTL = passwordTTL;
}
}
config_setting_t* turnsServerConfig = config_lookup(&config, "turns");
if(turnsServerConfig && CONFIG_TRUE == config_setting_is_group(turnsServerConfig)) {
const char* server = nullptr;
if(CONFIG_TRUE == config_setting_lookup_string(turnsServerConfig, "server", &server)) {
loadedConfig.turnsServer = server;
}
const char* username = nullptr;
if(CONFIG_TRUE == config_setting_lookup_string(turnsServerConfig, "username", &username)) {
loadedConfig.turnsUsername = username;
}
const char* credential = nullptr;
if(CONFIG_TRUE == config_setting_lookup_string(turnsServerConfig, "credential", &credential)) {
loadedConfig.turnsCredential = credential;
}
const char* secret = nullptr;
if(CONFIG_TRUE == config_setting_lookup_string(turnsServerConfig, "static-auth-secret", &secret)) {
loadedConfig.turnsStaticAuthSecret = secret;
}
int passwordTTL = 0;
if(CONFIG_TRUE == config_setting_lookup_int(turnsServerConfig, "password-ttl", &passwordTTL)) {
if(passwordTTL > 0)
loadedConfig.turnsPasswordTTL = passwordTTL;
}
}
config_setting_t* sourcesConfig = config_lookup(&config, "sources");
if(sourcesConfig && CONFIG_TRUE == config_setting_is_list(sourcesConfig)) {
const int sourcesCount = config_setting_length(sourcesConfig);
for(int sourceIdx = 0; sourceIdx < sourcesCount; ++sourceIdx) {
config_setting_t* sourceConfig =
config_setting_get_elem(sourcesConfig, sourceIdx);
if(!sourceConfig || CONFIG_FALSE == config_setting_is_group(sourceConfig)) {
Log()->warn("Wrong source config format. Source skipped.");
break;
}
const char* name;
if(CONFIG_FALSE == config_setting_lookup_string(sourceConfig, "name", &name)) {
Log()->warn("Missing source name. Source skipped.");
break;
}
const char* token;
if(CONFIG_FALSE == config_setting_lookup_string(sourceConfig, "token", &token)) {
Log()->warn(
"Missing auth token for source \"{}\". Source skipped.",
name);
break;
}
loadedConfig.backAuthTokens.emplace(name, token);
}
}
config_setting_t* debugConfig = config_lookup(&config, "debug");
if(debugConfig && CONFIG_TRUE == config_setting_is_group(debugConfig)) {
int logLevel = 0;
if(CONFIG_TRUE == config_setting_lookup_int(debugConfig, "log-level", &logLevel)) {
if(logLevel > 0) {
loadedConfig.logLevel =
static_cast<spdlog::level::level_enum>(
spdlog::level::critical - std::min<int>(logLevel, spdlog::level::critical));
}
}
int lwsLogLevel = 0;
if(CONFIG_TRUE == config_setting_lookup_int(debugConfig, "lws-log-level", &lwsLogLevel)) {
if(lwsLogLevel > 0) {
loadedConfig.lwsLogLevel =
static_cast<spdlog::level::level_enum>(
spdlog::level::critical - std::min<int>(lwsLogLevel, spdlog::level::critical));
}
}
}
}
if(!someConfigFound)
return false;
bool success = true;
if(!loadedConfig.frontPort) {
Log()->error("Missing \"server.front-port\"");
success = false;
}
if(!loadedConfig.backPort) {
Log()->error("Missing \"server.back-port\"");
success = false;
}
if(success)
*config = loadedConfig;
return success;
}
int main(int argc, char *argv[])
{
Config config {};
config.httpConfig.bindToLoopbackOnly = false;
#ifdef SNAPCRAFT_BUILD
if(const gchar* snapPath = g_getenv("SNAP"))
config.httpConfig.wwwRoot = std::string(snapPath) + "/www";
#endif
if(!LoadConfig(&config))
return -1;
InitLwsLogger(config.lwsLogLevel);
InitHttpServerLogger(config.logLevel);
InitWsServerLogger(config.logLevel);
InitSignalingServerLogger(config.logLevel);
return SignalingServerMain(config);
}