From 48b85abe1d09e76324e69e5047835f71a4b9e515 Mon Sep 17 00:00:00 2001 From: taynpg Date: Mon, 7 Apr 2025 11:49:13 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E7=9B=B4=E8=BF=9E=E5=8F=82=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/config.h | 1 + client/main.cpp | 59 +++++++++++++++++++++++++++++++------------------ 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/client/config.h b/client/config.h index 36a486c..c699fd7 100644 --- a/client/config.h +++ b/client/config.h @@ -16,6 +16,7 @@ struct TransmSet { struct CmdParam { std::string removeValue; std::string appendValue; + std::string connectValue; bool showValue{false}; long use_config{-1}; bool parsed{false}; diff --git a/client/main.cpp b/client/main.cpp index 90fb73e..9c90fa8 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -30,6 +30,7 @@ int parse_cmd(int argc, char** argv, CmdParam& param) app.add_flag("-d, --direct", param.direct_use, "添加服务器时直接使用此服务器。"); app.add_flag("-l, --last", param.last_use, "直接使用之前最后一次使用的服务器。"); app.add_flag("-n, --null", param.null_use, "先运行在选择服务器。"); + app.add_option("-c, --connect", param.connectValue, "直连服务器((地址格式:127.0.0.1:9898)。"); if (argc == 1) { std::cout << app.help() << std::endl; @@ -179,42 +180,58 @@ int main(int argc, char* argv[]) if (!param.parsed) { return 0; } - if (!exec_cmd(param, run)) { - TLOGW("exec_cmd failed!"); - return -1; - } - if (!run && !param.direct_use && !param.last_use && !param.null_use) { - return 0; - } std::string ip; long port{}; - std::vector sets; - if (!g_Config->read_ini(sets)) { + if (!param.connectValue.empty()) { + std::regex pattern(R"((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d+))"); + std::smatch matches; + + if (std::regex_match(param.connectValue, matches, pattern) && matches.size() == 3) { + ip = matches[1].str(); + port = std::stol(matches[2].str()); + run = true; + } else { + TLOGE("Invalid connect value [{}]", param.connectValue); + return -1; + } + } else if (!exec_cmd(param, run)) { + TLOGW("exec_cmd failed!"); return -1; } - if (param.null_use) { - if (!select_server(sets, ip, port)) { + if (!run && !param.direct_use && !param.last_use && !param.null_use) { + return 0; + } + + if (ip.empty()) { + std::vector sets; + if (!g_Config->read_ini(sets)) { return -1; } - } else { - if (param.last_use) { - if (!g_Config->get_last_use(ip, port)) { + + if (param.null_use) { + if (!select_server(sets, ip, port)) { return -1; } } else { - TransmSet use; - if (!g_Config->get_ini(sets, param.use_config, use)) { - TLOGW("Not found config by num:[{}]", param.use_config); - return -1; + if (param.last_use) { + if (!g_Config->get_last_use(ip, port)) { + return -1; + } + } else { + TransmSet use; + if (!g_Config->get_ini(sets, param.use_config, use)) { + TLOGW("Not found config by num:[{}]", param.use_config); + return -1; + } + ip = use.ip; + port = use.port; } - ip = use.ip; - port = use.port; } + g_Config->save_last_use(ip, port); } - g_Config->save_last_use(ip, port); TLOGI("Build At {} {} under {} on {}", __DATE__, __TIME__, VERSION_GIT_COMMIT, VERSION_GIT_BRANCH); TLOGI("use ip => [{}], port => [{}]", ip, port); CClient client;