From cc8dbc6f6e7ef9cde2fffdb2f4d050b799b444f7 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Fri, 18 Mar 2022 09:10:52 +0000 Subject: [PATCH] Fix docker-entrypoint.sh arguments and don't require specifying an IP address in servers --- resources/docker-entrypoint.sh | 2 +- src/cli/android-znca-api-server-frida.ts | 12 ++++++++---- src/cli/nso/http-server.ts | 18 ++++++++++++++---- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/resources/docker-entrypoint.sh b/resources/docker-entrypoint.sh index 29fbbf9..6ab0109 100755 --- a/resources/docker-entrypoint.sh +++ b/resources/docker-entrypoint.sh @@ -2,4 +2,4 @@ mkdir -p /data/android -exec /app/bin/nxapi.js --data-path /data $@ +exec /app/bin/nxapi.js --data-path /data "$@" diff --git a/src/cli/android-znca-api-server-frida.ts b/src/cli/android-znca-api-server-frida.ts index df2c568..859c4ba 100644 --- a/src/cli/android-znca-api-server-frida.ts +++ b/src/cli/android-znca-api-server-frida.ts @@ -76,7 +76,11 @@ export async function handler(argv: ArgumentsCamelCase) { app.post('/api/znca/f', bodyParser.json(), async (req, res) => { try { - console.log('Received request from %s, port %d', req.socket.remoteAddress, req.socket.remotePort); + console.log('[%s] %s %s HTTP/%s from %s, port %d%s, %s', + new Date(), req.method, req.url, req.httpVersion, + req.socket.remoteAddress, req.socket.remotePort, + req.headers['x-forwarded-for'] ? ', ' + req.headers['x-forwarded-for'] : '', + req.headers['user-agent']); await ready; const data: { @@ -127,10 +131,10 @@ export async function handler(argv: ArgumentsCamelCase) { }); for (const address of argv.listen) { - const match = address.match(/^(?:((?:\d+\.){3}\d+)|\[(.*)\]):(\d+)$/); - if (!match || !net.isIP(match[1] || match[2])) throw new Error('Not a valid address/port'); + const match = address.match(/^((?:((?:\d+\.){3}\d+)|\[(.*)\]):)(\d+)$/); + if (!match || (match[1] && !net.isIP(match[2] || match[3]))) throw new Error('Not a valid address/port'); - const server = app.listen(parseInt(match[3]), match[1] || match[2]); + const server = app.listen(parseInt(match[4]), match[2] || match[3] || '::'); server.on('listening', () => { const address = server.address() as net.AddressInfo; console.log('Listening on %s, port %d', address.address, address.port); diff --git a/src/cli/nso/http-server.ts b/src/cli/nso/http-server.ts index 714ff11..3edc712 100644 --- a/src/cli/nso/http-server.ts +++ b/src/cli/nso/http-server.ts @@ -17,7 +17,7 @@ declare global { } } -const debug = createDebug('cli:http-server'); +const debug = createDebug('cli:nso:http-server'); export const command = 'http-server'; export const desc = 'Starts a HTTP server to access the Nintendo Switch Online app API'; @@ -47,6 +47,16 @@ export async function handler(argv: ArgumentsCamelCase) { const app = express(); + app.use('/api/znc', (req, res, next) => { + console.log('[%s] %s %s HTTP/%s from %s, port %d%s, %s', + new Date(), req.method, req.url, req.httpVersion, + req.socket.remoteAddress, req.socket.remotePort, + req.headers['x-forwarded-for'] ? ', ' + req.headers['x-forwarded-for'] : '', + req.headers['user-agent']); + + next(); + }); + const localAuth: express.RequestHandler = async (req, res, next) => { if (argv.requireToken || !req.query.user) return next(); @@ -311,10 +321,10 @@ export async function handler(argv: ArgumentsCamelCase) { }); for (const address of argv.listen) { - const match = address.match(/^(?:((?:\d+\.){3}\d+)|\[(.*)\]):(\d+)$/); - if (!match || !net.isIP(match[1] || match[2])) throw new Error('Not a valid address/port'); + const match = address.match(/^((?:((?:\d+\.){3}\d+)|\[(.*)\]):)(\d+)$/); + if (!match || (match[1] && !net.isIP(match[2] || match[3]))) throw new Error('Not a valid address/port'); - const server = app.listen(parseInt(match[3]), match[1] || match[2]); + const server = app.listen(parseInt(match[4]), match[2] || match[3] || '::'); server.on('listening', () => { const address = server.address() as net.AddressInfo; console.log('Listening on %s, port %d', address.address, address.port);