Skip to content

Commit

Permalink
Add: force passive server ip
Browse files Browse the repository at this point in the history
Add: force an IP address in PASS replies. -- for NAT
  • Loading branch information
MakingL committed Jan 2, 2020
1 parent 5195922 commit 63e3540
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
server:
# 监听端口,0.0.0.0 允许任意主机连接
listen_host: "0.0.0.0" # server 端监听的主机地址
listen_port: 2222 # 命令连接的监听的端口

Expand All @@ -7,4 +8,8 @@ PORT:

PASV: # PASV 模式,指定该模式下可以使用的端口范围
port_low: 22222 # 数据连接所用的端口下限
port_high: 55555 # 数据连接所用的端口上限
port_high: 55555 # 数据连接所用的端口上限
# PASV 模式中强制使用的 IP 地址,在 NAT 网关后的主机可能不能自动解析到公网 IP
# 或者在网关采用动态 IP 的情况下有用
force_passive_ip: "" # 若使用程序自动解析的 ip 地址,则填为空字符串 ""

8 changes: 8 additions & 0 deletions src/configure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<std::string>();
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);
Expand Down
1 change: 1 addition & 0 deletions src/configure.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/data_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 63e3540

Please sign in to comment.