исправления для защиты запуска вебки с некорректными аргументами командной строки
This commit is contained in:
parent
51431210f0
commit
a6a4391123
32
src/main.cpp
32
src/main.cpp
@ -538,6 +538,26 @@ int main(int argc, char *argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "nossl") != 0 && strcmp(argv[1], "ssl") != 0) {
|
||||
std::cerr << "Unsupported ssl mode: " << argv[1] << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int serverPort;
|
||||
try {
|
||||
size_t idx = 0;
|
||||
serverPort = std::stoi(std::string(argv[3]), &idx);
|
||||
if (serverPort < 0 || serverPort > 0xffff) {
|
||||
throw std::invalid_argument("Out of range");
|
||||
}
|
||||
if (idx != strlen(argv[3])) {
|
||||
throw std::invalid_argument("Invalid number");
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Wrong server port `" << argv[3] << "`: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
init_logging();
|
||||
boost::log::core::get()->add_thread_attribute("Scope", boost::log::attributes::named_scope());
|
||||
|
||||
@ -563,11 +583,11 @@ int main(int argc, char *argv[]) {
|
||||
std::unique_ptr<http::server::Server> s;
|
||||
|
||||
if (strcmp(argv[1], "nossl") == 0) {
|
||||
s = std::make_unique<http::server::Server>(argv[2], argv[3]);
|
||||
BOOST_LOG_TRIVIAL(info) << "Run server on " << argv[2] << ":" << serverPort;
|
||||
s = std::make_unique<http::server::Server>(argv[2], serverPort);
|
||||
resources.registerResources(*s);
|
||||
s->run();
|
||||
|
||||
} else if (strcmp(argv[1], "ssl") == 0) {
|
||||
} else {
|
||||
std::vector<char> cert; http::resource::loadFile("cert.pem", cert);
|
||||
std::vector<char> key; http::resource::loadFile("key.pem", key);
|
||||
std::vector<char> dh; http::resource::loadFile("dh.pem", dh);
|
||||
@ -584,12 +604,10 @@ int main(int argc, char *argv[]) {
|
||||
ctx->use_private_key(boost::asio::buffer(key), ssl::context::file_format::pem);
|
||||
ctx->use_tmp_dh(boost::asio::buffer(dh));
|
||||
|
||||
s = std::make_unique<http::server::Server>(argv[2], argv[3], ctx);
|
||||
BOOST_LOG_TRIVIAL(info) << "Run server on " << argv[2] << ":" << serverPort;
|
||||
s = std::make_unique<http::server::Server>(argv[2], serverPort, ctx);
|
||||
resources.registerResources(*s);
|
||||
s->run();
|
||||
} else {
|
||||
std::cerr << "Unsupported ssl mode: " << argv[1] << std::endl;
|
||||
return 1;
|
||||
}
|
||||
} catch (std::exception &e) {
|
||||
BOOST_LOG_TRIVIAL(error) << e.what() << std::endl;
|
||||
|
@ -23,7 +23,7 @@ namespace http::server {
|
||||
connections_.clear();
|
||||
}
|
||||
|
||||
Server::Server(const std::string &address, const std::string &port)
|
||||
Server::Server(const std::string &address, int port)
|
||||
: io_context_(1), signals_(io_context_), acceptor_(io_context_) {
|
||||
// Register to handle the signals that indicate when the server should exit.
|
||||
// It is safe to register for the same signal multiple times in a program,
|
||||
@ -39,7 +39,7 @@ namespace http::server {
|
||||
// Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
|
||||
boost::asio::ip::tcp::resolver resolver(io_context_);
|
||||
boost::asio::ip::tcp::endpoint endpoint =
|
||||
*resolver.resolve(address, port).begin();
|
||||
*resolver.resolve(address, std::to_string(port)).begin();
|
||||
acceptor_.open(endpoint.protocol());
|
||||
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
|
||||
acceptor_.bind(endpoint);
|
||||
@ -48,7 +48,7 @@ namespace http::server {
|
||||
doAccept();
|
||||
}
|
||||
|
||||
Server::Server(const std::string &address, const std::string &port, std::shared_ptr<boost::asio::ssl::context> ctx):
|
||||
Server::Server(const std::string &address, int port, std::shared_ptr<boost::asio::ssl::context> ctx):
|
||||
ssl_ctx(std::move(ctx)), io_context_(1), signals_(io_context_), acceptor_(io_context_) {
|
||||
// Register to handle the signals that indicate when the server should exit.
|
||||
// It is safe to register for the same signal multiple times in a program,
|
||||
@ -63,7 +63,7 @@ namespace http::server {
|
||||
|
||||
// Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
|
||||
boost::asio::ip::tcp::resolver resolver(io_context_);
|
||||
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(address, port).begin();
|
||||
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(address, std::to_string(port)).begin();
|
||||
acceptor_.open(endpoint.protocol());
|
||||
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
|
||||
acceptor_.bind(endpoint);
|
||||
|
@ -44,8 +44,8 @@ namespace http::server {
|
||||
Server &operator=(const Server &) = delete;
|
||||
|
||||
/// Construct the server to listen on the specified TCP address and port
|
||||
explicit Server(const std::string &address, const std::string &port);
|
||||
explicit Server(const std::string &address, const std::string &port, std::shared_ptr<boost::asio::ssl::context> ctx);
|
||||
explicit Server(const std::string &address, int port);
|
||||
explicit Server(const std::string &address, int port, std::shared_ptr<boost::asio::ssl::context> ctx);
|
||||
|
||||
std::vector<std::unique_ptr<resource::BasicResource>> resources;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user