From c5d0fc41ea6fafa15f4c172adec88dd4f88122ea Mon Sep 17 00:00:00 2001 From: Patrice Vignola Date: Wed, 15 Feb 2023 12:21:43 -0800 Subject: [PATCH] Add option to specify manual port (#45) --- include/arg_parser.hpp | 3 +++ src/arg_parser.cpp | 9 +++++++++ src/arg_parser_test.cpp | 2 ++ src/main.cpp | 9 ++++++++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/arg_parser.hpp b/include/arg_parser.hpp index 64cabc7..5009c62 100644 --- a/include/arg_parser.hpp +++ b/include/arg_parser.hpp @@ -2,6 +2,7 @@ #define ARG_PARSER_HPP #include +#include #include #include @@ -25,6 +26,7 @@ class ArgParser { absl::string_view get_help_message() const { return help_message_; }; bool signing_disabled() const { return signing_disabled_; } std::vector get_entropy() const; + const absl::optional& get_port() const { return port_; } private: AuthProvider auth_provider_; @@ -33,6 +35,7 @@ class ArgParser { bool signing_disabled_; bool help_wanted_; EntropyType entropy_type_; + absl::optional port_; }; #endif // ARG_PARSER_HPP \ No newline at end of file diff --git a/src/arg_parser.cpp b/src/arg_parser.cpp index fb8f54e..c35352c 100644 --- a/src/arg_parser.cpp +++ b/src/arg_parser.cpp @@ -35,6 +35,11 @@ ArgParser::ArgParser(int argc, const char* const* argv) { "addresses listed.", cxxopts::value()->default_value("false"), ""); + options.add_option("", "p", "port", + "Port to run the authentication server on. If not " + "provided, a random port will be chosen.", + cxxopts::value(), ""); + options.add_option("", "h", "help", "Print usage", cxxopts::value(), ""); @@ -64,6 +69,10 @@ ArgParser::ArgParser(int argc, const char* const* argv) { throw std::runtime_error( absl::StrCat("invalid entropy type `", entropy_type, "`")); } + + if (parse_result.count("port") > 0) { + port_ = parse_result["port"].as(); + } } catch (const cxxopts::option_not_exists_exception& ex) { throw std::runtime_error(absl::StrCat( "error when parsing arguments: ", ex.what(), "\n", help_message_)); diff --git a/src/arg_parser_test.cpp b/src/arg_parser_test.cpp index f431204..a5a68d5 100644 --- a/src/arg_parser_test.cpp +++ b/src/arg_parser_test.cpp @@ -21,6 +21,8 @@ static constexpr const char* usage_message = R"( recommended to verify that the contributions listed in the transcript are actually generated by the addresses listed. + -p, --port arg Port to run the authentication server on. If not + provided, a random port will be chosen. -h, --help Print usage )"; diff --git a/src/main.cpp b/src/main.cpp index b4ca0e1..86cabf5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,7 @@ #include #include +// NOLINTNEXTLINE(readability-function-cognitive-complexity) int main(int argc, char** argv) { try { const ArgParser arg_parser(argc, argv); @@ -28,7 +29,13 @@ int main(int argc, char** argv) { SecretGenerator<> secret_generator(arg_parser.get_entropy(), num_secrets); const auto& auth_provider = arg_parser.get_auth_provider(); - const auto port = port_picker::pick_unused_port(); + + uint16_t port = 0; + if (arg_parser.get_port().has_value()) { + port = *arg_parser.get_port(); + } else { + port = port_picker::pick_unused_port(); + } std::promise auth_info_promise; auto auth_callback = [&auth_info_promise](AuthInfo&& auth_info) {