import crypto from 'node:crypto'; import http from 'node:http'; import https from 'node:https'; const panelURL = process.env.SEA_PANEL_URL?.trim(); const panelAPIKey = process.env.SEA_PANEL_API_KEY?.trim(); const insecureTLS = process.env.SEA_PANEL_TLS_INSECURE === 'true'; if (!panelURL || !panelAPIKey) process.exit(2); const base = new URL(panelURL); const client = base.protocol === 'https:' ? https : http; const agent = base.protocol === 'https:' ? new https.Agent({ rejectUnauthorized: !insecureTLS }) : undefined; const md5 = (value) => crypto.createHash('md5').update(value).digest('hex'); function panelForm(pathname, values = {}) { const requestTime = Math.floor(Date.now() / 1000).toString(); const body = Buffer.from(new URLSearchParams({ ...values, request_time: requestTime, request_token: md5(requestTime + md5(panelAPIKey)) }).toString()); return new Promise((resolve, reject) => { const request = client.request(new URL(pathname, base), { method: 'POST', agent, timeout: 30_000, headers: { 'content-type': 'application/x-www-form-urlencoded', 'content-length': body.length }, }, (response) => { let data = ''; response.setEncoding('utf8'); response.on('data', (chunk) => { data += chunk; }); response.on('end', () => { if (response.statusCode < 200 || response.statusCode > 299) return reject(new Error(`panel returned HTTP ${response.statusCode}`)); try { resolve(JSON.parse(data)); } catch { reject(new Error('panel returned non-JSON data')); } }); }); request.on('timeout', () => request.destroy(new Error('panel request timed out'))); request.on('error', reject); request.end(body); }); } function rows(payload) { if (Array.isArray(payload)) return payload; if (Array.isArray(payload?.data)) return payload.data; if (Array.isArray(payload?.data?.data)) return payload.data.data; return []; } const routes = { 'Node.js': '/project/nodejs/get_project_list/stype', Python: '/project/python/GetProjectList/stype', Go: '/project/go/get_project_list/stype', Java: '/mod/java/project/project_list/stype', }; const inventory = {}; for (const [runtime, route] of Object.entries(routes)) { const payload = await panelForm(route, { search: 'sv', p: '1', limit: '100' }); inventory[runtime] = rows(payload).map((item) => ({ name: item.name ?? item.project_name, path: item.path ?? item.project_cwd ?? item.project_path, port: item.port, run: item.run, listenOK: item.listen_ok, pid: item.pid, listen: item.listen, })).filter((item) => /^sv[0-9a-f]{10}(?:_[0-9]+)?$/.test(item.name ?? '')); } console.log(JSON.stringify(inventory, null, 2));