Module: Lich::Common::GameTransport
- Defined in:
- documented/common/game_transport.rb
Overview
Chooses and opens the transport Game.open speaks the wire protocol over. Two modes:
- DIRECT (default) -- a raw TCP socket to the game host's native port. The connection method Lich has always used. Automatically falls back to WEBSOCKET if the raw TCP connect itself can't reach the host -- the shape a firewall blocking the game port while leaving 443 open actually takes. Mirrors Authenticator.authenticate's EAccess -> WebLogin fallback from #1570.
- WEBSOCKET (also selectable explicitly) -- the game host's browser
WebSocket-to-TCP shim, reached over TLS on port 443. See
https://github.com/GenieClient/Genie5/issues/356 (phase 2): the shim
relays whatever bytes the client sends, so nothing above the
transport layer needs to change -- only how the bytes get there.
Confirmed live end-to-end against both production DragonRealms and
GemStone IV: first via a standalone script sending the web client's
own "
key\n /FE:WebFE .../XML" handshake, then via a real Lich session -- whose frontend sends a plain "key\n/FE:WRAYTH .../XML" (no " " prefix, a different /FE: value) -- played for an extended, interactive session and shut down cleanly. See docs/connection/websocket-shim-probe-findings.md (pass 3) for the full writeup.
Defined Under Namespace
Classes: UnknownModeError
Constant Summary collapse
- DIRECT =
:direct- WEBSOCKET =
:websocket- MODES =
Valid game transport mode symbols.
[DIRECT, WEBSOCKET].freeze
- DIRECT_CONNECT_TIMEOUT =
Bounds DIRECT's TCP connect so a firewalled/blocked port (packets silently dropped, no RST) fails in seconds instead of the OS's default SYN-retry timeout (commonly 60s+ on Linux) -- same rationale as EAccess::CONNECT_TIMEOUT. Without this, the fallback below would still work, just not "fail fast" in any meaningful sense.
10- DIRECT_CONNECTIVITY_ERRORS =
Berkeley-socket-level errors consistent with "this host/port isn't reachable" -- a blocked port, a captive network, a DNS resolver that only permits certain domains -- as opposed to a fatal problem the WebSocket transport would hit identically (in which case falling back would just delay the real error). Mirrors Authenticator.authenticate's EAccess -> WebLogin fallback: only falls back on transport-level unreachability.
IO::TimeoutError is in this list, not just Errno::ETIMEDOUT: Socket.tcp's fast-fallback connector (Ruby 3.4+) raises IO::TimeoutError -- an IOError, not an Errno class -- for a connect timeout against a hostname (a bare IP literal still raises Errno::ETIMEDOUT). Since GAMEHOST is always a hostname, this is the exception a blocked-port firewall actually produces.
EACCES/EPERM are POSIX connect(2)'s documented errors for "a local firewall rule forbids this connection" -- deliberately included, not just a leftover reachability code: Lich is normally run by the same person the local block (if any) belongs to, most often their own Windows Firewall, VPN client, or a work laptop's MDM policy getting in the way of their own tool, not a third party's restriction this transport should respect blindly. A local block that genuinely is someone else's deliberate policy still fails, just after a slightly slower detour through the WebSocket attempt.
[ Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::EACCES, Errno::EPERM, IO::TimeoutError, SocketError ].freeze
- WINDOWS_CONNECTIVITY_ERRNOS =
The same fast-fallback connector can also raise a bare SystemCallError -- not the matching Errno subclass -- carrying only the raw platform errno, observed on Windows for a refused port (WSA 10061) where Errno::ECONNREFUSED above never matches. These are the WSA codes for the classes in DIRECT_CONNECTIVITY_ERRORS: WSAENETUNREACH, WSAETIMEDOUT, WSAECONNREFUSED, WSAEHOSTUNREACH, WSAEACCES (Windows Firewall blocking the connection).
[10_051, 10_060, 10_061, 10_065, 10_013].freeze
- DEFAULT_SHIM_PORT =
Defaults for the WebSocket shim path/headers. Confirmed live against play.net's own web client (
style/js/all_web_fe_min.js,SimuSocket.tryWebSocket) -- see docs/connection/websocket-shim-probe-findings.md for the full writeup and how these were pulled directly from that bundle rather than guessed. 443- DEFAULT_SHIM_PATH_FORMAT =
"/shim/%<port>d"- DEFAULT_ORIGIN_FORMAT =
"https://%<host>s"- DEFAULT_SUBPROTOCOL =
"websocket_shim-protocol"- DEFAULT_USER_AGENT =
Default User-Agent header sent by the WebSocket transport.
Matches the user agent of the real Genie/play.net web client to ensure compatibility with the shim's requirements.
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " \ "(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
- WEBSOCKET_HOST_OVERRIDES =
The real web client does not open its WebSocket against the literal GAMEHOST --
DynamicData.actualHostinall_web_fe_min.jsremaps it to one of two fixed hostnames by matching a pattern against GAMEHOST, in this order (first match wins, mirroring the source's own if/elsif-equivalent ternary chain):GAMEHOST =~ /gs|chimera/i -> "chimera.play.net" (GemStone family) GAMEHOST =~ /dr|hydra/i -> "hydra.play.net" (DragonRealms family) otherwise -> GAMEHOST unchanged (dead in practice -- every known instance matches one of the above)This isn't a workaround for a TLS quirk -- it's what the shim actually expects to be dialed at. It also happens to explain the TLS hostname mismatch a naive literal-GAMEHOST connect hits: hydra.play.net and chimera.play.net are both covered by the shared edge certificate's *.play.net SAN; the raw GAMEHOST (e.g. dr.simutronics.net, a .net name) is not. See docs/connection/websocket-shim-probe-findings.md.
[ [/gs|chimera/i, "chimera.play.net"], [/dr|hydra/i, "hydra.play.net"] ].freeze
Class Method Summary collapse
-
.configure_socket(socket, host) ⇒ Object
private
Applies the same keepalive/linger/timeout/buffer tuning to
socketregardless of transport. -
.direct_connectivity_error?(error) ⇒ Boolean
Whether
erroris one GameTransport.open_direct should fall back to WEBSOCKET for -- either a class in DIRECT_CONNECTIVITY_ERRORS or a bare SystemCallError carrying one of WINDOWS_CONNECTIVITY_ERRNOS (see its comment). -
.open(host, port, mode: DIRECT, **opts) ⇒ Socket, Lich::Common::WebSocket::Stream
A connected, configured, drop-in-compatible game socket -- #puts, #gets, #wait_readable, #close, #closed?, #sync=.
-
.open_direct(host, port, **websocket_opts) ⇒ Object
private
Automatically falls back to GameTransport.open_websocket if the raw TCP connect can't reach
host:+port+ at all -- see DIRECT_CONNECTIVITY_ERRORS for exactly which failures count, and the module doc for why. - .open_websocket(host, port, shim_port: DEFAULT_SHIM_PORT, ws_host: websocket_host_for(host), path: format(DEFAULT_SHIM_PATH_FORMAT, port: port), origin: format(DEFAULT_ORIGIN_FORMAT, host: ws_host), subprotocol: DEFAULT_SUBPROTOCOL, user_agent: DEFAULT_USER_AGENT, extra_headers: {}, connect_timeout: 10, fallback: false) ⇒ Object private
-
.websocket_host_for(gamehost) ⇒ String
The hostname to actually dial/verify/Host-header for the WebSocket transport -- see WEBSOCKET_HOST_OVERRIDES.
Class Method Details
.configure_socket(socket, host) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Applies the same keepalive/linger/timeout/buffer tuning to socket
regardless of transport. For WEBSOCKET mode this runs on the raw
pre-TLS TCP socket (see the block passed to
WebSocket::Stream.connect in open_websocket) --
these are Berkeley-socket options, meaningless on the WebSocket
framing layered on top, so they have to land on the underlying fd
before it gets wrapped.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'documented/common/game_transport.rb', line 267 def self.configure_socket(socket, host) SocketConfigurator.configure(socket, keepalive: { enable: true, idle: 30, interval: 30 }, linger: { enable: true, timeout: 5 }, timeout: { recv: 30, send: 30 }, buffer_size: { recv: 32768, send: 32768 }, tcp_nodelay: true, tcp_maxrt: 10) Lich.log("Socket configured successfully for #{host}") if ARGV.include?("--debug") rescue StandardError => e Lich.log("Socket configuration error (continuing with defaults): #{e.class}: #{e.}") Lich.log("WARNING: Socket running with default OS settings - may be less reliable under network stress") end |
.direct_connectivity_error?(error) ⇒ Boolean
Returns whether error is one open_direct should fall
back to WEBSOCKET for -- either a class in
DIRECT_CONNECTIVITY_ERRORS or a bare SystemCallError carrying
one of WINDOWS_CONNECTIVITY_ERRNOS (see its comment).
100 101 102 103 104 |
# File 'documented/common/game_transport.rb', line 100 def self.direct_connectivity_error?(error) return true if DIRECT_CONNECTIVITY_ERRORS.any? { |klass| error.is_a?(klass) } error.is_a?(SystemCallError) && WINDOWS_CONNECTIVITY_ERRNOS.include?(error.errno) end |
.open(host, port, mode: DIRECT, **opts) ⇒ Socket, Lich::Common::WebSocket::Stream
Returns a connected, configured, drop-in-compatible game socket -- #puts, #gets, #wait_readable, #close, #closed?, #sync=.
167 168 169 170 171 172 173 174 175 176 |
# File 'documented/common/game_transport.rb', line 167 def self.open(host, port, mode: DIRECT, **opts) case mode when DIRECT open_direct(host, port, **opts) when WEBSOCKET open_websocket(host, port, **opts) else raise UnknownModeError, "unknown game transport mode: #{mode.inspect} (expected one of #{MODES.inspect})" end end |
.open_direct(host, port, **websocket_opts) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Automatically falls back to open_websocket if the raw TCP connect
can't reach host:+port+ at all -- see DIRECT_CONNECTIVITY_ERRORS
for exactly which failures count, and the module doc for why.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'documented/common/game_transport.rb', line 184 def self.open_direct(host, port, **websocket_opts) socket = Socket.tcp(host, port, connect_timeout: DIRECT_CONNECT_TIMEOUT) configured = false begin configure_socket(socket, host) configured = true ensure socket.close unless configured end Lich.log "info: connected via direct TCP transport (#{host}:#{port})" socket rescue SystemCallError, IO::TimeoutError, SocketError => e raise unless direct_connectivity_error?(e) Lich.log "warn: direct TCP transport unreachable (#{host}:#{port}, #{e.class}: #{e.}); " \ "falling back to WebSocket transport" begin open_websocket(host, port, fallback: true, **websocket_opts) rescue Lich::Common::WebSocket::Stream::ConnectionError => ws_error # Left alone, only ws_error would reach the caller -- naming port # 443 and the WebSocket host, not the game host:port that actually # failed. Whoever reads this (a log, a console error, a bug # report) needs the real failure, not just where the fallback # gave up. raise ws_error.class, "direct #{host}:#{port} unreachable (#{e.class}: #{e.}); " \ "WebSocket fallback also failed (#{ws_error.})" end end |
.open_websocket(host, port, shim_port: DEFAULT_SHIM_PORT, ws_host: websocket_host_for(host), path: format(DEFAULT_SHIM_PATH_FORMAT, port: port), origin: format(DEFAULT_ORIGIN_FORMAT, host: ws_host), subprotocol: DEFAULT_SUBPROTOCOL, user_agent: DEFAULT_USER_AGENT, extra_headers: {}, connect_timeout: 10, fallback: false) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'documented/common/game_transport.rb', line 228 def self.open_websocket(host, port, shim_port: DEFAULT_SHIM_PORT, ws_host: websocket_host_for(host), path: format(DEFAULT_SHIM_PATH_FORMAT, port: port), origin: format(DEFAULT_ORIGIN_FORMAT, host: ws_host), subprotocol: DEFAULT_SUBPROTOCOL, user_agent: DEFAULT_USER_AGENT, extra_headers: {}, connect_timeout: 10, fallback: false) stream = Lich::Common::WebSocket::Stream.connect( host: ws_host, port: shim_port, path: path, origin: origin, subprotocol: subprotocol, user_agent: user_agent, extra_headers: extra_headers, connect_timeout: connect_timeout ) { |raw_socket| configure_socket(raw_socket, ws_host) } remap_note = ws_host == host ? "" : " (remapped from GAMEHOST #{host})" fallback_note = fallback ? " (fallback from direct TCP)" : "" Lich.log "info: connected via WebSocket transport (wss://#{ws_host}:#{shim_port}#{path}#{remap_note}#{fallback_note})" stream rescue Lich::Common::WebSocket::Stream::ConnectionError => e Lich.log "warn: WebSocket transport connect failed (wss://#{ws_host}:#{shim_port}#{path}): #{e.}" raise end |
.websocket_host_for(gamehost) ⇒ String
Returns the hostname to actually dial/verify/Host-header for the WebSocket transport -- see WEBSOCKET_HOST_OVERRIDES.
151 152 153 154 |
# File 'documented/common/game_transport.rb', line 151 def self.websocket_host_for(gamehost) _pattern, override = WEBSOCKET_HOST_OVERRIDES.find { |pattern, _| gamehost.match?(pattern) } override || gamehost end |