diff --git a/src/index.ts b/src/index.ts index a2dbed8..cc1ac96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,15 +41,16 @@ router.onRadioInfo((station, band, freq) => { }); function resolveClientIp(c: Parameters[0]): string { - const rawIp = c.req.header('x-forwarded-for') ?? getConnInfo(c).remote.address ?? 'unknown'; + const forwarded = c.req.header('x-forwarded-for')?.split(',')[0]?.trim(); + const rawIp = forwarded || getConnInfo(c).remote.address || 'unknown'; return rawIp.startsWith('::ffff:') ? rawIp.slice(7) : rawIp; } app.post('/turn/:label', async (c) => { const label = c.req.param('label'); - const body = await c.req.parseBody(); - const az = Number(body['ROT']); - if (isNaN(az)) return c.text('Invalid azimuth', 400); + const rot = (await c.req.parseBody())['ROT']; + const az = typeof rot === 'string' && rot.trim() !== '' ? Number(rot) : NaN; + if (!Number.isInteger(az) || az < 0) return c.text('Invalid azimuth', 400); const sourceIp = resolveClientIp(c); const ok = router.turn(label, az, sourceIp); return ok ? c.text(`Turning ${label} to ${az}`) : c.text('Unknown rotator', 404);