-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
137 lines (124 loc) · 4.01 KB
/
main.cpp
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
#include <liburing.h>
#include <signal.h>
#include <filesystem>
#include "src/Engine.hpp"
#include "src/Logger.hpp"
#include "src/Settings.hpp"
#define QUEUE_DEPTH 512
Engine* engine_ = nullptr;
void SigIntHandler(int signo) {
printf("^C pressed. Shutting down.\n");
io_uring_queue_exit(engine_->ring_);
exit(0);
}
void ParserArguments(int argc, char** argv) {
for (int i = 0; i < argc; ++i) {
if (strcmp(argv[i], "-debug") == 0) {
Log::PrintDebug = true;
continue;
}
if (strcmp(argv[i], "-silent") == 0) {
Log::NoLog = true;
continue;
}
if (strcmp(argv[i], "-ssl") == 0) {
Settings::UseSSL = true;
continue;
}
if (memcmp(argv[i], "-buffer-size=", 13) == 0) {
std::string tmp(argv[i]);
Settings::HttpBufferSize = stoi(tmp.substr(13));
continue;
}
if (memcmp(argv[i], "-server-port=", 13) == 0) {
std::string tmp(argv[i]);
Settings::ServerPort = stoi(tmp.substr(13));
continue;
}
if (memcmp(argv[i], "-dns-port=", 10) == 0) {
std::string tmp(argv[i]);
Settings::DnsPort = stoi(tmp.substr(10));
continue;
}
if (strcmp(argv[i], "-no-cache") == 0) {
Settings::UseCache = false;
continue;
}
if (strcmp(argv[i], "-hls") == 0) {
Settings::HLSMode = true;
continue;
}
if (strcmp(argv[i], "-astra") == 0) {
Settings::AstraMode = true;
continue;
}
if (memcmp(argv[i], "-cache-dir=", 11) == 0) {
std::string tmp(argv[i]);
Settings::CacheDir = tmp.substr(11);
continue;
}
if (memcmp(argv[i], "-listen-mode=", 13) == 0) {
std::string tmp(argv[i]);
std::string mode = tmp.substr(13);
if (mode == "unix") {
Settings::ListenMode = 3;
} else if (mode == "ipv6") {
Settings::ListenMode = 2;
} else {
Settings::ListenMode = 1;
}
continue;
}
if (memcmp(argv[i], "-unix-path=", 11) == 0) {
std::string tmp(argv[i]);
Settings::UnixPath = tmp.substr(11);
continue;
}
if (memcmp(argv[i], "-proxy=", 7) == 0) {
std::string tmp(argv[i]);
Settings::Proxy = tmp.substr(7);
if (Settings::Proxy.front() != '/') {
Settings::Proxy = "/" + Settings::Proxy;
}
continue;
}
if (memcmp(argv[i], "-base-url=", 10) == 0) {
std::string tmp(argv[i]);
Settings::BaseUrl = tmp.substr(10);
if (Settings::BaseUrl.front() != '/') {
Settings::BaseUrl = "/" + Settings::BaseUrl;
}
continue;
}
if (memcmp(argv[i], "-host-file=", 11) == 0) {
std::string tmp(argv[i]);
Settings::HostFile = tmp.substr(11);
continue;
}
}
}
int main(int argc, char** argv) {
ParserArguments(argc, argv);
if (Settings::CacheDir.empty()) {
std::filesystem::path cwd = std::filesystem::current_path() / "cache/";
Settings::CacheDir = cwd.string();
}
if (!std::filesystem::is_directory(Settings::CacheDir) ||
!std::filesystem::exists(Settings::CacheDir)) {
std::filesystem::create_directory(Settings::CacheDir);
}
engine_ = new Engine();
engine_->SetupListeningSocket(Settings::ServerPort);
if (Settings::ListenMode == 3) {
Log(__FILE__, __LINE__)
<< "Started server on " << Settings::UnixPath;
} else {
Log(__FILE__, __LINE__)
<< "Started server on localhost:" << Settings::ServerPort;
}
signal(SIGINT, SigIntHandler);
signal(SIGPIPE, SIG_IGN);
io_uring_queue_init(QUEUE_DEPTH, engine_->ring_, 0);
engine_->Run();
return 0;
}