From 63e354026fcd8c745fbc5b92f16e73d8d7970b18 Mon Sep 17 00:00:00 2001 From: MichaelLee Date: Thu, 2 Jan 2020 15:57:35 +0800 Subject: [PATCH] Add: force passive server ip Add: force an IP address in PASS replies. -- for NAT --- config.yaml | 7 ++++++- src/configure.cpp | 8 ++++++++ src/configure.h | 1 + src/data_handle.cpp | 5 +++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/config.yaml b/config.yaml index 29fa52b..d1bf74f 100644 --- a/config.yaml +++ b/config.yaml @@ -1,4 +1,5 @@ server: + # 监听端口,0.0.0.0 允许任意主机连接 listen_host: "0.0.0.0" # server 端监听的主机地址 listen_port: 2222 # 命令连接的监听的端口 @@ -7,4 +8,8 @@ PORT: PASV: # PASV 模式,指定该模式下可以使用的端口范围 port_low: 22222 # 数据连接所用的端口下限 - port_high: 55555 # 数据连接所用的端口上限 \ No newline at end of file + port_high: 55555 # 数据连接所用的端口上限 + # PASV 模式中强制使用的 IP 地址,在 NAT 网关后的主机可能不能自动解析到公网 IP + # 或者在网关采用动态 IP 的情况下有用 + force_passive_ip: "" # 若使用程序自动解析的 ip 地址,则填为空字符串 "" + diff --git a/src/configure.cpp b/src/configure.cpp index d868489..e545d0a 100644 --- a/src/configure.cpp +++ b/src/configure.cpp @@ -11,6 +11,7 @@ namespace configure { const char *config_file = "config.yaml"; /* 配置文件 */ std::string SERVER_LISTEN_HOST; + std::string FORCE_PASSIVE_SERVER_IP; int SERVER_LISTEN_PORT; int PORT_CONN_PORT; @@ -60,6 +61,13 @@ namespace configure { exit(EXIT_FAILURE); } + if (pasv_config["force_passive_ip"].IsDefined()) { + FORCE_PASSIVE_SERVER_IP = pasv_config["force_passive_ip"].as(); + utility::debug_info(std::string("Force Server IP Address: ") + FORCE_PASSIVE_SERVER_IP); + } else { + FORCE_PASSIVE_SERVER_IP = ""; + } + utility::debug_info(std::string("PASV port low: ") + std::to_string(PASV_PORT_LOW)); utility::debug_info(std::string("PASV port high: ") + std::to_string(PASV_PORT_HIGH)); assert(PASV_PORT_LOW > 0 && PASV_PORT_LOW < 65536); diff --git a/src/configure.h b/src/configure.h index 98c526c..0952a37 100644 --- a/src/configure.h +++ b/src/configure.h @@ -11,6 +11,7 @@ namespace configure { /* 配置信息 */ extern std::string SERVER_LISTEN_HOST; + extern std::string FORCE_PASSIVE_SERVER_IP; extern int SERVER_LISTEN_PORT; extern int PORT_CONN_PORT; diff --git a/src/data_handle.cpp b/src/data_handle.cpp index d0b6688..888d5b3 100644 --- a/src/data_handle.cpp +++ b/src/data_handle.cpp @@ -175,6 +175,11 @@ void CLDataHandle::do_get_fd() { } const char *CLDataHandle::get_local_ip() { + if (!configure::FORCE_PASSIVE_SERVER_IP.empty()) { + m_ip_addr = configure::FORCE_PASSIVE_SERVER_IP; + return m_ip_addr.c_str(); + } + if (!m_ip_addr.empty() && m_b_pasv_mode) return m_ip_addr.c_str(); char ip[48] = {0};