diff --git a/.env.example b/.env.example index 56c1eff..940fe3d 100644 --- a/.env.example +++ b/.env.example @@ -1,22 +1,26 @@ # Id generation =========================================================== # If you change this, check the collision probability https://zelark.github.io/nano-id-cc/ -NORAY_OID_LENGTH=21 # For 10 id/hour, 15 trillion years -# NORAY_OID_LENGTH=4 # For 10 id/hour, 2 days -# NORAY_OID_LENGTH=5 # For 10 id/hour, 19 days -# NORAY_OID_LENGTH=6 # For 10 id/hour, 155 days +# For 10 id/hour, 15 trillion years +NORAY_OID_LENGTH=21 +# For 10 id/hour, 2 days +# NORAY_OID_LENGTH=4 +# For 10 id/hour, 19 days +# NORAY_OID_LENGTH=5 +# For 10 id/hour, 155 days +# NORAY_OID_LENGTH=6 NORAY_OID_CHARSET=useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict NORAY_PID_LENGTH=128 NORAY_PID_CHARSET=useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict # Socket ====================================================================== # TCP hostname to listen on -NORAY_SOCKET_HOST=::1 +NORAY_SOCKET_HOST=0.0.0.0 # TCP port to listen on NORAY_SOCKET_PORT=8890 # HTTP ======================================================================== # HTTP hostname to listen on -NORAY_HTTP_HOST=::1 +NORAY_HTTP_HOST=0.0.0.0 # HTTP port to listen on NORAY_HTTP_PORT=8891 diff --git a/README.md b/README.md index 90b9c5e..5496a8d 100644 --- a/README.md +++ b/README.md @@ -69,20 +69,41 @@ listening for incoming connections. Logs are written to `stdout`. ### Usage with Docker -Create `.env` file from `.env.example`. +Create `.env` file based on `.env.example`. Build and run docker: ``` docker build . -t noray -docker run -p 8090:8090 -p 8091:8091 --env-file=.env -t noray +docker run -p 8890:8890 -p 8891:8891 -p 8809:8809/udp -p 49152-51200:49152-51200/udp --env-file=.env -t noray ``` Or run prebuilt docker: ``` -docker run -p 8090:8090 -p 8091:8091 --env-file=.env -t ghcr.io/foxssake/noray:main +docker run -p 8890:8890 -p 8891:8891 -p 8809:8809/udp -p 49152-51200:49152-51200/udp --env-file=.env -t ghcr.io/foxssake/noray:main ``` +The above will expose the following ports: + +* Port 8890 for clients to register and request connections +* Port 8891 to expose metrics over HTTP +* Port 8809 for the remote port registrar +* Ports 49152 to 51200 for relays + * Make sure these are the same ports as configured in `.env`! + +Note that exposing a lot of relay ports can severely impact deploy time. + +In case of relays not working - i.e. clients can register and request +connections, but the handshake process fails -, Docker might be mapping ports +as data arrives from outside of the container. In these cases, try running +noray using the [host network]: + +``` +docker run --network host --env-file=.env -t noray +``` + +[host network]: https://docs.docker.com/engine/network/tutorials/host/ + #### EADDRNOTAVAIL If you get an `EADDRNOTAVAIL` error when trying to listen on an IPv6 address, diff --git a/package.json b/package.json index 173ee11..ba49adb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@foxssake/noray", - "version": "1.4.2", + "version": "1.4.3", "description": "Online multiplayer orchestrator and potential game platform", "main": "src/noray.mjs", "bin": { diff --git a/src/config.mjs b/src/config.mjs index 6c4c9ef..a4e957a 100644 --- a/src/config.mjs +++ b/src/config.mjs @@ -22,12 +22,12 @@ export class NorayConfig { } socket = { - host: env.NORAY_SOCKET_HOST ?? '::1', + host: env.NORAY_SOCKET_HOST ?? '0.0.0.0', port: integer(env.NORAY_SOCKET_PORT) ?? 8890 } http = { - host: env.NORAY_HTTP_HOST ?? '::1', + host: env.NORAY_HTTP_HOST ?? '0.0.0.0', port: integer(env.NORAY_HTTP_PORT) ?? 8891 } diff --git a/src/hosts/host.commands.mjs b/src/hosts/host.commands.mjs index 3e707d7..9596169 100644 --- a/src/hosts/host.commands.mjs +++ b/src/hosts/host.commands.mjs @@ -36,6 +36,10 @@ export function handleRegisterHost (hostRepository) { socket.remoteAddress, socket.remotePort ) + socket.on('error', err => { + log.error(err) + }) + socket.on('close', () => { log.info( { oid: host.oid, pid: host.pid }, diff --git a/src/noray.mjs b/src/noray.mjs index 2d6a1ec..d80ace8 100644 --- a/src/noray.mjs +++ b/src/noray.mjs @@ -58,9 +58,14 @@ export class Noray extends EventEmitter { config.socket.host, config.socket.port ) + socket.on('error', err => { + this.#log.error(err) + }) + socket.on('connection', conn => { this.#protocolServer.attach(conn) conn.on('close', () => this.#protocolServer.detach(conn)) + conn.on('error', err => this.#log.error(err)) }) this.emit('listening', config.socket.port, config.socket.host) diff --git a/src/protocol/protocol.server.mjs b/src/protocol/protocol.server.mjs index d49832b..3183282 100644 --- a/src/protocol/protocol.server.mjs +++ b/src/protocol/protocol.server.mjs @@ -57,6 +57,11 @@ export class ProtocolServer extends events.EventEmitter { }) rl.on('line', line => this.#handleLine(socket, line)) + rl.on('error', err => { + this.detach(socket) + log.error('Socket connection abruptly lost!') + log.error(err) + }) this.#readers.set(socket, rl) activeConnectionGauge.inc()