167 lines
7.0 KiB
JavaScript
167 lines
7.0 KiB
JavaScript
import crypto from 'node:crypto';
|
|
import http from 'node:http';
|
|
import https from 'node:https';
|
|
|
|
const baseURL = process.env.SEA_PANEL_URL?.trim();
|
|
const apiKey = process.env.SEA_PANEL_API_KEY?.trim();
|
|
const insecureTLS = process.env.SEA_PANEL_TLS_INSECURE === 'true';
|
|
|
|
if (!baseURL || !apiKey) {
|
|
console.error('SEA_PANEL_URL and SEA_PANEL_API_KEY are required.');
|
|
process.exit(2);
|
|
}
|
|
|
|
const base = new URL(baseURL);
|
|
if (!['http:', 'https:'].includes(base.protocol) || base.username || base.password) {
|
|
console.error('SEA_PANEL_URL must be an HTTP(S) URL without embedded credentials.');
|
|
process.exit(2);
|
|
}
|
|
|
|
const md5 = (value) => crypto.createHash('md5').update(value).digest('hex');
|
|
const client = base.protocol === 'https:' ? https : http;
|
|
const agent = base.protocol === 'https:' ? new https.Agent({ rejectUnauthorized: !insecureTLS }) : undefined;
|
|
|
|
async function request(method, pathname, values = {}) {
|
|
const timestamp = Math.floor(Date.now() / 1000).toString();
|
|
const body = new URLSearchParams({ ...values, request_time: timestamp, request_token: md5(timestamp + md5(apiKey)) });
|
|
const url = new URL(pathname, base);
|
|
const payload = method === 'GET' ? '' : body.toString();
|
|
if (method === 'GET') url.search = body.toString();
|
|
return new Promise((resolve, reject) => {
|
|
const req = client.request(url, {
|
|
method,
|
|
agent,
|
|
timeout: 20_000,
|
|
headers: method === 'POST' ? {
|
|
'content-type': 'application/x-www-form-urlencoded',
|
|
'content-length': Buffer.byteLength(payload),
|
|
} : undefined,
|
|
}, (response) => {
|
|
let data = '';
|
|
response.setEncoding('utf8');
|
|
response.on('data', (chunk) => {
|
|
data += chunk;
|
|
if (data.length > 2 * 1024 * 1024) req.destroy(new Error('panel response exceeded 2 MB'));
|
|
});
|
|
response.on('end', () => {
|
|
if (response.statusCode < 200 || response.statusCode > 299) {
|
|
reject(new Error(`panel returned HTTP ${response.statusCode}`));
|
|
return;
|
|
}
|
|
try { resolve(JSON.parse(data)); } catch { reject(new Error('panel returned non-JSON data')); }
|
|
});
|
|
});
|
|
req.on('timeout', () => req.destroy(new Error('panel request timed out')));
|
|
req.on('error', reject);
|
|
req.end(payload);
|
|
});
|
|
}
|
|
|
|
const versionPattern = /^[A-Za-z0-9][A-Za-z0-9._-]{0,39}$/;
|
|
const clean = (items) => [...new Set((Array.isArray(items) ? items : []).map((value) => String(value).trim()).filter((value) => versionPattern.test(value)))];
|
|
const probe = async (name, run, extract) => {
|
|
try {
|
|
const payload = await run();
|
|
if (payload && (payload.status === false || payload.status === 'false' || payload.status === 'error')) {
|
|
return [name, { status: 'not_installed', versions: [] }];
|
|
}
|
|
const versions = clean(extract(payload));
|
|
return [name, { status: versions.length ? 'available' : 'not_installed', versions }];
|
|
} catch {
|
|
return [name, { status: 'probe_failed', versions: [] }];
|
|
}
|
|
};
|
|
|
|
let system;
|
|
try {
|
|
system = await request('POST', '/system', { action: 'GetSystemTotal' });
|
|
} catch (error) {
|
|
console.error(`Panel connection failed: ${error.message}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Optional compatibility diagnostics for adapting a specific aaPanel release.
|
|
// These are read-only version endpoints; the flag is intentionally off during
|
|
// normal installation so preflight output stays compact.
|
|
if (process.env.SEA_PANEL_SHOW_RUNTIME_METADATA === 'true') {
|
|
const [node, python, go, java] = await Promise.all([
|
|
request('POST', '/project/nodejs/get_nodejs_version/stype'),
|
|
request('POST', '/plugin', { action: 'a', name: 'pythonmamager', s: 'GetPythonV' }),
|
|
request('POST', '/project/go/list_go_sdk/stype'),
|
|
request('GET', '/mod/java/project/get_system_info/stype'),
|
|
]);
|
|
const filesystemPaths = (value, prefix = '$', found = []) => {
|
|
if (typeof value === 'string' && value.startsWith('/')) found.push({ key: prefix, value });
|
|
else if (Array.isArray(value)) value.forEach((item, index) => filesystemPaths(item, `${prefix}[${index}]`, found));
|
|
else if (value && typeof value === 'object') Object.entries(value).forEach(([key, item]) => filesystemPaths(item, `${prefix}.${key}`, found));
|
|
return found;
|
|
};
|
|
console.log(JSON.stringify({
|
|
node,
|
|
python: { installed: Array.isArray(python) ? python : [], manager: 'pythonmamager' },
|
|
go: { installed: go.installed, used: go.used, paths: filesystemPaths(go) },
|
|
java: { installed: java.data?.jdk_info?.filter((item) => item.operation === 1) },
|
|
}, null, 2));
|
|
process.exit(0);
|
|
}
|
|
|
|
// Read-only route-shape diagnostics for aaPanel releases that ship both the
|
|
// legacy Java project model and the newer /mod implementation. Project names
|
|
// and configuration values are intentionally omitted from the output.
|
|
if (process.env.SEA_PANEL_PROBE_JAVA_ROUTES === 'true') {
|
|
const routes = [
|
|
'/mod/java/project/project_list',
|
|
'/mod/java/project/project_list/stype',
|
|
'/mod/java/project/get_project_list',
|
|
'/mod/java/project/get_project_list/stype',
|
|
'/project/java/get_project_list',
|
|
'/project/java/get_project_list/stype',
|
|
];
|
|
const result = {};
|
|
for (const route of routes) {
|
|
try {
|
|
const payload = await request('POST', route, { p: '1', limit: '20' });
|
|
const rows = Array.isArray(payload?.data) ? payload.data : [];
|
|
result[route] = {
|
|
status: payload?.status,
|
|
message: String(payload?.msg ?? payload?.message ?? '').slice(0, 160),
|
|
count: rows.length,
|
|
rowShape: rows.length ? {
|
|
keys: Object.keys(rows[0]).sort(),
|
|
runType: typeof rows[0].run,
|
|
pidType: typeof rows[0].pid,
|
|
listenOKType: typeof rows[0].listen_ok,
|
|
} : undefined,
|
|
};
|
|
} catch (error) {
|
|
result[route] = { error: error.message };
|
|
}
|
|
}
|
|
console.log(JSON.stringify(result, null, 2));
|
|
process.exit(0);
|
|
}
|
|
|
|
const runtimes = Object.fromEntries(await Promise.all([
|
|
probe('PHP', () => request('POST', '/site', { action: 'GetPHPVersion' }), (data) => {
|
|
const items = Array.isArray(data) ? data : Array.isArray(data?.data) ? data.data : [];
|
|
return items.filter((item) => item?.status && item.version !== '00').map((item) => {
|
|
const version = String(item.version).trim();
|
|
return version.includes('.') || version.length < 2 ? version : `${version.slice(0, -1)}.${version.slice(-1)}`;
|
|
});
|
|
}),
|
|
probe('Node.js', () => request('POST', '/project/nodejs/get_nodejs_version/stype'), (data) => Array.isArray(data) ? data : data?.data),
|
|
probe('Python', () => request('POST', '/plugin', { action: 'a', name: 'pythonmamager', s: 'GetPythonV' }), (data) => data),
|
|
probe('Go', () => request('POST', '/project/go/list_go_sdk/stype'), (data) => data.installed),
|
|
probe('Java', () => request('GET', '/mod/java/project/get_system_info/stype'), (data) => data.data?.jdk_info?.filter((item) => item.operation === 1 || item.path).map((item) => item.name) ?? []),
|
|
]));
|
|
|
|
console.log(JSON.stringify({
|
|
panel: {
|
|
version: String(system.version ?? ''),
|
|
system: String(system.system ?? ''),
|
|
cpuCores: Number(system.cpuNum ?? 0),
|
|
memoryMB: Number(system.memTotal ?? 0),
|
|
},
|
|
runtimes,
|
|
}, null, 2));
|