forked from erigontech/silkworm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry.cpp
143 lines (117 loc) · 4.77 KB
/
sentry.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
138
139
140
141
142
143
/*
Copyright 2022 The Silkworm Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <filesystem>
#include <CLI/CLI.hpp>
#include <boost/process/environment.hpp>
#include <silkworm/buildinfo.h>
#include <silkworm/core/common/util.hpp>
#include <silkworm/node/rpc/common/util.hpp>
#include <silkworm/sentry/sentry.hpp>
#include <silkworm/sentry/settings.hpp>
#include "common.hpp"
using namespace silkworm;
using namespace silkworm::cmd;
using namespace silkworm::sentry;
Settings sentry_parse_cli_settings(int argc, char* argv[]) {
CLI::App cli{"Sentry - P2P proxy"};
Settings settings;
settings.build_info = silkworm_get_buildinfo();
add_logging_options(cli, settings.log_settings);
cli.add_option("--sentry.api.addr", settings.api_address, "GRPC API endpoint")
->capture_default_str()
->check(IPEndPointValidator(/*allow_empty=*/true));
cli.add_option("--port", settings.port)
->description("Network listening port for incoming peers TCP connections and discovery UDP requests")
->check(CLI::Range(1024, 65535))
->capture_default_str();
auto nat_option = cli.add_option("--nat", [&settings](const CLI::results_t& results) {
return lexical_cast(results[0], settings.nat);
});
nat_option->description(
"NAT port mapping mechanism (none|extip:<IP>)\n"
"- none no NAT, use a local IP as public\n"
"- extip:1.2.3.4 use the given public IP");
nat_option->default_str("none");
add_option_num_contexts(cli, settings.num_contexts);
add_option_wait_mode(cli, settings.wait_mode);
add_option_data_dir(cli, settings.data_dir_path);
auto node_key_path_option = cli.add_option("--nodekey", [&settings](const CLI::results_t& results) {
try {
settings.node_key = {{std::filesystem::path(results[0])}};
return true;
} catch (const std::exception& e) {
log::Error() << e.what();
return false;
}
});
node_key_path_option->description("P2P node key file");
auto node_key_hex_option = cli.add_option("--nodekeyhex", [&settings](const CLI::results_t& results) {
auto key_bytes = from_hex(results[0]);
if (key_bytes) {
settings.node_key = {{key_bytes.value()}};
}
return key_bytes.has_value();
});
node_key_hex_option->description("P2P node key as a hex string");
auto static_peers_option = cli.add_option("--staticpeers", [&settings](const CLI::results_t& results) {
try {
for (auto& result : results) {
if (result.empty()) continue;
settings.static_peers.emplace_back(result);
}
} catch (const std::exception& e) {
log::Error() << e.what();
return false;
}
return true;
});
static_peers_option->description("Peers enode URLs to connect to without discovery");
static_peers_option->type_size(1, INT_MAX);
cli.add_option("--maxpeers", settings.max_peers)
->description("Maximum number of P2P network peers")
->check(CLI::Range(0, 1000))
->capture_default_str();
try {
cli.parse(argc, argv);
} catch (const CLI::ParseError& pe) {
cli.exit(pe);
throw;
}
return settings;
}
void sentry_main(Settings settings) {
log::init(settings.log_settings);
log::set_thread_name("main");
// TODO(canepat): this could be an option in Silkworm logging facility
silkworm::rpc::Grpc2SilkwormLogGuard log_guard;
Sentry sentry{std::move(settings)};
sentry.start();
const auto pid = boost::this_process::get_id();
const auto tid = std::this_thread::get_id();
log::Info() << "Sentry is now running [pid=" << pid << ", main thread=" << tid << "]";
sentry.join();
log::Info() << "Sentry exiting [pid=" << pid << ", main thread=" << tid << "]";
}
int main(int argc, char* argv[]) {
try {
sentry_main(sentry_parse_cli_settings(argc, argv));
} catch (const CLI::ParseError& pe) {
return -1;
} catch (const std::exception& e) {
log::Critical() << "Sentry exiting due to exception: " << e.what();
return -2;
} catch (...) {
log::Critical() << "Sentry exiting due to unexpected exception";
return -3;
}
}