mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Merge #5064
5064: Add HTTP masterserver registering and HTTP masterserver r=def- a=heinrich5991 Registering ----------- The idea is that game servers push their server info to the masterservers every 15 seconds or when the server info changes, but not more than once per second. The game servers do not support the old registering protocol anymore, the backward compatibility is handled by the masterserver. The register call is a HTTP POST to a URL like `https://master1.ddnet.tw/ddnet/15/register` and looks like this: ```http POST /ddnet/15/register HTTP/1.1 Address: tw-0.6+udp://connecting-address.invalid:8303 Secret: 81fa3955-6f83-4290-818d-31c0906b1118 Challenge-Secret: 81fa3955-6f83-4290-818d-31c0906b1118:tw0.6/ipv6 Info-Serial: 0 { "max_clients": 64, "max_players": 64, "passworded": false, "game_type": "TestDDraceNetwork", "name": "My DDNet server", "map": { "name": "dm1", "sha256": "0b0c481d77519c32fbe85624ef16ec0fa9991aec7367ad538bd280f28d8c26cf", "size": 5805 }, "version": "0.6.4, 16.0.3", "clients": [] } ``` The `Address` header declares that the server wants to register itself as a `tw-0.6+udp` server, i.e. a server speaking a Teeworlds-0.6-compatible protocol. The free-form `Secret` header is used as a server identity, the server list will be deduplicated via this secret. The free-form `Challenge-Secret` is sent back via UDP for a port forward check. This might have security implications as the masterserver can be asked to send a UDP packet containing some user-controlled bytes. This is somewhat mitigated by the fact that it can only go to an attacker-controlled IP address. The `Info-Serial` header is an integer field that should increase each time the server info (in the body) changes. The masterserver uses that field to ensure that it doesn't use old server infos. The body is a free-form JSON object set by the game server. It should contain certain keys in the correct form to be accepted by clients. The body is optional if the masterserver already confirmed the reception of the info with the given `Info-Serial`. Not shown in this payload is the `Connless-Token` header that is used for Teeworlds 0.7 style communication. Also not shown is the `Challenge-Token` that should be included once the server receives the challenge token via UDP. The masterserver responds with a `200 OK` with a body like this: ``` {"status":"success"} ``` The `status` field can be `success` if the server was successfully registered on the masterserver, `need_challenge` if the masterserver wants the correct `Challenge-Token` header before the register process is successful, `need_info` if the server sent an empty body but the masterserver doesn't actually know the server info. It can also be `error` if the request was malformed, only in this case an HTTP status code except `200 OK` is sent. Synchronization --------------- The masterserver keeps state and outputs JSON files every second. ```json { "servers": [ { "addresses": [ "tw-0.6+udp://127.0.0.1:8303", "tw-0.6+udp://[::1]:8303" ], "info_serial": 0, "info": { "max_clients": 64, "max_players": 64, "passworded": false, "game_type": "TestDDraceNetwork", "name": "My DDNet server", "map": { "name": "dm1", "sha256": "0b0c481d77519c32fbe85624ef16ec0fa9991aec7367ad538bd280f28d8c26cf", "size": 5805 }, "version": "0.6.4, 16.0.3", "clients": [] } } ] } ``` `servers.json` (or configured by `--out`) is a server list that is compatible with DDNet 15.5+ clients. It is a JSON object containing a single key `servers` with a list of game servers. Each game server is represented by a JSON object with an `addresses` key containing a list of all known addresses of the server and an `info` key containing the free-form server info sent by the game server. The free-form `info` JSON object re-encoded by the master server and thus canonicalized and stripped of any whitespace characters outside strings. ```json { "kind": "mastersrv", "now": 1816002, "secrets": { "tw-0.6+udp://127.0.0.1:8303": { "ping_time": 1811999, "secret": "42d8f991-f2fa-46e5-a9ae-ebcc93846feb" }, "tw-0.6+udp://[::1]:8303": { "ping_time": 1811999, "secret": "42d8f991-f2fa-46e5-a9ae-ebcc93846feb" } }, "servers": { "42d8f991-f2fa-46e5-a9ae-ebcc93846feb": { "info_serial": 0, "info": { "max_clients": 64, "max_players": 64, "passworded": false, "game_type": "TestDDraceNetwork", "name": "My DDNet server", "map": { "name": "dm1", "sha256": "0b0c481d77519c32fbe85624ef16ec0fa9991aec7367ad538bd280f28d8c26cf", "size": 5805 }, "version": "0.6.4, 16.0.3", "clients": [] } } } } ``` `--write-dump` outputs a JSON file compatible with `--read-dump-dir`, this can be used to synchronize servers across different masterservers. `--read-dump-dir` is also used to ingest servers from the backward compatibility layer that pings each server for their server info using the old protocol. The `kind` field describe that this is `mastersrv` output and not from a `backcompat`. This is used for prioritizing `mastersrv` information over `backcompat` information. The `now` field contains an integer describing the current time in milliseconds relative an unspecified epoch that is fixed for each JSON file. This is done instead of using the current time as the epoch for better compression of non-changing data. `secrets` is a map from each server address and to a JSON object containing the last ping time (`ping_time`) in milliseconds relative to the same epoch as before, and the server secret (`secret`) that is used to unify server infos from different addresses of the same logical server. `servers` is a map from the aforementioned `secret`s to the corresponding `info_serial` and `info`. ```json [ "tw-0.6+udp://127.0.0.1:8303", "tw-0.6+udp://[::1]:8303" ] ``` `--write-addresses` outputs a JSON file containing all addresses corresponding to servers that are registered to HTTP masterservers. It does not contain the servers that are obtained via backward compatibility measures. This file can be used by an old-style masterserver to also list new-style servers without the game servers having to register there. An implementation of this can be found at https://github.com/heinrich5991/teeworlds/tree/mastersrv_6_backcompat for Teeworlds 0.5/0.6 masterservers and at https://github.com/heinrich5991/teeworlds/tree/mastersrv_7_backcompat for Teeworlds 0.7 masterservers. All these JSON files can be sent over the network in an efficient way using https://github.com/heinrich5991/twmaster-collect. It establishes a zstd-compressed TCP connection authenticated by a string token that is sent in plain-text. It watches the specified file and transmits it every time it changes. Due to the zstd-compression, the data sent over the network is similar to the size of a diff. Implementation -------------- The masterserver implementation was done in Rust. The current gameserver register implementation doesn't support more than one masterserver for registering. Co-authored-by: heinrich5991 <heinrich5991@gmail.com>
This commit is contained in:
commit
e7140caf8b
|
@ -1656,7 +1656,6 @@ set_src(ENGINE_INTERFACE GLOB src/engine
|
||||||
kernel.h
|
kernel.h
|
||||||
keys.h
|
keys.h
|
||||||
map.h
|
map.h
|
||||||
masterserver.h
|
|
||||||
message.h
|
message.h
|
||||||
server.h
|
server.h
|
||||||
serverbrowser.h
|
serverbrowser.h
|
||||||
|
@ -1709,6 +1708,7 @@ set_src(ENGINE_SHARED GLOB_RECURSE src/engine/shared
|
||||||
map.cpp
|
map.cpp
|
||||||
map.h
|
map.h
|
||||||
masterserver.cpp
|
masterserver.cpp
|
||||||
|
masterserver.h
|
||||||
memheap.cpp
|
memheap.cpp
|
||||||
memheap.h
|
memheap.h
|
||||||
netban.cpp
|
netban.cpp
|
||||||
|
@ -1801,10 +1801,6 @@ set(GAME_GENERATED_SHARED
|
||||||
src/game/generated/protocol7.h
|
src/game/generated/protocol7.h
|
||||||
src/game/generated/protocolglue.h
|
src/game/generated/protocolglue.h
|
||||||
)
|
)
|
||||||
set(MASTERSRV_SHARED
|
|
||||||
src/mastersrv/mastersrv.cpp
|
|
||||||
src/mastersrv/mastersrv.h
|
|
||||||
)
|
|
||||||
set(DEPS ${DEP_JSON} ${DEP_MD5} ${ZLIB_DEP})
|
set(DEPS ${DEP_JSON} ${DEP_MD5} ${ZLIB_DEP})
|
||||||
|
|
||||||
# Libraries
|
# Libraries
|
||||||
|
@ -1823,8 +1819,7 @@ set(LIBS
|
||||||
# Targets
|
# Targets
|
||||||
add_library(engine-shared EXCLUDE_FROM_ALL OBJECT ${ENGINE_INTERFACE} ${ENGINE_SHARED} ${ENGINE_UUID_SHARED} ${BASE})
|
add_library(engine-shared EXCLUDE_FROM_ALL OBJECT ${ENGINE_INTERFACE} ${ENGINE_SHARED} ${ENGINE_UUID_SHARED} ${BASE})
|
||||||
add_library(game-shared EXCLUDE_FROM_ALL OBJECT ${GAME_SHARED} ${GAME_GENERATED_SHARED})
|
add_library(game-shared EXCLUDE_FROM_ALL OBJECT ${GAME_SHARED} ${GAME_GENERATED_SHARED})
|
||||||
add_library(mastersrv-shared EXCLUDE_FROM_ALL OBJECT ${MASTERSRV_SHARED})
|
list(APPEND TARGETS_OWN engine-shared game-shared)
|
||||||
list(APPEND TARGETS_OWN engine-shared game-shared mastersrv-shared)
|
|
||||||
|
|
||||||
if(DISCORD AND NOT DISCORD_DYNAMIC)
|
if(DISCORD AND NOT DISCORD_DYNAMIC)
|
||||||
add_library(discord-shared SHARED IMPORTED)
|
add_library(discord-shared SHARED IMPORTED)
|
||||||
|
@ -2108,7 +2103,6 @@ if(CLIENT)
|
||||||
${DEPS_CLIENT}
|
${DEPS_CLIENT}
|
||||||
$<TARGET_OBJECTS:engine-shared>
|
$<TARGET_OBJECTS:engine-shared>
|
||||||
$<TARGET_OBJECTS:game-shared>
|
$<TARGET_OBJECTS:game-shared>
|
||||||
$<TARGET_OBJECTS:mastersrv-shared>
|
|
||||||
)
|
)
|
||||||
else()
|
else()
|
||||||
add_executable(${TARGET_CLIENT} WIN32
|
add_executable(${TARGET_CLIENT} WIN32
|
||||||
|
@ -2118,7 +2112,6 @@ if(CLIENT)
|
||||||
${DEPS_CLIENT}
|
${DEPS_CLIENT}
|
||||||
$<TARGET_OBJECTS:engine-shared>
|
$<TARGET_OBJECTS:engine-shared>
|
||||||
$<TARGET_OBJECTS:game-shared>
|
$<TARGET_OBJECTS:game-shared>
|
||||||
$<TARGET_OBJECTS:mastersrv-shared>
|
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
target_link_libraries(${TARGET_CLIENT} ${LIBS_CLIENT})
|
target_link_libraries(${TARGET_CLIENT} ${LIBS_CLIENT})
|
||||||
|
@ -2305,7 +2298,6 @@ if(SERVER)
|
||||||
${SERVER_ICON}
|
${SERVER_ICON}
|
||||||
$<TARGET_OBJECTS:engine-shared>
|
$<TARGET_OBJECTS:engine-shared>
|
||||||
$<TARGET_OBJECTS:game-shared>
|
$<TARGET_OBJECTS:game-shared>
|
||||||
$<TARGET_OBJECTS:mastersrv-shared>
|
|
||||||
)
|
)
|
||||||
target_link_libraries(${TARGET_SERVER} ${LIBS_SERVER})
|
target_link_libraries(${TARGET_SERVER} ${LIBS_SERVER})
|
||||||
list(APPEND TARGETS_OWN ${TARGET_SERVER})
|
list(APPEND TARGETS_OWN ${TARGET_SERVER})
|
||||||
|
@ -2326,21 +2318,13 @@ endif()
|
||||||
########################################################################
|
########################################################################
|
||||||
|
|
||||||
if(TOOLS)
|
if(TOOLS)
|
||||||
set(MASTERSRV_SRC src/mastersrv/main_mastersrv.cpp)
|
|
||||||
set_src(TWPING_SRC GLOB src/twping twping.cpp)
|
set_src(TWPING_SRC GLOB src/twping twping.cpp)
|
||||||
|
|
||||||
set(TARGET_MASTERSRV mastersrv)
|
|
||||||
set(TARGET_TWPING twping)
|
set(TARGET_TWPING twping)
|
||||||
|
add_executable(${TARGET_TWPING} EXCLUDE_FROM_ALL ${TWPING_SRC} $<TARGET_OBJECTS:engine-shared> ${DEPS})
|
||||||
add_executable(${TARGET_MASTERSRV} EXCLUDE_FROM_ALL ${MASTERSRV_SRC} $<TARGET_OBJECTS:engine-shared> $<TARGET_OBJECTS:mastersrv-shared> ${DEPS})
|
|
||||||
|
|
||||||
add_executable(${TARGET_TWPING} EXCLUDE_FROM_ALL ${TWPING_SRC} $<TARGET_OBJECTS:engine-shared> $<TARGET_OBJECTS:mastersrv-shared> ${DEPS})
|
|
||||||
|
|
||||||
target_link_libraries(${TARGET_MASTERSRV} ${LIBS})
|
|
||||||
target_link_libraries(${TARGET_TWPING} ${LIBS})
|
target_link_libraries(${TARGET_TWPING} ${LIBS})
|
||||||
|
|
||||||
list(APPEND TARGETS_OWN ${TARGET_MASTERSRV} ${TARGET_TWPING})
|
list(APPEND TARGETS_OWN ${TARGET_TWPING})
|
||||||
list(APPEND TARGETS_LINK ${TARGET_MASTERSRV} ${TARGET_TWPING})
|
list(APPEND TARGETS_LINK ${TARGET_TWPING})
|
||||||
|
|
||||||
set(TARGETS_TOOLS)
|
set(TARGETS_TOOLS)
|
||||||
set_src(TOOLS_SRC GLOB src/tools
|
set_src(TOOLS_SRC GLOB src/tools
|
||||||
|
@ -2350,7 +2334,6 @@ if(TOOLS)
|
||||||
crapnet.cpp
|
crapnet.cpp
|
||||||
dilate.cpp
|
dilate.cpp
|
||||||
dummy_map.cpp
|
dummy_map.cpp
|
||||||
fake_server.cpp
|
|
||||||
map_convert_07.cpp
|
map_convert_07.cpp
|
||||||
map_diff.cpp
|
map_diff.cpp
|
||||||
map_extract.cpp
|
map_extract.cpp
|
||||||
|
@ -2376,9 +2359,6 @@ if(TOOLS)
|
||||||
if(TOOL MATCHES "^config_")
|
if(TOOL MATCHES "^config_")
|
||||||
list(APPEND EXTRA_TOOL_SRC "src/tools/config_common.h")
|
list(APPEND EXTRA_TOOL_SRC "src/tools/config_common.h")
|
||||||
endif()
|
endif()
|
||||||
if(TOOL MATCHES "^fake_server")
|
|
||||||
list(APPEND TOOL_DEPS $<TARGET_OBJECTS:mastersrv-shared>)
|
|
||||||
endif()
|
|
||||||
set(EXCLUDE_FROM_ALL)
|
set(EXCLUDE_FROM_ALL)
|
||||||
if(DEV)
|
if(DEV)
|
||||||
set(EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL)
|
set(EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL)
|
||||||
|
@ -2502,10 +2482,9 @@ if(GTEST_FOUND OR DOWNLOAD_GTEST)
|
||||||
${TESTS_EXTRA}
|
${TESTS_EXTRA}
|
||||||
$<TARGET_OBJECTS:engine-shared>
|
$<TARGET_OBJECTS:engine-shared>
|
||||||
$<TARGET_OBJECTS:game-shared>
|
$<TARGET_OBJECTS:game-shared>
|
||||||
$<TARGET_OBJECTS:mastersrv-shared>
|
|
||||||
${DEPS}
|
${DEPS}
|
||||||
)
|
)
|
||||||
target_link_libraries(${TARGET_TESTRUNNER} ${LIBS} ${CURL_LIBRARIES} ${MYSQL_LIBRARIES} ${GTEST_LIBRARIES})
|
target_link_libraries(${TARGET_TESTRUNNER} ${LIBS} ${MYSQL_LIBRARIES} ${GTEST_LIBRARIES})
|
||||||
target_include_directories(${TARGET_TESTRUNNER} SYSTEM PRIVATE ${GTEST_INCLUDE_DIRS})
|
target_include_directories(${TARGET_TESTRUNNER} SYSTEM PRIVATE ${GTEST_INCLUDE_DIRS})
|
||||||
|
|
||||||
list(APPEND TARGETS_OWN ${TARGET_TESTRUNNER})
|
list(APPEND TARGETS_OWN ${TARGET_TESTRUNNER})
|
||||||
|
|
|
@ -44,6 +44,7 @@
|
||||||
#include <engine/shared/filecollection.h>
|
#include <engine/shared/filecollection.h>
|
||||||
#include <engine/shared/http.h>
|
#include <engine/shared/http.h>
|
||||||
#include <engine/shared/json.h>
|
#include <engine/shared/json.h>
|
||||||
|
#include <engine/shared/masterserver.h>
|
||||||
#include <engine/shared/network.h>
|
#include <engine/shared/network.h>
|
||||||
#include <engine/shared/packer.h>
|
#include <engine/shared/packer.h>
|
||||||
#include <engine/shared/protocol.h>
|
#include <engine/shared/protocol.h>
|
||||||
|
@ -56,8 +57,6 @@
|
||||||
|
|
||||||
#include <game/version.h>
|
#include <game/version.h>
|
||||||
|
|
||||||
#include <mastersrv/mastersrv.h>
|
|
||||||
|
|
||||||
#include <engine/client/demoedit.h>
|
#include <engine/client/demoedit.h>
|
||||||
#include <engine/client/serverbrowser.h>
|
#include <engine/client/serverbrowser.h>
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
#include <engine/graphics.h>
|
#include <engine/graphics.h>
|
||||||
#include <engine/input.h>
|
#include <engine/input.h>
|
||||||
#include <engine/map.h>
|
#include <engine/map.h>
|
||||||
#include <engine/masterserver.h>
|
|
||||||
#include <engine/shared/config.h>
|
#include <engine/shared/config.h>
|
||||||
#include <engine/shared/demo.h>
|
#include <engine/shared/demo.h>
|
||||||
#include <engine/shared/fifo.h>
|
#include <engine/shared/fifo.h>
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
|
|
||||||
#include <engine/shared/config.h>
|
#include <engine/shared/config.h>
|
||||||
#include <engine/shared/json.h>
|
#include <engine/shared/json.h>
|
||||||
|
#include <engine/shared/masterserver.h>
|
||||||
#include <engine/shared/memheap.h>
|
#include <engine/shared/memheap.h>
|
||||||
#include <engine/shared/network.h>
|
#include <engine/shared/network.h>
|
||||||
#include <engine/shared/protocol.h>
|
#include <engine/shared/protocol.h>
|
||||||
|
@ -27,8 +28,6 @@
|
||||||
#include <engine/serverbrowser.h>
|
#include <engine/serverbrowser.h>
|
||||||
#include <engine/storage.h>
|
#include <engine/storage.h>
|
||||||
|
|
||||||
#include <mastersrv/mastersrv.h>
|
|
||||||
|
|
||||||
#include <engine/external/json-parser/json.h>
|
#include <engine/external/json-parser/json.h>
|
||||||
|
|
||||||
#include <game/client/components/menus.h> // PAGE_DDNET
|
#include <game/client/components/menus.h> // PAGE_DDNET
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
#include <engine/config.h>
|
#include <engine/config.h>
|
||||||
#include <engine/console.h>
|
#include <engine/console.h>
|
||||||
#include <engine/external/json-parser/json.h>
|
#include <engine/external/json-parser/json.h>
|
||||||
#include <engine/masterserver.h>
|
|
||||||
#include <engine/serverbrowser.h>
|
#include <engine/serverbrowser.h>
|
||||||
#include <engine/shared/config.h>
|
#include <engine/shared/config.h>
|
||||||
#include <engine/shared/http.h>
|
#include <engine/shared/http.h>
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
||||||
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
||||||
#ifndef ENGINE_MASTERSERVER_H
|
|
||||||
#define ENGINE_MASTERSERVER_H
|
|
||||||
|
|
||||||
#include "kernel.h"
|
|
||||||
|
|
||||||
class IMasterServer : public IInterface
|
|
||||||
{
|
|
||||||
MACRO_INTERFACE("masterserver", 0)
|
|
||||||
public:
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
MAX_MASTERSERVERS = 4
|
|
||||||
};
|
|
||||||
|
|
||||||
virtual void Init() = 0;
|
|
||||||
virtual void SetDefault() = 0;
|
|
||||||
virtual int Load() = 0;
|
|
||||||
virtual int Save() = 0;
|
|
||||||
|
|
||||||
virtual int RefreshAddresses(int Nettype) = 0;
|
|
||||||
virtual void Update() = 0;
|
|
||||||
virtual bool IsRefreshing() const = 0;
|
|
||||||
virtual NETADDR GetAddr(int Index) const = 0;
|
|
||||||
virtual void SetCount(int Index, int Count) = 0;
|
|
||||||
virtual int GetCount(int Index) const = 0;
|
|
||||||
virtual const char *GetName(int Index) const = 0;
|
|
||||||
virtual bool IsValid(int Index) const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class IEngineMasterServer : public IMasterServer
|
|
||||||
{
|
|
||||||
MACRO_INTERFACE("enginemasterserver", 0)
|
|
||||||
public:
|
|
||||||
};
|
|
||||||
|
|
||||||
extern IEngineMasterServer *CreateEngineMasterServer();
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,329 +1,656 @@
|
||||||
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
||||||
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
||||||
#include <base/system.h>
|
|
||||||
#include <engine/console.h>
|
|
||||||
#include <engine/masterserver.h>
|
|
||||||
#include <engine/shared/config.h>
|
|
||||||
#include <engine/shared/network.h>
|
|
||||||
|
|
||||||
#include <mastersrv/mastersrv.h>
|
|
||||||
|
|
||||||
#include "register.h"
|
#include "register.h"
|
||||||
|
|
||||||
CRegister::CRegister(bool Sixup)
|
#include <base/log.h>
|
||||||
|
#include <engine/console.h>
|
||||||
|
#include <engine/engine.h>
|
||||||
|
#include <engine/shared/config.h>
|
||||||
|
#include <engine/shared/http.h>
|
||||||
|
#include <engine/shared/json.h>
|
||||||
|
#include <engine/shared/masterserver.h>
|
||||||
|
#include <engine/shared/network.h>
|
||||||
|
#include <engine/shared/uuid_manager.h>
|
||||||
|
|
||||||
|
class CRegister : public IRegister
|
||||||
{
|
{
|
||||||
m_Sixup = Sixup;
|
enum
|
||||||
m_pName = Sixup ? "regsixup" : "register";
|
{
|
||||||
m_LastTokenRequest = 0;
|
STATUS_NONE = 0,
|
||||||
|
STATUS_OK,
|
||||||
|
STATUS_NEEDCHALLENGE,
|
||||||
|
STATUS_NEEDINFO,
|
||||||
|
|
||||||
m_pNetServer = 0;
|
PROTOCOL_TW6_IPV6 = 0,
|
||||||
m_pMasterServer = 0;
|
PROTOCOL_TW6_IPV4,
|
||||||
m_pConsole = 0;
|
PROTOCOL_TW7_IPV6,
|
||||||
|
PROTOCOL_TW7_IPV4,
|
||||||
|
NUM_PROTOCOLS,
|
||||||
|
};
|
||||||
|
|
||||||
m_RegisterState = REGISTERSTATE_START;
|
static bool StatusFromString(int *pResult, const char *pString);
|
||||||
m_RegisterStateStart = 0;
|
static const char *ProtocolToScheme(int Protocol);
|
||||||
m_RegisterFirst = 1;
|
static const char *ProtocolToString(int Protocol);
|
||||||
m_RegisterCount = 0;
|
static bool ProtocolFromString(int *pResult, const char *pString);
|
||||||
|
static const char *ProtocolToSystem(int Protocol);
|
||||||
|
static IPRESOLVE ProtocolToIpresolve(int Protocol);
|
||||||
|
|
||||||
mem_zero(m_aMasterserverInfo, sizeof(m_aMasterserverInfo));
|
static void ConchainOnConfigChange(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
|
||||||
m_RegisterRegisteredServer = -1;
|
|
||||||
|
class CGlobal
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
~CGlobal()
|
||||||
|
{
|
||||||
|
lock_destroy(m_Lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CRegister::FeedToken(NETADDR Addr, SECURITY_TOKEN ResponseToken)
|
LOCK m_Lock = lock_create();
|
||||||
|
int m_InfoSerial GUARDED_BY(m_Lock) = -1;
|
||||||
|
int m_LatestSuccessfulInfoSerial GUARDED_BY(m_Lock) = -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CProtocol
|
||||||
{
|
{
|
||||||
Addr.port = 0;
|
class CShared
|
||||||
for(auto &MasterserverInfo : m_aMasterserverInfo)
|
|
||||||
{
|
{
|
||||||
NETADDR Addr2 = MasterserverInfo.m_Addr;
|
public:
|
||||||
Addr2.port = 0;
|
CShared(std::shared_ptr<CGlobal> pGlobal) :
|
||||||
if(net_addr_comp(&Addr, &Addr2) == 0)
|
m_pGlobal(std::move(pGlobal))
|
||||||
{
|
{
|
||||||
MasterserverInfo.m_Token = ResponseToken;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
~CShared()
|
||||||
|
{
|
||||||
|
lock_destroy(m_Lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CRegister::RegisterNewState(int State)
|
std::shared_ptr<CGlobal> m_pGlobal;
|
||||||
{
|
LOCK m_Lock = lock_create();
|
||||||
m_RegisterState = State;
|
int m_NumTotalRequests GUARDED_BY(m_Lock) = 0;
|
||||||
m_RegisterStateStart = time_get();
|
int m_LatestResponseStatus GUARDED_BY(m_Lock) = STATUS_NONE;
|
||||||
}
|
int m_LatestResponseIndex GUARDED_BY(m_Lock) = -1;
|
||||||
|
};
|
||||||
|
|
||||||
void CRegister::RegisterSendFwcheckresponse(NETADDR *pAddr, SECURITY_TOKEN ResponseToken)
|
class CJob : public IJob
|
||||||
{
|
{
|
||||||
CNetChunk Packet;
|
int m_Protocol;
|
||||||
Packet.m_ClientID = -1;
|
int m_ServerPort;
|
||||||
Packet.m_Address = *pAddr;
|
int m_Index;
|
||||||
Packet.m_Flags = NETSENDFLAG_CONNLESS;
|
int m_InfoSerial;
|
||||||
Packet.m_DataSize = sizeof(SERVERBROWSE_FWRESPONSE);
|
std::shared_ptr<CShared> m_pShared;
|
||||||
Packet.m_pData = SERVERBROWSE_FWRESPONSE;
|
std::unique_ptr<CHttpRequest> m_pRegister;
|
||||||
if(m_Sixup)
|
void Run() override;
|
||||||
m_pNetServer->SendConnlessSixup(&Packet, ResponseToken);
|
|
||||||
|
public:
|
||||||
|
CJob(int Protocol, int ServerPort, int Index, int InfoSerial, std::shared_ptr<CShared> pShared, std::unique_ptr<CHttpRequest> &&pRegister) :
|
||||||
|
m_Protocol(Protocol),
|
||||||
|
m_ServerPort(ServerPort),
|
||||||
|
m_Index(Index),
|
||||||
|
m_InfoSerial(InfoSerial),
|
||||||
|
m_pShared(std::move(pShared)),
|
||||||
|
m_pRegister(std::move(pRegister))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
virtual ~CJob() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
CRegister *m_pParent;
|
||||||
|
int m_Protocol;
|
||||||
|
|
||||||
|
std::shared_ptr<CShared> m_pShared;
|
||||||
|
bool m_NewChallengeToken = false;
|
||||||
|
bool m_HaveChallengeToken = false;
|
||||||
|
char m_aChallengeToken[128] = {0};
|
||||||
|
|
||||||
|
void CheckChallengeStatus();
|
||||||
|
|
||||||
|
public:
|
||||||
|
int64_t m_PrevRegister = -1;
|
||||||
|
int64_t m_NextRegister = -1;
|
||||||
|
|
||||||
|
CProtocol(CRegister *pParent, int Protocol);
|
||||||
|
void OnToken(const char *pToken);
|
||||||
|
void SendRegister();
|
||||||
|
void Update();
|
||||||
|
};
|
||||||
|
|
||||||
|
CConfig *m_pConfig;
|
||||||
|
IConsole *m_pConsole;
|
||||||
|
IEngine *m_pEngine;
|
||||||
|
int m_ServerPort;
|
||||||
|
char m_aConnlessTokenHex[16];
|
||||||
|
|
||||||
|
std::shared_ptr<CGlobal> m_pGlobal = std::make_shared<CGlobal>();
|
||||||
|
bool m_aProtocolEnabled[NUM_PROTOCOLS] = {true, true, true, true};
|
||||||
|
CProtocol m_aProtocols[NUM_PROTOCOLS];
|
||||||
|
|
||||||
|
int m_NumExtraHeaders = 0;
|
||||||
|
char m_aaExtraHeaders[8][128];
|
||||||
|
|
||||||
|
char m_aVerifyPacketPrefix[sizeof(SERVERBROWSE_CHALLENGE) + UUID_MAXSTRSIZE];
|
||||||
|
CUuid m_Secret = RandomUuid();
|
||||||
|
CUuid m_ChallengeSecret = RandomUuid();
|
||||||
|
bool m_GotServerInfo = false;
|
||||||
|
char m_aServerInfo[16384];
|
||||||
|
|
||||||
|
public:
|
||||||
|
CRegister(CConfig *pConfig, IConsole *pConsole, IEngine *pEngine, int ServerPort, unsigned SixupSecurityToken);
|
||||||
|
void Update() override;
|
||||||
|
void OnConfigChange() override;
|
||||||
|
bool OnPacket(const CNetChunk *pPacket) override;
|
||||||
|
void OnNewInfo(const char *pInfo) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool CRegister::StatusFromString(int *pResult, const char *pString)
|
||||||
|
{
|
||||||
|
if(str_comp(pString, "success") == 0)
|
||||||
|
{
|
||||||
|
*pResult = STATUS_OK;
|
||||||
|
}
|
||||||
|
else if(str_comp(pString, "need_challenge") == 0)
|
||||||
|
{
|
||||||
|
*pResult = STATUS_NEEDCHALLENGE;
|
||||||
|
}
|
||||||
|
else if(str_comp(pString, "need_info") == 0)
|
||||||
|
{
|
||||||
|
*pResult = STATUS_NEEDINFO;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
m_pNetServer->Send(&Packet);
|
{
|
||||||
|
*pResult = -1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CRegister::RegisterSendHeartbeat(NETADDR Addr, SECURITY_TOKEN ResponseToken)
|
const char *CRegister::ProtocolToScheme(int Protocol)
|
||||||
{
|
{
|
||||||
unsigned char aData[sizeof(SERVERBROWSE_HEARTBEAT) + 2];
|
switch(Protocol)
|
||||||
unsigned short Port = m_pNetServer->Address().port;
|
{
|
||||||
CNetChunk Packet;
|
case PROTOCOL_TW6_IPV6: return "tw-0.6+udp://";
|
||||||
|
case PROTOCOL_TW6_IPV4: return "tw-0.6+udp://";
|
||||||
|
case PROTOCOL_TW7_IPV6: return "tw-0.7+udp://";
|
||||||
|
case PROTOCOL_TW7_IPV4: return "tw-0.7+udp://";
|
||||||
|
}
|
||||||
|
dbg_assert(false, "invalid protocol");
|
||||||
|
dbg_break();
|
||||||
|
}
|
||||||
|
|
||||||
mem_copy(aData, SERVERBROWSE_HEARTBEAT, sizeof(SERVERBROWSE_HEARTBEAT));
|
const char *CRegister::ProtocolToString(int Protocol)
|
||||||
|
{
|
||||||
|
switch(Protocol)
|
||||||
|
{
|
||||||
|
case PROTOCOL_TW6_IPV6: return "tw0.6/ipv6";
|
||||||
|
case PROTOCOL_TW6_IPV4: return "tw0.6/ipv4";
|
||||||
|
case PROTOCOL_TW7_IPV6: return "tw0.7/ipv6";
|
||||||
|
case PROTOCOL_TW7_IPV4: return "tw0.7/ipv4";
|
||||||
|
}
|
||||||
|
dbg_assert(false, "invalid protocol");
|
||||||
|
dbg_break();
|
||||||
|
}
|
||||||
|
|
||||||
Packet.m_ClientID = -1;
|
bool CRegister::ProtocolFromString(int *pResult, const char *pString)
|
||||||
Packet.m_Address = Addr;
|
{
|
||||||
Packet.m_Flags = NETSENDFLAG_CONNLESS;
|
if(str_comp(pString, "tw0.6/ipv6") == 0)
|
||||||
Packet.m_DataSize = sizeof(SERVERBROWSE_HEARTBEAT) + 2;
|
{
|
||||||
Packet.m_pData = &aData;
|
*pResult = PROTOCOL_TW6_IPV6;
|
||||||
|
}
|
||||||
// supply the set port that the master can use if it has problems
|
else if(str_comp(pString, "tw0.6/ipv4") == 0)
|
||||||
if(g_Config.m_SvExternalPort)
|
{
|
||||||
Port = g_Config.m_SvExternalPort;
|
*pResult = PROTOCOL_TW6_IPV4;
|
||||||
aData[sizeof(SERVERBROWSE_HEARTBEAT)] = Port >> 8;
|
}
|
||||||
aData[sizeof(SERVERBROWSE_HEARTBEAT) + 1] = Port & 0xff;
|
else if(str_comp(pString, "tw0.7/ipv6") == 0)
|
||||||
if(m_Sixup)
|
{
|
||||||
m_pNetServer->SendConnlessSixup(&Packet, ResponseToken);
|
*pResult = PROTOCOL_TW7_IPV6;
|
||||||
|
}
|
||||||
|
else if(str_comp(pString, "tw0.7/ipv4") == 0)
|
||||||
|
{
|
||||||
|
*pResult = PROTOCOL_TW7_IPV4;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
m_pNetServer->Send(&Packet);
|
{
|
||||||
|
*pResult = -1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CRegister::RegisterSendCountRequest(NETADDR Addr, SECURITY_TOKEN ResponseToken)
|
const char *CRegister::ProtocolToSystem(int Protocol)
|
||||||
{
|
{
|
||||||
CNetChunk Packet;
|
switch(Protocol)
|
||||||
Packet.m_ClientID = -1;
|
{
|
||||||
Packet.m_Address = Addr;
|
case PROTOCOL_TW6_IPV6: return "register/6/ipv6";
|
||||||
Packet.m_Flags = NETSENDFLAG_CONNLESS;
|
case PROTOCOL_TW6_IPV4: return "register/6/ipv4";
|
||||||
Packet.m_DataSize = sizeof(SERVERBROWSE_GETCOUNT);
|
case PROTOCOL_TW7_IPV6: return "register/7/ipv6";
|
||||||
Packet.m_pData = SERVERBROWSE_GETCOUNT;
|
case PROTOCOL_TW7_IPV4: return "register/7/ipv4";
|
||||||
if(m_Sixup)
|
}
|
||||||
m_pNetServer->SendConnlessSixup(&Packet, ResponseToken);
|
dbg_assert(false, "invalid protocol");
|
||||||
else
|
dbg_break();
|
||||||
m_pNetServer->Send(&Packet);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CRegister::RegisterGotCount(CNetChunk *pChunk)
|
IPRESOLVE CRegister::ProtocolToIpresolve(int Protocol)
|
||||||
{
|
{
|
||||||
unsigned char *pData = (unsigned char *)pChunk->m_pData;
|
switch(Protocol)
|
||||||
int Count = (pData[sizeof(SERVERBROWSE_COUNT)] << 8) | pData[sizeof(SERVERBROWSE_COUNT) + 1];
|
|
||||||
|
|
||||||
for(auto &MasterserverInfo : m_aMasterserverInfo)
|
|
||||||
{
|
{
|
||||||
if(net_addr_comp(&MasterserverInfo.m_Addr, &pChunk->m_Address) == 0)
|
case PROTOCOL_TW6_IPV6: return IPRESOLVE::V6;
|
||||||
{
|
case PROTOCOL_TW6_IPV4: return IPRESOLVE::V4;
|
||||||
MasterserverInfo.m_Count = Count;
|
case PROTOCOL_TW7_IPV6: return IPRESOLVE::V6;
|
||||||
break;
|
case PROTOCOL_TW7_IPV4: return IPRESOLVE::V4;
|
||||||
}
|
}
|
||||||
|
dbg_assert(false, "invalid protocol");
|
||||||
|
dbg_break();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CRegister::ConchainOnConfigChange(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
|
||||||
|
{
|
||||||
|
pfnCallback(pResult, pCallbackUserData);
|
||||||
|
if(pResult->NumArguments())
|
||||||
|
{
|
||||||
|
((CRegister *)pUserData)->OnConfigChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CRegister::Init(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, CConfig *pConfig, IConsole *pConsole)
|
void CRegister::CProtocol::SendRegister()
|
||||||
{
|
|
||||||
m_pNetServer = pNetServer;
|
|
||||||
m_pMasterServer = pMasterServer;
|
|
||||||
m_pConfig = pConfig;
|
|
||||||
m_pConsole = pConsole;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CRegister::RegisterUpdate(int Nettype)
|
|
||||||
{
|
{
|
||||||
int64_t Now = time_get();
|
int64_t Now = time_get();
|
||||||
int64_t Freq = time_freq();
|
int64_t Freq = time_freq();
|
||||||
|
|
||||||
if(!g_Config.m_SvRegister)
|
char aAddress[64];
|
||||||
return;
|
str_format(aAddress, sizeof(aAddress), "%sconnecting-address.invalid:%d", ProtocolToScheme(m_Protocol), m_pParent->m_ServerPort);
|
||||||
|
|
||||||
m_pMasterServer->Update();
|
char aSecret[UUID_MAXSTRSIZE];
|
||||||
|
FormatUuid(m_pParent->m_Secret, aSecret, sizeof(aSecret));
|
||||||
|
|
||||||
if(m_Sixup && (m_RegisterState == REGISTERSTATE_HEARTBEAT || m_RegisterState == REGISTERSTATE_REGISTERED) && Now > m_LastTokenRequest + Freq * 5)
|
char aChallengeUuid[UUID_MAXSTRSIZE];
|
||||||
{
|
FormatUuid(m_pParent->m_ChallengeSecret, aChallengeUuid, sizeof(aChallengeUuid));
|
||||||
m_pNetServer->SendTokenSixup(m_aMasterserverInfo[m_RegisterRegisteredServer].m_Addr, NET_SECURITY_TOKEN_UNKNOWN);
|
char aChallengeSecret[64];
|
||||||
m_LastTokenRequest = Now;
|
str_format(aChallengeSecret, sizeof(aChallengeSecret), "%s:%s", aChallengeUuid, ProtocolToString(m_Protocol));
|
||||||
}
|
|
||||||
|
|
||||||
if(m_RegisterState == REGISTERSTATE_START)
|
lock_wait(m_pShared->m_pGlobal->m_Lock);
|
||||||
{
|
int InfoSerial = m_pShared->m_pGlobal->m_InfoSerial;
|
||||||
m_RegisterCount = 0;
|
bool SendInfo = InfoSerial > m_pShared->m_pGlobal->m_LatestSuccessfulInfoSerial;
|
||||||
m_RegisterFirst = 1;
|
lock_unlock(m_pShared->m_pGlobal->m_Lock);
|
||||||
RegisterNewState(REGISTERSTATE_UPDATE_ADDRS);
|
|
||||||
m_pMasterServer->RefreshAddresses(Nettype);
|
|
||||||
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, m_pName, "refreshing ip addresses");
|
|
||||||
}
|
|
||||||
else if(m_RegisterState == REGISTERSTATE_UPDATE_ADDRS)
|
|
||||||
{
|
|
||||||
m_RegisterRegisteredServer = -1;
|
|
||||||
|
|
||||||
if(!m_pMasterServer->IsRefreshing())
|
std::unique_ptr<CHttpRequest> pRegister;
|
||||||
|
if(SendInfo)
|
||||||
{
|
{
|
||||||
int i;
|
pRegister = HttpPostJson(m_pParent->m_pConfig->m_SvRegisterUrl, m_pParent->m_aServerInfo);
|
||||||
for(i = 0; i < IMasterServer::MAX_MASTERSERVERS; i++)
|
|
||||||
{
|
|
||||||
if(!m_pMasterServer->IsValid(i))
|
|
||||||
{
|
|
||||||
m_aMasterserverInfo[i].m_Valid = 0;
|
|
||||||
m_aMasterserverInfo[i].m_Count = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
NETADDR Addr = m_pMasterServer->GetAddr(i);
|
|
||||||
m_aMasterserverInfo[i].m_Addr = Addr;
|
|
||||||
if(m_Sixup)
|
|
||||||
m_aMasterserverInfo[i].m_Addr.port = 8283;
|
|
||||||
m_aMasterserverInfo[i].m_Valid = 1;
|
|
||||||
m_aMasterserverInfo[i].m_Count = -1;
|
|
||||||
m_aMasterserverInfo[i].m_LastSend = 0;
|
|
||||||
m_aMasterserverInfo[i].m_Token = NET_SECURITY_TOKEN_UNKNOWN;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, m_pName, "fetching server counts");
|
|
||||||
m_LastTokenRequest = Now;
|
|
||||||
RegisterNewState(REGISTERSTATE_QUERY_COUNT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(m_RegisterState == REGISTERSTATE_QUERY_COUNT)
|
|
||||||
{
|
|
||||||
int Left = 0;
|
|
||||||
for(auto &MasterserverInfo : m_aMasterserverInfo)
|
|
||||||
{
|
|
||||||
if(!MasterserverInfo.m_Valid)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if(MasterserverInfo.m_Count == -1)
|
|
||||||
{
|
|
||||||
Left++;
|
|
||||||
if(MasterserverInfo.m_LastSend + Freq < Now)
|
|
||||||
{
|
|
||||||
MasterserverInfo.m_LastSend = Now;
|
|
||||||
if(m_Sixup && MasterserverInfo.m_Token == NET_SECURITY_TOKEN_UNKNOWN)
|
|
||||||
m_pNetServer->SendTokenSixup(MasterserverInfo.m_Addr, NET_SECURITY_TOKEN_UNKNOWN);
|
|
||||||
else
|
|
||||||
RegisterSendCountRequest(MasterserverInfo.m_Addr, MasterserverInfo.m_Token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if we are done or timed out
|
|
||||||
if(Left == 0 || Now > m_RegisterStateStart + Freq * 5)
|
|
||||||
{
|
|
||||||
// choose server
|
|
||||||
int Best = -1;
|
|
||||||
int i;
|
|
||||||
for(i = 0; i < IMasterServer::MAX_MASTERSERVERS; i++)
|
|
||||||
{
|
|
||||||
if(!m_aMasterserverInfo[i].m_Valid || m_aMasterserverInfo[i].m_Count == -1)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if(Best == -1 || m_aMasterserverInfo[i].m_Count < m_aMasterserverInfo[Best].m_Count)
|
|
||||||
Best = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// server chosen
|
|
||||||
m_RegisterRegisteredServer = Best;
|
|
||||||
if(m_RegisterRegisteredServer == -1)
|
|
||||||
{
|
|
||||||
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, m_pName, "WARNING: No master servers. Retrying in 60 seconds");
|
|
||||||
RegisterNewState(REGISTERSTATE_ERROR);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char aBuf[256];
|
pRegister = HttpPost(m_pParent->m_pConfig->m_SvRegisterUrl, (unsigned char *)"", 0);
|
||||||
str_format(aBuf, sizeof(aBuf), "chose '%s' as master, sending heartbeats", m_pMasterServer->GetName(m_RegisterRegisteredServer));
|
|
||||||
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, m_pName, aBuf);
|
|
||||||
m_aMasterserverInfo[m_RegisterRegisteredServer].m_LastSend = 0;
|
|
||||||
RegisterNewState(REGISTERSTATE_HEARTBEAT);
|
|
||||||
}
|
}
|
||||||
}
|
pRegister->HeaderString("Address", aAddress);
|
||||||
}
|
pRegister->HeaderString("Secret", aSecret);
|
||||||
else if(m_RegisterState == REGISTERSTATE_HEARTBEAT)
|
if(m_Protocol == PROTOCOL_TW7_IPV6 || m_Protocol == PROTOCOL_TW7_IPV4)
|
||||||
{
|
{
|
||||||
// check if we should send heartbeat
|
pRegister->HeaderString("Connless-Token", m_pParent->m_aConnlessTokenHex);
|
||||||
if(Now > m_aMasterserverInfo[m_RegisterRegisteredServer].m_LastSend + Freq * 15)
|
}
|
||||||
|
pRegister->HeaderString("Challenge-Secret", aChallengeSecret);
|
||||||
|
if(m_HaveChallengeToken)
|
||||||
{
|
{
|
||||||
m_aMasterserverInfo[m_RegisterRegisteredServer].m_LastSend = Now;
|
pRegister->HeaderString("Challenge-Token", m_aChallengeToken);
|
||||||
RegisterSendHeartbeat(m_aMasterserverInfo[m_RegisterRegisteredServer].m_Addr, m_aMasterserverInfo[m_RegisterRegisteredServer].m_Token);
|
}
|
||||||
|
pRegister->HeaderInt("Info-Serial", InfoSerial);
|
||||||
|
for(int i = 0; i < m_pParent->m_NumExtraHeaders; i++)
|
||||||
|
{
|
||||||
|
pRegister->Header(m_pParent->m_aaExtraHeaders[i]);
|
||||||
|
}
|
||||||
|
pRegister->LogProgress(HTTPLOG::FAILURE);
|
||||||
|
pRegister->IpResolve(ProtocolToIpresolve(m_Protocol));
|
||||||
|
|
||||||
|
lock_wait(m_pShared->m_Lock);
|
||||||
|
if(m_pShared->m_LatestResponseStatus != STATUS_OK)
|
||||||
|
{
|
||||||
|
log_info(ProtocolToSystem(m_Protocol), "registering...");
|
||||||
|
}
|
||||||
|
int RequestIndex = m_pShared->m_NumTotalRequests;
|
||||||
|
m_pShared->m_NumTotalRequests += 1;
|
||||||
|
lock_unlock(m_pShared->m_Lock);
|
||||||
|
m_pParent->m_pEngine->AddJob(std::make_shared<CJob>(m_Protocol, m_pParent->m_ServerPort, RequestIndex, InfoSerial, m_pShared, std::move(pRegister)));
|
||||||
|
m_NewChallengeToken = false;
|
||||||
|
|
||||||
|
m_PrevRegister = Now;
|
||||||
|
m_NextRegister = Now + 15 * Freq;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Now > m_RegisterStateStart + Freq * 60)
|
CRegister::CProtocol::CProtocol(CRegister *pParent, int Protocol) :
|
||||||
|
m_pParent(pParent),
|
||||||
|
m_Protocol(Protocol),
|
||||||
|
m_pShared(std::make_shared<CShared>(pParent->m_pGlobal))
|
||||||
{
|
{
|
||||||
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, m_pName, "WARNING: Master server is not responding, switching master");
|
|
||||||
RegisterNewState(REGISTERSTATE_START);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(m_RegisterState == REGISTERSTATE_REGISTERED)
|
|
||||||
{
|
|
||||||
if(m_RegisterFirst)
|
|
||||||
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, m_pName, "server registered");
|
|
||||||
|
|
||||||
m_RegisterFirst = 0;
|
|
||||||
|
|
||||||
// check if we should send new heartbeat again
|
|
||||||
if(Now > m_RegisterStateStart + Freq)
|
|
||||||
{
|
|
||||||
if(m_RegisterCount == 120) // redo the whole process after 60 minutes to balance out the master servers
|
|
||||||
RegisterNewState(REGISTERSTATE_START);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_RegisterCount++;
|
|
||||||
RegisterNewState(REGISTERSTATE_HEARTBEAT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(m_RegisterState == REGISTERSTATE_ERROR)
|
|
||||||
{
|
|
||||||
// check for restart
|
|
||||||
if(Now > m_RegisterStateStart + Freq * 60)
|
|
||||||
RegisterNewState(REGISTERSTATE_START);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int CRegister::RegisterProcessPacket(CNetChunk *pPacket, SECURITY_TOKEN ResponseToken)
|
void CRegister::CProtocol::CheckChallengeStatus()
|
||||||
{
|
{
|
||||||
// check for masterserver address
|
lock_wait(m_pShared->m_Lock);
|
||||||
bool Valid = false;
|
// No requests in flight?
|
||||||
for(auto &MasterserverInfo : m_aMasterserverInfo)
|
if(m_pShared->m_LatestResponseIndex == m_pShared->m_NumTotalRequests - 1)
|
||||||
{
|
{
|
||||||
if(net_addr_comp_noport(&pPacket->m_Address, &MasterserverInfo.m_Addr) == 0)
|
switch(m_pShared->m_LatestResponseStatus)
|
||||||
{
|
{
|
||||||
Valid = true;
|
case STATUS_NEEDCHALLENGE:
|
||||||
|
if(m_NewChallengeToken)
|
||||||
|
{
|
||||||
|
// Immediately resend if we got the token.
|
||||||
|
m_NextRegister = time_get();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case STATUS_NEEDINFO:
|
||||||
|
// Act immediately if the master requests more info.
|
||||||
|
m_NextRegister = time_get();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!Valid)
|
lock_unlock(m_pShared->m_Lock);
|
||||||
return 0;
|
|
||||||
|
|
||||||
if(pPacket->m_DataSize == sizeof(SERVERBROWSE_FWCHECK) &&
|
|
||||||
mem_comp(pPacket->m_pData, SERVERBROWSE_FWCHECK, sizeof(SERVERBROWSE_FWCHECK)) == 0)
|
|
||||||
{
|
|
||||||
RegisterSendFwcheckresponse(&pPacket->m_Address, ResponseToken);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(pPacket->m_DataSize == sizeof(SERVERBROWSE_FWOK) &&
|
|
||||||
mem_comp(pPacket->m_pData, SERVERBROWSE_FWOK, sizeof(SERVERBROWSE_FWOK)) == 0)
|
|
||||||
{
|
|
||||||
if(m_RegisterFirst)
|
|
||||||
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, m_pName, "no firewall/nat problems detected");
|
|
||||||
|
|
||||||
if(m_RegisterState == REGISTERSTATE_HEARTBEAT || m_RegisterState == REGISTERSTATE_REGISTERED)
|
|
||||||
RegisterNewState(REGISTERSTATE_REGISTERED);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(pPacket->m_DataSize == sizeof(SERVERBROWSE_FWERROR) &&
|
|
||||||
mem_comp(pPacket->m_pData, SERVERBROWSE_FWERROR, sizeof(SERVERBROWSE_FWERROR)) == 0)
|
|
||||||
{
|
|
||||||
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, m_pName, "ERROR: the master server reports that clients can not connect to this server.");
|
|
||||||
char aBuf[256];
|
|
||||||
str_format(aBuf, sizeof(aBuf), "ERROR: configure your firewall/nat to let through udp on port %d.", m_pNetServer->Address().port);
|
|
||||||
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, m_pName, aBuf);
|
|
||||||
//RegisterNewState(REGISTERSTATE_ERROR);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(pPacket->m_DataSize == sizeof(SERVERBROWSE_COUNT) + 2 &&
|
|
||||||
mem_comp(pPacket->m_pData, SERVERBROWSE_COUNT, sizeof(SERVERBROWSE_COUNT)) == 0)
|
|
||||||
{
|
|
||||||
RegisterGotCount(pPacket);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
void CRegister::CProtocol::Update()
|
||||||
|
{
|
||||||
|
CheckChallengeStatus();
|
||||||
|
if(time_get() >= m_NextRegister)
|
||||||
|
{
|
||||||
|
SendRegister();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CRegister::CProtocol::OnToken(const char *pToken)
|
||||||
|
{
|
||||||
|
m_NewChallengeToken = true;
|
||||||
|
m_HaveChallengeToken = true;
|
||||||
|
str_copy(m_aChallengeToken, pToken, sizeof(m_aChallengeToken));
|
||||||
|
|
||||||
|
CheckChallengeStatus();
|
||||||
|
if(time_get() >= m_NextRegister)
|
||||||
|
{
|
||||||
|
SendRegister();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CRegister::CProtocol::CJob::Run()
|
||||||
|
{
|
||||||
|
IEngine::RunJobBlocking(m_pRegister.get());
|
||||||
|
if(m_pRegister->State() != HTTP_DONE)
|
||||||
|
{
|
||||||
|
// TODO: log the error response content from master
|
||||||
|
// TODO: exponential backoff
|
||||||
|
log_error(ProtocolToSystem(m_Protocol), "error response from master");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
json_value *pJson = m_pRegister->ResultJson();
|
||||||
|
if(!pJson)
|
||||||
|
{
|
||||||
|
log_error(ProtocolToSystem(m_Protocol), "non-JSON response from master");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const json_value &Json = *pJson;
|
||||||
|
const json_value &StatusString = Json["status"];
|
||||||
|
if(StatusString.type != json_string)
|
||||||
|
{
|
||||||
|
json_value_free(pJson);
|
||||||
|
log_error(ProtocolToSystem(m_Protocol), "invalid JSON response from master");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int Status;
|
||||||
|
if(StatusFromString(&Status, StatusString))
|
||||||
|
{
|
||||||
|
log_error(ProtocolToSystem(m_Protocol), "invalid status from master: %s", (const char *)StatusString);
|
||||||
|
json_value_free(pJson);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lock_wait(m_pShared->m_Lock);
|
||||||
|
if(Status != STATUS_OK || Status != m_pShared->m_LatestResponseStatus)
|
||||||
|
{
|
||||||
|
log_debug(ProtocolToSystem(m_Protocol), "status: %s", (const char *)StatusString);
|
||||||
|
}
|
||||||
|
if(Status == m_pShared->m_LatestResponseStatus)
|
||||||
|
{
|
||||||
|
log_error(ProtocolToSystem(m_Protocol), "ERROR: the master server reports that clients can not connect to this server.");
|
||||||
|
log_error(ProtocolToSystem(m_Protocol), "ERROR: configure your firewall/nat to let through udp on port %d.", m_ServerPort);
|
||||||
|
}
|
||||||
|
json_value_free(pJson);
|
||||||
|
if(m_Index > m_pShared->m_LatestResponseIndex)
|
||||||
|
{
|
||||||
|
m_pShared->m_LatestResponseIndex = m_Index;
|
||||||
|
m_pShared->m_LatestResponseStatus = Status;
|
||||||
|
}
|
||||||
|
lock_unlock(m_pShared->m_Lock);
|
||||||
|
if(Status == STATUS_OK)
|
||||||
|
{
|
||||||
|
lock_wait(m_pShared->m_pGlobal->m_Lock);
|
||||||
|
if(m_InfoSerial > m_pShared->m_pGlobal->m_LatestSuccessfulInfoSerial)
|
||||||
|
{
|
||||||
|
m_pShared->m_pGlobal->m_LatestSuccessfulInfoSerial = m_InfoSerial;
|
||||||
|
}
|
||||||
|
lock_unlock(m_pShared->m_pGlobal->m_Lock);
|
||||||
|
}
|
||||||
|
else if(Status == STATUS_NEEDINFO)
|
||||||
|
{
|
||||||
|
lock_wait(m_pShared->m_pGlobal->m_Lock);
|
||||||
|
if(m_InfoSerial == m_pShared->m_pGlobal->m_LatestSuccessfulInfoSerial)
|
||||||
|
{
|
||||||
|
// Tell other requests that they need to send the info again.
|
||||||
|
m_pShared->m_pGlobal->m_LatestSuccessfulInfoSerial -= 1;
|
||||||
|
}
|
||||||
|
lock_unlock(m_pShared->m_pGlobal->m_Lock);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CRegister::CRegister(CConfig *pConfig, IConsole *pConsole, IEngine *pEngine, int ServerPort, unsigned SixupSecurityToken) :
|
||||||
|
m_pConfig(pConfig),
|
||||||
|
m_pConsole(pConsole),
|
||||||
|
m_pEngine(pEngine),
|
||||||
|
m_ServerPort(ServerPort),
|
||||||
|
m_aProtocols{
|
||||||
|
CProtocol(this, PROTOCOL_TW6_IPV6),
|
||||||
|
CProtocol(this, PROTOCOL_TW6_IPV4),
|
||||||
|
CProtocol(this, PROTOCOL_TW7_IPV6),
|
||||||
|
CProtocol(this, PROTOCOL_TW7_IPV4),
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const int HEADER_LEN = sizeof(SERVERBROWSE_CHALLENGE);
|
||||||
|
mem_copy(m_aVerifyPacketPrefix, SERVERBROWSE_CHALLENGE, HEADER_LEN);
|
||||||
|
FormatUuid(m_ChallengeSecret, m_aVerifyPacketPrefix + HEADER_LEN, sizeof(m_aVerifyPacketPrefix) - HEADER_LEN);
|
||||||
|
m_aVerifyPacketPrefix[HEADER_LEN + UUID_MAXSTRSIZE - 1] = ':';
|
||||||
|
|
||||||
|
// The DDNet code uses the `unsigned` security token in memory byte order.
|
||||||
|
unsigned char TokenBytes[4];
|
||||||
|
mem_copy(TokenBytes, &SixupSecurityToken, sizeof(TokenBytes));
|
||||||
|
str_format(m_aConnlessTokenHex, sizeof(m_aConnlessTokenHex), "%08x", bytes_be_to_uint(TokenBytes));
|
||||||
|
|
||||||
|
m_pConsole->Chain("sv_register", ConchainOnConfigChange, this);
|
||||||
|
m_pConsole->Chain("sv_sixup", ConchainOnConfigChange, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CRegister::Update()
|
||||||
|
{
|
||||||
|
if(!m_GotServerInfo)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for(int i = 0; i < NUM_PROTOCOLS; i++)
|
||||||
|
{
|
||||||
|
if(!m_aProtocolEnabled[i])
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
m_aProtocols[i].Update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CRegister::OnConfigChange()
|
||||||
|
{
|
||||||
|
const char *pProtocols = m_pConfig->m_SvRegister;
|
||||||
|
if(str_comp(pProtocols, "1") == 0)
|
||||||
|
{
|
||||||
|
for(auto &Enabled : m_aProtocolEnabled)
|
||||||
|
{
|
||||||
|
Enabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(str_comp(pProtocols, "0") == 0)
|
||||||
|
{
|
||||||
|
for(auto &Enabled : m_aProtocolEnabled)
|
||||||
|
{
|
||||||
|
Enabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for(auto &Enabled : m_aProtocolEnabled)
|
||||||
|
{
|
||||||
|
Enabled = false;
|
||||||
|
}
|
||||||
|
char aBuf[16];
|
||||||
|
while((pProtocols = str_next_token(pProtocols, ",", aBuf, sizeof(aBuf))))
|
||||||
|
{
|
||||||
|
int Protocol;
|
||||||
|
if(str_comp(aBuf, "ipv6") == 0)
|
||||||
|
{
|
||||||
|
m_aProtocolEnabled[PROTOCOL_TW6_IPV6] = true;
|
||||||
|
m_aProtocolEnabled[PROTOCOL_TW7_IPV6] = true;
|
||||||
|
}
|
||||||
|
else if(str_comp(aBuf, "ipv4") == 0)
|
||||||
|
{
|
||||||
|
m_aProtocolEnabled[PROTOCOL_TW6_IPV4] = true;
|
||||||
|
m_aProtocolEnabled[PROTOCOL_TW7_IPV4] = true;
|
||||||
|
}
|
||||||
|
else if(str_comp(aBuf, "tw0.6") == 0)
|
||||||
|
{
|
||||||
|
m_aProtocolEnabled[PROTOCOL_TW6_IPV6] = true;
|
||||||
|
m_aProtocolEnabled[PROTOCOL_TW6_IPV4] = true;
|
||||||
|
}
|
||||||
|
else if(str_comp(aBuf, "tw0.7") == 0)
|
||||||
|
{
|
||||||
|
m_aProtocolEnabled[PROTOCOL_TW7_IPV6] = true;
|
||||||
|
m_aProtocolEnabled[PROTOCOL_TW7_IPV4] = true;
|
||||||
|
}
|
||||||
|
else if(!ProtocolFromString(&Protocol, aBuf))
|
||||||
|
{
|
||||||
|
m_aProtocolEnabled[Protocol] = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log_warn("register", "unknown protocol '%s'", aBuf);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!m_pConfig->m_SvSixup)
|
||||||
|
{
|
||||||
|
m_aProtocolEnabled[PROTOCOL_TW7_IPV6] = false;
|
||||||
|
m_aProtocolEnabled[PROTOCOL_TW7_IPV4] = false;
|
||||||
|
}
|
||||||
|
m_NumExtraHeaders = 0;
|
||||||
|
const char *pRegisterExtra = m_pConfig->m_SvRegisterExtra;
|
||||||
|
char aHeader[128];
|
||||||
|
while((pRegisterExtra = str_next_token(pRegisterExtra, ",", aHeader, sizeof(aHeader))))
|
||||||
|
{
|
||||||
|
if(m_NumExtraHeaders == (int)std::size(m_aaExtraHeaders))
|
||||||
|
{
|
||||||
|
log_warn("register", "reached maximum of %d extra headers, dropping '%s' and all further headers", m_NumExtraHeaders, aHeader);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(!str_find(aHeader, ": "))
|
||||||
|
{
|
||||||
|
log_warn("register", "header '%s' doesn't contain mandatory ': ', ignoring", aHeader);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
str_copy(m_aaExtraHeaders[m_NumExtraHeaders], aHeader, sizeof(m_aaExtraHeaders));
|
||||||
|
m_NumExtraHeaders += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CRegister::OnPacket(const CNetChunk *pPacket)
|
||||||
|
{
|
||||||
|
if((pPacket->m_Flags & NETSENDFLAG_CONNLESS) == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(pPacket->m_DataSize >= (int)sizeof(m_aVerifyPacketPrefix) &&
|
||||||
|
mem_comp(pPacket->m_pData, m_aVerifyPacketPrefix, sizeof(m_aVerifyPacketPrefix)) == 0)
|
||||||
|
{
|
||||||
|
CUnpacker Unpacker;
|
||||||
|
Unpacker.Reset(pPacket->m_pData, pPacket->m_DataSize);
|
||||||
|
Unpacker.GetRaw(sizeof(m_aVerifyPacketPrefix));
|
||||||
|
const char *pProtocol = Unpacker.GetString(0);
|
||||||
|
const char *pToken = Unpacker.GetString(0);
|
||||||
|
if(Unpacker.Error())
|
||||||
|
{
|
||||||
|
log_error("register", "got errorneous challenge packet from master");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
log_debug("register", "got challenge token, protocol='%s' token='%s'", pProtocol, pToken);
|
||||||
|
int Protocol;
|
||||||
|
if(ProtocolFromString(&Protocol, pProtocol))
|
||||||
|
{
|
||||||
|
log_error("register", "got challenge packet with unknown protocol");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
m_aProtocols[Protocol].OnToken(pToken);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CRegister::OnNewInfo(const char *pInfo)
|
||||||
|
{
|
||||||
|
log_trace("register", "info: %s", pInfo);
|
||||||
|
if(m_GotServerInfo && str_comp(m_aServerInfo, pInfo) == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_GotServerInfo = true;
|
||||||
|
str_copy(m_aServerInfo, pInfo, sizeof(m_aServerInfo));
|
||||||
|
lock_wait(m_pGlobal->m_Lock);
|
||||||
|
m_pGlobal->m_InfoSerial += 1;
|
||||||
|
lock_unlock(m_pGlobal->m_Lock);
|
||||||
|
|
||||||
|
// Immediately send new info if it changes, but at most once per second.
|
||||||
|
int64_t Now = time_get();
|
||||||
|
int64_t Freq = time_freq();
|
||||||
|
int64_t MaximumPrevRegister = -1;
|
||||||
|
int64_t MinimumNextRegister = -1;
|
||||||
|
int MinimumNextRegisterProtocol = -1;
|
||||||
|
for(int i = 0; i < NUM_PROTOCOLS; i++)
|
||||||
|
{
|
||||||
|
if(!m_aProtocolEnabled[i])
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(m_aProtocols[i].m_NextRegister == -1)
|
||||||
|
{
|
||||||
|
m_aProtocols[i].m_NextRegister = Now;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(m_aProtocols[i].m_PrevRegister > MaximumPrevRegister)
|
||||||
|
{
|
||||||
|
MaximumPrevRegister = m_aProtocols[i].m_PrevRegister;
|
||||||
|
}
|
||||||
|
if(MinimumNextRegisterProtocol == -1 || m_aProtocols[i].m_NextRegister < MinimumNextRegister)
|
||||||
|
{
|
||||||
|
MinimumNextRegisterProtocol = i;
|
||||||
|
MinimumNextRegister = m_aProtocols[i].m_NextRegister;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int i = 0; i < NUM_PROTOCOLS; i++)
|
||||||
|
{
|
||||||
|
if(!m_aProtocolEnabled[i])
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(i == MinimumNextRegisterProtocol)
|
||||||
|
{
|
||||||
|
m_aProtocols[i].m_NextRegister = std::min(m_aProtocols[i].m_NextRegister, MaximumPrevRegister + Freq);
|
||||||
|
}
|
||||||
|
if(Now >= m_aProtocols[i].m_NextRegister)
|
||||||
|
{
|
||||||
|
m_aProtocols[i].SendRegister();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IRegister *CreateRegister(CConfig *pConfig, IConsole *pConsole, IEngine *pEngine, int ServerPort, unsigned SixupSecurityToken)
|
||||||
|
{
|
||||||
|
return new CRegister(pConfig, pConsole, pEngine, ServerPort, SixupSecurityToken);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,61 +1,27 @@
|
||||||
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
||||||
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
||||||
#ifndef ENGINE_SERVER_REGISTER_H
|
#ifndef ENGINE_SERVER_REGISTER_H
|
||||||
#define ENGINE_SERVER_REGISTER_H
|
#define ENGINE_SERVER_REGISTER_H
|
||||||
|
|
||||||
#include <engine/masterserver.h>
|
class CConfig;
|
||||||
#include <engine/shared/network.h>
|
class IConsole;
|
||||||
|
class IEngine;
|
||||||
|
struct CNetChunk;
|
||||||
|
|
||||||
class CRegister
|
class IRegister
|
||||||
{
|
{
|
||||||
enum
|
|
||||||
{
|
|
||||||
REGISTERSTATE_START = 0,
|
|
||||||
REGISTERSTATE_UPDATE_ADDRS,
|
|
||||||
REGISTERSTATE_QUERY_COUNT,
|
|
||||||
REGISTERSTATE_HEARTBEAT,
|
|
||||||
REGISTERSTATE_REGISTERED,
|
|
||||||
REGISTERSTATE_ERROR
|
|
||||||
};
|
|
||||||
|
|
||||||
struct CMasterserverInfo
|
|
||||||
{
|
|
||||||
NETADDR m_Addr;
|
|
||||||
int m_Count;
|
|
||||||
int m_Valid;
|
|
||||||
int64_t m_LastSend;
|
|
||||||
SECURITY_TOKEN m_Token;
|
|
||||||
};
|
|
||||||
|
|
||||||
class CNetServer *m_pNetServer;
|
|
||||||
class IEngineMasterServer *m_pMasterServer;
|
|
||||||
class CConfig *m_pConfig;
|
|
||||||
class IConsole *m_pConsole;
|
|
||||||
|
|
||||||
bool m_Sixup;
|
|
||||||
const char *m_pName;
|
|
||||||
int64_t m_LastTokenRequest;
|
|
||||||
|
|
||||||
int m_RegisterState;
|
|
||||||
int64_t m_RegisterStateStart;
|
|
||||||
int m_RegisterFirst;
|
|
||||||
int m_RegisterCount;
|
|
||||||
|
|
||||||
CMasterserverInfo m_aMasterserverInfo[IMasterServer::MAX_MASTERSERVERS];
|
|
||||||
int m_RegisterRegisteredServer;
|
|
||||||
|
|
||||||
void RegisterNewState(int State);
|
|
||||||
void RegisterSendFwcheckresponse(NETADDR *pAddr, SECURITY_TOKEN ResponseToken);
|
|
||||||
void RegisterSendHeartbeat(NETADDR Addr, SECURITY_TOKEN ResponseToken);
|
|
||||||
void RegisterSendCountRequest(NETADDR Addr, SECURITY_TOKEN ResponseToken);
|
|
||||||
void RegisterGotCount(struct CNetChunk *pChunk);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CRegister(bool Sixup);
|
virtual ~IRegister() {}
|
||||||
void Init(class CNetServer *pNetServer, class IEngineMasterServer *pMasterServer, class CConfig *pConfig, class IConsole *pConsole);
|
|
||||||
void RegisterUpdate(int Nettype);
|
virtual void Update() = 0;
|
||||||
int RegisterProcessPacket(struct CNetChunk *pPacket, SECURITY_TOKEN ResponseToken = 0);
|
// Call `OnConfigChange` if you change relevant config variables
|
||||||
void FeedToken(NETADDR Addr, SECURITY_TOKEN ResponseToken);
|
// without going through the console.
|
||||||
|
virtual void OnConfigChange() = 0;
|
||||||
|
// Returns `true` if the packet was a packet related to registering
|
||||||
|
// code and doesn't have to processed furtherly.
|
||||||
|
virtual bool OnPacket(const CNetChunk *pPacket) = 0;
|
||||||
|
// `pInfo` must be an encoded JSON object.
|
||||||
|
virtual void OnNewInfo(const char *pInfo) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
IRegister *CreateRegister(CConfig *pConfig, IConsole *pConsole, IEngine *pEngine, int ServerPort, unsigned SixupSecurityToken);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
#include <engine/console.h>
|
#include <engine/console.h>
|
||||||
#include <engine/engine.h>
|
#include <engine/engine.h>
|
||||||
#include <engine/map.h>
|
#include <engine/map.h>
|
||||||
#include <engine/masterserver.h>
|
|
||||||
#include <engine/server.h>
|
#include <engine/server.h>
|
||||||
#include <engine/storage.h>
|
#include <engine/storage.h>
|
||||||
|
|
||||||
|
@ -25,6 +24,8 @@
|
||||||
#include <engine/shared/econ.h>
|
#include <engine/shared/econ.h>
|
||||||
#include <engine/shared/fifo.h>
|
#include <engine/shared/fifo.h>
|
||||||
#include <engine/shared/filecollection.h>
|
#include <engine/shared/filecollection.h>
|
||||||
|
#include <engine/shared/json.h>
|
||||||
|
#include <engine/shared/masterserver.h>
|
||||||
#include <engine/shared/netban.h>
|
#include <engine/shared/netban.h>
|
||||||
#include <engine/shared/network.h>
|
#include <engine/shared/network.h>
|
||||||
#include <engine/shared/packer.h>
|
#include <engine/shared/packer.h>
|
||||||
|
@ -32,8 +33,6 @@
|
||||||
#include <engine/shared/protocol_ex.h>
|
#include <engine/shared/protocol_ex.h>
|
||||||
#include <engine/shared/snapshot.h>
|
#include <engine/shared/snapshot.h>
|
||||||
|
|
||||||
#include <mastersrv/mastersrv.h>
|
|
||||||
|
|
||||||
#include <game/version.h>
|
#include <game/version.h>
|
||||||
|
|
||||||
// DDRace
|
// DDRace
|
||||||
|
@ -363,8 +362,7 @@ void CServer::CClient::Reset()
|
||||||
m_Flags = 0;
|
m_Flags = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CServer::CServer() :
|
CServer::CServer()
|
||||||
m_Register(false), m_RegSixup(true)
|
|
||||||
{
|
{
|
||||||
m_pConfig = &g_Config;
|
m_pConfig = &g_Config;
|
||||||
for(int i = 0; i < MAX_CLIENTS; i++)
|
for(int i = 0; i < MAX_CLIENTS; i++)
|
||||||
|
@ -402,6 +400,7 @@ CServer::CServer() :
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_pConnectionPool = new CDbConnectionPool();
|
m_pConnectionPool = new CDbConnectionPool();
|
||||||
|
m_pRegister = nullptr;
|
||||||
|
|
||||||
m_aErrorShutdownReason[0] = 0;
|
m_aErrorShutdownReason[0] = 0;
|
||||||
|
|
||||||
|
@ -415,6 +414,7 @@ CServer::~CServer()
|
||||||
free(pCurrentMapData);
|
free(pCurrentMapData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete m_pRegister;
|
||||||
delete m_pConnectionPool;
|
delete m_pConnectionPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2220,11 +2220,96 @@ void CServer::ExpireServerInfo()
|
||||||
m_ServerInfoNeedsUpdate = true;
|
m_ServerInfoNeedsUpdate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CServer::UpdateRegisterServerInfo()
|
||||||
|
{
|
||||||
|
// count the players
|
||||||
|
int PlayerCount = 0, ClientCount = 0;
|
||||||
|
for(int i = 0; i < MAX_CLIENTS; i++)
|
||||||
|
{
|
||||||
|
if(m_aClients[i].m_State != CClient::STATE_EMPTY)
|
||||||
|
{
|
||||||
|
if(GameServer()->IsClientPlayer(i))
|
||||||
|
PlayerCount++;
|
||||||
|
|
||||||
|
ClientCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int MaxPlayers = maximum(m_NetServer.MaxClients() - maximum(g_Config.m_SvSpectatorSlots, g_Config.m_SvReservedSlots), PlayerCount);
|
||||||
|
int MaxClients = maximum(m_NetServer.MaxClients() - g_Config.m_SvReservedSlots, ClientCount);
|
||||||
|
char aName[256];
|
||||||
|
char aGameType[32];
|
||||||
|
char aMapName[64];
|
||||||
|
char aVersion[64];
|
||||||
|
char aMapSha256[SHA256_MAXSTRSIZE];
|
||||||
|
|
||||||
|
sha256_str(m_aCurrentMapSha256[MAP_TYPE_SIX], aMapSha256, sizeof(aMapSha256));
|
||||||
|
|
||||||
|
char aInfo[16384];
|
||||||
|
str_format(aInfo, sizeof(aInfo),
|
||||||
|
"{"
|
||||||
|
"\"max_clients\":%d,"
|
||||||
|
"\"max_players\":%d,"
|
||||||
|
"\"passworded\":%s,"
|
||||||
|
"\"game_type\":\"%s\","
|
||||||
|
"\"name\":\"%s\","
|
||||||
|
"\"map\":{"
|
||||||
|
"\"name\":\"%s\","
|
||||||
|
"\"sha256\":\"%s\","
|
||||||
|
"\"size\":%d"
|
||||||
|
"},"
|
||||||
|
"\"version\":\"%s\","
|
||||||
|
"\"clients\":[",
|
||||||
|
MaxClients,
|
||||||
|
MaxPlayers,
|
||||||
|
JsonBool(g_Config.m_Password[0]),
|
||||||
|
EscapeJson(aGameType, sizeof(aGameType), GameServer()->GameType()),
|
||||||
|
EscapeJson(aName, sizeof(aName), g_Config.m_SvName),
|
||||||
|
EscapeJson(aMapName, sizeof(aMapName), m_aCurrentMap),
|
||||||
|
aMapSha256,
|
||||||
|
m_aCurrentMapSize[MAP_TYPE_SIX],
|
||||||
|
EscapeJson(aVersion, sizeof(aVersion), GameServer()->Version()));
|
||||||
|
|
||||||
|
bool FirstPlayer = true;
|
||||||
|
for(int i = 0; i < MAX_CLIENTS; i++)
|
||||||
|
{
|
||||||
|
if(m_aClients[i].m_State != CClient::STATE_EMPTY)
|
||||||
|
{
|
||||||
|
char aCName[32];
|
||||||
|
char aCClan[32];
|
||||||
|
|
||||||
|
char aClientInfo[256];
|
||||||
|
str_format(aClientInfo, sizeof(aClientInfo),
|
||||||
|
"%s{"
|
||||||
|
"\"name\":\"%s\","
|
||||||
|
"\"clan\":\"%s\","
|
||||||
|
"\"country\":%d,"
|
||||||
|
"\"score\":%d,"
|
||||||
|
"\"is_player\":%s"
|
||||||
|
"}",
|
||||||
|
!FirstPlayer ? "," : "",
|
||||||
|
EscapeJson(aCName, sizeof(aCName), ClientName(i)),
|
||||||
|
EscapeJson(aCClan, sizeof(aCClan), ClientClan(i)),
|
||||||
|
m_aClients[i].m_Country,
|
||||||
|
m_aClients[i].m_Score,
|
||||||
|
JsonBool(GameServer()->IsClientPlayer(i)));
|
||||||
|
str_append(aInfo, aClientInfo, sizeof(aInfo));
|
||||||
|
FirstPlayer = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
str_append(aInfo, "]}", sizeof(aInfo));
|
||||||
|
|
||||||
|
m_pRegister->OnNewInfo(aInfo);
|
||||||
|
}
|
||||||
|
|
||||||
void CServer::UpdateServerInfo(bool Resend)
|
void CServer::UpdateServerInfo(bool Resend)
|
||||||
{
|
{
|
||||||
if(m_RunServer == UNINITIALIZED)
|
if(m_RunServer == UNINITIALIZED)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
UpdateRegisterServerInfo();
|
||||||
|
|
||||||
for(int i = 0; i < 3; i++)
|
for(int i = 0; i < 3; i++)
|
||||||
for(int j = 0; j < 2; j++)
|
for(int j = 0; j < 2; j++)
|
||||||
CacheServerInfo(&m_aServerInfoCache[i * 2 + j], i, j);
|
CacheServerInfo(&m_aServerInfoCache[i * 2 + j], i, j);
|
||||||
|
@ -2267,17 +2352,7 @@ void CServer::PumpNetwork(bool PacketWaiting)
|
||||||
{
|
{
|
||||||
if(Packet.m_ClientID == -1)
|
if(Packet.m_ClientID == -1)
|
||||||
{
|
{
|
||||||
// stateless
|
if(ResponseToken == NET_SECURITY_TOKEN_UNKNOWN && m_pRegister->OnPacket(&Packet))
|
||||||
if(!(Packet.m_Flags & NETSENDFLAG_CONNLESS))
|
|
||||||
{
|
|
||||||
m_RegSixup.FeedToken(Packet.m_Address, ResponseToken);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(ResponseToken != NET_SECURITY_TOKEN_UNKNOWN && Config()->m_SvSixup &&
|
|
||||||
m_RegSixup.RegisterProcessPacket(&Packet, ResponseToken))
|
|
||||||
continue;
|
|
||||||
if(ResponseToken == NET_SECURITY_TOKEN_UNKNOWN && m_Register.RegisterProcessPacket(&Packet))
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -2441,6 +2516,10 @@ int CServer::LoadMap(const char *pMapName)
|
||||||
if(!File)
|
if(!File)
|
||||||
{
|
{
|
||||||
Config()->m_SvSixup = 0;
|
Config()->m_SvSixup = 0;
|
||||||
|
if(m_pRegister)
|
||||||
|
{
|
||||||
|
m_pRegister->OnConfigChange();
|
||||||
|
}
|
||||||
str_format(aBufMsg, sizeof(aBufMsg), "couldn't load map %s", aBuf);
|
str_format(aBufMsg, sizeof(aBufMsg), "couldn't load map %s", aBuf);
|
||||||
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "sixup", aBufMsg);
|
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "sixup", aBufMsg);
|
||||||
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "sixup", "disabling 0.7 compatibility");
|
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "sixup", "disabling 0.7 compatibility");
|
||||||
|
@ -2472,12 +2551,6 @@ int CServer::LoadMap(const char *pMapName)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CServer::InitRegister(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, CConfig *pConfig, IConsole *pConsole)
|
|
||||||
{
|
|
||||||
m_Register.Init(pNetServer, pMasterServer, pConfig, pConsole);
|
|
||||||
m_RegSixup.Init(pNetServer, pMasterServer, pConfig, pConsole);
|
|
||||||
}
|
|
||||||
|
|
||||||
int CServer::Run()
|
int CServer::Run()
|
||||||
{
|
{
|
||||||
if(m_RunServer == UNINITIALIZED)
|
if(m_RunServer == UNINITIALIZED)
|
||||||
|
@ -2548,6 +2621,9 @@ int CServer::Run()
|
||||||
m_UPnP.Open(BindAddr);
|
m_UPnP.Open(BindAddr);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
IEngine *pEngine = Kernel()->RequestInterface<IEngine>();
|
||||||
|
m_pRegister = CreateRegister(&g_Config, m_pConsole, pEngine, this->Port(), m_NetServer.GetGlobalToken());
|
||||||
|
|
||||||
m_NetServer.SetCallbacks(NewClientCallback, NewClientNoAuthCallback, ClientRejoinCallback, DelClientCallback, this);
|
m_NetServer.SetCallbacks(NewClientCallback, NewClientNoAuthCallback, ClientRejoinCallback, DelClientCallback, this);
|
||||||
|
|
||||||
m_Econ.Init(Config(), Console(), &m_ServerBan);
|
m_Econ.Init(Config(), Console(), &m_ServerBan);
|
||||||
|
@ -2575,6 +2651,7 @@ int CServer::Run()
|
||||||
|
|
||||||
// process pending commands
|
// process pending commands
|
||||||
m_pConsole->StoreCommands(false);
|
m_pConsole->StoreCommands(false);
|
||||||
|
m_pRegister->OnConfigChange();
|
||||||
|
|
||||||
if(m_AuthManager.IsGenerated())
|
if(m_AuthManager.IsGenerated())
|
||||||
{
|
{
|
||||||
|
@ -2742,9 +2819,7 @@ int CServer::Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
// master server stuff
|
// master server stuff
|
||||||
m_Register.RegisterUpdate(m_NetServer.NetType());
|
m_pRegister->Update();
|
||||||
if(Config()->m_SvSixup)
|
|
||||||
m_RegSixup.RegisterUpdate(m_NetServer.NetType());
|
|
||||||
|
|
||||||
if(m_ServerInfoNeedsUpdate)
|
if(m_ServerInfoNeedsUpdate)
|
||||||
UpdateServerInfo();
|
UpdateServerInfo();
|
||||||
|
@ -3736,7 +3811,6 @@ int main(int argc, const char **argv)
|
||||||
IEngineMap *pEngineMap = CreateEngineMap();
|
IEngineMap *pEngineMap = CreateEngineMap();
|
||||||
IGameServer *pGameServer = CreateGameServer();
|
IGameServer *pGameServer = CreateGameServer();
|
||||||
IConsole *pConsole = CreateConsole(CFGFLAG_SERVER | CFGFLAG_ECON);
|
IConsole *pConsole = CreateConsole(CFGFLAG_SERVER | CFGFLAG_ECON);
|
||||||
IEngineMasterServer *pEngineMasterServer = CreateEngineMasterServer();
|
|
||||||
IStorage *pStorage = CreateStorage(IStorage::STORAGETYPE_SERVER, argc, argv);
|
IStorage *pStorage = CreateStorage(IStorage::STORAGETYPE_SERVER, argc, argv);
|
||||||
IConfigManager *pConfigManager = CreateConfigManager();
|
IConfigManager *pConfigManager = CreateConfigManager();
|
||||||
IEngineAntibot *pEngineAntibot = CreateEngineAntibot();
|
IEngineAntibot *pEngineAntibot = CreateEngineAntibot();
|
||||||
|
@ -3752,8 +3826,6 @@ int main(int argc, const char **argv)
|
||||||
set_exception_handler_log_file(aBuf);
|
set_exception_handler_log_file(aBuf);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
pServer->InitRegister(&pServer->m_NetServer, pEngineMasterServer, pConfigManager->Values(), pConsole);
|
|
||||||
|
|
||||||
{
|
{
|
||||||
bool RegisterFail = false;
|
bool RegisterFail = false;
|
||||||
|
|
||||||
|
@ -3765,8 +3837,6 @@ int main(int argc, const char **argv)
|
||||||
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConsole);
|
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConsole);
|
||||||
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pStorage);
|
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pStorage);
|
||||||
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfigManager);
|
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfigManager);
|
||||||
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pEngineMasterServer); // register as both
|
|
||||||
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMasterServer *>(pEngineMasterServer), false);
|
|
||||||
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pEngineAntibot);
|
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pEngineAntibot);
|
||||||
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IAntibot *>(pEngineAntibot), false);
|
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IAntibot *>(pEngineAntibot), false);
|
||||||
|
|
||||||
|
@ -3780,8 +3850,6 @@ int main(int argc, const char **argv)
|
||||||
pEngine->Init();
|
pEngine->Init();
|
||||||
pConfigManager->Init();
|
pConfigManager->Init();
|
||||||
pConsole->Init();
|
pConsole->Init();
|
||||||
pEngineMasterServer->Init();
|
|
||||||
pEngineMasterServer->Load();
|
|
||||||
|
|
||||||
// register all console commands
|
// register all console commands
|
||||||
pServer->RegisterCommands();
|
pServer->RegisterCommands();
|
||||||
|
|
|
@ -98,6 +98,7 @@ class CServer : public IServer
|
||||||
class IConsole *m_pConsole;
|
class IConsole *m_pConsole;
|
||||||
class IStorage *m_pStorage;
|
class IStorage *m_pStorage;
|
||||||
class IEngineAntibot *m_pAntibot;
|
class IEngineAntibot *m_pAntibot;
|
||||||
|
class IRegister *m_pRegister;
|
||||||
|
|
||||||
#if defined(CONF_UPNP)
|
#if defined(CONF_UPNP)
|
||||||
CUPnP m_UPnP;
|
CUPnP m_UPnP;
|
||||||
|
@ -252,8 +253,6 @@ public:
|
||||||
unsigned int m_aCurrentMapSize[NUM_MAP_TYPES];
|
unsigned int m_aCurrentMapSize[NUM_MAP_TYPES];
|
||||||
|
|
||||||
CDemoRecorder m_aDemoRecorder[MAX_CLIENTS + 1];
|
CDemoRecorder m_aDemoRecorder[MAX_CLIENTS + 1];
|
||||||
CRegister m_Register;
|
|
||||||
CRegister m_RegSixup;
|
|
||||||
CAuthManager m_AuthManager;
|
CAuthManager m_AuthManager;
|
||||||
|
|
||||||
int64_t m_ServerInfoFirstRequest;
|
int64_t m_ServerInfoFirstRequest;
|
||||||
|
@ -362,6 +361,7 @@ public:
|
||||||
void GetServerInfoSixup(CPacker *pPacker, int Token, bool SendClients);
|
void GetServerInfoSixup(CPacker *pPacker, int Token, bool SendClients);
|
||||||
bool RateLimitServerInfoConnless();
|
bool RateLimitServerInfoConnless();
|
||||||
void SendServerInfoConnless(const NETADDR *pAddr, int Token, int Type);
|
void SendServerInfoConnless(const NETADDR *pAddr, int Token, int Type);
|
||||||
|
void UpdateRegisterServerInfo();
|
||||||
void UpdateServerInfo(bool Resend = false);
|
void UpdateServerInfo(bool Resend = false);
|
||||||
|
|
||||||
void PumpNetwork(bool PacketWaiting);
|
void PumpNetwork(bool PacketWaiting);
|
||||||
|
@ -375,7 +375,6 @@ public:
|
||||||
void StopRecord(int ClientID) override;
|
void StopRecord(int ClientID) override;
|
||||||
bool IsRecording(int ClientID) override;
|
bool IsRecording(int ClientID) override;
|
||||||
|
|
||||||
void InitRegister(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, CConfig *pConfig, IConsole *pConsole);
|
|
||||||
int Run();
|
int Run();
|
||||||
|
|
||||||
static void ConTestingCommands(IConsole::IResult *pResult, void *pUser);
|
static void ConTestingCommands(IConsole::IResult *pResult, void *pUser);
|
||||||
|
|
|
@ -146,7 +146,9 @@ MACRO_CONFIG_STR(SvMap, sv_map, 128, "Sunny Side Up", CFGFLAG_SERVER, "Map to us
|
||||||
MACRO_CONFIG_INT(SvMaxClients, sv_max_clients, MAX_CLIENTS, 1, MAX_CLIENTS, CFGFLAG_SERVER, "Maximum number of clients that are allowed on a server")
|
MACRO_CONFIG_INT(SvMaxClients, sv_max_clients, MAX_CLIENTS, 1, MAX_CLIENTS, CFGFLAG_SERVER, "Maximum number of clients that are allowed on a server")
|
||||||
MACRO_CONFIG_INT(SvMaxClientsPerIP, sv_max_clients_per_ip, 4, 1, MAX_CLIENTS, CFGFLAG_SERVER, "Maximum number of clients with the same IP that can connect to the server")
|
MACRO_CONFIG_INT(SvMaxClientsPerIP, sv_max_clients_per_ip, 4, 1, MAX_CLIENTS, CFGFLAG_SERVER, "Maximum number of clients with the same IP that can connect to the server")
|
||||||
MACRO_CONFIG_INT(SvHighBandwidth, sv_high_bandwidth, 0, 0, 1, CFGFLAG_SERVER, "Use high bandwidth mode. Doubles the bandwidth required for the server. LAN use only")
|
MACRO_CONFIG_INT(SvHighBandwidth, sv_high_bandwidth, 0, 0, 1, CFGFLAG_SERVER, "Use high bandwidth mode. Doubles the bandwidth required for the server. LAN use only")
|
||||||
MACRO_CONFIG_INT(SvRegister, sv_register, 1, 0, 1, CFGFLAG_SERVER, "Register server with master server for public listing")
|
MACRO_CONFIG_STR(SvRegister, sv_register, 16, "1", CFGFLAG_SERVER, "Register server with master server for public listing, can also accept a comma-separated list of protocols to register on, like 'ipv4,ipv6'")
|
||||||
|
MACRO_CONFIG_STR(SvRegisterExtra, sv_register_extra, 256, "", CFGFLAG_SERVER, "Extra headers to send to the register endpoint, comma separated 'Header: Value' pairs")
|
||||||
|
MACRO_CONFIG_STR(SvRegisterUrl, sv_register_url, 128, "https://master1.ddnet.tw/ddnet/15/register", CFGFLAG_SERVER, "Masterserver URL to register to")
|
||||||
MACRO_CONFIG_STR(SvRconPassword, sv_rcon_password, 32, "", CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, "Remote console password (full access)")
|
MACRO_CONFIG_STR(SvRconPassword, sv_rcon_password, 32, "", CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, "Remote console password (full access)")
|
||||||
MACRO_CONFIG_STR(SvRconModPassword, sv_rcon_mod_password, 32, "", CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, "Remote console password for moderators (limited access)")
|
MACRO_CONFIG_STR(SvRconModPassword, sv_rcon_mod_password, 32, "", CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, "Remote console password for moderators (limited access)")
|
||||||
MACRO_CONFIG_STR(SvRconHelperPassword, sv_rcon_helper_password, 32, "", CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, "Remote console password for helpers (limited access)")
|
MACRO_CONFIG_STR(SvRconHelperPassword, sv_rcon_helper_password, 32, "", CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, "Remote console password for helpers (limited access)")
|
||||||
|
|
|
@ -991,8 +991,18 @@ CConsole::~CConsole()
|
||||||
while(pCommand)
|
while(pCommand)
|
||||||
{
|
{
|
||||||
CCommand *pNext = pCommand->m_pNext;
|
CCommand *pNext = pCommand->m_pNext;
|
||||||
if(pCommand->m_pfnCallback == Con_Chain)
|
{
|
||||||
delete static_cast<CChain *>(pCommand->m_pUserData);
|
FCommandCallback pfnCallback = pCommand->m_pfnCallback;
|
||||||
|
void *pUserData = pCommand->m_pUserData;
|
||||||
|
CChain *pChain = nullptr;
|
||||||
|
while(pfnCallback == Con_Chain)
|
||||||
|
{
|
||||||
|
pChain = static_cast<CChain *>(pUserData);
|
||||||
|
pfnCallback = pChain->m_pfnCallback;
|
||||||
|
pUserData = pChain->m_pCallbackUserData;
|
||||||
|
delete pChain;
|
||||||
|
}
|
||||||
|
}
|
||||||
// Temp commands are on m_TempCommands heap, so don't delete them
|
// Temp commands are on m_TempCommands heap, so don't delete them
|
||||||
if(!pCommand->m_Temp)
|
if(!pCommand->m_Temp)
|
||||||
delete pCommand;
|
delete pCommand;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "http.h"
|
#include "http.h"
|
||||||
|
|
||||||
|
#include <base/log.h>
|
||||||
#include <base/math.h>
|
#include <base/math.h>
|
||||||
#include <base/system.h>
|
#include <base/system.h>
|
||||||
#include <engine/engine.h>
|
#include <engine/engine.h>
|
||||||
|
@ -44,6 +45,33 @@ static void CurlUnlock(CURL *pHandle, curl_lock_data Data, void *pUser) RELEASE(
|
||||||
lock_unlock(gs_aLocks[GetLockIndex(Data)]);
|
lock_unlock(gs_aLocks[GetLockIndex(Data)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CurlDebug(CURL *pHandle, curl_infotype Type, char *pData, size_t DataSize, void *pUser)
|
||||||
|
{
|
||||||
|
char TypeChar;
|
||||||
|
switch(Type)
|
||||||
|
{
|
||||||
|
case CURLINFO_TEXT:
|
||||||
|
TypeChar = '*';
|
||||||
|
break;
|
||||||
|
case CURLINFO_HEADER_OUT:
|
||||||
|
TypeChar = '<';
|
||||||
|
break;
|
||||||
|
case CURLINFO_HEADER_IN:
|
||||||
|
TypeChar = '>';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
while(const char *pLineEnd = (const char *)memchr(pData, '\n', DataSize))
|
||||||
|
{
|
||||||
|
int LineLength = pLineEnd - pData;
|
||||||
|
log_debug("curl", "%c %.*s", TypeChar, LineLength, pData);
|
||||||
|
pData += LineLength + 1;
|
||||||
|
DataSize -= LineLength + 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
bool HttpInit(IStorage *pStorage)
|
bool HttpInit(IStorage *pStorage)
|
||||||
{
|
{
|
||||||
if(curl_global_init(CURL_GLOBAL_DEFAULT))
|
if(curl_global_init(CURL_GLOBAL_DEFAULT))
|
||||||
|
@ -159,6 +187,7 @@ int CHttpRequest::RunImpl(CURL *pUser)
|
||||||
if(g_Config.m_DbgCurl)
|
if(g_Config.m_DbgCurl)
|
||||||
{
|
{
|
||||||
curl_easy_setopt(pHandle, CURLOPT_VERBOSE, 1L);
|
curl_easy_setopt(pHandle, CURLOPT_VERBOSE, 1L);
|
||||||
|
curl_easy_setopt(pHandle, CURLOPT_DEBUGFUNCTION, CurlDebug);
|
||||||
}
|
}
|
||||||
char aErr[CURL_ERROR_SIZE];
|
char aErr[CURL_ERROR_SIZE];
|
||||||
curl_easy_setopt(pHandle, CURLOPT_ERRORBUFFER, aErr);
|
curl_easy_setopt(pHandle, CURLOPT_ERRORBUFFER, aErr);
|
||||||
|
@ -208,6 +237,10 @@ int CHttpRequest::RunImpl(CURL *pUser)
|
||||||
{
|
{
|
||||||
Header("Content-Type: application/json");
|
Header("Content-Type: application/json");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Header("Content-Type:");
|
||||||
|
}
|
||||||
curl_easy_setopt(pHandle, CURLOPT_POSTFIELDS, m_pBody);
|
curl_easy_setopt(pHandle, CURLOPT_POSTFIELDS, m_pBody);
|
||||||
curl_easy_setopt(pHandle, CURLOPT_POSTFIELDSIZE, m_BodyLength);
|
curl_easy_setopt(pHandle, CURLOPT_POSTFIELDSIZE, m_BodyLength);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1,231 +1,12 @@
|
||||||
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
#include "masterserver.h"
|
||||||
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
||||||
#include <cstdio> // sscanf
|
|
||||||
|
|
||||||
#include <base/system.h>
|
const unsigned char SERVERBROWSE_GETINFO[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'g', 'i', 'e', '3'};
|
||||||
|
const unsigned char SERVERBROWSE_INFO[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'i', 'n', 'f', '3'};
|
||||||
|
|
||||||
#include <engine/engine.h>
|
const unsigned char SERVERBROWSE_GETINFO_64_LEGACY[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'f', 's', 't', 'd'};
|
||||||
#include <engine/masterserver.h>
|
const unsigned char SERVERBROWSE_INFO_64_LEGACY[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'd', 't', 's', 'f'};
|
||||||
#include <engine/storage.h>
|
|
||||||
|
|
||||||
#include "linereader.h"
|
const unsigned char SERVERBROWSE_INFO_EXTENDED[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'i', 'e', 'x', 't'};
|
||||||
|
const unsigned char SERVERBROWSE_INFO_EXTENDED_MORE[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'i', 'e', 'x', '+'};
|
||||||
|
|
||||||
class CMasterServer : public IEngineMasterServer
|
const unsigned char SERVERBROWSE_CHALLENGE[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'c', 'h', 'a', 'l'};
|
||||||
{
|
|
||||||
public:
|
|
||||||
// master server functions
|
|
||||||
struct CMasterInfo
|
|
||||||
{
|
|
||||||
char m_aHostname[128];
|
|
||||||
NETADDR m_Addr;
|
|
||||||
bool m_Valid;
|
|
||||||
int m_Count;
|
|
||||||
std::shared_ptr<CHostLookup> m_pLookup;
|
|
||||||
};
|
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
STATE_INIT,
|
|
||||||
STATE_UPDATE,
|
|
||||||
STATE_READY,
|
|
||||||
};
|
|
||||||
|
|
||||||
CMasterInfo m_aMasterServers[MAX_MASTERSERVERS];
|
|
||||||
std::shared_ptr<CHostLookup> m_apLookup[MAX_MASTERSERVERS];
|
|
||||||
int m_State;
|
|
||||||
IEngine *m_pEngine;
|
|
||||||
IStorage *m_pStorage;
|
|
||||||
|
|
||||||
CMasterServer()
|
|
||||||
{
|
|
||||||
SetDefault();
|
|
||||||
m_State = STATE_INIT;
|
|
||||||
m_pEngine = 0;
|
|
||||||
m_pStorage = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int RefreshAddresses(int Nettype) override
|
|
||||||
{
|
|
||||||
if(m_State != STATE_INIT && m_State != STATE_READY)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
dbg_msg("engine/mastersrv", "refreshing master server addresses");
|
|
||||||
|
|
||||||
// add lookup jobs
|
|
||||||
for(int i = 0; i < MAX_MASTERSERVERS; i++)
|
|
||||||
{
|
|
||||||
*m_apLookup[i] = CHostLookup(m_aMasterServers[i].m_aHostname, Nettype);
|
|
||||||
m_pEngine->AddJob(m_apLookup[i]);
|
|
||||||
m_aMasterServers[i].m_Valid = false;
|
|
||||||
m_aMasterServers[i].m_Count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_State = STATE_UPDATE;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Update() override
|
|
||||||
{
|
|
||||||
// check if we need to update
|
|
||||||
if(m_State != STATE_UPDATE)
|
|
||||||
return;
|
|
||||||
m_State = STATE_READY;
|
|
||||||
|
|
||||||
for(int i = 0; i < MAX_MASTERSERVERS; i++)
|
|
||||||
{
|
|
||||||
if(m_apLookup[i]->Status() != IJob::STATE_DONE)
|
|
||||||
m_State = STATE_UPDATE;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(m_apLookup[i]->m_Result == 0)
|
|
||||||
{
|
|
||||||
m_aMasterServers[i].m_Addr = m_apLookup[i]->m_Addr;
|
|
||||||
m_aMasterServers[i].m_Addr.port = 8300;
|
|
||||||
m_aMasterServers[i].m_Valid = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_aMasterServers[i].m_Valid = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(m_State == STATE_READY)
|
|
||||||
{
|
|
||||||
dbg_msg("engine/mastersrv", "saving addresses");
|
|
||||||
Save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsRefreshing() const override
|
|
||||||
{
|
|
||||||
return m_State != STATE_READY;
|
|
||||||
}
|
|
||||||
|
|
||||||
NETADDR GetAddr(int Index) const override
|
|
||||||
{
|
|
||||||
return m_aMasterServers[Index].m_Addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetCount(int Index, int Count) override
|
|
||||||
{
|
|
||||||
m_aMasterServers[Index].m_Count = Count;
|
|
||||||
}
|
|
||||||
|
|
||||||
int GetCount(int Index) const override
|
|
||||||
{
|
|
||||||
return m_aMasterServers[Index].m_Count;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *GetName(int Index) const override
|
|
||||||
{
|
|
||||||
return m_aMasterServers[Index].m_aHostname;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsValid(int Index) const override
|
|
||||||
{
|
|
||||||
return m_aMasterServers[Index].m_Valid;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Init() override
|
|
||||||
{
|
|
||||||
m_pEngine = Kernel()->RequestInterface<IEngine>();
|
|
||||||
m_pStorage = Kernel()->RequestInterface<IStorage>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetDefault() override
|
|
||||||
{
|
|
||||||
mem_zero(m_aMasterServers, sizeof(m_aMasterServers));
|
|
||||||
for(int i = 0; i < MAX_MASTERSERVERS; i++)
|
|
||||||
{
|
|
||||||
str_format(m_aMasterServers[i].m_aHostname, sizeof(m_aMasterServers[i].m_aHostname), "master%d.teeworlds.com", i + 1);
|
|
||||||
m_apLookup[i] = std::make_shared<CHostLookup>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int Load() override
|
|
||||||
{
|
|
||||||
if(!m_pStorage)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
// try to open file
|
|
||||||
IOHANDLE File = m_pStorage->OpenFile("masters.cfg", IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_SAVE);
|
|
||||||
if(!File)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
CLineReader LineReader;
|
|
||||||
LineReader.Init(File);
|
|
||||||
while(true)
|
|
||||||
{
|
|
||||||
CMasterInfo Info = {{0}};
|
|
||||||
const char *pLine = LineReader.Get();
|
|
||||||
if(!pLine)
|
|
||||||
break;
|
|
||||||
|
|
||||||
// parse line
|
|
||||||
char aAddrStr[NETADDR_MAXSTRSIZE];
|
|
||||||
if(sscanf(pLine, "%127s %47s", Info.m_aHostname, aAddrStr) == 2 && net_addr_from_str(&Info.m_Addr, aAddrStr) == 0)
|
|
||||||
{
|
|
||||||
Info.m_Addr.port = 8300;
|
|
||||||
bool Added = false;
|
|
||||||
for(auto &MasterServer : m_aMasterServers)
|
|
||||||
{
|
|
||||||
if(str_comp(MasterServer.m_aHostname, Info.m_aHostname) == 0)
|
|
||||||
{
|
|
||||||
MasterServer = Info;
|
|
||||||
Added = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!Added)
|
|
||||||
{
|
|
||||||
for(auto &MasterServer : m_aMasterServers)
|
|
||||||
{
|
|
||||||
if(MasterServer.m_Addr.type == NETTYPE_INVALID)
|
|
||||||
{
|
|
||||||
MasterServer = Info;
|
|
||||||
Added = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!Added)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
io_close(File);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Save() override
|
|
||||||
{
|
|
||||||
if(!m_pStorage)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
// try to open file
|
|
||||||
IOHANDLE File = m_pStorage->OpenFile("masters.cfg", IOFLAG_WRITE, IStorage::TYPE_SAVE);
|
|
||||||
if(!File)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
for(auto &MasterServer : m_aMasterServers)
|
|
||||||
{
|
|
||||||
char aAddrStr[NETADDR_MAXSTRSIZE];
|
|
||||||
if(MasterServer.m_Addr.type != NETTYPE_INVALID)
|
|
||||||
net_addr_str(&MasterServer.m_Addr, aAddrStr, sizeof(aAddrStr), true);
|
|
||||||
else
|
|
||||||
aAddrStr[0] = 0;
|
|
||||||
char aBuf[256];
|
|
||||||
str_format(aBuf, sizeof(aBuf), "%s %s", MasterServer.m_aHostname, aAddrStr);
|
|
||||||
io_write(File, aBuf, str_length(aBuf));
|
|
||||||
io_write_newline(File);
|
|
||||||
}
|
|
||||||
|
|
||||||
io_close(File);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
IEngineMasterServer *CreateEngineMasterServer() { return new CMasterServer; }
|
|
||||||
|
|
26
src/engine/shared/masterserver.h
Normal file
26
src/engine/shared/masterserver.h
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
||||||
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
||||||
|
#ifndef ENGINE_SHARED_MASTERSERVER_H
|
||||||
|
#define ENGINE_SHARED_MASTERSERVER_H
|
||||||
|
|
||||||
|
#define SERVERBROWSE_SIZE 8
|
||||||
|
extern const unsigned char SERVERBROWSE_GETINFO[SERVERBROWSE_SIZE];
|
||||||
|
extern const unsigned char SERVERBROWSE_INFO[SERVERBROWSE_SIZE];
|
||||||
|
|
||||||
|
extern const unsigned char SERVERBROWSE_GETINFO_64_LEGACY[SERVERBROWSE_SIZE];
|
||||||
|
extern const unsigned char SERVERBROWSE_INFO_64_LEGACY[SERVERBROWSE_SIZE];
|
||||||
|
|
||||||
|
extern const unsigned char SERVERBROWSE_INFO_EXTENDED[SERVERBROWSE_SIZE];
|
||||||
|
extern const unsigned char SERVERBROWSE_INFO_EXTENDED_MORE[SERVERBROWSE_SIZE];
|
||||||
|
|
||||||
|
extern const unsigned char SERVERBROWSE_CHALLENGE[SERVERBROWSE_SIZE];
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
SERVERINFO_VANILLA = 0,
|
||||||
|
SERVERINFO_64_LEGACY,
|
||||||
|
SERVERINFO_EXTENDED,
|
||||||
|
SERVERINFO_EXTENDED_MORE,
|
||||||
|
SERVERINFO_INGAME,
|
||||||
|
};
|
||||||
|
#endif // ENGINE_SHARED_MASTERSERVER_H
|
|
@ -425,6 +425,7 @@ public:
|
||||||
const char *ErrorString(int ClientID);
|
const char *ErrorString(int ClientID);
|
||||||
|
|
||||||
// anti spoof
|
// anti spoof
|
||||||
|
SECURITY_TOKEN GetGlobalToken();
|
||||||
SECURITY_TOKEN GetToken(const NETADDR &Addr);
|
SECURITY_TOKEN GetToken(const NETADDR &Addr);
|
||||||
// vanilla token/gametick shouldn't be negative
|
// vanilla token/gametick shouldn't be negative
|
||||||
SECURITY_TOKEN GetVanillaToken(const NETADDR &Addr) { return absolute(GetToken(Addr)); }
|
SECURITY_TOKEN GetVanillaToken(const NETADDR &Addr) { return absolute(GetToken(Addr)); }
|
||||||
|
|
|
@ -135,6 +135,11 @@ int CNetServer::Update()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECURITY_TOKEN CNetServer::GetGlobalToken()
|
||||||
|
{
|
||||||
|
static NETADDR NullAddr = {0};
|
||||||
|
return GetToken(NullAddr);
|
||||||
|
}
|
||||||
SECURITY_TOKEN CNetServer::GetToken(const NETADDR &Addr)
|
SECURITY_TOKEN CNetServer::GetToken(const NETADDR &Addr)
|
||||||
{
|
{
|
||||||
SHA256_CTX Sha256;
|
SHA256_CTX Sha256;
|
||||||
|
@ -652,7 +657,7 @@ int CNetServer::Recv(CNetChunk *pChunk, SECURITY_TOKEN *pResponseToken)
|
||||||
{
|
{
|
||||||
if(m_RecvUnpacker.m_Data.m_Flags & NET_PACKETFLAG_CONNLESS)
|
if(m_RecvUnpacker.m_Data.m_Flags & NET_PACKETFLAG_CONNLESS)
|
||||||
{
|
{
|
||||||
if(Sixup && Token != GetToken(Addr))
|
if(Sixup && Token != GetToken(Addr) && Token != GetGlobalToken())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
pChunk->m_Flags = NETSENDFLAG_CONNLESS;
|
pChunk->m_Flags = NETSENDFLAG_CONNLESS;
|
||||||
|
|
|
@ -35,7 +35,6 @@
|
||||||
#include <game/client/lineinput.h>
|
#include <game/client/lineinput.h>
|
||||||
#include <game/generated/client_data.h>
|
#include <game/generated/client_data.h>
|
||||||
#include <game/localization.h>
|
#include <game/localization.h>
|
||||||
#include <mastersrv/mastersrv.h>
|
|
||||||
|
|
||||||
#include "controls.h"
|
#include "controls.h"
|
||||||
#include "countryflags.h"
|
#include "countryflags.h"
|
||||||
|
|
1040
src/mastersrv/Cargo.lock
generated
Normal file
1040
src/mastersrv/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
32
src/mastersrv/Cargo.toml
Normal file
32
src/mastersrv/Cargo.toml
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
[package]
|
||||||
|
name = "mastersrv"
|
||||||
|
version = "0.0.1"
|
||||||
|
authors = ["heinrich5991 <heinrich5991@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
arrayvec = { version = "0.5.2", features = ["serde"] }
|
||||||
|
base64 = "0.13.0"
|
||||||
|
bytes = "1.1.0"
|
||||||
|
clap = { version = "2.34.0", default-features = false, features = [
|
||||||
|
"suggestions",
|
||||||
|
"wrap_help",
|
||||||
|
] }
|
||||||
|
csv = "1.1.6"
|
||||||
|
env_logger = "0.8.3"
|
||||||
|
headers = "0.3.7"
|
||||||
|
hex = "0.4.3"
|
||||||
|
ipnet = { version = "2.5.0", features = ["serde"] }
|
||||||
|
log = "0.4.17"
|
||||||
|
rand = "0.8.4"
|
||||||
|
serde = { version = "1.0.126", features = ["derive"] }
|
||||||
|
serde_json = { version = "1.0.64", features = [
|
||||||
|
"float_roundtrip",
|
||||||
|
"preserve_order",
|
||||||
|
"raw_value",
|
||||||
|
] }
|
||||||
|
sha2 = "0.10.0"
|
||||||
|
tokio = { version = "1.6.0", features = ["macros", "rt", "rt-multi-thread"] }
|
||||||
|
tokio-stream = { version = "0.1.8", features = ["net"] }
|
||||||
|
url = { version = "2.2.2", features = ["serde"] }
|
||||||
|
warp = { version = "0.3.1", default-features = false }
|
|
@ -1,549 +0,0 @@
|
||||||
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
||||||
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
||||||
#include <base/logger.h>
|
|
||||||
#include <base/system.h>
|
|
||||||
|
|
||||||
#include <engine/config.h>
|
|
||||||
#include <engine/console.h>
|
|
||||||
#include <engine/kernel.h>
|
|
||||||
#include <engine/storage.h>
|
|
||||||
|
|
||||||
#include <engine/shared/config.h>
|
|
||||||
#include <engine/shared/netban.h>
|
|
||||||
#include <engine/shared/network.h>
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
#include "mastersrv.h"
|
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
MTU = 1400,
|
|
||||||
MAX_SERVERS_PER_PACKET = 75,
|
|
||||||
MAX_PACKETS = 16,
|
|
||||||
MAX_SERVERS = MAX_SERVERS_PER_PACKET * MAX_PACKETS,
|
|
||||||
EXPIRE_TIME = 90
|
|
||||||
};
|
|
||||||
|
|
||||||
struct CCheckServer
|
|
||||||
{
|
|
||||||
enum ServerType m_Type;
|
|
||||||
NETADDR m_Address;
|
|
||||||
NETADDR m_AltAddress;
|
|
||||||
int m_TryCount;
|
|
||||||
int64_t m_TryTime;
|
|
||||||
};
|
|
||||||
|
|
||||||
static CCheckServer m_aCheckServers[MAX_SERVERS];
|
|
||||||
static int m_NumCheckServers = 0;
|
|
||||||
|
|
||||||
struct CServerEntry
|
|
||||||
{
|
|
||||||
enum ServerType m_Type;
|
|
||||||
NETADDR m_Address;
|
|
||||||
int64_t m_Expire;
|
|
||||||
};
|
|
||||||
|
|
||||||
static CServerEntry m_aServers[MAX_SERVERS];
|
|
||||||
static int m_NumServers = 0;
|
|
||||||
|
|
||||||
struct CPacketData
|
|
||||||
{
|
|
||||||
int m_Size;
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
unsigned char m_aHeader[sizeof(SERVERBROWSE_LIST)];
|
|
||||||
CMastersrvAddr m_aServers[MAX_SERVERS_PER_PACKET];
|
|
||||||
} m_Data;
|
|
||||||
};
|
|
||||||
|
|
||||||
CPacketData m_aPackets[MAX_PACKETS];
|
|
||||||
static int m_NumPackets = 0;
|
|
||||||
|
|
||||||
// legacy code
|
|
||||||
struct CPacketDataLegacy
|
|
||||||
{
|
|
||||||
int m_Size;
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
unsigned char m_aHeader[sizeof(SERVERBROWSE_LIST_LEGACY)];
|
|
||||||
CMastersrvAddrLegacy m_aServers[MAX_SERVERS_PER_PACKET];
|
|
||||||
} m_Data;
|
|
||||||
};
|
|
||||||
|
|
||||||
CPacketDataLegacy m_aPacketsLegacy[MAX_PACKETS];
|
|
||||||
static int m_NumPacketsLegacy = 0;
|
|
||||||
|
|
||||||
struct CCountPacketData
|
|
||||||
{
|
|
||||||
unsigned char m_Header[sizeof(SERVERBROWSE_COUNT)];
|
|
||||||
unsigned char m_High;
|
|
||||||
unsigned char m_Low;
|
|
||||||
};
|
|
||||||
|
|
||||||
static CCountPacketData m_CountData;
|
|
||||||
static CCountPacketData m_CountDataLegacy;
|
|
||||||
|
|
||||||
CNetBan m_NetBan;
|
|
||||||
|
|
||||||
static CNetClient m_NetChecker; // NAT/FW checker
|
|
||||||
static CNetClient m_NetOp; // main
|
|
||||||
|
|
||||||
IConsole *m_pConsole;
|
|
||||||
|
|
||||||
void BuildPackets()
|
|
||||||
{
|
|
||||||
CServerEntry *pCurrent = &m_aServers[0];
|
|
||||||
int ServersLeft = m_NumServers;
|
|
||||||
m_NumPackets = 0;
|
|
||||||
m_NumPacketsLegacy = 0;
|
|
||||||
int PacketIndex = 0;
|
|
||||||
int PacketIndexLegacy = 0;
|
|
||||||
while(ServersLeft-- && (m_NumPackets + m_NumPacketsLegacy) < MAX_PACKETS)
|
|
||||||
{
|
|
||||||
if(pCurrent->m_Type == SERVERTYPE_NORMAL)
|
|
||||||
{
|
|
||||||
if(PacketIndex % MAX_SERVERS_PER_PACKET == 0)
|
|
||||||
{
|
|
||||||
PacketIndex = 0;
|
|
||||||
m_NumPackets++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy header
|
|
||||||
mem_copy(m_aPackets[m_NumPackets - 1].m_Data.m_aHeader, SERVERBROWSE_LIST, sizeof(SERVERBROWSE_LIST));
|
|
||||||
|
|
||||||
// copy server addresses
|
|
||||||
if(pCurrent->m_Address.type == NETTYPE_IPV6)
|
|
||||||
{
|
|
||||||
mem_copy(m_aPackets[m_NumPackets - 1].m_Data.m_aServers[PacketIndex].m_aIp, pCurrent->m_Address.ip,
|
|
||||||
sizeof(m_aPackets[m_NumPackets - 1].m_Data.m_aServers[PacketIndex].m_aIp));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
static char IPV4Mapping[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (char)0xFF, (char)0xFF};
|
|
||||||
|
|
||||||
mem_copy(m_aPackets[m_NumPackets - 1].m_Data.m_aServers[PacketIndex].m_aIp, IPV4Mapping, sizeof(IPV4Mapping));
|
|
||||||
m_aPackets[m_NumPackets - 1].m_Data.m_aServers[PacketIndex].m_aIp[12] = pCurrent->m_Address.ip[0];
|
|
||||||
m_aPackets[m_NumPackets - 1].m_Data.m_aServers[PacketIndex].m_aIp[13] = pCurrent->m_Address.ip[1];
|
|
||||||
m_aPackets[m_NumPackets - 1].m_Data.m_aServers[PacketIndex].m_aIp[14] = pCurrent->m_Address.ip[2];
|
|
||||||
m_aPackets[m_NumPackets - 1].m_Data.m_aServers[PacketIndex].m_aIp[15] = pCurrent->m_Address.ip[3];
|
|
||||||
}
|
|
||||||
|
|
||||||
m_aPackets[m_NumPackets - 1].m_Data.m_aServers[PacketIndex].m_aPort[0] = (pCurrent->m_Address.port >> 8) & 0xff;
|
|
||||||
m_aPackets[m_NumPackets - 1].m_Data.m_aServers[PacketIndex].m_aPort[1] = pCurrent->m_Address.port & 0xff;
|
|
||||||
|
|
||||||
PacketIndex++;
|
|
||||||
|
|
||||||
m_aPackets[m_NumPackets - 1].m_Size = sizeof(SERVERBROWSE_LIST) + sizeof(CMastersrvAddr) * PacketIndex;
|
|
||||||
|
|
||||||
pCurrent++;
|
|
||||||
}
|
|
||||||
else if(pCurrent->m_Type == SERVERTYPE_LEGACY)
|
|
||||||
{
|
|
||||||
if(PacketIndexLegacy % MAX_SERVERS_PER_PACKET == 0)
|
|
||||||
{
|
|
||||||
PacketIndexLegacy = 0;
|
|
||||||
m_NumPacketsLegacy++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy header
|
|
||||||
mem_copy(m_aPacketsLegacy[m_NumPacketsLegacy - 1].m_Data.m_aHeader, SERVERBROWSE_LIST_LEGACY, sizeof(SERVERBROWSE_LIST_LEGACY));
|
|
||||||
|
|
||||||
// copy server addresses
|
|
||||||
mem_copy(m_aPacketsLegacy[m_NumPacketsLegacy - 1].m_Data.m_aServers[PacketIndexLegacy].m_aIp, pCurrent->m_Address.ip,
|
|
||||||
sizeof(m_aPacketsLegacy[m_NumPacketsLegacy - 1].m_Data.m_aServers[PacketIndexLegacy].m_aIp));
|
|
||||||
// 0.5 has the port in little endian on the network
|
|
||||||
m_aPacketsLegacy[m_NumPacketsLegacy - 1].m_Data.m_aServers[PacketIndexLegacy].m_aPort[0] = pCurrent->m_Address.port & 0xff;
|
|
||||||
m_aPacketsLegacy[m_NumPacketsLegacy - 1].m_Data.m_aServers[PacketIndexLegacy].m_aPort[1] = (pCurrent->m_Address.port >> 8) & 0xff;
|
|
||||||
|
|
||||||
PacketIndexLegacy++;
|
|
||||||
|
|
||||||
m_aPacketsLegacy[m_NumPacketsLegacy - 1].m_Size = sizeof(SERVERBROWSE_LIST_LEGACY) + sizeof(CMastersrvAddrLegacy) * PacketIndexLegacy;
|
|
||||||
|
|
||||||
pCurrent++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*pCurrent = m_aServers[m_NumServers - 1];
|
|
||||||
m_NumServers--;
|
|
||||||
dbg_msg("mastersrv", "ERROR: server of invalid type, dropping it");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SendOk(NETADDR *pAddr)
|
|
||||||
{
|
|
||||||
CNetChunk p;
|
|
||||||
p.m_ClientID = -1;
|
|
||||||
p.m_Address = *pAddr;
|
|
||||||
p.m_Flags = NETSENDFLAG_CONNLESS;
|
|
||||||
p.m_DataSize = sizeof(SERVERBROWSE_FWOK);
|
|
||||||
p.m_pData = SERVERBROWSE_FWOK;
|
|
||||||
|
|
||||||
// send on both to be sure
|
|
||||||
m_NetChecker.Send(&p);
|
|
||||||
m_NetOp.Send(&p);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SendError(NETADDR *pAddr)
|
|
||||||
{
|
|
||||||
CNetChunk p;
|
|
||||||
p.m_ClientID = -1;
|
|
||||||
p.m_Address = *pAddr;
|
|
||||||
p.m_Flags = NETSENDFLAG_CONNLESS;
|
|
||||||
p.m_DataSize = sizeof(SERVERBROWSE_FWERROR);
|
|
||||||
p.m_pData = SERVERBROWSE_FWERROR;
|
|
||||||
m_NetOp.Send(&p);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SendCheck(NETADDR *pAddr)
|
|
||||||
{
|
|
||||||
CNetChunk p;
|
|
||||||
p.m_ClientID = -1;
|
|
||||||
p.m_Address = *pAddr;
|
|
||||||
p.m_Flags = NETSENDFLAG_CONNLESS;
|
|
||||||
p.m_DataSize = sizeof(SERVERBROWSE_FWCHECK);
|
|
||||||
p.m_pData = SERVERBROWSE_FWCHECK;
|
|
||||||
m_NetChecker.Send(&p);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddCheckserver(NETADDR *pInfo, NETADDR *pAlt, ServerType Type)
|
|
||||||
{
|
|
||||||
// add server
|
|
||||||
if(m_NumCheckServers == MAX_SERVERS)
|
|
||||||
{
|
|
||||||
dbg_msg("mastersrv", "ERROR: mastersrv is full");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
char aAddrStr[NETADDR_MAXSTRSIZE];
|
|
||||||
net_addr_str(pInfo, aAddrStr, sizeof(aAddrStr), true);
|
|
||||||
char aAltAddrStr[NETADDR_MAXSTRSIZE];
|
|
||||||
net_addr_str(pAlt, aAltAddrStr, sizeof(aAltAddrStr), true);
|
|
||||||
dbg_msg("mastersrv", "checking: %s (%s)", aAddrStr, aAltAddrStr);
|
|
||||||
m_aCheckServers[m_NumCheckServers].m_Address = *pInfo;
|
|
||||||
m_aCheckServers[m_NumCheckServers].m_AltAddress = *pAlt;
|
|
||||||
m_aCheckServers[m_NumCheckServers].m_TryCount = 0;
|
|
||||||
m_aCheckServers[m_NumCheckServers].m_TryTime = 0;
|
|
||||||
m_aCheckServers[m_NumCheckServers].m_Type = Type;
|
|
||||||
m_NumCheckServers++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddServer(NETADDR *pInfo, ServerType Type)
|
|
||||||
{
|
|
||||||
// see if server already exists in list
|
|
||||||
for(int i = 0; i < m_NumServers; i++)
|
|
||||||
{
|
|
||||||
if(net_addr_comp(&m_aServers[i].m_Address, pInfo) == 0)
|
|
||||||
{
|
|
||||||
char aAddrStr[NETADDR_MAXSTRSIZE];
|
|
||||||
net_addr_str(pInfo, aAddrStr, sizeof(aAddrStr), true);
|
|
||||||
dbg_msg("mastersrv", "updated: %s", aAddrStr);
|
|
||||||
m_aServers[i].m_Expire = time_get() + time_freq() * EXPIRE_TIME;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// add server
|
|
||||||
if(m_NumServers == MAX_SERVERS)
|
|
||||||
{
|
|
||||||
dbg_msg("mastersrv", "ERROR: mastersrv is full");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
char aAddrStr[NETADDR_MAXSTRSIZE];
|
|
||||||
net_addr_str(pInfo, aAddrStr, sizeof(aAddrStr), true);
|
|
||||||
dbg_msg("mastersrv", "added: %s", aAddrStr);
|
|
||||||
m_aServers[m_NumServers].m_Address = *pInfo;
|
|
||||||
m_aServers[m_NumServers].m_Expire = time_get() + time_freq() * EXPIRE_TIME;
|
|
||||||
m_aServers[m_NumServers].m_Type = Type;
|
|
||||||
m_NumServers++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void UpdateServers()
|
|
||||||
{
|
|
||||||
int64_t Now = time_get();
|
|
||||||
int64_t Freq = time_freq();
|
|
||||||
for(int i = 0; i < m_NumCheckServers; i++)
|
|
||||||
{
|
|
||||||
if(Now > m_aCheckServers[i].m_TryTime + Freq)
|
|
||||||
{
|
|
||||||
if(m_aCheckServers[i].m_TryCount == 10)
|
|
||||||
{
|
|
||||||
char aAddrStr[NETADDR_MAXSTRSIZE];
|
|
||||||
net_addr_str(&m_aCheckServers[i].m_Address, aAddrStr, sizeof(aAddrStr), true);
|
|
||||||
char aAltAddrStr[NETADDR_MAXSTRSIZE];
|
|
||||||
net_addr_str(&m_aCheckServers[i].m_AltAddress, aAltAddrStr, sizeof(aAltAddrStr), true);
|
|
||||||
dbg_msg("mastersrv", "check failed: %s (%s)", aAddrStr, aAltAddrStr);
|
|
||||||
|
|
||||||
// FAIL!!
|
|
||||||
SendError(&m_aCheckServers[i].m_Address);
|
|
||||||
m_aCheckServers[i] = m_aCheckServers[m_NumCheckServers - 1];
|
|
||||||
m_NumCheckServers--;
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_aCheckServers[i].m_TryCount++;
|
|
||||||
m_aCheckServers[i].m_TryTime = Now;
|
|
||||||
if(m_aCheckServers[i].m_TryCount & 1)
|
|
||||||
SendCheck(&m_aCheckServers[i].m_Address);
|
|
||||||
else
|
|
||||||
SendCheck(&m_aCheckServers[i].m_AltAddress);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PurgeServers()
|
|
||||||
{
|
|
||||||
int64_t Now = time_get();
|
|
||||||
int i = 0;
|
|
||||||
while(i < m_NumServers)
|
|
||||||
{
|
|
||||||
if(m_aServers[i].m_Expire < Now)
|
|
||||||
{
|
|
||||||
// remove server
|
|
||||||
char aAddrStr[NETADDR_MAXSTRSIZE];
|
|
||||||
net_addr_str(&m_aServers[i].m_Address, aAddrStr, sizeof(aAddrStr), true);
|
|
||||||
dbg_msg("mastersrv", "expired: %s", aAddrStr);
|
|
||||||
m_aServers[i] = m_aServers[m_NumServers - 1];
|
|
||||||
m_NumServers--;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ReloadBans()
|
|
||||||
{
|
|
||||||
m_NetBan.UnbanAll();
|
|
||||||
m_pConsole->ExecuteFile("master.cfg", -1, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, const char **argv)
|
|
||||||
{
|
|
||||||
int64_t LastBuild = 0, LastBanReload = 0;
|
|
||||||
ServerType Type = SERVERTYPE_INVALID;
|
|
||||||
NETADDR BindAddr;
|
|
||||||
|
|
||||||
cmdline_fix(&argc, &argv);
|
|
||||||
|
|
||||||
log_set_global_logger_default();
|
|
||||||
net_init();
|
|
||||||
|
|
||||||
mem_copy(m_CountData.m_Header, SERVERBROWSE_COUNT, sizeof(SERVERBROWSE_COUNT));
|
|
||||||
mem_copy(m_CountDataLegacy.m_Header, SERVERBROWSE_COUNT_LEGACY, sizeof(SERVERBROWSE_COUNT_LEGACY));
|
|
||||||
|
|
||||||
IKernel *pKernel = IKernel::Create();
|
|
||||||
IStorage *pStorage = CreateStorage(IStorage::STORAGETYPE_BASIC, argc, argv);
|
|
||||||
IConfigManager *pConfigManager = CreateConfigManager();
|
|
||||||
m_pConsole = CreateConsole(CFGFLAG_MASTER);
|
|
||||||
|
|
||||||
bool RegisterFail = !pKernel->RegisterInterface(pStorage);
|
|
||||||
RegisterFail |= !pKernel->RegisterInterface(m_pConsole);
|
|
||||||
RegisterFail |= !pKernel->RegisterInterface(pConfigManager);
|
|
||||||
|
|
||||||
if(RegisterFail)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
pConfigManager->Init();
|
|
||||||
m_pConsole->Init();
|
|
||||||
m_NetBan.Init(m_pConsole, pStorage);
|
|
||||||
if(argc > 1)
|
|
||||||
m_pConsole->ParseArguments(argc - 1, &argv[1]);
|
|
||||||
|
|
||||||
if(g_Config.m_Bindaddr[0] && net_host_lookup(g_Config.m_Bindaddr, &BindAddr, NETTYPE_ALL) == 0)
|
|
||||||
{
|
|
||||||
// got bindaddr
|
|
||||||
BindAddr.type = NETTYPE_ALL;
|
|
||||||
BindAddr.port = MASTERSERVER_PORT;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
mem_zero(&BindAddr, sizeof(BindAddr));
|
|
||||||
BindAddr.type = NETTYPE_ALL;
|
|
||||||
BindAddr.port = MASTERSERVER_PORT;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!m_NetOp.Open(BindAddr))
|
|
||||||
{
|
|
||||||
dbg_msg("mastersrv", "couldn't start network (op)");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
BindAddr.port = MASTERSERVER_PORT + 1;
|
|
||||||
if(!m_NetChecker.Open(BindAddr))
|
|
||||||
{
|
|
||||||
dbg_msg("mastersrv", "couldn't start network (checker)");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// process pending commands
|
|
||||||
m_pConsole->StoreCommands(false);
|
|
||||||
|
|
||||||
dbg_msg("mastersrv", "started");
|
|
||||||
|
|
||||||
while(true)
|
|
||||||
{
|
|
||||||
m_NetOp.Update();
|
|
||||||
m_NetChecker.Update();
|
|
||||||
|
|
||||||
// process m_aPackets
|
|
||||||
CNetChunk Packet;
|
|
||||||
while(m_NetOp.Recv(&Packet))
|
|
||||||
{
|
|
||||||
// check if the server is banned
|
|
||||||
if(m_NetBan.IsBanned(&Packet.m_Address, 0, 0))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if(Packet.m_DataSize == sizeof(SERVERBROWSE_HEARTBEAT) + 2 &&
|
|
||||||
mem_comp(Packet.m_pData, SERVERBROWSE_HEARTBEAT, sizeof(SERVERBROWSE_HEARTBEAT)) == 0)
|
|
||||||
{
|
|
||||||
NETADDR Alt;
|
|
||||||
unsigned char *d = (unsigned char *)Packet.m_pData;
|
|
||||||
Alt = Packet.m_Address;
|
|
||||||
Alt.port =
|
|
||||||
(d[sizeof(SERVERBROWSE_HEARTBEAT)] << 8) |
|
|
||||||
d[sizeof(SERVERBROWSE_HEARTBEAT) + 1];
|
|
||||||
|
|
||||||
// add it
|
|
||||||
AddCheckserver(&Packet.m_Address, &Alt, SERVERTYPE_NORMAL);
|
|
||||||
}
|
|
||||||
else if(Packet.m_DataSize == sizeof(SERVERBROWSE_HEARTBEAT_LEGACY) + 2 &&
|
|
||||||
mem_comp(Packet.m_pData, SERVERBROWSE_HEARTBEAT_LEGACY, sizeof(SERVERBROWSE_HEARTBEAT_LEGACY)) == 0)
|
|
||||||
{
|
|
||||||
NETADDR Alt;
|
|
||||||
unsigned char *d = (unsigned char *)Packet.m_pData;
|
|
||||||
Alt = Packet.m_Address;
|
|
||||||
Alt.port =
|
|
||||||
(d[sizeof(SERVERBROWSE_HEARTBEAT)] << 8) |
|
|
||||||
d[sizeof(SERVERBROWSE_HEARTBEAT) + 1];
|
|
||||||
|
|
||||||
// add it
|
|
||||||
AddCheckserver(&Packet.m_Address, &Alt, SERVERTYPE_LEGACY);
|
|
||||||
}
|
|
||||||
|
|
||||||
else if(Packet.m_DataSize == sizeof(SERVERBROWSE_GETCOUNT) &&
|
|
||||||
mem_comp(Packet.m_pData, SERVERBROWSE_GETCOUNT, sizeof(SERVERBROWSE_GETCOUNT)) == 0)
|
|
||||||
{
|
|
||||||
dbg_msg("mastersrv", "count requested, responding with %d", m_NumServers);
|
|
||||||
|
|
||||||
CNetChunk p;
|
|
||||||
p.m_ClientID = -1;
|
|
||||||
p.m_Address = Packet.m_Address;
|
|
||||||
p.m_Flags = NETSENDFLAG_CONNLESS;
|
|
||||||
p.m_DataSize = sizeof(m_CountData);
|
|
||||||
p.m_pData = &m_CountData;
|
|
||||||
m_CountData.m_High = (m_NumServers >> 8) & 0xff;
|
|
||||||
m_CountData.m_Low = m_NumServers & 0xff;
|
|
||||||
m_NetOp.Send(&p);
|
|
||||||
}
|
|
||||||
else if(Packet.m_DataSize == sizeof(SERVERBROWSE_GETCOUNT_LEGACY) &&
|
|
||||||
mem_comp(Packet.m_pData, SERVERBROWSE_GETCOUNT_LEGACY, sizeof(SERVERBROWSE_GETCOUNT_LEGACY)) == 0)
|
|
||||||
{
|
|
||||||
dbg_msg("mastersrv", "count requested, responding with %d", m_NumServers);
|
|
||||||
|
|
||||||
CNetChunk p;
|
|
||||||
p.m_ClientID = -1;
|
|
||||||
p.m_Address = Packet.m_Address;
|
|
||||||
p.m_Flags = NETSENDFLAG_CONNLESS;
|
|
||||||
p.m_DataSize = sizeof(m_CountData);
|
|
||||||
p.m_pData = &m_CountDataLegacy;
|
|
||||||
m_CountDataLegacy.m_High = (m_NumServers >> 8) & 0xff;
|
|
||||||
m_CountDataLegacy.m_Low = m_NumServers & 0xff;
|
|
||||||
m_NetOp.Send(&p);
|
|
||||||
}
|
|
||||||
else if(Packet.m_DataSize == sizeof(SERVERBROWSE_GETLIST) &&
|
|
||||||
mem_comp(Packet.m_pData, SERVERBROWSE_GETLIST, sizeof(SERVERBROWSE_GETLIST)) == 0)
|
|
||||||
{
|
|
||||||
// someone requested the list
|
|
||||||
dbg_msg("mastersrv", "requested, responding with %d m_aServers", m_NumServers);
|
|
||||||
|
|
||||||
CNetChunk p;
|
|
||||||
p.m_ClientID = -1;
|
|
||||||
p.m_Address = Packet.m_Address;
|
|
||||||
p.m_Flags = NETSENDFLAG_CONNLESS;
|
|
||||||
|
|
||||||
for(int i = 0; i < m_NumPackets; i++)
|
|
||||||
{
|
|
||||||
p.m_DataSize = m_aPackets[i].m_Size;
|
|
||||||
p.m_pData = &m_aPackets[i].m_Data;
|
|
||||||
m_NetOp.Send(&p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(Packet.m_DataSize == sizeof(SERVERBROWSE_GETLIST_LEGACY) &&
|
|
||||||
mem_comp(Packet.m_pData, SERVERBROWSE_GETLIST_LEGACY, sizeof(SERVERBROWSE_GETLIST_LEGACY)) == 0)
|
|
||||||
{
|
|
||||||
// someone requested the list
|
|
||||||
dbg_msg("mastersrv", "requested, responding with %d m_aServers", m_NumServers);
|
|
||||||
|
|
||||||
CNetChunk p;
|
|
||||||
p.m_ClientID = -1;
|
|
||||||
p.m_Address = Packet.m_Address;
|
|
||||||
p.m_Flags = NETSENDFLAG_CONNLESS;
|
|
||||||
|
|
||||||
for(int i = 0; i < m_NumPacketsLegacy; i++)
|
|
||||||
{
|
|
||||||
p.m_DataSize = m_aPacketsLegacy[i].m_Size;
|
|
||||||
p.m_pData = &m_aPacketsLegacy[i].m_Data;
|
|
||||||
m_NetOp.Send(&p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// process m_aPackets
|
|
||||||
while(m_NetChecker.Recv(&Packet))
|
|
||||||
{
|
|
||||||
// check if the server is banned
|
|
||||||
if(m_NetBan.IsBanned(&Packet.m_Address, 0, 0))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if(Packet.m_DataSize == sizeof(SERVERBROWSE_FWRESPONSE) &&
|
|
||||||
mem_comp(Packet.m_pData, SERVERBROWSE_FWRESPONSE, sizeof(SERVERBROWSE_FWRESPONSE)) == 0)
|
|
||||||
{
|
|
||||||
Type = SERVERTYPE_INVALID;
|
|
||||||
// remove it from checking
|
|
||||||
for(int i = 0; i < m_NumCheckServers; i++)
|
|
||||||
{
|
|
||||||
if(net_addr_comp(&m_aCheckServers[i].m_Address, &Packet.m_Address) == 0 ||
|
|
||||||
net_addr_comp(&m_aCheckServers[i].m_AltAddress, &Packet.m_Address) == 0)
|
|
||||||
{
|
|
||||||
Type = m_aCheckServers[i].m_Type;
|
|
||||||
m_NumCheckServers--;
|
|
||||||
m_aCheckServers[i] = m_aCheckServers[m_NumCheckServers];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// drops servers that were not in the CheckServers list
|
|
||||||
if(Type == SERVERTYPE_INVALID)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
AddServer(&Packet.m_Address, Type);
|
|
||||||
SendOk(&Packet.m_Address);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(time_get() - LastBanReload > time_freq() * 300)
|
|
||||||
{
|
|
||||||
LastBanReload = time_get();
|
|
||||||
|
|
||||||
ReloadBans();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(time_get() - LastBuild > time_freq() * 5)
|
|
||||||
{
|
|
||||||
LastBuild = time_get();
|
|
||||||
|
|
||||||
PurgeServers();
|
|
||||||
UpdateServers();
|
|
||||||
BuildPackets();
|
|
||||||
}
|
|
||||||
|
|
||||||
// be nice to the CPU
|
|
||||||
std::this_thread::sleep_for(std::chrono::microseconds(1000));
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
#include "mastersrv.h"
|
|
||||||
|
|
||||||
const int MASTERSERVER_PORT = 8300;
|
|
||||||
|
|
||||||
const unsigned char SERVERBROWSE_HEARTBEAT[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'b', 'e', 'a', '2'};
|
|
||||||
|
|
||||||
const unsigned char SERVERBROWSE_GETLIST[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'r', 'e', 'q', '2'};
|
|
||||||
const unsigned char SERVERBROWSE_LIST[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'l', 'i', 's', '2'};
|
|
||||||
|
|
||||||
const unsigned char SERVERBROWSE_GETCOUNT[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'c', 'o', 'u', '2'};
|
|
||||||
const unsigned char SERVERBROWSE_COUNT[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 's', 'i', 'z', '2'};
|
|
||||||
|
|
||||||
const unsigned char SERVERBROWSE_GETINFO[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'g', 'i', 'e', '3'};
|
|
||||||
const unsigned char SERVERBROWSE_INFO[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'i', 'n', 'f', '3'};
|
|
||||||
|
|
||||||
const unsigned char SERVERBROWSE_GETINFO_64_LEGACY[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'f', 's', 't', 'd'};
|
|
||||||
const unsigned char SERVERBROWSE_INFO_64_LEGACY[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'd', 't', 's', 'f'};
|
|
||||||
|
|
||||||
const unsigned char SERVERBROWSE_INFO_EXTENDED[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'i', 'e', 'x', 't'};
|
|
||||||
const unsigned char SERVERBROWSE_INFO_EXTENDED_MORE[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'i', 'e', 'x', '+'};
|
|
||||||
|
|
||||||
const unsigned char SERVERBROWSE_FWCHECK[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'f', 'w', '?', '?'};
|
|
||||||
const unsigned char SERVERBROWSE_FWRESPONSE[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'f', 'w', '!', '!'};
|
|
||||||
const unsigned char SERVERBROWSE_FWOK[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'f', 'w', 'o', 'k'};
|
|
||||||
const unsigned char SERVERBROWSE_FWERROR[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'f', 'w', 'e', 'r'};
|
|
||||||
|
|
||||||
const unsigned char SERVERBROWSE_HEARTBEAT_LEGACY[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'b', 'e', 'a', 't'};
|
|
||||||
|
|
||||||
const unsigned char SERVERBROWSE_GETLIST_LEGACY[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'r', 'e', 'q', 't'};
|
|
||||||
const unsigned char SERVERBROWSE_LIST_LEGACY[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'l', 'i', 's', 't'};
|
|
||||||
|
|
||||||
const unsigned char SERVERBROWSE_GETCOUNT_LEGACY[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 'c', 'o', 'u', 'n'};
|
|
||||||
const unsigned char SERVERBROWSE_COUNT_LEGACY[SERVERBROWSE_SIZE] = {255, 255, 255, 255, 's', 'i', 'z', 'e'};
|
|
|
@ -1,67 +0,0 @@
|
||||||
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
||||||
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
||||||
#ifndef MASTERSRV_MASTERSRV_H
|
|
||||||
#define MASTERSRV_MASTERSRV_H
|
|
||||||
extern const int MASTERSERVER_PORT;
|
|
||||||
|
|
||||||
enum ServerType
|
|
||||||
{
|
|
||||||
SERVERTYPE_INVALID = -1,
|
|
||||||
SERVERTYPE_NORMAL,
|
|
||||||
SERVERTYPE_LEGACY
|
|
||||||
};
|
|
||||||
|
|
||||||
struct CMastersrvAddr
|
|
||||||
{
|
|
||||||
unsigned char m_aIp[16];
|
|
||||||
unsigned char m_aPort[2];
|
|
||||||
};
|
|
||||||
|
|
||||||
#define SERVERBROWSE_SIZE 8
|
|
||||||
extern const unsigned char SERVERBROWSE_HEARTBEAT[SERVERBROWSE_SIZE];
|
|
||||||
|
|
||||||
extern const unsigned char SERVERBROWSE_GETLIST[SERVERBROWSE_SIZE];
|
|
||||||
extern const unsigned char SERVERBROWSE_LIST[SERVERBROWSE_SIZE];
|
|
||||||
|
|
||||||
extern const unsigned char SERVERBROWSE_GETCOUNT[SERVERBROWSE_SIZE];
|
|
||||||
extern const unsigned char SERVERBROWSE_COUNT[SERVERBROWSE_SIZE];
|
|
||||||
|
|
||||||
extern const unsigned char SERVERBROWSE_GETINFO[SERVERBROWSE_SIZE];
|
|
||||||
extern const unsigned char SERVERBROWSE_INFO[SERVERBROWSE_SIZE];
|
|
||||||
|
|
||||||
extern const unsigned char SERVERBROWSE_GETINFO_64_LEGACY[SERVERBROWSE_SIZE];
|
|
||||||
extern const unsigned char SERVERBROWSE_INFO_64_LEGACY[SERVERBROWSE_SIZE];
|
|
||||||
|
|
||||||
extern const unsigned char SERVERBROWSE_INFO_EXTENDED[SERVERBROWSE_SIZE];
|
|
||||||
extern const unsigned char SERVERBROWSE_INFO_EXTENDED_MORE[SERVERBROWSE_SIZE];
|
|
||||||
|
|
||||||
extern const unsigned char SERVERBROWSE_FWCHECK[SERVERBROWSE_SIZE];
|
|
||||||
extern const unsigned char SERVERBROWSE_FWRESPONSE[SERVERBROWSE_SIZE];
|
|
||||||
extern const unsigned char SERVERBROWSE_FWOK[SERVERBROWSE_SIZE];
|
|
||||||
extern const unsigned char SERVERBROWSE_FWERROR[SERVERBROWSE_SIZE];
|
|
||||||
|
|
||||||
// packet headers for the 0.5 branch
|
|
||||||
|
|
||||||
struct CMastersrvAddrLegacy
|
|
||||||
{
|
|
||||||
unsigned char m_aIp[4];
|
|
||||||
unsigned char m_aPort[2];
|
|
||||||
};
|
|
||||||
|
|
||||||
extern const unsigned char SERVERBROWSE_HEARTBEAT_LEGACY[SERVERBROWSE_SIZE];
|
|
||||||
|
|
||||||
extern const unsigned char SERVERBROWSE_GETLIST_LEGACY[SERVERBROWSE_SIZE];
|
|
||||||
extern const unsigned char SERVERBROWSE_LIST_LEGACY[SERVERBROWSE_SIZE];
|
|
||||||
|
|
||||||
extern const unsigned char SERVERBROWSE_GETCOUNT_LEGACY[SERVERBROWSE_SIZE];
|
|
||||||
extern const unsigned char SERVERBROWSE_COUNT_LEGACY[SERVERBROWSE_SIZE];
|
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
SERVERINFO_VANILLA = 0,
|
|
||||||
SERVERINFO_64_LEGACY,
|
|
||||||
SERVERINFO_EXTENDED,
|
|
||||||
SERVERINFO_EXTENDED_MORE,
|
|
||||||
SERVERINFO_INGAME,
|
|
||||||
};
|
|
||||||
#endif
|
|
189
src/mastersrv/src/addr.rs
Normal file
189
src/mastersrv/src/addr.rs
Normal file
|
@ -0,0 +1,189 @@
|
||||||
|
use arrayvec::ArrayString;
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Write;
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||||
|
pub enum Protocol {
|
||||||
|
V5,
|
||||||
|
V6,
|
||||||
|
V7,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||||
|
pub struct Addr {
|
||||||
|
// `ip`, `port` come before `protocol` so that the order groups addresses
|
||||||
|
// with the same IP addresses together.
|
||||||
|
pub ip: IpAddr,
|
||||||
|
pub port: u16,
|
||||||
|
pub protocol: Protocol,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Protocol {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
self.as_str().fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct UnknownProtocol;
|
||||||
|
|
||||||
|
impl FromStr for Protocol {
|
||||||
|
type Err = UnknownProtocol;
|
||||||
|
fn from_str(s: &str) -> Result<Protocol, UnknownProtocol> {
|
||||||
|
use self::Protocol::*;
|
||||||
|
Ok(match s {
|
||||||
|
"tw-0.5+udp" => V5,
|
||||||
|
"tw-0.6+udp" => V6,
|
||||||
|
"tw-0.7+udp" => V7,
|
||||||
|
_ => return Err(UnknownProtocol),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::Serialize for Protocol {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_str(self.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ProtocolVisitor;
|
||||||
|
|
||||||
|
impl<'de> serde::de::Visitor<'de> for ProtocolVisitor {
|
||||||
|
type Value = Protocol;
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
f.write_str("one of \"tw-0.5+udp\", \"tw-0.6+udp\" and \"tw-0.7+udp\"")
|
||||||
|
}
|
||||||
|
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Protocol, E> {
|
||||||
|
let invalid_value = || E::invalid_value(serde::de::Unexpected::Str(v), &self);
|
||||||
|
Ok(Protocol::from_str(v).map_err(|_| invalid_value())?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> serde::Deserialize<'de> for Protocol {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Protocol, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::de::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
deserializer.deserialize_str(ProtocolVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Protocol {
|
||||||
|
fn as_str(self) -> &'static str {
|
||||||
|
use self::Protocol::*;
|
||||||
|
match self {
|
||||||
|
V5 => "tw-0.5+udp",
|
||||||
|
V6 => "tw-0.6+udp",
|
||||||
|
V7 => "tw-0.7+udp",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Addr {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let mut buf: ArrayString<[u8; 128]> = ArrayString::new();
|
||||||
|
write!(
|
||||||
|
&mut buf,
|
||||||
|
"{}://{}",
|
||||||
|
self.protocol,
|
||||||
|
SocketAddr::new(self.ip, self.port)
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
buf.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct InvalidAddr;
|
||||||
|
|
||||||
|
impl FromStr for Addr {
|
||||||
|
type Err = InvalidAddr;
|
||||||
|
fn from_str(s: &str) -> Result<Addr, InvalidAddr> {
|
||||||
|
let url = Url::parse(s).map_err(|_| InvalidAddr)?;
|
||||||
|
let protocol: Protocol = url.scheme().parse().map_err(|_| InvalidAddr)?;
|
||||||
|
let mut ip_port: ArrayString<[u8; 64]> = ArrayString::new();
|
||||||
|
write!(
|
||||||
|
&mut ip_port,
|
||||||
|
"{}:{}",
|
||||||
|
url.host_str().ok_or(InvalidAddr)?,
|
||||||
|
url.port().ok_or(InvalidAddr)?
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let sock_addr: SocketAddr = ip_port.parse().map_err(|_| InvalidAddr)?;
|
||||||
|
Ok(Addr {
|
||||||
|
ip: sock_addr.ip(),
|
||||||
|
port: sock_addr.port(),
|
||||||
|
protocol,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::Serialize for Addr {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
let mut buf: ArrayString<[u8; 128]> = ArrayString::new();
|
||||||
|
write!(&mut buf, "{}", self).unwrap();
|
||||||
|
serializer.serialize_str(&buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AddrVisitor;
|
||||||
|
|
||||||
|
impl<'de> serde::de::Visitor<'de> for AddrVisitor {
|
||||||
|
type Value = Addr;
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
f.write_str("a URL like tw-0.6+udp://127.0.0.1:8303")
|
||||||
|
}
|
||||||
|
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Addr, E> {
|
||||||
|
let invalid_value = || E::invalid_value(serde::de::Unexpected::Str(v), &self);
|
||||||
|
Ok(Addr::from_str(v).map_err(|_| invalid_value())?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> serde::Deserialize<'de> for Addr {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Addr, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::de::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
deserializer.deserialize_str(AddrVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::Addr;
|
||||||
|
use super::Protocol;
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn addr_from_str() {
|
||||||
|
assert_eq!(
|
||||||
|
Addr::from_str("tw-0.6+udp://127.0.0.1:8303").unwrap(),
|
||||||
|
Addr {
|
||||||
|
ip: IpAddr::from_str("127.0.0.1").unwrap(),
|
||||||
|
port: 8303,
|
||||||
|
protocol: Protocol::V6,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Addr::from_str("tw-0.6+udp://[::1]:8303").unwrap(),
|
||||||
|
Addr {
|
||||||
|
ip: IpAddr::from_str("::1").unwrap(),
|
||||||
|
port: 8303,
|
||||||
|
protocol: Protocol::V6,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
49
src/mastersrv/src/locations.rs
Normal file
49
src/mastersrv/src/locations.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
use arrayvec::ArrayString;
|
||||||
|
use ipnet::Ipv4Net;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
pub type Location = ArrayString<[u8; 12]>;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct LocationRecord {
|
||||||
|
network: Ipv4Net,
|
||||||
|
location: Location,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct LocationsError(String);
|
||||||
|
|
||||||
|
pub struct Locations {
|
||||||
|
locations: Vec<LocationRecord>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Locations {
|
||||||
|
pub fn empty() -> Locations {
|
||||||
|
Locations {
|
||||||
|
locations: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn read(filename: &Path) -> Result<Locations, LocationsError> {
|
||||||
|
let mut reader = csv::Reader::from_path(filename)
|
||||||
|
.map_err(|e| LocationsError(format!("error opening {:?}: {}", filename, e)))?;
|
||||||
|
let locations: Result<Vec<_>, _> = reader.deserialize().collect();
|
||||||
|
Ok(Locations {
|
||||||
|
locations: locations
|
||||||
|
.map_err(|e| LocationsError(format!("error deserializing: {}", e)))?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
pub fn lookup(&self, addr: IpAddr) -> Option<Location> {
|
||||||
|
let ipv4_addr = match addr {
|
||||||
|
IpAddr::V4(a) => a,
|
||||||
|
IpAddr::V6(_) => return None, // sad smiley
|
||||||
|
};
|
||||||
|
for LocationRecord { network, location } in &self.locations {
|
||||||
|
if network.contains(&ipv4_addr) {
|
||||||
|
return Some(*location);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
1048
src/mastersrv/src/main.rs
Normal file
1048
src/mastersrv/src/main.rs
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,227 +0,0 @@
|
||||||
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
||||||
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
||||||
#include <base/system.h>
|
|
||||||
#include <cstdlib> //rand
|
|
||||||
#include <engine/shared/config.h>
|
|
||||||
#include <engine/shared/network.h>
|
|
||||||
#include <mastersrv/mastersrv.h>
|
|
||||||
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
CNetServer *pNet;
|
|
||||||
|
|
||||||
int Progression = 50;
|
|
||||||
int GameType = 0;
|
|
||||||
int Flags = 0;
|
|
||||||
|
|
||||||
const char *pVersion = "trunk";
|
|
||||||
const char *pMap = "somemap";
|
|
||||||
const char *pServerName = "unnamed server";
|
|
||||||
|
|
||||||
NETADDR aMasterServers[16] = {{0, {0}, 0}};
|
|
||||||
int NumMasters = 0;
|
|
||||||
|
|
||||||
const char *PlayerNames[16] = {0};
|
|
||||||
int PlayerScores[16] = {0};
|
|
||||||
int NumPlayers = 0;
|
|
||||||
int MaxPlayers = 0;
|
|
||||||
|
|
||||||
char aInfoMsg[1024];
|
|
||||||
int aInfoMsgSize;
|
|
||||||
|
|
||||||
static void SendHeartBeats()
|
|
||||||
{
|
|
||||||
static unsigned char s_aData[sizeof(SERVERBROWSE_HEARTBEAT) + 2];
|
|
||||||
CNetChunk Packet;
|
|
||||||
|
|
||||||
mem_copy(s_aData, SERVERBROWSE_HEARTBEAT, sizeof(SERVERBROWSE_HEARTBEAT));
|
|
||||||
|
|
||||||
Packet.m_ClientID = -1;
|
|
||||||
Packet.m_Flags = NETSENDFLAG_CONNLESS;
|
|
||||||
Packet.m_DataSize = sizeof(SERVERBROWSE_HEARTBEAT) + 2;
|
|
||||||
Packet.m_pData = &s_aData;
|
|
||||||
|
|
||||||
/* supply the set port that the master can use if it has problems */
|
|
||||||
s_aData[sizeof(SERVERBROWSE_HEARTBEAT)] = 0;
|
|
||||||
s_aData[sizeof(SERVERBROWSE_HEARTBEAT) + 1] = 0;
|
|
||||||
|
|
||||||
for(int i = 0; i < NumMasters; i++)
|
|
||||||
{
|
|
||||||
Packet.m_Address = aMasterServers[i];
|
|
||||||
pNet->Send(&Packet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void WriteStr(const char *pStr)
|
|
||||||
{
|
|
||||||
int l = str_length(pStr) + 1;
|
|
||||||
mem_copy(&aInfoMsg[aInfoMsgSize], pStr, l);
|
|
||||||
aInfoMsgSize += l;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void WriteInt(int i)
|
|
||||||
{
|
|
||||||
char aBuf[64];
|
|
||||||
str_format(aBuf, sizeof(aBuf), "%d", i);
|
|
||||||
WriteStr(aBuf);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void BuildInfoMsg()
|
|
||||||
{
|
|
||||||
aInfoMsgSize = sizeof(SERVERBROWSE_INFO);
|
|
||||||
mem_copy(aInfoMsg, SERVERBROWSE_INFO, aInfoMsgSize);
|
|
||||||
WriteInt(-1);
|
|
||||||
|
|
||||||
WriteStr(pVersion);
|
|
||||||
WriteStr(pServerName);
|
|
||||||
WriteStr(pMap);
|
|
||||||
WriteInt(GameType);
|
|
||||||
WriteInt(Flags);
|
|
||||||
WriteInt(Progression);
|
|
||||||
WriteInt(NumPlayers);
|
|
||||||
WriteInt(MaxPlayers);
|
|
||||||
|
|
||||||
for(int i = 0; i < NumPlayers; i++)
|
|
||||||
{
|
|
||||||
WriteStr(PlayerNames[i]);
|
|
||||||
WriteInt(PlayerScores[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void SendServerInfo(NETADDR *pAddr)
|
|
||||||
{
|
|
||||||
CNetChunk p;
|
|
||||||
p.m_ClientID = -1;
|
|
||||||
p.m_Address = *pAddr;
|
|
||||||
p.m_Flags = NETSENDFLAG_CONNLESS;
|
|
||||||
p.m_DataSize = aInfoMsgSize;
|
|
||||||
p.m_pData = aInfoMsg;
|
|
||||||
pNet->Send(&p);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void SendFWCheckResponse(NETADDR *pAddr)
|
|
||||||
{
|
|
||||||
CNetChunk p;
|
|
||||||
p.m_ClientID = -1;
|
|
||||||
p.m_Address = *pAddr;
|
|
||||||
p.m_Flags = NETSENDFLAG_CONNLESS;
|
|
||||||
p.m_DataSize = sizeof(SERVERBROWSE_FWRESPONSE);
|
|
||||||
p.m_pData = SERVERBROWSE_FWRESPONSE;
|
|
||||||
pNet->Send(&p);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int Run()
|
|
||||||
{
|
|
||||||
int64_t NextHeartBeat = 0;
|
|
||||||
NETADDR BindAddr = {NETTYPE_IPV4, {0}, 0};
|
|
||||||
|
|
||||||
if(!pNet->Open(BindAddr, 0, 0, 0))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
while(true)
|
|
||||||
{
|
|
||||||
CNetChunk p;
|
|
||||||
pNet->Update();
|
|
||||||
SECURITY_TOKEN ResponseToken;
|
|
||||||
while(pNet->Recv(&p, &ResponseToken))
|
|
||||||
{
|
|
||||||
if(p.m_ClientID == -1)
|
|
||||||
{
|
|
||||||
if(p.m_DataSize == sizeof(SERVERBROWSE_GETINFO) &&
|
|
||||||
mem_comp(p.m_pData, SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO)) == 0)
|
|
||||||
{
|
|
||||||
SendServerInfo(&p.m_Address);
|
|
||||||
}
|
|
||||||
else if(p.m_DataSize == sizeof(SERVERBROWSE_FWCHECK) &&
|
|
||||||
mem_comp(p.m_pData, SERVERBROWSE_FWCHECK, sizeof(SERVERBROWSE_FWCHECK)) == 0)
|
|
||||||
{
|
|
||||||
SendFWCheckResponse(&p.m_Address);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send heartbeats if needed */
|
|
||||||
if(NextHeartBeat < time_get())
|
|
||||||
{
|
|
||||||
NextHeartBeat = time_get() + time_freq() * (15 + (rand() % 15));
|
|
||||||
SendHeartBeats();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::microseconds(100000));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, const char **argv)
|
|
||||||
{
|
|
||||||
cmdline_fix(&argc, &argv);
|
|
||||||
pNet = new CNetServer;
|
|
||||||
|
|
||||||
while(argc)
|
|
||||||
{
|
|
||||||
// ?
|
|
||||||
/*if(str_comp(*argv, "-m") == 0)
|
|
||||||
{
|
|
||||||
argc--; argv++;
|
|
||||||
net_host_lookup(*argv, &aMasterServers[NumMasters], NETTYPE_IPV4);
|
|
||||||
argc--; argv++;
|
|
||||||
aMasterServers[NumMasters].port = str_toint(*argv);
|
|
||||||
NumMasters++;
|
|
||||||
}
|
|
||||||
else */
|
|
||||||
if(str_comp(*argv, "-p") == 0)
|
|
||||||
{
|
|
||||||
argc--;
|
|
||||||
argv++;
|
|
||||||
PlayerNames[NumPlayers++] = *argv;
|
|
||||||
argc--;
|
|
||||||
argv++;
|
|
||||||
PlayerScores[NumPlayers] = str_toint(*argv);
|
|
||||||
}
|
|
||||||
else if(str_comp(*argv, "-a") == 0)
|
|
||||||
{
|
|
||||||
argc--;
|
|
||||||
argv++;
|
|
||||||
pMap = *argv;
|
|
||||||
}
|
|
||||||
else if(str_comp(*argv, "-x") == 0)
|
|
||||||
{
|
|
||||||
argc--;
|
|
||||||
argv++;
|
|
||||||
MaxPlayers = str_toint(*argv);
|
|
||||||
}
|
|
||||||
else if(str_comp(*argv, "-t") == 0)
|
|
||||||
{
|
|
||||||
argc--;
|
|
||||||
argv++;
|
|
||||||
GameType = str_toint(*argv);
|
|
||||||
}
|
|
||||||
else if(str_comp(*argv, "-g") == 0)
|
|
||||||
{
|
|
||||||
argc--;
|
|
||||||
argv++;
|
|
||||||
Progression = str_toint(*argv);
|
|
||||||
}
|
|
||||||
else if(str_comp(*argv, "-f") == 0)
|
|
||||||
{
|
|
||||||
argc--;
|
|
||||||
argv++;
|
|
||||||
Flags = str_toint(*argv);
|
|
||||||
}
|
|
||||||
else if(str_comp(*argv, "-n") == 0)
|
|
||||||
{
|
|
||||||
argc--;
|
|
||||||
argv++;
|
|
||||||
pServerName = *argv;
|
|
||||||
}
|
|
||||||
|
|
||||||
argc--;
|
|
||||||
argv++;
|
|
||||||
}
|
|
||||||
|
|
||||||
BuildInfoMsg();
|
|
||||||
int RunReturn = Run();
|
|
||||||
|
|
||||||
delete pNet;
|
|
||||||
cmdline_free(argc, argv);
|
|
||||||
return RunReturn;
|
|
||||||
}
|
|
|
@ -1,9 +1,9 @@
|
||||||
#include <base/math.h>
|
#include <base/math.h>
|
||||||
#include <base/system.h>
|
#include <base/system.h>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <engine/shared/masterserver.h>
|
||||||
#include <engine/shared/network.h>
|
#include <engine/shared/network.h>
|
||||||
#include <engine/shared/packer.h>
|
#include <engine/shared/packer.h>
|
||||||
#include <mastersrv/mastersrv.h>
|
|
||||||
|
|
||||||
static CNetClient g_NetOp; // main
|
static CNetClient g_NetOp; // main
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue