66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
import http from 'node:http';
|
|
|
|
const moduleId = 'intranet-penetration-core';
|
|
const port = Number.parseInt(process.env.PORT || '8787', 10);
|
|
const provider = (process.env.TUNNEL_PROVIDER || '').trim();
|
|
|
|
function sendJson(res, statusCode, payload) {
|
|
const body = JSON.stringify(payload);
|
|
res.writeHead(statusCode, {
|
|
'Content-Type': 'application/json; charset=utf-8',
|
|
'Content-Length': Buffer.byteLength(body),
|
|
});
|
|
res.end(body);
|
|
}
|
|
|
|
function providerNotConfigured(res, action) {
|
|
sendJson(res, 501, {
|
|
moduleId,
|
|
status: 'provider_not_configured',
|
|
action,
|
|
error: '内网穿透模块已加载,但尚未配置真实穿透提供商',
|
|
});
|
|
}
|
|
|
|
const server = http.createServer((req, res) => {
|
|
const url = new URL(req.url || '/', `http://${req.headers.host || '127.0.0.1'}`);
|
|
|
|
if (req.method === 'GET' && url.pathname === '/health') {
|
|
sendJson(res, 200, { moduleId, status: 'healthy' });
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'GET' && url.pathname === '/status') {
|
|
if (!provider) {
|
|
providerNotConfigured(res, 'status');
|
|
return;
|
|
}
|
|
providerNotConfigured(res, 'status');
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && url.pathname === '/start') {
|
|
if (!provider) {
|
|
providerNotConfigured(res, 'start');
|
|
return;
|
|
}
|
|
providerNotConfigured(res, 'start');
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && url.pathname === '/stop') {
|
|
if (!provider) {
|
|
providerNotConfigured(res, 'stop');
|
|
return;
|
|
}
|
|
providerNotConfigured(res, 'stop');
|
|
return;
|
|
}
|
|
|
|
sendJson(res, 404, { moduleId, error: '接口不存在' });
|
|
});
|
|
|
|
server.listen(port, '0.0.0.0', () => {
|
|
console.log(`${moduleId} listening on ${port}`);
|
|
});
|