mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
feat(net): internal-only servers listen on loopback when master is local
With bind_ip empty, chat, dashboard and UGC bind 127.0.0.1 only when master is a loopback host; otherwise nothing changes. A set bind_ip always wins. Auth, master and the worlds are unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -161,7 +161,10 @@ locally. See [docs/UgcServer.md](docs/UgcServer.md).
|
||||
* **Traffic:** every server counts its packets and HTTP requests and reports them for the dashboard.
|
||||
* **Databases:** MySQL/MariaDB and SQLite kept in step by parity tests; new tables for the dashboard, UGC, API keys,
|
||||
traffic and more; settings kept in the database for the dashboard, with the `.ini` files listing every setting.
|
||||
* `bind_ip` for the server sockets; login steps timed from auth through master to the world.
|
||||
* `bind_ip` for the server sockets (empty: chat, dashboard and UGC listen on loopback when master is local); servers master
|
||||
starts no longer inherit its sockets; login steps timed from auth through master to the world.
|
||||
* **Audit round 5:** mission/item preconditions read as AND-groups of OR-alternatives, waypoint delay, bounce and start
|
||||
fixes, and the property browser friends flag.
|
||||
* **Build identifier:** every server logs its version, branch and commit at startup and sends it in its handshake
|
||||
reply, after the bytes the client reads.
|
||||
* **CDClient:** the hot lookup tables (ComponentsRegistry, ItemComponent, Objects) are read straight from the client's
|
||||
@@ -410,6 +413,8 @@ Navigate to `build/sharedconfig.ini` and fill in the following fields:
|
||||
* `masterconfig.ini` contains options related to permissions you want to run your servers with.
|
||||
* `sharedconfig.ini` contains several options that are shared across all servers
|
||||
* `bind_ip` sets the local IPv4 address the servers listen on (empty, the default, listens on all interfaces). Players are still sent `external_ip`, so the two can differ behind NAT or a proxy.
|
||||
* Left empty, the chat, dashboard and UGC servers (only other servers connect to their UDP sockets) listen on `127.0.0.1` when master is on the same machine, and on all interfaces when it is not, so servers on other machines still connect. A `bind_ip` set in `sharedconfig.ini` or one server's ini always wins; `0.0.0.0` forces all interfaces.
|
||||
* Servers that master starts no longer inherit its sockets, so master's port is free the moment master stops.
|
||||
* `worldconfig.ini` contains several options to turn on Quality of Life improvements should you want them. If you would like the most vanilla experience possible, you will need to turn some of these settings off.
|
||||
|
||||
## Verify your setup
|
||||
|
||||
@@ -274,6 +274,22 @@ std::optional<std::string> GeneralUtils::ParseBindAddress(const std::string_view
|
||||
return std::string{ trimmed };
|
||||
}
|
||||
|
||||
bool GeneralUtils::IsLoopbackHost(const std::string_view host) {
|
||||
const auto first = host.find_first_not_of(" \t\r\n");
|
||||
if (first == std::string_view::npos) return false;
|
||||
const auto trimmed = host.substr(first, host.find_last_not_of(" \t\r\n") - first + 1);
|
||||
if (CaseInsensitiveStringCompare(trimmed, "localhost") || trimmed == "::1" || trimmed == "[::1]") return true;
|
||||
// 127.x.y.z must be a whole, valid address
|
||||
const auto parsed = ParseBindAddress(trimmed);
|
||||
return parsed && parsed->rfind("127.", 0) == 0;
|
||||
}
|
||||
|
||||
std::optional<std::string> GeneralUtils::ResolveBindAddress(const std::string_view bindIp, const bool internalOnly, const std::string_view masterHost) {
|
||||
if (bindIp.find_first_not_of(" \t\r\n") != std::string_view::npos) return ParseBindAddress(bindIp);
|
||||
if (internalOnly && IsLoopbackHost(masterHost)) return std::string{ "127.0.0.1" };
|
||||
return std::string{};
|
||||
}
|
||||
|
||||
std::vector<std::string> GeneralUtils::SplitString(const std::string_view str, const char delimiter) {
|
||||
std::vector<std::string> vector = std::vector<std::string>();
|
||||
std::string current = "";
|
||||
|
||||
@@ -169,6 +169,20 @@ namespace GeneralUtils {
|
||||
*/
|
||||
[[nodiscard]] std::optional<std::string> ParseBindAddress(const std::string_view value);
|
||||
|
||||
/**
|
||||
* Whether a host name or address is this machine only: localhost, 127.0.0.0/8, ::1 or [::1].
|
||||
*/
|
||||
[[nodiscard]] bool IsLoopbackHost(const std::string_view host);
|
||||
|
||||
/**
|
||||
* The address a server's RakNet sockets bind to. A bind_ip that is set (anything but empty, 0.0.0.0 included)
|
||||
* always wins and is read like ParseBindAddress. When it is empty, a server only other servers connect to
|
||||
* (internalOnly) binds to 127.0.0.1 if master is on this machine (masterHost is a loopback host), and everything
|
||||
* else listens on all interfaces (an empty string), as it always did.
|
||||
* @return nullopt when bind_ip is set but not an IPv4 address
|
||||
*/
|
||||
[[nodiscard]] std::optional<std::string> ResolveBindAddress(const std::string_view bindIp, const bool internalOnly, const std::string_view masterHost);
|
||||
|
||||
/**
|
||||
* Transparent string hasher - used to allow string_view key lookups for maps storing std::string keys
|
||||
* https://www.reddit.com/r/cpp_questions/comments/12xw3sn/find_stdstring_view_in_unordered_map_with/jhki225/
|
||||
|
||||
@@ -274,9 +274,12 @@ bool dServer::IsConnected(const SystemAddress& sysAddr) {
|
||||
}
|
||||
|
||||
bool dServer::Startup() {
|
||||
// bind_ip picks the local interface the sockets listen on; players are still sent external_ip
|
||||
// bind_ip picks the local interface the sockets listen on; players are still sent external_ip.
|
||||
// Left empty, the servers only other servers connect to (chat, dashboard, UGC) listen on this machine only when
|
||||
// master is on it too; with master elsewhere they stay reachable, as before.
|
||||
const auto bindIP = mConfig->GetValue("bind_ip");
|
||||
const auto bindAddress = GeneralUtils::ParseBindAddress(bindIP);
|
||||
const bool internalOnly = mServerType == ServiceType::CHAT || mServerType == ServiceType::DASHBOARD || mServerType == ServiceType::UGC;
|
||||
const auto bindAddress = GeneralUtils::ResolveBindAddress(bindIP, internalOnly, mMasterIP);
|
||||
if (!bindAddress) {
|
||||
LOG("bind_ip \"%s\" is not an IPv4 address (leave it empty to listen on all interfaces)", bindIP.c_str());
|
||||
return false;
|
||||
|
||||
@@ -4,7 +4,7 @@ DarkflameServer ships with a web dashboard for managing accounts, moderating, an
|
||||
the separate NexusDashboard. Master starts it when `enable_dashboard=1` is set in `masterconfig.ini`, and it listens on
|
||||
the `port` in `dashboardconfig.ini` (2006 by default). It also talks to master over UDP on `net_port` and the port after
|
||||
it (2010 and 2011 by default); keep those clear of the other servers' ports. Those UDP ports listen on `bind_ip` from
|
||||
`sharedconfig.ini` like every other server; the web page itself listens on `listen_ip`.
|
||||
`sharedconfig.ini` like every other server (empty: loopback only when master is on this machine); the web page itself listens on `listen_ip`.
|
||||
|
||||
This page is for server operators. Everything below the first section is optional.
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@ external_ip=localhost
|
||||
# This only changes where the servers listen: players are still sent external_ip, so
|
||||
# use it when the public address isn't one this machine can bind to (NAT, a proxy).
|
||||
# The servers refuse to start if this isn't an IPv4 address.
|
||||
# Set here or in one server's own ini (127.0.0.2 for one child on another loopback address, say), it always wins.
|
||||
# Left empty, the chat, dashboard and UGC servers (only other servers connect to their UDP sockets) listen on
|
||||
# 127.0.0.1 when master is on this machine (master_ip is localhost, 127.x.x.x or ::1) and on all interfaces when
|
||||
# master is elsewhere. Auth, master and the worlds listen on all interfaces. Use 0.0.0.0 to force all interfaces.
|
||||
bind_ip=
|
||||
|
||||
# 0 or 1, should not compile chat hash map to file
|
||||
|
||||
@@ -23,3 +23,39 @@ TEST(BindAddressTests, RejectsEverythingElse) {
|
||||
EXPECT_FALSE(GeneralUtils::ParseBindAddress(bad).has_value()) << bad;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(BindAddressTests, LoopbackHosts) {
|
||||
for (const auto* host : { "localhost", "LocalHost", "127.0.0.1", "127.0.0.2", "127.255.255.254", "::1", "[::1]", " 127.0.0.1\r" }) {
|
||||
EXPECT_TRUE(GeneralUtils::IsLoopbackHost(host)) << host;
|
||||
}
|
||||
for (const auto* host : { "", "0.0.0.0", "10.0.0.5", "192.168.1.20", "example.com", "128.0.0.1", "127.0.0", "127.0.0.1.5", "1127.0.0.1" }) {
|
||||
EXPECT_FALSE(GeneralUtils::IsLoopbackHost(host)) << host;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(BindAddressTests, InternalOnlyServersBindLoopbackWhenMasterIsLocal) {
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("", true, "localhost"), "127.0.0.1");
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("", true, "127.0.0.1"), "127.0.0.1");
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress(" ", true, "::1"), "127.0.0.1");
|
||||
}
|
||||
|
||||
TEST(BindAddressTests, InternalOnlyServersListenOnAllWhenMasterIsRemote) {
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("", true, "10.0.0.5"), std::string{});
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("", true, "master.example.com"), std::string{});
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("", true, ""), std::string{});
|
||||
}
|
||||
|
||||
TEST(BindAddressTests, ExplicitBindIpOverridesTheDefault) {
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("127.0.0.2", true, "10.0.0.5"), "127.0.0.2");
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("192.168.1.20", true, "localhost"), "192.168.1.20");
|
||||
// 0.0.0.0 set on purpose means all interfaces even next to a local master
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("0.0.0.0", true, "localhost"), std::string{});
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("localhost", true, "10.0.0.5"), "127.0.0.1");
|
||||
EXPECT_FALSE(GeneralUtils::ResolveBindAddress("example.com", true, "localhost").has_value());
|
||||
}
|
||||
|
||||
TEST(BindAddressTests, GameFacingServersAreUnchanged) {
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("", false, "localhost"), std::string{});
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("", false, "127.0.0.1"), std::string{});
|
||||
EXPECT_EQ(GeneralUtils::ResolveBindAddress("192.168.1.20", false, "localhost"), "192.168.1.20");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user