Add IP stack port remapping
This commit is contained in:
+18
-3
@@ -1356,9 +1356,24 @@ asio::awaitable<void> IPStackSimulator::open_server_connection(
|
|||||||
string conn_str = this->str_for_tcp_connection(c, conn);
|
string conn_str = this->str_for_tcp_connection(c, conn);
|
||||||
|
|
||||||
// Figure out which logical port the connection should go to
|
// Figure out which logical port the connection should go to
|
||||||
auto port_config_it = this->state->number_to_port_config.find(conn->server_port);
|
uint16_t effective_server_port = conn->server_port;
|
||||||
|
auto remap_it = this->state->ip_stack_port_remap.find(effective_server_port);
|
||||||
|
if (remap_it != this->state->ip_stack_port_remap.end()) {
|
||||||
|
this->log.info_f(
|
||||||
|
"Remapping IP stack TCP destination port {} to {} for connection {}",
|
||||||
|
effective_server_port,
|
||||||
|
remap_it->second,
|
||||||
|
conn_str);
|
||||||
|
effective_server_port = remap_it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto port_config_it = this->state->number_to_port_config.find(effective_server_port);
|
||||||
if (port_config_it == this->state->number_to_port_config.end()) {
|
if (port_config_it == this->state->number_to_port_config.end()) {
|
||||||
this->log.error_f("TCP connection {} is to undefined port {}", conn_str, conn->server_port);
|
this->log.error_f(
|
||||||
|
"TCP connection {} is to undefined port {} after remap from {}",
|
||||||
|
conn_str,
|
||||||
|
effective_server_port,
|
||||||
|
conn->server_port);
|
||||||
co_await this->close_tcp_connection(c, conn);
|
co_await this->close_tcp_connection(c, conn);
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
@@ -1371,7 +1386,7 @@ asio::awaitable<void> IPStackSimulator::open_server_connection(
|
|||||||
co_await this->close_tcp_connection(c, conn);
|
co_await this->close_tcp_connection(c, conn);
|
||||||
co_return;
|
co_return;
|
||||||
} else {
|
} else {
|
||||||
this->state->game_server->connect_channel(conn->server_channel, conn->server_port, port_config->behavior);
|
this->state->game_server->connect_channel(conn->server_channel, effective_server_port, port_config->behavior);
|
||||||
this->log.info_f("Connected TCP connection {} to game server", conn_str);
|
this->log.info_f("Connected TCP connection {} to game server", conn_str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -737,6 +737,23 @@ void ServerState::load_config_early() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this->set_port_configuration(parse_port_configuration(this->config_json->at("PortConfiguration")));
|
this->set_port_configuration(parse_port_configuration(this->config_json->at("PortConfiguration")));
|
||||||
|
|
||||||
|
this->ip_stack_port_remap.clear();
|
||||||
|
try {
|
||||||
|
for (const auto& item : this->config_json->at("IPStackPortRemap").as_dict()) {
|
||||||
|
uint64_t from_port = stoull(item.first, nullptr, 0);
|
||||||
|
uint64_t to_port = item.second->as_int();
|
||||||
|
|
||||||
|
if (from_port > 0xFFFF || to_port > 0xFFFF) {
|
||||||
|
throw runtime_error("IPStackPortRemap port number is out of range");
|
||||||
|
}
|
||||||
|
|
||||||
|
this->ip_stack_port_remap.emplace(
|
||||||
|
static_cast<uint16_t>(from_port),
|
||||||
|
static_cast<uint16_t>(to_port));
|
||||||
|
}
|
||||||
|
} catch (const out_of_range&) {
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
auto spec = this->parse_port_spec(this->config_json->at("DNSServerPort"));
|
auto spec = this->parse_port_spec(this->config_json->at("DNSServerPort"));
|
||||||
this->dns_server_addr = std::move(spec.first);
|
this->dns_server_addr = std::move(spec.first);
|
||||||
|
|||||||
@@ -112,6 +112,7 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
|
|||||||
std::string name;
|
std::string name;
|
||||||
std::unordered_map<std::string, std::shared_ptr<PortConfiguration>> name_to_port_config;
|
std::unordered_map<std::string, std::shared_ptr<PortConfiguration>> name_to_port_config;
|
||||||
std::unordered_map<uint16_t, std::shared_ptr<PortConfiguration>> number_to_port_config;
|
std::unordered_map<uint16_t, std::shared_ptr<PortConfiguration>> number_to_port_config;
|
||||||
|
std::unordered_map<uint16_t, uint16_t> ip_stack_port_remap;
|
||||||
std::string username;
|
std::string username;
|
||||||
std::string dns_server_addr;
|
std::string dns_server_addr;
|
||||||
uint16_t dns_server_port = 0;
|
uint16_t dns_server_port = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user