diff --git a/CLAUDE.md b/CLAUDE.md index 68bbe0a..2b1d64f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -32,6 +32,9 @@ continuity. | `make terrain-capture` | Renders the terrain crack check and a skirtless control in the capture container. The control must crack and the real terrain must not. Needs Docker. | | `tools/godot.sh -- --connect=HOST:PORT --autoplay --client-seconds=N` | Runs the client headless against a server for N seconds, played by its script, and prints one JSON line saying how far it got. `--offline` plays a server in the same process instead. On a real display, with no options, the game starts an offline match; see `src/app/client_config.gd`. | | `make client-check` | Starts a server and a client as two processes, joins them over loopback UDP and checks the client plays (about a minute, real time). | +| `tools/godot.sh -- --list-server [--port=N]` | Runs a list server (default UDP port 24660). Game servers started with `--listen --list=HOST[:PORT] --name=NAME` register with it; see `src/server/list_server.gd`. | +| `tools/godot.sh -- --browse [--list=HOST[:PORT]] [--lan-target=IP] [--lan-port=N]` | Looks for servers on the LAN and in list servers and prints them as one JSON line. Menus (A10) will show the same list. | +| `make discovery-check` | Starts a list server and a game server as processes and checks `--browse` finds the game both on the LAN (loopback) and through the list (about 15 s). | | `make client-captures` | Renders what a network client sees on the barge, gliding and landed (`captures/client_.png`). Needs Docker. Look at them. | | `make capture-test` | Proves the capture harness works and rejects a blank frame. Needs Docker, so it isn't part of `make check`. | diff --git a/Makefile b/Makefile index 8d26f08..59cc823 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Tidewall build entry points. Everything runs headless with the repo-local engine. GODOT := tools/godot.sh -.PHONY: check import parse test test-selfcheck smoke capture-test terrain-capture binaries verify-engine bot-matches match match-captures client-check client-captures +.PHONY: check import parse test test-selfcheck smoke capture-test terrain-capture binaries verify-engine bot-matches match match-captures client-check client-captures discovery-check ## Everything that must pass before a push. check: verify-engine binaries parse test test-selfcheck smoke @@ -51,6 +51,10 @@ match-captures: import client-check: import @tools/client_check.sh +## Finds a real game server on the LAN (over loopback) and through a real list server. +discovery-check: import + @tools/discovery_check.sh + ## Renders what a network client sees on the barge, gliding and landed. Needs Docker. client-captures: import @for stage in barge glide landed; do tools/capture.sh client_$$stage captures/client_$$stage.png; done diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index c236c14..0d1676a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -144,6 +144,39 @@ timeout (5 to 15 seconds without an acknowledgement), and the owner sees the sam raise an engine error; anything a joined player sends that isn't understood is counted and dropped. +## Finding and joining games + +People join a server three ways, none needing a third-party service: + +- **By address.** `--connect=HOST[:PORT]`; the game port defaults to 24650. +- **On the LAN.** A listening server answers discovery QUERY packets on its game port + plus one (`ServerBeacon`), unless started with `--no-lan`. A scan + (`ServerBrowser.scan_lan()`) broadcasts a QUERY to the discovery ports of eight game + ports, 24650 and the next seven even ports, so several servers on one machine are + found. +- **Through a list server.** Anyone can run one: `--list-server [--port=N]` + (`ListServer`, default UDP 24660). A game server started with `--list=HOST[:PORT]` + sends a REGISTER every 15 s. The list server answers an unknown or wrong token with + a CHALLENGE carrying a token derived from a per-start secret and the sender's + address and port, and lists the server only when a REGISTER echoes it. Servers are + listed under the address their packets come from, never a claimed one. Entries + expire after 45 s, with at most 8 per address and 256 in all. Clients ask for the + list four servers per page, one page after another. + +The messages are in `NetDiscovery`. Every QUERY is padded to 512 bytes and no answer +is larger, so neither the beacon nor the list server can amplify a spoofed request. +Both rate-limit each source address (`SourceLimiter`: 20 packets a second, at most +4,096 addresses tracked per second). + +**Lobby, backfill and late arrivals.** People who connect during the lobby or the +countdown take places (`Match.join()`); bots are only added when the match starts, +into whatever places are still empty, so people joining and leaving in the lobby +never displace or strand a bot. Someone who connects after the barge leaves, or +during results, becomes a watcher (`Match.watch()`): they spectate a living player +(the winner during results) through the same snapshots an eliminated player gets, +and take a place ahead of any bots when the next lobby opens. The server still turns +people away when `NetServer.max_players` people are connected. + ## Snapshot format `NetSnapshot` (`src/net/`) owns the layouts. An entity travels as a **record**: a list diff --git a/src/app/client_config.gd b/src/app/client_config.gd index 40f8d28..43a3e60 100644 --- a/src/app/client_config.gd +++ b/src/app/client_config.gd @@ -4,6 +4,9 @@ ## tidewall --connect=HOST[:PORT] join a server ## options: --name=NAME --seed=N --bots=N (offline) --autoplay ## --client-seconds=N quit after N seconds, saying what happened +## tidewall --browse [--list=HOST[:PORT]]... [--lan-target=ADDRESS]... [--lan-port=N] +## print the servers found on the LAN and in list +## servers as one JSON line, then quit ## ## Menus replace most of this later. Under tools/godot.sh, which has no window, a mode ## has to be asked for: --offline or --connect. @@ -23,7 +26,15 @@ var bots: int = DEFAULT_BOTS var autoplay: bool = false ## Quit after this many seconds; 0 runs until the window is closed. var seconds: float = 0.0 -## True when --offline or --connect was given. +## True for --browse: find servers and print them. +var browse: bool = false +## List servers to ask when browsing: {"host", "port"}. +var lists: Array[Dictionary] = [] +## Where a LAN scan sends its queries; the broadcast address unless given. +var lan_targets: Array[String] = [] +## The first game port a LAN scan asks about (see ServerBrowser.scan_lan()). +var lan_port: int = ServerConfig.DEFAULT_PORT +## True when --offline, --connect or --browse was given. var asked: bool = false var errors: Array[String] = [] @@ -38,6 +49,24 @@ static func parse(args: PackedStringArray) -> ClientConfig: config.asked = true "--autoplay": config.autoplay = true + "--browse": + config.asked = true + config.browse = true + "--list": + var target: Dictionary = ServerConfig.parse_host_port(value, NetDiscovery.DEFAULT_LIST_PORT) + if target.is_empty(): + config.errors.append("--list needs HOST or HOST:PORT, got '%s'" % value) + else: + config.lists.append(target) + "--lan-port": + config.lan_port = value.to_int() if value.is_valid_int() else 0 + if config.lan_port < 1 or config.lan_port > 65534: + config.errors.append("--lan-port must be between 1 and 65534, got '%s'" % value) + "--lan-target": + if value.is_valid_ip_address(): + config.lan_targets.append(value) + else: + config.errors.append("--lan-target needs an IP address, got '%s'" % value) "--connect": config.asked = true var at: int = value.rfind(":") @@ -67,6 +96,8 @@ static func parse(args: PackedStringArray) -> ClientConfig: _: if arg.begins_with("--"): config.errors.append("unknown option '%s'" % arg) + if config.browse and not config.host.is_empty(): + config.errors.append("--browse and --connect can't be used together") return config diff --git a/src/app/main.gd b/src/app/main.gd index b60e5aa..b2026b6 100644 --- a/src/app/main.gd +++ b/src/app/main.gd @@ -3,6 +3,8 @@ ## tools/godot.sh -- --server [options] headless dedicated server (see ServerConfig) ## tidewall [options] the game: an offline match, or ## tidewall --connect=HOST[:PORT] a match on a server (see ClientConfig) +## tidewall --browse list the servers it can find +## tools/godot.sh -- --list-server a list server (see ListServerRunner) ## ## Without a window (tools/godot.sh is always headless) the game only starts when ## --offline or --connect asks for it, so a bare run prints a line and stops. @@ -11,6 +13,7 @@ extends Node var _config: ClientConfig var _session: ClientSession var _left: float = 0.0 +var _browser: ServerBrowser = null func _ready() -> void: @@ -20,6 +23,9 @@ func _ready() -> void: var all: PackedStringArray = OS.get_cmdline_args() if all.has("--server"): args = all + if args.has("--list-server") or OS.get_cmdline_args().has("--list-server"): + add_child(ListServerRunner.new(args if args.has("--list-server") else OS.get_cmdline_args())) + return var config: ServerConfig = ServerConfig.parse(args) if config.server: add_child(HeadlessServer.new(config)) @@ -33,6 +39,9 @@ func _ready() -> void: if DisplayServer.get_name() == "headless" and not _config.asked: print("Tidewall") return + if _config.browse: + _start_browsing() + return _session = ClientSession.new() _session.input.autoplay = _config.autoplay if _config.host.is_empty(): @@ -48,7 +57,39 @@ func _ready() -> void: _left = _config.seconds +func _start_browsing() -> void: + _browser = ServerBrowser.new() + if _browser.open() != OK: + printerr("tidewall: couldn't open a UDP socket to look for servers") + get_tree().quit(1) + return + var targets: Array[String] = _config.lan_targets.duplicate() + if targets.is_empty(): + targets.append("255.255.255.255") + _browser.scan_lan(targets, _config.lan_port) + for target: Dictionary in _config.lists: + var host: String = String(target["host"]) + var address: String = host if host.is_valid_ip_address() else IP.resolve_hostname(host) + if address.is_empty(): + printerr("tidewall: couldn't find list server '%s'" % host) + continue + _browser.ask_list(address, int(target["port"])) + _left = _config.seconds if _config.seconds > 0.0 else 2.0 + + func _process(delta: float) -> void: + if _browser != null: + _browser.poll() + _left -= delta + if _left <= 0.0: + var found: Array[Dictionary] = _browser.sorted() + for server: Dictionary in found: + server["phase"] = Match.PHASE_NAMES[int(server["phase"])] if int(server["phase"]) < Match.PHASE_NAMES.size() else "unknown" + print(JSON.stringify({"event": "servers", "servers": found})) + _browser.close() + _browser = null + get_tree().quit(0) + return if _session == null or _config.seconds <= 0.0: return _left -= delta diff --git a/src/net/net_discovery.gd b/src/net/net_discovery.gd new file mode 100644 index 0000000..97cc2dc --- /dev/null +++ b/src/net/net_discovery.gd @@ -0,0 +1,231 @@ +## Finding games: the small UDP messages for LAN discovery and the list server, kept +## apart from the game protocol (NetProtocol), which runs over ENet on the game port. +## +## Everything is little-endian and starts with u8 type and the 4 bytes "TDWD": +## +## QUERY client -> game server or list server. u16 protocol version, u8 page, +## zero padding up to QUERY_BYTES. +## INFO game server -> client. u16 protocol version, u16 game port, +## u8 people, u8 max players, u8 phase, u8 name length, name. +## REGISTER game server -> list server. u32 token, then the same fields as INFO. +## CHALLENGE list server -> game server. u32 token. +## LIST list server -> client. u8 page, u8 pages, u8 count, then per server: +## u8 address length, address (ASCII), and the same fields as INFO. +## +## No answer is ever bigger than the QUERY that asked for it, so neither kind of +## server can be used to flood a third party with more bytes than it was sent. A +## REGISTER is answered with a CHALLENGE that is smaller still, and a server is only +## listed once it echoes the token, which proves it receives at the address it +## claims to send from. Decoders return an empty Dictionary for anything malformed +## and never raise an error. +class_name NetDiscovery +extends RefCounted + +const MAGIC: String = "TDWD" +const MSG_QUERY: int = 1 +const MSG_INFO: int = 2 +const MSG_REGISTER: int = 3 +const MSG_CHALLENGE: int = 4 +const MSG_LIST: int = 5 + +## Every QUERY is padded to this size; every answer fits in it. +const QUERY_BYTES: int = 512 +## A game server answers LAN queries on its game port plus this. +const DISCOVERY_PORT_OFFSET: int = 1 +## A LAN scan asks the discovery ports of this many game ports, from the default up in +## steps of two, so a few servers on one machine are all found. +const LAN_SCAN_SERVERS: int = 8 +const DEFAULT_LIST_PORT: int = 24660 +const SERVER_NAME_MAX_CHARS: int = 32 +const SERVER_NAME_MAX_BYTES: int = 48 +const DEFAULT_SERVER_NAME: String = "Tidewall server" +## Longest address text: a full IPv6 address. +const ADDRESS_MAX_BYTES: int = 45 +const ENTRIES_PER_PAGE: int = 4 +const MAX_PAGES: int = 64 + +const _HEADER: int = 5 +## u16 version, u16 port, u8 people, u8 max, u8 phase, u8 name length. +const _INFO_FIXED: int = 8 + + +## A server's description: {"version", "port", "players", "max_players", "phase", +## "name"}. Everything is clamped to what fits on the wire, and people to places. +static func info(port: int, players: int, max_players: int, phase: int, server_name: String, version: int = NetProtocol.VERSION) -> Dictionary: + return { + "version": clampi(version, 0, 0xFFFF), "port": clampi(port, 0, 0xFFFF), + "players": clampi(players, 0, clampi(max_players, 0, 255)), "max_players": clampi(max_players, 0, 255), + "phase": clampi(phase, 0, 255), "name": clean_server_name(server_name), + } + + +## A server name fit to show, cleaned like a player name but longer. +static func clean_server_name(raw: String) -> String: + return NetProtocol.clean_name(raw, SERVER_NAME_MAX_CHARS, SERVER_NAME_MAX_BYTES, DEFAULT_SERVER_NAME) + + +static func encode_query(page: int = 0, version: int = NetProtocol.VERSION) -> PackedByteArray: + var out: StreamPeerBuffer = _writer(MSG_QUERY) + out.put_u16(version) + out.put_u8(clampi(page, 0, 255)) + var bytes: PackedByteArray = out.data_array + bytes.resize(QUERY_BYTES) + return bytes + + +## {"version", "page"} or empty. Short queries are refused: the padding is what keeps +## answers from being bigger than questions. +static func decode_query(bytes: PackedByteArray) -> Dictionary: + if bytes.size() != QUERY_BYTES or not _is(bytes, MSG_QUERY): + return {} + return {"version": bytes.decode_u16(5), "page": bytes[7]} + + +static func encode_info(server: Dictionary) -> PackedByteArray: + var out: StreamPeerBuffer = _writer(MSG_INFO) + _put_info(out, server) + return out.data_array + + +static func decode_info(bytes: PackedByteArray) -> Dictionary: + if not _is(bytes, MSG_INFO): + return {} + var result: Dictionary = _read_info(bytes, _HEADER) + if result.is_empty() or int(result["end"]) != bytes.size(): + return {} + result.erase("end") + return result + + +static func encode_register(token: int, server: Dictionary) -> PackedByteArray: + var out: StreamPeerBuffer = _writer(MSG_REGISTER) + out.put_u32(token & 0xFFFFFFFF) + _put_info(out, server) + return out.data_array + + +## {"token", and the INFO fields} or empty. +static func decode_register(bytes: PackedByteArray) -> Dictionary: + if bytes.size() < _HEADER + 4 or not _is(bytes, MSG_REGISTER): + return {} + var result: Dictionary = _read_info(bytes, _HEADER + 4) + if result.is_empty() or int(result["end"]) != bytes.size(): + return {} + result.erase("end") + result["token"] = bytes.decode_u32(_HEADER) + return result + + +static func encode_challenge(token: int) -> PackedByteArray: + var out: StreamPeerBuffer = _writer(MSG_CHALLENGE) + out.put_u32(token & 0xFFFFFFFF) + return out.data_array + + +static func decode_challenge(bytes: PackedByteArray) -> Dictionary: + if bytes.size() != _HEADER + 4 or not _is(bytes, MSG_CHALLENGE): + return {} + return {"token": bytes.decode_u32(_HEADER)} + + +## One page of the list. Each entry is an INFO Dictionary with "address" added. +static func encode_list(page: int, pages: int, entries: Array[Dictionary]) -> PackedByteArray: + var out: StreamPeerBuffer = _writer(MSG_LIST) + var count: int = mini(entries.size(), ENTRIES_PER_PAGE) + out.put_u8(clampi(page, 0, 255)) + out.put_u8(clampi(pages, 0, 255)) + out.put_u8(count) + for index: int in count: + var entry: Dictionary = entries[index] + var address: PackedByteArray = String(entry.get("address", "")).to_ascii_buffer() + if address.size() > ADDRESS_MAX_BYTES: + address = PackedByteArray() + out.put_u8(address.size()) + out.put_data(address) + _put_info(out, entry) + return out.data_array + + +## {"page", "pages", "servers": Array[Dictionary]} or empty. +static func decode_list(bytes: PackedByteArray) -> Dictionary: + if bytes.size() < _HEADER + 3 or bytes.size() > QUERY_BYTES or not _is(bytes, MSG_LIST): + return {} + var count: int = bytes[_HEADER + 2] + if count > ENTRIES_PER_PAGE: + return {} + var servers: Array[Dictionary] = [] + var at: int = _HEADER + 3 + for _index: int in count: + if at >= bytes.size(): + return {} + var size: int = bytes[at] + if size == 0 or size > ADDRESS_MAX_BYTES or at + 1 + size > bytes.size(): + return {} + var address: String = _address(bytes.slice(at + 1, at + 1 + size)) + if address.is_empty(): + return {} + var entry: Dictionary = _read_info(bytes, at + 1 + size) + if entry.is_empty(): + return {} + at = int(entry["end"]) + entry.erase("end") + entry["address"] = address + servers.append(entry) + if at != bytes.size(): + return {} + return {"page": bytes[_HEADER], "pages": bytes[_HEADER + 1], "servers": servers} + + +static func _put_info(out: StreamPeerBuffer, server: Dictionary) -> void: + var name_bytes: PackedByteArray = clean_server_name(String(server.get("name", ""))).to_utf8_buffer() + out.put_u16(clampi(int(server.get("version", 0)), 0, 0xFFFF)) + out.put_u16(clampi(int(server.get("port", 0)), 0, 0xFFFF)) + out.put_u8(clampi(int(server.get("players", 0)), 0, 255)) + out.put_u8(clampi(int(server.get("max_players", 0)), 0, 255)) + out.put_u8(clampi(int(server.get("phase", 0)), 0, 255)) + out.put_u8(name_bytes.size()) + out.put_data(name_bytes) + + +## The INFO fields starting at `at`, with "end" the index after them, or empty. +static func _read_info(bytes: PackedByteArray, at: int) -> Dictionary: + if at + _INFO_FIXED > bytes.size(): + return {} + var name_size: int = bytes[at + 7] + if name_size > SERVER_NAME_MAX_BYTES or at + _INFO_FIXED + name_size > bytes.size(): + return {} + var port: int = bytes.decode_u16(at + 2) + var players: int = bytes[at + 4] + var max_players: int = bytes[at + 5] + if port == 0 or max_players == 0 or players > max_players: + return {} + var raw: String = NetProtocol._utf8(bytes.slice(at + _INFO_FIXED, at + _INFO_FIXED + name_size)) + if name_size > 0 and raw.is_empty(): + return {} + return { + "version": bytes.decode_u16(at), "port": port, "players": players, "max_players": max_players, + "phase": bytes[at + 6], "name": clean_server_name(raw), "end": at + _INFO_FIXED + name_size, + } + + +## Address text made only of the characters an IP address uses, or empty. +static func _address(bytes: PackedByteArray) -> String: + for byte: int in bytes: + var digit: bool = byte >= 0x30 and byte <= 0x39 + var hex: bool = (byte >= 0x61 and byte <= 0x66) or (byte >= 0x41 and byte <= 0x46) + if not (digit or hex or byte == 0x2E or byte == 0x3A): + return "" + var text: String = bytes.get_string_from_ascii() + return text if text.is_valid_ip_address() else "" + + +static func _is(bytes: PackedByteArray, kind: int) -> bool: + return bytes.size() >= _HEADER and bytes[0] == kind and bytes.slice(1, 5) == MAGIC.to_ascii_buffer() + + +static func _writer(kind: int) -> StreamPeerBuffer: + var out: StreamPeerBuffer = StreamPeerBuffer.new() + out.big_endian = false + out.put_u8(kind) + out.put_data(MAGIC.to_ascii_buffer()) + return out diff --git a/src/net/net_discovery.gd.uid b/src/net/net_discovery.gd.uid new file mode 100644 index 0000000..4b81f4f --- /dev/null +++ b/src/net/net_discovery.gd.uid @@ -0,0 +1 @@ +uid://cvyxcnh6fypyq diff --git a/src/net/net_protocol.gd b/src/net/net_protocol.gd index 6bb28a5..a7b9eab 100644 --- a/src/net/net_protocol.gd +++ b/src/net/net_protocol.gd @@ -48,8 +48,8 @@ const MAX_HANDSHAKE_BYTES: int = 96 ## A name fit to show: no control or formatting characters, single spaces, at most -## NAME_MAX_CHARS characters, and never empty. -static func clean_name(raw: String) -> String: +## NAME_MAX_CHARS characters, and never empty. Server names use longer limits. +static func clean_name(raw: String, max_chars: int = NAME_MAX_CHARS, max_bytes: int = NAME_MAX_BYTES, fallback: String = DEFAULT_NAME) -> String: var kept: String = "" var spaced: bool = true for index: int in raw.length(): @@ -67,12 +67,12 @@ static func clean_name(raw: String) -> String: elif not hidden: kept += String.chr(code) spaced = false - if kept.length() >= NAME_MAX_CHARS: + if kept.length() >= max_chars: break kept = kept.strip_edges() - while kept.to_utf8_buffer().size() > NAME_MAX_BYTES: + while kept.to_utf8_buffer().size() > max_bytes: kept = kept.substr(0, kept.length() - 1) - return kept if not kept.is_empty() else DEFAULT_NAME + return kept if not kept.is_empty() else fallback static func encode_hello(player_name: String, version: int = VERSION) -> PackedByteArray: diff --git a/src/net/server_browser.gd b/src/net/server_browser.gd new file mode 100644 index 0000000..de29e45 --- /dev/null +++ b/src/net/server_browser.gd @@ -0,0 +1,114 @@ +## The client's side of finding games: asks the LAN and any list servers which +## Tidewall servers there are, and gathers the answers. Menus (A10) show `servers`; +## until then `tidewall --browse` prints them. +## +## Each found server is an INFO Dictionary (see NetDiscovery) with "address", "port" +## being the game port to connect to, "source" ("lan" or "list") and "compatible" +## (same protocol version as this game). +class_name ServerBrowser +extends RefCounted + +## Found servers by "address:port". +var servers: Dictionary = {} +## Answers that couldn't be read, so far. +var bad_packets: int = 0 + +var _socket: PacketPeerUDP = null +## List servers asked: "ip:port" -> pages still to ask for. +var _lists: Dictionary = {} + + +func open() -> Error: + close() + var socket: PacketPeerUDP = PacketPeerUDP.new() + var error: Error = socket.bind(0, "*") + if error != OK: + return error + socket.set_broadcast_enabled(true) + _socket = socket + return OK + + +func close() -> void: + if _socket != null: + _socket.close() + _socket = null + + +## Asks every server on the LAN. `targets` are the addresses to send to: the broadcast +## address by default, or particular hosts. `first_port` is the first game port asked +## about; the scan covers NetDiscovery.LAN_SCAN_SERVERS of them, two apart. +func scan_lan(targets: Array[String] = ["255.255.255.255"], first_port: int = ServerConfig.DEFAULT_PORT) -> void: + if _socket == null and open() != OK: + return + var query: PackedByteArray = NetDiscovery.encode_query() + for target: String in targets: + for index: int in NetDiscovery.LAN_SCAN_SERVERS: + var port: int = first_port + index * 2 + NetDiscovery.DISCOVERY_PORT_OFFSET + if port > 65535: + break + _socket.set_dest_address(target, port) + _socket.put_packet(query) + + +## Asks a list server for its first page; the rest are asked for as answers arrive. +## `host` must be an IP address. +func ask_list(host: String, port: int = NetDiscovery.DEFAULT_LIST_PORT) -> void: + if _socket == null and open() != OK: + return + _lists["%s:%d" % [host, port]] = true + _socket.set_dest_address(host, port) + _socket.put_packet(NetDiscovery.encode_query(0)) + + +## Reads the answers that have arrived. +func poll() -> void: + if _socket == null: + return + for _index: int in 256: + if _socket.get_available_packet_count() <= 0: + break + var bytes: PackedByteArray = _socket.get_packet() + var from_ip: String = _socket.get_packet_ip() + var from_port: int = _socket.get_packet_port() + var info: Dictionary = NetDiscovery.decode_info(bytes) + if not info.is_empty(): + info["address"] = from_ip + _found(info, "lan") + continue + var page: Dictionary = NetDiscovery.decode_list(bytes) + var key: String = "%s:%d" % [from_ip, from_port] + if page.is_empty() or not _lists.has(key): + bad_packets += 1 + continue + for server: Dictionary in page["servers"]: + _found(server, "list") + # Pages are asked for one at a time, each only after the one before it came. + var next: int = int(page["page"]) + 1 + if next < int(page["pages"]) and next < NetDiscovery.MAX_PAGES: + _socket.set_dest_address(from_ip, from_port) + _socket.put_packet(NetDiscovery.encode_query(next)) + + +## The servers found, sorted: compatible first, then most people, then by name. +func sorted() -> Array[Dictionary]: + var result: Array[Dictionary] = [] + for key: String in servers: + result.append(servers[key]) + result.sort_custom(func(a: Dictionary, b: Dictionary) -> bool: + if a["compatible"] != b["compatible"]: + return bool(a["compatible"]) + if a["players"] != b["players"]: + return int(a["players"]) > int(b["players"]) + return String(a["name"]) < String(b["name"])) + return result + + +func _found(server: Dictionary, source: String) -> void: + var key: String = "%s:%d" % [server["address"], server["port"]] + # A LAN answer comes straight from the server, so it wins over a list's copy. + if servers.has(key) and servers[key]["source"] == "lan" and source == "list": + return + server["source"] = source + server["compatible"] = int(server["version"]) == NetProtocol.VERSION + servers[key] = server diff --git a/src/net/server_browser.gd.uid b/src/net/server_browser.gd.uid new file mode 100644 index 0000000..f64d7aa --- /dev/null +++ b/src/net/server_browser.gd.uid @@ -0,0 +1 @@ +uid://bkig85oib5la1 diff --git a/src/net/source_limiter.gd b/src/net/source_limiter.gd new file mode 100644 index 0000000..9e120ab --- /dev/null +++ b/src/net/source_limiter.gd @@ -0,0 +1,44 @@ +## Counts packets per sending address over fixed windows and says when one has sent +## too many. Used by everything that answers unauthenticated UDP (the LAN beacon and +## the list server) so a flood from one address is mostly ignored. Memory is bounded: +## once `max_sources` addresses have been seen in a window, new ones are refused +## until the next window. +class_name SourceLimiter +extends RefCounted + +var per_window: int = 20 +var window_msec: int = 1000 +var max_sources: int = 4096 +## Packets refused so far. +var refused: int = 0 + +var _window_start: int = -1 +var _counts: Dictionary = {} + + +func _init(packets_per_window: int = 20, window: int = 1000, sources: int = 4096) -> void: + per_window = packets_per_window + window_msec = window + max_sources = sources + + +## True if a packet from `address` may be handled now. `now_msec` is for tests. +func allow(address: String, now_msec: int = -1) -> bool: + var now: int = now_msec if now_msec >= 0 else Time.get_ticks_msec() + if _window_start < 0 or now - _window_start >= window_msec: + _window_start = now + _counts.clear() + var count: int = int(_counts.get(address, 0)) + if count == 0 and _counts.size() >= max_sources: + refused += 1 + return false + if count >= per_window: + refused += 1 + return false + _counts[address] = count + 1 + return true + + +## Addresses being counted in the current window. +func tracked() -> int: + return _counts.size() diff --git a/src/net/source_limiter.gd.uid b/src/net/source_limiter.gd.uid new file mode 100644 index 0000000..dd3b47f --- /dev/null +++ b/src/net/source_limiter.gd.uid @@ -0,0 +1 @@ +uid://c14fge33247u4 diff --git a/src/server/headless_server.gd b/src/server/headless_server.gd index fd21b23..c11c12c 100644 --- a/src/server/headless_server.gd +++ b/src/server/headless_server.gd @@ -19,6 +19,8 @@ var config: ServerConfig var loop: ServerLoop ## The network side, when the server was started with --listen. var game: NetGame = null +## Makes the server findable on the LAN and in list servers, when listening. +var beacon: ServerBeacon = null var _errors: ErrorCounter = ErrorCounter.new() var _accumulator: float = 0.0 @@ -54,6 +56,7 @@ func _ready() -> void: _finish(2) return game = NetGame.new(loop, net) + _open_beacon() var started: Dictionary = config.to_dictionary() started["engine"] = Engine.get_version_info()["string"] started["tick_rate_hz"] = Tuning.TICK_RATE_HZ @@ -72,7 +75,13 @@ func _process(delta: float) -> void: var fields: Dictionary = {"handle": event["handle"], "name": event["name"], "players": game.net.player_count()} if event["type"] == "left": fields["reason"] = event["reason"] + if event.has("watching"): + fields["watching"] = event["watching"] _log("player_" + String(event["type"]), fields) + if beacon != null: + beacon.players = game.net.player_count() + beacon.phase = loop.match_flow.phase + beacon.poll() var due: int = UNTHROTTLED_BATCH if not config.unthrottled: _accumulator += delta @@ -95,6 +104,30 @@ func _exit_tree() -> void: _finish(0) +func _open_beacon() -> void: + if not config.lan and config.lists.is_empty(): + return + beacon = ServerBeacon.new() + beacon.server_name = config.server_name + beacon.game_port = game.net.port() + beacon.max_players = game.net.max_players + for target: Dictionary in config.lists: + var host: String = String(target["host"]) + var address: String = host if host.is_valid_ip_address() else IP.resolve_hostname(host) + if address.is_empty(): + _log("list_unresolved", {"host": host, "message": "couldn't find list server '%s'; not registering with it" % host}) + continue + beacon.add_list(address, int(target["port"])) + # Without LAN discovery the socket still has to exist to talk to list servers, so + # it takes any free port instead of the well-known one. + var error: Error = beacon.open(0 if config.lan else ServerBeacon.ANY_PORT) + if error != OK: + _log("beacon_failed", {"message": "couldn't open the discovery port %d: %s; the server can still be joined by address" % [beacon.game_port + NetDiscovery.DISCOVERY_PORT_OFFSET, error_string(error)]}) + beacon = null + return + _log("beacon", {"port": beacon.port(), "lan": config.lan, "lists": config.lists.size()}) + + func _tick() -> void: if config.fault == "script-error" and loop.ticks == FAULT_TICK: _deliberate_script_error() @@ -118,6 +151,8 @@ func _finish(requested_code: int) -> void: var code: int = requested_code if game != null: game.net.close() + if beacon != null: + beacon.close() if loop != null: var stats: Dictionary = loop.timing_stats() stats["state_hash"] = loop.sim.state_hash() diff --git a/src/server/list_server.gd b/src/server/list_server.gd new file mode 100644 index 0000000..38db89b --- /dev/null +++ b/src/server/list_server.gd @@ -0,0 +1,152 @@ +## A self-hostable list of public game servers. Game servers REGISTER every few +## seconds; clients QUERY it a page at a time. Nothing third-party is involved: anyone +## can run one with `tools/godot.sh -- --list-server [--port=N]`. +## +## A server is listed under the address its packets come from, never one it claims, +## and only after it has echoed a CHALLENGE token sent to that address, so nobody can +## list a machine they don't control. Entries expire unless renewed, there is a cap +## per address and overall, and every source is rate-limited. +class_name ListServer +extends RefCounted + +## An entry not renewed within this long disappears. +const ENTRY_TTL_MSEC: int = 45000 +const MAX_ENTRIES: int = 256 +const MAX_PER_ADDRESS: int = 8 + +var limiter: SourceLimiter = SourceLimiter.new(20, 1000, 4096) +## Packets dropped as malformed, unexpected, or over a limit, so far. +var ignored: int = 0 +var challenges_sent: int = 0 +var queries_answered: int = 0 + +var _socket: PacketPeerUDP = null +var _secret: PackedByteArray = PackedByteArray() +## "address:source port" -> {"address", "info": Dictionary, "seen_msec"}. +var _entries: Dictionary = {} + + +func _init() -> void: + var crypto: Crypto = Crypto.new() + _secret = crypto.generate_random_bytes(16) + + +func listen(port: int = NetDiscovery.DEFAULT_LIST_PORT, address: String = "*") -> Error: + close() + var socket: PacketPeerUDP = PacketPeerUDP.new() + var error: Error = socket.bind(port, address) + if error != OK: + return error + _socket = socket + return OK + + +func port() -> int: + return _socket.get_local_port() if _socket != null else 0 + + +func close() -> void: + if _socket != null: + _socket.close() + _socket = null + + +func entry_count() -> int: + return _entries.size() + + +## The listed servers, oldest registration first, each an INFO Dictionary with +## "address". +func servers() -> Array[Dictionary]: + var result: Array[Dictionary] = [] + for key: String in _entries: + var entry: Dictionary = _entries[key] + var server: Dictionary = (entry["info"] as Dictionary).duplicate() + server["address"] = entry["address"] + result.append(server) + return result + + +## Handles what arrived and forgets servers that went quiet. `now_msec` is for tests. +func poll(now_msec: int = -1) -> void: + if _socket == null: + return + var now: int = now_msec if now_msec >= 0 else Time.get_ticks_msec() + for _index: int in 256: + if _socket.get_available_packet_count() <= 0: + break + var bytes: PackedByteArray = _socket.get_packet() + handle(bytes, _socket.get_packet_ip(), _socket.get_packet_port(), now) + expire(now) + + +## Handles one packet from `from_ip`:`from_port`, sending any answer. Public so the +## fuzz test can drive it without a socket's rate. +func handle(bytes: PackedByteArray, from_ip: String, from_port: int, now: int) -> void: + if not limiter.allow(from_ip, now): + ignored += 1 + return + var query: Dictionary = NetDiscovery.decode_query(bytes) + if not query.is_empty(): + _answer(int(query["page"]), from_ip, from_port) + return + var register: Dictionary = NetDiscovery.decode_register(bytes) + if register.is_empty(): + ignored += 1 + return + var token: int = token_for(from_ip, from_port) + if int(register["token"]) != token: + challenges_sent += 1 + _send(NetDiscovery.encode_challenge(token), from_ip, from_port) + return + var key: String = "%s:%d" % [from_ip, from_port] + if not _entries.has(key): + if _entries.size() >= MAX_ENTRIES or _count_at(from_ip) >= MAX_PER_ADDRESS: + ignored += 1 + return + register.erase("token") + _entries[key] = {"address": from_ip, "info": register, "seen_msec": now} + + +func expire(now: int) -> void: + for key: String in _entries.keys(): + if now - int(_entries[key]["seen_msec"]) > ENTRY_TTL_MSEC: + _entries.erase(key) + + +## The token a server at this address must echo. Derived from a secret made when the +## list server starts, so there is nothing to store per challenge. +func token_for(from_ip: String, from_port: int) -> int: + var context: HashingContext = HashingContext.new() + context.start(HashingContext.HASH_SHA256) + context.update(_secret) + context.update(("%s|%d" % [from_ip, from_port]).to_utf8_buffer()) + var digest: PackedByteArray = context.finish() + var token: int = digest.decode_u32(0) + return token if token != 0 else 1 + + +func _answer(page: int, to_ip: String, to_port: int) -> void: + var listed: Array[Dictionary] = servers() + var pages: int = mini(maxi(ceili(listed.size() / float(NetDiscovery.ENTRIES_PER_PAGE)), 1), NetDiscovery.MAX_PAGES) + if page >= pages: + page = pages - 1 + var first: int = page * NetDiscovery.ENTRIES_PER_PAGE + var chosen: Array[Dictionary] = listed.slice(first, first + NetDiscovery.ENTRIES_PER_PAGE) + queries_answered += 1 + _send(NetDiscovery.encode_list(page, pages, chosen), to_ip, to_port) + + +func _count_at(address: String) -> int: + var count: int = 0 + for key: String in _entries: + if _entries[key]["address"] == address: + count += 1 + return count + + +func _send(bytes: PackedByteArray, to_ip: String, to_port: int) -> void: + if _socket == null: + return + _socket.set_dest_address(to_ip, to_port) + _socket.put_packet(bytes) diff --git a/src/server/list_server.gd.uid b/src/server/list_server.gd.uid new file mode 100644 index 0000000..116f0e0 --- /dev/null +++ b/src/server/list_server.gd.uid @@ -0,0 +1 @@ +uid://bsd3tdjjfxawj diff --git a/src/server/list_server_runner.gd b/src/server/list_server_runner.gd new file mode 100644 index 0000000..55ff771 --- /dev/null +++ b/src/server/list_server_runner.gd @@ -0,0 +1,93 @@ +## Runs a ListServer as its own headless process: +## +## tools/godot.sh -- --list-server [--port=N] [--seconds=N] +## +## Logs JSON lines like the game server: a start line, a status line every minute with +## how many servers are listed, and a stop line. `--seconds` stops it after that long, +## for tests; without it, it runs until stopped. Exit codes: 0 clean, 2 bad command +## line or the port couldn't be opened. +class_name ListServerRunner +extends Node + +const STATUS_EVERY_MSEC: int = 60000 + +var list: ListServer = ListServer.new() +var port: int = NetDiscovery.DEFAULT_LIST_PORT +var seconds: float = 0.0 +var errors: Array[String] = [] + +var _started_msec: int = 0 +var _next_status: int = 0 + + +## Parses the arguments after `--list-server`. +func _init(args: PackedStringArray) -> void: + var start: int = args.find("--list-server") + for index: int in range(start + 1, args.size()): + var parts: PackedStringArray = args[index].split("=", true, 1) + var value: String = parts[1] if parts.size() == 2 else "" + match parts[0]: + "--port": + port = value.to_int() if value.is_valid_int() else 0 + if port < 1 or port > 65535: + errors.append("--port must be between 1 and 65535, got '%s'" % value) + "--seconds": + seconds = value.to_float() if value.is_valid_float() else 0.0 + if seconds <= 0.0: + errors.append("--seconds must be a number above 0, got '%s'" % value) + _: + errors.append("unknown argument '%s'" % args[index]) + + +func _ready() -> void: + _started_msec = Time.get_ticks_msec() + if not errors.is_empty(): + for problem: String in errors: + _log("config_error", {"message": problem}) + get_tree().quit(2) + return + var error: Error = list.listen(port) + if error != OK: + _log("listen_failed", {"port": port, "message": "couldn't listen on UDP port %d: %s" % [port, error_string(error)]}) + get_tree().quit(2) + return + Engine.max_fps = 60 + _next_status = _started_msec + STATUS_EVERY_MSEC + _log("start", {"port": list.port(), "engine": Engine.get_version_info()["string"]}) + + +func _process(_delta: float) -> void: + if list.port() == 0: + return + list.poll() + var now: int = Time.get_ticks_msec() + if now >= _next_status: + _next_status = now + STATUS_EVERY_MSEC + _log("status", _counts()) + if seconds > 0.0 and now - _started_msec >= int(seconds * 1000.0): + _stop() + + +func _exit_tree() -> void: + if list.port() != 0: + _stop() + + +func _stop() -> void: + _log("stop", _counts()) + list.close() + if is_inside_tree(): + get_tree().quit(0) + + +func _counts() -> Dictionary: + return { + "servers": list.entry_count(), "queries": list.queries_answered, + "challenges": list.challenges_sent, "ignored": list.ignored, "rate_limited": list.limiter.refused, + } + + +func _log(event: String, fields: Dictionary) -> void: + var record: Dictionary = {"t_ms": Time.get_ticks_msec() - _started_msec, "event": event} + record.merge(fields, false) + print(JSON.stringify(record)) diff --git a/src/server/list_server_runner.gd.uid b/src/server/list_server_runner.gd.uid new file mode 100644 index 0000000..d6475bb --- /dev/null +++ b/src/server/list_server_runner.gd.uid @@ -0,0 +1 @@ +uid://dl6t6rs2ridvo diff --git a/src/server/net_game.gd b/src/server/net_game.gd index d5c0f13..d214038 100644 --- a/src/server/net_game.gd +++ b/src/server/net_game.gd @@ -45,11 +45,18 @@ func poll() -> void: for event: Dictionary in net.events.duplicate(): var handle: int = int(event.get("handle", 0)) if event["type"] == "joined": - if loop.match_flow.join(handle): + # In the lobby they take a place; once a match is under way they watch it + # and take a place in the next one. + var watching: bool = false + if not loop.match_flow.join(handle): + watching = loop.match_flow.watch(handle) + if loop.match_flow.participants.has(handle) or watching: adopt(handle) _send_match(handle) net.send(handle, NetProtocol.CHANNEL_CONTROL, world_notice(), true) - events.append(event) + var joined: Dictionary = event.duplicate() + joined["watching"] = watching + events.append(joined) else: net.kick(handle, NetProtocol.REASON_FULL) elif event["type"] == "left" and sender.has_client(handle): @@ -163,15 +170,16 @@ func match_notice() -> PackedByteArray: ## The entity a participant is watching: their own player, the player they spectate -## once eliminated, or 0 when they aren't in a match. +## once eliminated (or, for someone who arrived mid-match, from the start), or 0 when +## they aren't in a match. func viewer_of(handle: int) -> int: var flow: Match = loop.match_flow - if flow.phase == Match.LOBBY or flow.phase == Match.COUNTDOWN or not flow.entity_of.has(handle): + if flow.phase == Match.LOBBY or flow.phase == Match.COUNTDOWN: return 0 var target: int = flow.spectate_target(handle) if target != 0 and flow.entity_of.has(target): return int(flow.entity_of[target]) - return int(flow.entity_of[handle]) + return int(flow.entity_of.get(handle, 0)) ## An OWN message for a participant if what they carry has changed since they were diff --git a/src/server/server_beacon.gd b/src/server/server_beacon.gd new file mode 100644 index 0000000..ac0bb3f --- /dev/null +++ b/src/server/server_beacon.gd @@ -0,0 +1,114 @@ +## Makes a game server findable. It answers LAN QUERY packets on the game port plus +## NetDiscovery.DISCOVERY_PORT_OFFSET, and, given list servers, registers with each +## every HEARTBEAT_MSEC, answering their CHALLENGE so they list it. +## +## The owner keeps `players` and `phase` current and calls poll() every frame. +class_name ServerBeacon +extends RefCounted + +const HEARTBEAT_MSEC: int = 15000 +## After a CHALLENGE, the answer goes out at once rather than at the next heartbeat. +const ANSWER_DELAY_MSEC: int = 0 +## For open(): any free port, which LAN scans won't ask. For list servers only. +const ANY_PORT: int = -1 + +var server_name: String = NetDiscovery.DEFAULT_SERVER_NAME +var game_port: int = ServerConfig.DEFAULT_PORT +var max_players: int = Tuning.SOLO_PLAYERS +var players: int = 0 +var phase: int = Match.LOBBY +## LAN queries answered, and packets ignored (malformed, flooding or unasked-for). +var answered: int = 0 +var ignored: int = 0 +## Registrations sent, and challenges answered, per list server, for tests and logs. +var registrations: int = 0 +var limiter: SourceLimiter = SourceLimiter.new() + +var _socket: PacketPeerUDP = null +## Each list server: {"host", "port", "token", "next_msec"}. +var _lists: Array[Dictionary] = [] + + +## Opens the discovery socket. `port` 0 uses the game port plus the offset, ANY_PORT +## a free one; tests pass their own. Returns OK or the error. +func open(port: int = 0, address: String = "*") -> Error: + close() + var socket: PacketPeerUDP = PacketPeerUDP.new() + var wanted: int = port if port > 0 else game_port + NetDiscovery.DISCOVERY_PORT_OFFSET + if port == ANY_PORT: + wanted = 0 + var error: Error = socket.bind(wanted, address) + if error != OK: + return error + _socket = socket + return OK + + +func is_open() -> bool: + return _socket != null + + +func port() -> int: + return _socket.get_local_port() if _socket != null else 0 + + +## Adds a list server to register with. `host` must be an IP address; resolve names +## first (ServerConfig does). +func add_list(host: String, port: int) -> void: + _lists.append({"host": host, "port": port, "token": 0, "next_msec": 0}) + + +func close() -> void: + if _socket != null: + _socket.close() + _socket = null + + +## What this server says about itself. +func describe() -> Dictionary: + return NetDiscovery.info(game_port, players, max_players, phase, server_name) + + +## Answers what arrived and sends any heartbeat that is due. `now_msec` is for tests. +func poll(now_msec: int = -1) -> void: + if _socket == null: + return + var now: int = now_msec if now_msec >= 0 else Time.get_ticks_msec() + # A bounded number per frame, so a flood can't stall the game. + for _index: int in 64: + if _socket.get_available_packet_count() <= 0: + break + var bytes: PackedByteArray = _socket.get_packet() + var from_ip: String = _socket.get_packet_ip() + var from_port: int = _socket.get_packet_port() + _received(bytes, from_ip, from_port, now) + for list: Dictionary in _lists: + if now >= int(list["next_msec"]): + _register(list, now) + + +func _received(bytes: PackedByteArray, from_ip: String, from_port: int, now: int) -> void: + if not limiter.allow(from_ip, now): + ignored += 1 + return + if not NetDiscovery.decode_query(bytes).is_empty(): + _socket.set_dest_address(from_ip, from_port) + _socket.put_packet(NetDiscovery.encode_info(describe())) + answered += 1 + return + var challenge: Dictionary = NetDiscovery.decode_challenge(bytes) + if not challenge.is_empty(): + # Only from a list server this server chose to register with. + for list: Dictionary in _lists: + if list["host"] == from_ip and int(list["port"]) == from_port: + list["token"] = int(challenge["token"]) + list["next_msec"] = now + ANSWER_DELAY_MSEC + return + ignored += 1 + + +func _register(list: Dictionary, now: int) -> void: + list["next_msec"] = now + HEARTBEAT_MSEC + _socket.set_dest_address(String(list["host"]), int(list["port"])) + _socket.put_packet(NetDiscovery.encode_register(int(list["token"]), describe())) + registrations += 1 diff --git a/src/server/server_beacon.gd.uid b/src/server/server_beacon.gd.uid new file mode 100644 index 0000000..8123833 --- /dev/null +++ b/src/server/server_beacon.gd.uid @@ -0,0 +1 @@ +uid://dyt5a2sj2qlrf diff --git a/src/server/server_config.gd b/src/server/server_config.gd index a6b678c..38e793a 100644 --- a/src/server/server_config.gd +++ b/src/server/server_config.gd @@ -3,11 +3,15 @@ ## tools/godot.sh -- --server [--listen] [--port=N] [--seed=N] [--players=N] [--bots=N] ## [--bot-difficulty=mixed|easy|normal|hard] [--ticks=N] ## [--unthrottled] [--log-every=N] +## [--name=NAME] [--list=HOST[:PORT]]... [--no-lan] ## ## With `--listen` the server takes network players on UDP `--port`, waits in the lobby ## for the first of them, and with any `--bots` above 0 fills each match's empty places ## with bots. Without it the match starts at once with `--players` idle slots. ## +## A listening server answers LAN discovery on its port plus one unless `--no-lan`, +## and registers with each `--list` server (see ListServer) under `--name`. +## ## Unknown or malformed arguments are errors, so a typo can't silently run the wrong ## match. See `parse()`. class_name ServerConfig @@ -39,6 +43,12 @@ var unthrottled: bool = false ## Logs a progress line every this many ticks. 0 turns progress lines off. var log_every: int = 0 var fault: String = "" +## The name shown in server lists. +var server_name: String = NetDiscovery.DEFAULT_SERVER_NAME +## List servers to register with: {"host", "port"}. Hosts may be names. +var lists: Array[Dictionary] = [] +## Answer LAN discovery. On by default for a listening server. +var lan: bool = true ## Human-readable problems found while parsing. Empty when the config is usable. var errors: Array[String] = [] @@ -72,6 +82,9 @@ func to_dictionary() -> Dictionary: "bot_difficulty": "mixed" if bot_difficulty < 0 else String(Tuning.BOT_DIFFICULTIES[bot_difficulty]["name"]), "ticks": ticks, "unthrottled": unthrottled, + "name": server_name, + "lan": lan and listen, + "lists": lists.size(), } @@ -82,6 +95,9 @@ func _parse_one(arg: String) -> void: if arg == "--listen": listen = true return + if arg == "--no-lan": + lan = false + return var parts: PackedStringArray = arg.split("=", true, 1) if parts.size() != 2 or not arg.begins_with("--"): errors.append("unknown argument '%s'" % arg) @@ -118,10 +134,44 @@ func _parse_one(arg: String) -> void: errors.append("--bot-difficulty must be mixed, easy, normal or hard, got '%s'" % value) "--fault": fault = value + "--name": + server_name = NetDiscovery.clean_server_name(value) + "--list": + var target: Dictionary = parse_host_port(value, NetDiscovery.DEFAULT_LIST_PORT) + if target.is_empty(): + errors.append("--list needs HOST or HOST:PORT with a port from 1 to 65535, got '%s'" % value) + else: + lists.append(target) _: errors.append("unknown argument '%s'" % arg) +## {"host", "port"} from "HOST" or "HOST:PORT", or empty if it doesn't parse. An +## IPv6 address goes in brackets when it has a port: "[::1]:24660". +static func parse_host_port(text: String, default_port: int) -> Dictionary: + var host: String = text + var port: int = default_port + if text.begins_with("["): + var close: int = text.find("]") + if close < 0: + return {} + host = text.substr(1, close - 1) + var rest: String = text.substr(close + 1) + if not rest.is_empty(): + if not rest.begins_with(":") or not rest.substr(1).is_valid_int(): + return {} + port = rest.substr(1).to_int() + elif text.count(":") == 1: + var at: int = text.find(":") + host = text.substr(0, at) + if not text.substr(at + 1).is_valid_int(): + return {} + port = text.substr(at + 1).to_int() + if host.is_empty() or port < 1 or port > 65535: + return {} + return {"host": host, "port": port} + + func _check_int(key: String, value: String) -> bool: if value.is_valid_int(): return true @@ -145,5 +195,7 @@ func _validate() -> void: errors.append("--ticks can't be negative") if log_every < 0: errors.append("--log-every can't be negative") + if not listen and (not lists.is_empty() or not lan): + errors.append("--list and --no-lan are for a server started with --listen") if fault not in FAULTS: errors.append("unknown --fault '%s'" % fault) diff --git a/src/sim/match.gd b/src/sim/match.gd index 3d1b5d8..405da0f 100644 --- a/src/sim/match.gd +++ b/src/sim/match.gd @@ -36,6 +36,9 @@ var sim: Simulation ## Participant handles in join order, and which of them are bots. var participants: Array[int] = [] var bot_handles: Dictionary = {} +## People who arrived after the match started: they spectate, and become +## participants when the next lobby opens. See watch(). +var watchers: Array[int] = [] ## Handles playing in the current match, and their entity IDs both ways. var entity_of: Dictionary = {} var handle_of: Dictionary = {} @@ -103,7 +106,10 @@ func in_match() -> bool: ## Adds a participant. Returns false if the handle is already here or the lobby is ## full. During a match they wait for the next one. func join(handle: int, bot: bool = false) -> bool: - if participants.has(handle) or participants.size() >= rules.max_players: + # Once the barge has left, newcomers watch (see watch()) until the next lobby. + if in_match() or phase == RESULTS: + return false + if participants.has(handle) or watchers.has(handle) or participants.size() >= rules.max_players: return false participants.append(handle) if bot: @@ -115,6 +121,11 @@ func join(handle: int, bot: bool = false) -> bool: ## Removes a participant. In a match, they're placed as if eliminated and their player ## leaves the simulation. Returns false if the handle isn't here. func leave(handle: int) -> bool: + if watchers.has(handle): + watchers.erase(handle) + spectating.erase(handle) + events.append({"type": "left", "handle": handle}) + return true if not participants.has(handle): return false participants.erase(handle) @@ -134,6 +145,23 @@ func leave(handle: int) -> bool: return true +## Lets someone who arrives while a match is running (or showing results) watch it. +## They spectate a living player, or a winner during results, and join the next +## lobby as a participant ahead of any bots. Returns false outside a match, for a +## handle already here, and when the server already has a full match's worth of +## people waiting. +func watch(handle: int) -> bool: + if not (in_match() or phase == RESULTS): + return false + if participants.has(handle) or watchers.has(handle) or watchers.size() >= rules.max_players: + return false + watchers.append(handle) + var living: Array[int] = winners.duplicate() if phase == RESULTS and not winners.is_empty() else alive() + spectating[handle] = _retarget(handle, 0, living) + events.append({"type": "watching", "handle": handle, "target": spectating[handle]}) + return true + + ## Passes a participant's command to the simulation. Returns false outside a match, for ## a handle that isn't playing, or if the simulation rejected it. func submit_command(handle: int, command: PlayerCommand) -> bool: @@ -372,6 +400,13 @@ func _reset() -> void: _match_size = 0 sim = Simulation.new(match_seed(matches_started)) _enter(LOBBY) + # People who watched the last match take places first; bots only ever fill + # what is left when the next one starts. + var waiting: Array[int] = watchers.duplicate() + watchers.clear() + for handle: int in waiting: + if not join(handle): + watchers.append(handle) ## The living handle `handle` is watching, or 0 if they aren't spectating or nobody is diff --git a/test/client/test_client_session.gd b/test/client/test_client_session.gd index 450ad43..0ad93f9 100644 --- a/test/client/test_client_session.gd +++ b/test/client/test_client_session.gd @@ -226,3 +226,16 @@ func test_restored_entities_keep_their_ids_and_their_order() -> void: assert_eq(ids, [3, 5, 7, 12] as Array[int]) assert_eq(registry.find(3).archetype, SimEntity.ARCHETYPE_PICKUP) assert_eq(registry.create(SimEntity.ARCHETYPE_PLAYER).id, 13, "new IDs carry on past the highest") + + +func test_browse_options() -> void: + var browse: ClientConfig = ClientConfig.parse(PackedStringArray(["--browse", "--list=192.0.2.4", "--lan-target=127.0.0.1", "--lan-port=30000", "--client-seconds=1"])) + assert_true(browse.is_valid(), str(browse.errors)) + assert_true(browse.browse) + assert_true(browse.asked) + assert_eq(browse.lists, [{"host": "192.0.2.4", "port": NetDiscovery.DEFAULT_LIST_PORT}] as Array[Dictionary]) + assert_eq(browse.lan_targets, ["127.0.0.1"] as Array[String]) + assert_eq(browse.lan_port, 30000) + assert_eq(ClientConfig.parse(PackedStringArray(["--browse"])).lan_port, ServerConfig.DEFAULT_PORT) + assert_false(ClientConfig.parse(PackedStringArray(["--browse", "--lan-target=somewhere"])).is_valid()) + assert_false(ClientConfig.parse(PackedStringArray(["--browse", "--connect=192.0.2.7"])).is_valid()) diff --git a/test/net/test_net_discovery.gd b/test/net/test_net_discovery.gd new file mode 100644 index 0000000..aa7b6ed --- /dev/null +++ b/test/net/test_net_discovery.gd @@ -0,0 +1,140 @@ +extends TestCase + +## Issue N6: the discovery messages. Round trips, the rule that no answer is bigger +## than the query that asked for it, and malformed input refused without errors. + + +func _server(port: int = 24650, name: String = "Harbour Light") -> Dictionary: + return NetDiscovery.info(port, 3, 24, Match.PLAY, name) + + +func test_query_round_trip_and_padding() -> void: + var bytes: PackedByteArray = NetDiscovery.encode_query(7) + assert_eq(bytes.size(), NetDiscovery.QUERY_BYTES) + assert_eq(NetDiscovery.decode_query(bytes), {"version": NetProtocol.VERSION, "page": 7}) + assert_eq(NetDiscovery.decode_query(bytes.slice(0, 64)), {}, "an unpadded query is refused") + + +func test_info_round_trip() -> void: + var server: Dictionary = _server() + var decoded: Dictionary = NetDiscovery.decode_info(NetDiscovery.encode_info(server)) + assert_eq(decoded, server) + + +func test_register_and_challenge_round_trip() -> void: + var server: Dictionary = _server(24652) + var decoded: Dictionary = NetDiscovery.decode_register(NetDiscovery.encode_register(0xDEADBEEF, server)) + assert_eq(decoded["token"], 0xDEADBEEF) + decoded.erase("token") + assert_eq(decoded, server) + assert_eq(NetDiscovery.decode_challenge(NetDiscovery.encode_challenge(42)), {"token": 42}) + + +func test_list_round_trip() -> void: + var entries: Array[Dictionary] = [] + for index: int in NetDiscovery.ENTRIES_PER_PAGE: + var entry: Dictionary = _server(24650 + index * 2, "Server %d" % index) + entry["address"] = "2001:db8::%d" % index if index % 2 == 0 else "192.0.2.%d" % (index + 1) + entries.append(entry) + var decoded: Dictionary = NetDiscovery.decode_list(NetDiscovery.encode_list(1, 3, entries)) + assert_eq(decoded["page"], 1) + assert_eq(decoded["pages"], 3) + assert_eq(decoded["servers"], entries) + + +func test_no_answer_is_bigger_than_a_query() -> void: + # The longest possible names and addresses. + var long_name: String = "W".repeat(200) + var entries: Array[Dictionary] = [] + for index: int in NetDiscovery.ENTRIES_PER_PAGE + 3: + var entry: Dictionary = NetDiscovery.info(65535, 255, 255, 255, long_name) + entry["address"] = "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255" + entries.append(entry) + var list: PackedByteArray = NetDiscovery.encode_list(255, 255, entries) + assert_true(list.size() <= NetDiscovery.QUERY_BYTES, "a full page is %d bytes" % list.size()) + # That longest address isn't one an IP check accepts; a full-length plain one is. + for entry: Dictionary in entries: + entry["address"] = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" + var decoded: Dictionary = NetDiscovery.decode_list(NetDiscovery.encode_list(255, 255, entries)) + assert_eq((decoded.get("servers", []) as Array).size(), NetDiscovery.ENTRIES_PER_PAGE) + var info: PackedByteArray = NetDiscovery.encode_info(entries[0]) + assert_true(info.size() <= NetDiscovery.QUERY_BYTES) + assert_true(NetDiscovery.encode_challenge(1).size() < NetDiscovery.encode_register(0, entries[0]).size(), "a challenge is smaller than what prompted it") + # Multi-byte names are cut on the byte limit too. + var wide: Dictionary = NetDiscovery.info(1, 0, 1, 0, "浪".repeat(40)) + assert_true(String(wide["name"]).to_utf8_buffer().size() <= NetDiscovery.SERVER_NAME_MAX_BYTES) + + +func test_server_names_are_cleaned() -> void: + assert_eq(NetDiscovery.clean_server_name(" Harbour\u202E Light\t "), "Harbour Light") + assert_eq(NetDiscovery.clean_server_name(""), NetDiscovery.DEFAULT_SERVER_NAME) + assert_eq(NetDiscovery.clean_server_name("\u200B\u0007"), NetDiscovery.DEFAULT_SERVER_NAME) + assert_eq(NetDiscovery.clean_server_name("A".repeat(60)).length(), NetDiscovery.SERVER_NAME_MAX_CHARS) + + +func test_list_entries_with_bad_addresses_or_fields_are_refused() -> void: + var entry: Dictionary = _server() + entry["address"] = "192.0.2.1" + var good: PackedByteArray = NetDiscovery.encode_list(0, 1, [entry]) + assert_false(NetDiscovery.decode_list(good).is_empty()) + var bad_address: PackedByteArray = good.duplicate() + bad_address[9] = 0x2F # "/" in the address + assert_eq(NetDiscovery.decode_list(bad_address), {}) + var trailing: PackedByteArray = good.duplicate() + trailing.append(0) + assert_eq(NetDiscovery.decode_list(trailing), {}) + var crowded: Dictionary = NetDiscovery.info(24650, 30, 24, 0, "x") + assert_eq(NetDiscovery.decode_info(NetDiscovery.encode_info(crowded)).get("players", -1), 24, "clamped when made") + var raw: PackedByteArray = NetDiscovery.encode_info(_server()) + raw[9] = 30 # more people than places + assert_eq(NetDiscovery.decode_info(raw), {}) + raw = NetDiscovery.encode_info(_server()) + raw.encode_u16(7, 0) # no game port + assert_eq(NetDiscovery.decode_info(raw), {}) + + +## Random and mutated messages: every decoder and the list server's handler take them +## all without an engine error or growing without bound. +func test_fuzzed_messages_are_refused_quietly() -> void: + var rng: RandomNumberGenerator = RandomNumberGenerator.new() + rng.seed = 612 + var seeds: Array[PackedByteArray] = [ + NetDiscovery.encode_query(), NetDiscovery.encode_info(_server()), + NetDiscovery.encode_register(9, _server()), NetDiscovery.encode_challenge(9), + ] + var entry: Dictionary = _server() + entry["address"] = "198.51.100.4" + seeds.append(NetDiscovery.encode_list(0, 1, [entry, entry])) + var list: ListServer = ListServer.new() + list.limiter = SourceLimiter.new(1 << 30) + var accepted: int = 0 + for round_index: int in 40000: + var bytes: PackedByteArray + if round_index % 4 == 0: + bytes = PackedByteArray() + bytes.resize(rng.randi_range(0, 600)) + for index: int in bytes.size(): + bytes[index] = rng.randi_range(0, 255) + else: + bytes = seeds[rng.randi_range(0, seeds.size() - 1)].duplicate() + for _flip: int in rng.randi_range(1, 4): + if bytes.is_empty(): + break + match rng.randi_range(0, 2): + 0: + bytes[rng.randi_range(0, bytes.size() - 1)] = rng.randi_range(0, 255) + 1: + bytes.resize(rng.randi_range(0, bytes.size())) + 2: + bytes.append(rng.randi_range(0, 255)) + for decoded: Dictionary in [ + NetDiscovery.decode_query(bytes), NetDiscovery.decode_info(bytes), NetDiscovery.decode_register(bytes), + NetDiscovery.decode_challenge(bytes), NetDiscovery.decode_list(bytes), + ]: + if not decoded.is_empty(): + accepted += 1 + list.handle(bytes, "203.0.113.%d" % (round_index % 200), 40000 + round_index % 7, 0) + assert_true(accepted > 0, "some mutations are still valid messages") + assert_true(list.entry_count() <= ListServer.MAX_ENTRIES) + # Nothing registered without the token: a fuzzer can't guess it. + assert_eq(list.entry_count(), 0) diff --git a/test/net/test_net_discovery.gd.uid b/test/net/test_net_discovery.gd.uid new file mode 100644 index 0000000..6bb7536 --- /dev/null +++ b/test/net/test_net_discovery.gd.uid @@ -0,0 +1 @@ +uid://cj6ax5huaigav diff --git a/test/server/test_list_server.gd b/test/server/test_list_server.gd new file mode 100644 index 0000000..30a0bcd --- /dev/null +++ b/test/server/test_list_server.gd @@ -0,0 +1,185 @@ +extends TestCase + +## Issue N6: the list server, LAN discovery and the client's browser, over loopback +## UDP. The game servers here are fake: ServerBeacons, or raw sockets that misbehave. + +const HOST: String = "127.0.0.1" + +var list: ListServer +var browser: ServerBrowser +var beacons: Array[ServerBeacon] = [] +var sockets: Array[PacketPeerUDP] = [] +var clock: int = 1000 + + +func before_each() -> void: + list = ListServer.new() + assert_eq(list.listen(0, HOST), OK) + browser = ServerBrowser.new() + assert_eq(browser.open(), OK) + beacons.clear() + sockets.clear() + clock = 1000 + + +func after_each() -> void: + list.close() + browser.close() + for beacon: ServerBeacon in beacons: + beacon.close() + for socket: PacketPeerUDP in sockets: + socket.close() + + +func _beacon(name: String, game_port: int, players: int = 0, lan_port: int = 0) -> ServerBeacon: + var beacon: ServerBeacon = ServerBeacon.new() + beacon.server_name = name + beacon.game_port = game_port + beacon.players = players + assert_eq(beacon.open(lan_port if lan_port > 0 else ServerBeacon.ANY_PORT, HOST), OK) + beacons.append(beacon) + return beacon + + +## Polls everything until `done`, or real time runs out. The list server and beacons +## run on `clock`, which only moves when a test says so. +func _pump(done: Callable, limit_msec: int = 3000) -> bool: + var started: int = Time.get_ticks_msec() + while Time.get_ticks_msec() - started < limit_msec: + for beacon: ServerBeacon in beacons: + beacon.poll(clock) + list.poll(clock) + browser.poll() + if done.call(): + return true + OS.delay_msec(2) + return bool(done.call()) + + +func test_fake_servers_register_after_a_challenge_and_are_listed() -> void: + var names: Array[String] = [] + # All from one address, so at most MAX_PER_ADDRESS of them. + var count: int = ListServer.MAX_PER_ADDRESS + for index: int in count + 1: + var beacon: ServerBeacon = _beacon("Fake %02d" % index, 30000 + index, index) + beacon.add_list(HOST, list.port()) + if index < count: + names.append("Fake %02d" % index) + assert_true(_pump(func() -> bool: return list.entry_count() == index + 1), "%d listed" % list.entry_count()) + _pump(func() -> bool: return false, 300) + assert_eq(list.entry_count(), count, "one more from the same address is refused") + browser.ask_list(HOST, list.port()) + assert_true(_pump(func() -> bool: return browser.servers.size() == count), "%d found" % browser.servers.size()) + var found: Array[String] = [] + for server: Dictionary in browser.sorted(): + found.append(String(server["name"])) + assert_eq(server["address"], HOST) + assert_eq(server["source"], "list") + assert_true(server["compatible"]) + assert_eq(found[0], "Fake %02d" % (count - 1), "the busiest first") + found.sort() + assert_eq(found, names) + assert_true(list.queries_answered >= 2, "two pages of four") + + +func test_registrations_renew_and_silent_servers_expire() -> void: + var staying: ServerBeacon = _beacon("Staying", 30100) + staying.add_list(HOST, list.port()) + var going: ServerBeacon = _beacon("Going", 30102) + going.add_list(HOST, list.port()) + assert_true(_pump(func() -> bool: return list.entry_count() == 2)) + going.close() + beacons.erase(going) + # Heartbeats keep the one still running listed well past the time limit. + staying.players = 5 + for step: int in 4: + clock += ServerBeacon.HEARTBEAT_MSEC + var expected: int = staying.registrations + 1 + assert_true(_pump(func() -> bool: return staying.registrations >= expected)) + assert_true(_pump(func() -> bool: + var listed: Array[Dictionary] = list.servers() + return listed.size() >= 1 and int(listed[0]["players"]) == 5)) + list.expire(clock) + assert_eq(list.entry_count(), 1) + assert_eq(String(list.servers()[0]["name"]), "Staying", "updates replace the entry") + + +func test_a_wrong_or_missing_token_is_never_listed() -> void: + var socket: PacketPeerUDP = PacketPeerUDP.new() + assert_eq(socket.bind(0, HOST), OK) + sockets.append(socket) + socket.set_dest_address(HOST, list.port()) + # A server claiming a token it never got, and one ignoring its challenge. + socket.put_packet(NetDiscovery.encode_register(12345, NetDiscovery.info(30200, 0, 24, 0, "Liar"))) + socket.put_packet(NetDiscovery.encode_register(0, NetDiscovery.info(30200, 0, 24, 0, "Liar"))) + assert_true(_pump(func() -> bool: return socket.get_available_packet_count() >= 2)) + assert_eq(list.entry_count(), 0) + var challenge: Dictionary = NetDiscovery.decode_challenge(socket.get_packet()) + assert_false(challenge.is_empty()) + # A token is bound to the address and port it was sent to. + var other: PacketPeerUDP = PacketPeerUDP.new() + assert_eq(other.bind(0, HOST), OK) + sockets.append(other) + other.set_dest_address(HOST, list.port()) + other.put_packet(NetDiscovery.encode_register(int(challenge["token"]), NetDiscovery.info(30200, 0, 24, 0, "Thief"))) + assert_true(_pump(func() -> bool: return other.get_available_packet_count() >= 1)) + assert_eq(list.entry_count(), 0, "a token stolen by another socket doesn't work") + # The right socket echoing it is listed. + socket.put_packet(NetDiscovery.encode_register(int(challenge["token"]), NetDiscovery.info(30200, 0, 24, 0, "Honest"))) + assert_true(_pump(func() -> bool: return list.entry_count() == 1)) + assert_eq(String(list.servers()[0]["name"]), "Honest") + + +func test_limits_per_address_overall_and_per_second() -> void: + # Many fake servers from one address: only MAX_PER_ADDRESS are listed. + for index: int in ListServer.MAX_PER_ADDRESS + 4: + var port: int = 41000 + index + var token: int = list.token_for("192.0.2.9", port) + list.handle(NetDiscovery.encode_register(token, NetDiscovery.info(port, 0, 24, 0, "Crowd")), "192.0.2.9", port, clock + index * 100) + assert_eq(list.entry_count(), ListServer.MAX_PER_ADDRESS) + # Overall cap, from many addresses. + for index: int in ListServer.MAX_ENTRIES + 20: + var address: String = "198.51.%d.%d" % [index / 200, index % 200 + 1] + var token: int = list.token_for(address, 5000) + list.handle(NetDiscovery.encode_register(token, NetDiscovery.info(5000, 0, 24, 0, "Many")), address, 5000, clock + 5000 + index * 100) + assert_eq(list.entry_count(), ListServer.MAX_ENTRIES) + # A flood from one address within a second is mostly ignored. (Loopback, so + # the answers that do go out stay on this machine.) + var before: int = list.queries_answered + for _index: int in 500: + list.handle(NetDiscovery.encode_query(), "127.0.0.50", 9, clock + 100000) + assert_eq(list.queries_answered - before, list.limiter.per_window) + # Everything expires once nothing renews it. + list.expire(clock + 1000000) + assert_eq(list.entry_count(), 0) + + +func test_lan_scan_finds_servers_on_neighbouring_ports() -> void: + # Two servers on one machine, on the default port and the next one a scan asks. + var first_lan: int = 46001 + _beacon("Quay", first_lan - 1, 2, first_lan) + _beacon("Breakwater", first_lan + 1, 7, first_lan + 2) + var targets: Array[String] = [HOST] + browser.scan_lan(targets, first_lan - 1) + assert_true(_pump(func() -> bool: return browser.servers.size() == 2), "%d found" % browser.servers.size()) + var found: Array[Dictionary] = browser.sorted() + assert_eq(String(found[0]["name"]), "Breakwater") + assert_eq(found[0]["port"], first_lan + 1, "the game port, not the discovery port") + assert_eq(found[0]["players"], 7) + assert_eq(found[0]["source"], "lan") + assert_eq(String(found[1]["name"]), "Quay") + + +func test_a_beacon_ignores_challenges_it_did_not_ask_for_and_junk() -> void: + var beacon: ServerBeacon = _beacon("Quiet", 30300) + var socket: PacketPeerUDP = PacketPeerUDP.new() + assert_eq(socket.bind(0, HOST), OK) + sockets.append(socket) + socket.set_dest_address(HOST, beacon.port()) + socket.put_packet(NetDiscovery.encode_challenge(77)) + socket.put_packet(PackedByteArray([1, 2, 3])) + socket.put_packet(NetDiscovery.encode_query().slice(0, 40)) + assert_true(_pump(func() -> bool: return beacon.ignored >= 3)) + assert_eq(beacon.answered, 0) + assert_eq(beacon.registrations, 0) + assert_eq(socket.get_available_packet_count(), 0, "nothing was sent back") diff --git a/test/server/test_list_server.gd.uid b/test/server/test_list_server.gd.uid new file mode 100644 index 0000000..e1610cd --- /dev/null +++ b/test/server/test_list_server.gd.uid @@ -0,0 +1 @@ +uid://dhaq0b8hvhdcb diff --git a/test/server/test_net_game.gd b/test/server/test_net_game.gd index 149edce..fa72367 100644 --- a/test/server/test_net_game.gd +++ b/test/server/test_net_game.gd @@ -155,15 +155,41 @@ func test_leaving_takes_the_player_out_of_the_match() -> void: assert_eq(odo.state, NetClient.JOINED) -func test_a_full_match_turns_the_next_person_away() -> void: +func test_someone_arriving_mid_match_watches_a_living_player() -> void: _client("Mara") _client("Odo") assert_true(_pump(func() -> bool: return loop.match_flow.in_match())) - # Bots filled the other two places, so there is no room until this match ends. + # Bots filled the other two places, so the newcomer watches until the next match. + var late: NetClient = _client("Late") + var late_index: int = clients.size() - 1 + assert_true(_pump(func() -> bool: return late.state == NetClient.JOINED and receivers[late_index].own_id != 0), "the newcomer is sent someone to watch") + var flow: Match = loop.match_flow + assert_eq(flow.participants.size(), 4) + assert_true(flow.watchers.has(late.handle)) + assert_false(flow.entity_of.has(late.handle), "a watcher has no player of their own") + var watched: int = receivers[late_index].own_id + assert_true(flow.handle_of.has(watched), "the snapshot follows a player in the match") + assert_false(flow.sim.entities.find(watched).eliminated) + assert_true(receivers[late_index].own.is_empty(), "nobody else's inventory is sent") + assert_eq(joined.size(), 3) + assert_true(bool(joined[2]["watching"])) + assert_eq(net.player_count(), 3) + # Leaving while watching is tidy. + late.leave() + assert_true(_pump(func() -> bool: return left.size() == 1)) + assert_false(flow.watchers.has(late.handle)) + assert_false(flow.spectating.has(late.handle)) + assert_false(game.sender.has_client(late.handle)) + + +func test_more_people_than_the_server_holds_are_turned_away() -> void: + net.max_players = 2 + _client("Mara") + _client("Odo") + assert_true(_pump(func() -> bool: return net.player_count() == 2)) var late: NetClient = _client("Late") assert_true(_pump(func() -> bool: return late.state == NetClient.CLOSED)) assert_eq(late.close_reason, NetProtocol.REASON_FULL) - assert_eq(loop.match_flow.participants.size(), 4) assert_eq(net.player_count(), 2) diff --git a/test/server/test_server_config.gd b/test/server/test_server_config.gd index 4ef752f..4cd7c31 100644 --- a/test/server/test_server_config.gd +++ b/test/server/test_server_config.gd @@ -85,3 +85,26 @@ func test_ranges_are_checked() -> void: assert_false(_parse(["--server", too_many]).is_valid()) var full: Array[String] = ["--server", "--players=4", "--bots=%d" % (Tuning.SOLO_PLAYERS - 4)] assert_true(_parse(full).is_valid()) + + +func test_discovery_options() -> void: + var config: ServerConfig = _parse([ + "--server", "--listen", "--name= Harbour Light ", "--list=lists.example.org", + "--list=192.0.2.4:3000", "--list=[2001:db8::1]:4000", "--no-lan", + ]) + assert_true(config.is_valid(), str(config.errors)) + assert_eq(config.server_name, "Harbour Light") + assert_false(config.lan) + assert_eq(config.lists, [ + {"host": "lists.example.org", "port": NetDiscovery.DEFAULT_LIST_PORT}, + {"host": "192.0.2.4", "port": 3000}, + {"host": "2001:db8::1", "port": 4000}, + ] as Array[Dictionary]) + assert_true(_parse(["--server", "--listen"]).lan, "LAN discovery is on by default") + + +func test_bad_discovery_options() -> void: + for wrong: String in ["--list=", "--list=host:", "--list=host:0", "--list=host:99999", "--list=[::1", "--list=[::1]x"]: + assert_false(_parse(["--server", "--listen", wrong]).is_valid(), wrong) + assert_false(_parse(["--server", "--list=192.0.2.4"]).is_valid(), "a list server needs --listen") + assert_false(_parse(["--server", "--no-lan"]).is_valid()) diff --git a/test/sim/test_match.gd b/test/sim/test_match.gd index dca2bc7..6989412 100644 --- a/test/sim/test_match.gd +++ b/test/sim/test_match.gd @@ -199,17 +199,23 @@ func test_leaving_during_results_and_joining_during_a_match() -> void: m.join(1) m.join(2) _until(m, Match.DROP) - assert_true(m.join(3), "a late joiner waits for the next match") + assert_false(m.join(3), "nobody joins a match under way") + assert_true(m.watch(3), "a late arrival watches and waits for the next match") + assert_false(m.watch(3), "once") + assert_false(m.watch(1), "a participant isn't a watcher") assert_false(m.entity_of.has(3)) + assert_true(m.spectate_target(3) in [1, 2], "they watch someone still standing") assert_false(m.submit_command(3, PlayerCommand.new())) _eliminate(m, 2) m.step() assert_eq(m.phase, Match.RESULTS) assert_eq(m.winners, [1] as Array[int], "the late joiner isn't in this match") + assert_eq(m.spectate_target(3), 1, "during results they watch the winner") m.leave(1) assert_eq(m.winners, [1] as Array[int], "leaving during results keeps the result") _until(m, Match.LOBBY) assert_eq(m.participants, [2, 3] as Array[int]) + assert_eq(m.watchers.size(), 0) _until(m, Match.DROP) assert_true(m.entity_of.has(3), "the late joiner plays the next match") @@ -263,3 +269,64 @@ func test_ten_matches_in_a_row_start_clean() -> void: assert_eq(m.entity_of.size(), 0) assert_eq(seeds.size(), 10, "every match gets its own island") assert_eq(m.match_seed(0), SEED, "the first match uses the server's seed") + + +func test_watch_is_only_for_a_match_under_way() -> void: + var m: Match = Match.new(SEED, _rules()) + assert_false(m.watch(5), "in the lobby, people join") + m.join(1) + _until(m, Match.COUNTDOWN) + assert_false(m.watch(5), "the countdown still takes people") + assert_true(m.join(5)) + + +func test_bots_backfill_around_people_joining_and_leaving_in_the_lobby() -> void: + var rules: MatchRules = _rules(6) + rules.min_players = 2 + rules.lobby_ticks = 50 + var m: Match = Match.new(SEED, rules) + m.join(1) + m.join(2) + m.join(3) + _steps(m, 5) + m.leave(2) + m.join(4) + m.leave(1) + m.join(5) + m.join(6) + assert_eq(m.phase, Match.LOBBY) + assert_eq(m.bot_handles.size(), 0, "no bots are added while people come and go") + _until(m, Match.DROP) + var people: Array[int] = [] + for handle: int in m.participants: + if not m.bot_handles.has(handle): + people.append(handle) + people.sort() + assert_eq(people, [3, 4, 5, 6] as Array[int], "everyone still here has a place") + assert_eq(m.bot_handles.size(), 2, "bots fill exactly the places left") + assert_eq(m.participants.size(), 6) + for handle: int in people: + assert_true(m.entity_of.has(handle)) + + +func test_watchers_take_places_ahead_of_bots_in_the_next_match() -> void: + var m: Match = Match.new(SEED, _rules(4)) + m.join(1) + _until(m, Match.DROP) + assert_eq(m.bot_handles.size(), 3) + for handle: int in [7, 8, 9]: + assert_true(m.watch(handle)) + m.leave(8) + assert_false(m.watchers.has(8)) + for handle: int in m.participants.duplicate(): + if handle != 1: + _eliminate(m, handle) + m.step() + assert_eq(m.phase, Match.RESULTS) + _until(m, Match.LOBBY) + assert_true(_saw(m, "joined")) + assert_eq(m.participants, [1, 7, 9] as Array[int], "watchers take places, bots are gone") + _until(m, Match.DROP) + assert_eq(m.bot_handles.size(), 1, "one bot fills the one empty place") + for handle: int in [1, 7, 9]: + assert_true(m.entity_of.has(handle)) diff --git a/tools/discovery_check.sh b/tools/discovery_check.sh new file mode 100755 index 0000000..368c1b7 --- /dev/null +++ b/tools/discovery_check.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +# Starts a list server and a listening game server as real processes, then runs the +# client's --browse twice: once asking the LAN (loopback stands in for the broadcast +# address, which a sandbox may not route) and once asking the list server. Both must +# find the game server under its name and game port. About 15 seconds. +# +# tools/discovery_check.sh DISCOVERY_CHECK_PORT=N picks the game port (default 24687) +set -euo pipefail + +repo="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +port="${DISCOVERY_CHECK_PORT:-24687}" +list_port=$((port + 10)) +out="$repo/build/discovery-check" +mkdir -p "$out" + +nice -n 15 "$repo/tools/godot.sh" -- --list-server --port="$list_port" --seconds=14 >"$out/list.log" 2>&1 & +list=$! +nice -n 15 "$repo/tools/godot.sh" -- --server --listen --port="$port" --seed=3 --bots=1 --ticks=360 \ + --name="Discovery Check" --list="127.0.0.1:$list_port" >"$out/server.log" 2>&1 & +server=$! +trap 'kill "$list" "$server" 2>/dev/null || true' EXIT +sleep 4 +set +e +nice -n 15 "$repo/tools/godot.sh" -- --browse --lan-target=127.0.0.1 --lan-port="$((port - 2))" --client-seconds=1.5 >"$out/lan.log" 2>&1 +lan_code=$? +nice -n 15 "$repo/tools/godot.sh" -- --browse --lan-target=127.0.0.2 --list="127.0.0.1:$list_port" --client-seconds=1.5 >"$out/list-browse.log" 2>&1 +list_code=$? +wait "$server" +server_code=$? +wait "$list" +list_server_code=$? +set -e +trap - EXIT + +fail() { + echo "discovery-check: $1" >&2 + for log in lan list-browse server list; do echo "--- $log" >&2; tail -n 3 "$out/$log.log" >&2; done + exit 1 +} +[[ "$lan_code" -eq 0 && "$list_code" -eq 0 ]] || fail "a browse exited with $lan_code / $list_code" +[[ "$server_code" -eq 0 ]] || fail "the game server exited with $server_code" +[[ "$list_server_code" -eq 0 ]] || fail "the list server exited with $list_server_code" +lan="$(grep -m1 '"event":"servers"' "$out/lan.log" || true)" +listed="$(grep -m1 '"event":"servers"' "$out/list-browse.log" || true)" +grep -q "\"name\":\"Discovery Check\"" <<<"$lan" || fail "the LAN scan didn't find the server" +grep -q "\"port\":$port" <<<"$lan" || fail "the LAN scan didn't report the game port" +grep -q '"source":"lan"' <<<"$lan" || fail "the LAN answer wasn't marked as LAN" +grep -q "\"name\":\"Discovery Check\"" <<<"$listed" || fail "the list server didn't list the server" +grep -q '"source":"list"' <<<"$listed" || fail "the listed server wasn't marked as from the list" +grep -q '"servers":1' "$out/list.log" || fail "the list server never counted the registration" +grep -q '"errors":0,"event":"stop"' "$out/server.log" || fail "the game server logged errors" +if grep -q 'SCRIPT ERROR' "$out"/*.log; then fail "something logged script errors"; fi +echo "discovery-check: ok (found on the LAN and through the list server)" +echo " lan: $lan" +echo " list: $listed"