Phone UI and touch precision overhaul, proxy fixes, Orbital Uplink, four new maps
Phone panels (reviewed + QA'd, matrix of 22 layouts with real taps/bounding boxes): - selected-tower action bar over the board (Upgrade/Sell/Target/close), info panel updated in place so taps are never lost, scrollable palette for 7+ towers, overlays with pinned footers (Help "Got it", Play solo, Join always visible), tappable room chip to invite, reduced-motion/contrast/forced-colors fixes, paused state, partner cursor labels, bigger level pips and owner flags - touch input rewritten around "pointerdown commits nothing": pinch never builds, 10px tap slop, palm-proof pinch, long-press pings with any tool armed, nudge arrows for precise placement, hotkey repeat guard, proportional wheel zoom - new e2e: select/sell, info-panel stability, UI states, touch precision; camera unit tests Proxy/server (reproduced against real nginx 1.26 with NPM v2.16 config): - Origin check works when the proxy drops a non-default port, TRUSTED_PROXIES for proxies on global IPv6/CGNAT addresses, service worker no longer serves nginx's 502 page, DEPLOY.md corrected (NPM default read timeout is 90 s, Advanced-tab limits, path-prefix, cookie size, per-IP cap behaviour) Content: - Orbital Uplink: 4-level endgame tower (450 + 500/800/1200 credits): piercing armour-ignoring beam, blast and lingering slow, ground+air, range 11-14; glyph, beam effect, sound, help text, unit tests - Maps: The Canyon, Archipelago, Fortress, Pinwheel (7 maps total) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tb8pW2KAN4oFkh1aK1BfD8
This commit is contained in:
parent
dad0382e10
commit
a5bd09d505
22 changed files with 2423 additions and 405 deletions
|
|
@ -6,7 +6,7 @@
|
|||
* const { createServer } = require('./server');
|
||||
* const srv = await createServer({ port: 8080 }); // srv.port, srv.addresses, srv.close(), srv.rooms
|
||||
*
|
||||
* Run directly: PORT=8080 HOST=:: TRUST_PROXY=auto ALLOWED_ORIGINS=https://game.example.com node server/index.js
|
||||
* Run directly: PORT=8080 HOST=:: TRUST_PROXY=auto TRUSTED_PROXIES=100.64.0.0/10 ALLOWED_ORIGINS=https://game.example.com node server/index.js
|
||||
*/
|
||||
const http = require('http');
|
||||
const path = require('path');
|
||||
|
|
@ -14,7 +14,7 @@ const { WebSocketServer } = require('ws');
|
|||
const TD = require('../public/js/sim.js');
|
||||
const { RoomManager } = require('./room');
|
||||
const { createStaticHandler, applySecurityHeaders, CSP } = require('./static');
|
||||
const { parseTrustProxy, clientIp, originAllowed, TokenBucket, listenDual, listUrls } = require('./net');
|
||||
const { parseTrustProxy, parseTrustedProxies, isTrustedPeer, clientIp, originAllowed, TokenBucket, listenDual, listUrls } = require('./net');
|
||||
|
||||
const MAX_PAYLOAD = 2048; // bytes per client frame
|
||||
const RATE_PER_SEC = 60, RATE_BURST = 120;
|
||||
|
|
@ -40,11 +40,12 @@ async function createServer(userOpts) {
|
|||
const o = {
|
||||
port: 8080, host: '::', publicDir: path.join(__dirname, '..', 'public'),
|
||||
graceMs: 60000, snapshotHz: 20, maxRooms: 50,
|
||||
trustProxy: 'auto', allowedOrigins: [], maxConnPerIp: 40, pingMs: 25000, log: defaultLog,
|
||||
trustProxy: 'auto', trustedProxies: null, allowedOrigins: [], maxConnPerIp: 40, pingMs: 25000, log: defaultLog,
|
||||
joinTimeoutMs: 10000, // not in the protocol's option list: lets tests shorten the 10 s join window
|
||||
};
|
||||
for (const k of Object.keys(userOpts || {})) if (userOpts[k] !== undefined) o[k] = userOpts[k];
|
||||
o.trustProxy = parseTrustProxy(o.trustProxy);
|
||||
o.trustedProxies = parseTrustedProxies(o.trustedProxies); // null, or { list, has(ip) }: extra proxy addresses for 'auto'
|
||||
o.allowedOrigins = (o.allowedOrigins || []).map(String).filter(Boolean);
|
||||
o.snapshotHz = Math.min(60, Math.max(1, Number(o.snapshotHz) || 20));
|
||||
const log = typeof o.log === 'function' ? o.log : noop;
|
||||
|
|
@ -96,11 +97,13 @@ async function createServer(userOpts) {
|
|||
if (closing) return refuse(socket, 503, 'Service Unavailable');
|
||||
if ((req.url || '').split('?')[0] !== '/ws') return refuse(socket, 404, 'Not Found');
|
||||
if (!originAllowed(req, o)) {
|
||||
log('refused websocket from ' + clientIp(req, o.trustProxy) + ': Origin ' + JSON.stringify(req.headers.origin) +
|
||||
' does not match Host ' + JSON.stringify(req.headers.host) + ' (see ALLOWED_ORIGINS)');
|
||||
log('refused websocket from ' + clientIp(req, o.trustProxy, o.trustedProxies) + ': Origin ' + JSON.stringify(req.headers.origin) +
|
||||
' does not match Host ' + JSON.stringify(req.headers.host) + (isTrustedPeer(req, o.trustProxy, o.trustedProxies)
|
||||
? ' (see ALLOWED_ORIGINS)'
|
||||
: ' (this connection is not from a trusted proxy: see TRUST_PROXY, TRUSTED_PROXIES and ALLOWED_ORIGINS)'));
|
||||
return refuse(socket, 403, 'Forbidden');
|
||||
}
|
||||
const ip = clientIp(req, o.trustProxy);
|
||||
const ip = clientIp(req, o.trustProxy, o.trustedProxies);
|
||||
if (o.maxConnPerIp > 0 && (ipCount.get(ip) || 0) >= o.maxConnPerIp) {
|
||||
log('refused websocket from ' + ip + ': too many connections');
|
||||
return refuse(socket, 429, 'Too Many Requests');
|
||||
|
|
@ -280,6 +283,7 @@ async function main() {
|
|||
port: envInt('PORT', 8080),
|
||||
host: process.env.HOST || '::',
|
||||
trustProxy: process.env.TRUST_PROXY || 'auto',
|
||||
trustedProxies: process.env.TRUSTED_PROXIES || '',
|
||||
allowedOrigins: (process.env.ALLOWED_ORIGINS || '').split(',').map((s) => s.trim()).filter(Boolean),
|
||||
};
|
||||
if (process.env.MAX_CONN_PER_IP) opts.maxConnPerIp = envInt('MAX_CONN_PER_IP', 40);
|
||||
|
|
@ -288,7 +292,9 @@ async function main() {
|
|||
const shownProxy = parseTrustProxy(opts.trustProxy);
|
||||
console.log('Netwall is listening on ' + (srv.host.includes(':') ? '[' + srv.host + ']' : srv.host) + ':' + srv.port +
|
||||
(srv.host === '::' ? ' (dual stack: IPv4 + IPv6)' : srv.host === '0.0.0.0' ? ' (IPv4 only)' : ''));
|
||||
console.log(' trust proxy: ' + shownProxy + ', extra allowed origins: ' + (opts.allowedOrigins.join(', ') || '(none)'));
|
||||
console.log(' trust proxy: ' + shownProxy + (srv.options.trustedProxies && shownProxy === 'auto'
|
||||
? ' + ' + srv.options.trustedProxies.list.join(', ') : '') +
|
||||
', extra allowed origins: ' + (opts.allowedOrigins.join(', ') || '(none)'));
|
||||
console.log('Open:');
|
||||
for (const u of listUrls(srv.host, srv.port)) console.log(' ' + u);
|
||||
|
||||
|
|
|
|||
|
|
@ -54,11 +54,44 @@ function peerIp(req) {
|
|||
return normalizeIp(req.socket && req.socket.remoteAddress) || 'unknown';
|
||||
}
|
||||
|
||||
// Does this request come from a proxy whose X-Forwarded-* headers we believe?
|
||||
function isTrustedPeer(req, trustProxy) {
|
||||
// TRUSTED_PROXIES: extra proxy addresses believed under TRUST_PROXY=auto, on top of the private ranges. The built-in
|
||||
// "private" list misses a proxy that reaches the game over a global IPv6 address, Tailscale / CGNAT (100.64.0.0/10) or
|
||||
// any other routable address. Accepts an array or a comma separated string of IPs and CIDRs, IPv4 or IPv6
|
||||
// ("100.64.0.0/10, 2001:db8::6, 2001:db8:1::/48"). Returns null for an empty list, throws on a malformed entry (a
|
||||
// typo must stop the start-up, not silently leave the proxy untrusted). The result has .has(ip) and .list.
|
||||
function parseTrustedProxies(v) {
|
||||
const items = Array.isArray(v) ? v : String(v === undefined || v === null ? '' : v).split(',');
|
||||
const list = items.map((s) => String(s).trim()).filter(Boolean);
|
||||
if (!list.length) return null;
|
||||
const bl = new net.BlockList();
|
||||
for (const item of list) {
|
||||
const slash = item.indexOf('/');
|
||||
const addr = normalizeIp(slash < 0 ? item : item.slice(0, slash));
|
||||
const type = net.isIPv4(addr) ? 'ipv4' : net.isIPv6(addr) ? 'ipv6' : null;
|
||||
const max = type === 'ipv4' ? 32 : 128;
|
||||
const prefixText = slash < 0 ? null : item.slice(slash + 1);
|
||||
const prefix = prefixText === null ? max : (/^\d{1,3}$/.test(prefixText) ? Number(prefixText) : NaN);
|
||||
if (!type || !(prefix >= 0 && prefix <= max) ||
|
||||
(item.startsWith('::ffff:') && slash >= 0)) { // a v4-mapped prefix would need its own arithmetic: write the IPv4 form
|
||||
throw new Error('invalid TRUSTED_PROXIES entry ' + JSON.stringify(item) + ' (use an IP or CIDR such as 100.64.0.0/10 or 2001:db8::6)');
|
||||
}
|
||||
if (prefixText === null) bl.addAddress(addr, type); else bl.addSubnet(addr, prefix, type);
|
||||
}
|
||||
return {
|
||||
list,
|
||||
has(ip) {
|
||||
const a = normalizeIp(ip);
|
||||
return net.isIPv4(a) ? bl.check(a, 'ipv4') : net.isIPv6(a) ? bl.check(a, 'ipv6') : false;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Does this request come from a proxy whose X-Forwarded-* headers we believe? `trusted` = parseTrustedProxies() result.
|
||||
function isTrustedPeer(req, trustProxy, trusted) {
|
||||
if (trustProxy === true) return true;
|
||||
if (trustProxy === false) return false;
|
||||
return isPrivateIp(peerIp(req));
|
||||
const ip = peerIp(req);
|
||||
return isPrivateIp(ip) || !!(trusted && trusted.has(ip));
|
||||
}
|
||||
|
||||
// One entry of a forwarding header ("203.0.113.9", "[2001:db8::5]:443", "1.2.3.4:5555") -> bare IP or null.
|
||||
|
|
@ -95,17 +128,17 @@ function forwardedClientIp(value) {
|
|||
}
|
||||
|
||||
// The address used for logging and the per-IP socket cap.
|
||||
function clientIp(req, trustProxy) {
|
||||
function clientIp(req, trustProxy, trusted) {
|
||||
const peer = peerIp(req);
|
||||
if (!isTrustedPeer(req, trustProxy)) return peer;
|
||||
if (!isTrustedPeer(req, trustProxy, trusted)) return peer;
|
||||
return forwardedClientIp(req.headers['x-forwarded-for']) ||
|
||||
firstForwardedIp(req.headers['x-real-ip']) || peer;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Origin check
|
||||
// Scheme-less, lower-case "host[:port]" with the default ports 80 / 443 dropped.
|
||||
// Accepts a bare host ("Game.Example.com:443"), a Host header, or a full origin ("https://x.example").
|
||||
function normalizeHost(s) {
|
||||
// Scheme-less, lower-case host name and port of a bare host ("Game.Example.com:443"), a Host header or a full origin
|
||||
// ("https://x.example"): { name, port }, with port '' for none or for the default ports 80 / 443. null if unusable.
|
||||
function hostParts(s) {
|
||||
if (typeof s !== 'string') return null;
|
||||
let v = s.trim();
|
||||
if (!v) return null;
|
||||
|
|
@ -117,27 +150,43 @@ function normalizeHost(s) {
|
|||
try { u = new URL(v); } catch (e) { return null; }
|
||||
if (!u.hostname) return null;
|
||||
const port = u.port; // '' for the scheme's own default
|
||||
const h = u.hostname.toLowerCase(); // IPv6 keeps its brackets
|
||||
return port && port !== '80' && port !== '443' ? h + ':' + port : h;
|
||||
return { name: u.hostname.toLowerCase(), port: port && port !== '80' && port !== '443' ? port : '' }; // IPv6 keeps its brackets
|
||||
}
|
||||
|
||||
// "host[:port]" form of hostParts().
|
||||
function normalizeHost(s) {
|
||||
const p = hostParts(s);
|
||||
return p ? (p.port ? p.name + ':' + p.port : p.name) : null;
|
||||
}
|
||||
|
||||
// Returns true when a WebSocket upgrade with this Origin may proceed.
|
||||
// no Origin header -> true (non-browser clients)
|
||||
// Origin host == Host -> true
|
||||
// Origin host == X-Forwarded-Host (first) -> true, only when the peer is a trusted proxy
|
||||
// Origin host name == Host name, Host without a port -> true, only when the peer is a trusted proxy (see below)
|
||||
// Origin host in allowedOrigins -> true
|
||||
// opts: { trustProxy, trustedProxies (parseTrustedProxies() result), allowedOrigins }.
|
||||
function originAllowed(req, opts) {
|
||||
const origin = req.headers.origin;
|
||||
if (origin === undefined) return true;
|
||||
const o = (typeof origin === 'string' && origin.toLowerCase() !== 'null' && /^[a-z][a-z0-9+.-]*:\/\//i.test(origin))
|
||||
? normalizeHost(origin) : null;
|
||||
? hostParts(origin) : null;
|
||||
if (!o) return false;
|
||||
if (o === normalizeHost(req.headers.host)) return true;
|
||||
if (isTrustedPeer(req, opts.trustProxy)) {
|
||||
const h = hostParts(req.headers.host);
|
||||
if (h && o.name === h.name && o.port === h.port) return true;
|
||||
if (isTrustedPeer(req, opts.trustProxy, opts.trustedProxies)) {
|
||||
const xfh = req.headers['x-forwarded-host'];
|
||||
if (typeof xfh === 'string' && o === normalizeHost(xfh.split(',')[0])) return true;
|
||||
if (typeof xfh === 'string' && normalizeHost(xfh.split(',')[0]) === normalizeHost(origin)) return true;
|
||||
// Published on a port other than 80/443 (https://game.example.com:8443) behind nginx / nginx Proxy Manager: their
|
||||
// "proxy_set_header Host $host" drops the port, so Host is "game.example.com" while the browser's Origin keeps
|
||||
// ":8443", and neither the X-Forwarded-Host nor an ALLOWED_ORIGINS entry written without the port helps. A browser that
|
||||
// talks to this server directly on its port (8080 by default, never 80/443 unless you chose that) puts the port in
|
||||
// Host, so a Host WITHOUT one from a trusted proxy is what a stripped port looks like: accept the same host name on
|
||||
// any port. Another name still fails.
|
||||
if (h && !h.port && o.name === h.name) return true;
|
||||
}
|
||||
for (const a of opts.allowedOrigins || []) if (o === normalizeHost(a)) return true;
|
||||
const on = normalizeHost(origin);
|
||||
for (const a of opts.allowedOrigins || []) if (on === normalizeHost(a)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -220,6 +269,6 @@ function listUrls(bound, port, ifaces) {
|
|||
}
|
||||
|
||||
module.exports = {
|
||||
normalizeIp, isPrivateIp, parseTrustProxy, peerIp, isTrustedPeer, firstForwardedIp, forwardedClientIp, clientIp,
|
||||
normalizeHost, originAllowed, TokenBucket, listenDual, listUrls, fmtUrl,
|
||||
normalizeIp, isPrivateIp, parseTrustProxy, parseTrustedProxies, peerIp, isTrustedPeer, firstForwardedIp, forwardedClientIp, clientIp,
|
||||
hostParts, normalizeHost, originAllowed, TokenBucket, listenDual, listUrls, fmtUrl,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue