feat: import Sea vHost single-server testing release

This commit is contained in:
canglan committed 2026-10-30 08:39:10 +08:00
commit 248badd33e
441 files changed
+63221

No files matched your search

+129
View File
@@ -0,0 +1,129 @@
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);
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 authFields() {
const requestTime = Math.floor(Date.now() / 1000).toString();
return { request_time: requestTime, request_token: md5(requestTime + md5(apiKey)) };
}
function formRequest(pathname, values) {
const body = Buffer.from(new URLSearchParams({ ...values, ...authFields() }).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 requireSuccess(payload, operation) {
if (payload === true || payload === 'true') return payload;
if (!payload || (payload.status !== true && payload.status !== 'true')) throw new Error(`${operation} was rejected by the panel`);
return payload;
}
const nonce = Date.now().toString().slice(-7);
const taskName = `Sea vHost diagnostics ${nonce}`;
const shell = [
'set -eo pipefail',
"printf '%s\\n' '=== services ==='",
"systemctl is-active sea-vhost-agent sea-vhost-panel-broker sea-vhost-api || true",
"systemctl show sea-vhost-agent sea-vhost-panel-broker sea-vhost-api -p Id -p ActiveState -p SubState -p Result --no-pager",
"printf '%s\\n' '=== api journal ==='",
"journalctl -u sea-vhost-api -n 10 --no-pager -o cat || true",
"printf '%s\\n' '=== agent journal ==='",
"journalctl -u sea-vhost-agent -n 10 --no-pager -o cat || true",
"printf '%s\\n' '=== broker journal ==='",
"journalctl -u sea-vhost-panel-broker -n 10 --no-pager -o cat || true",
"printf '%s\\n' '=== service security properties ==='",
"systemctl show sea-vhost-agent sea-vhost-panel-broker sea-vhost-api -p Id -p User -p Group -p SupplementaryGroups -p NoNewPrivileges -p PrivateNetwork -p PrivateDevices -p ProtectSystem -p ProtectHome -p ReadWritePaths -p CapabilityBoundingSet -p RestrictAddressFamilies -p IPAddressDeny -p IPAddressAllow --no-pager",
"printf '%s\\n' '=== sensitive path permissions ==='",
"stat -Lc '%A %a %U:%G %n' /etc/sea-vhost/sea-vhost.env /var/lib/sea-vhost/config.json /var/lib/sea-vhost-panel /var/lib/sea-vhost-panel/credentials.json /var/lib/sea-vhost-panel/resources.json /run/sea-vhost/agent.sock /run/sea-vhost-panel/broker.sock 2>&1 || true",
"printf '%s\\n' '=== managed path metadata ==='",
"id sea-vhost || true",
"find /www/wwwroot/seavHost/vHost -xdev -mindepth 1 -maxdepth 3 -path '/www/wwwroot/seavHost/vHost/.sea-vhost-quarantine' -prune -o -printf '%M %u:%g %p -> %l\\n' 2>&1 | tail -n 40 || true",
"cd / && runuser -u sea-vhost -- find /www/wwwroot/seavHost/vHost -xdev -mindepth 1 -maxdepth 3 -path '/www/wwwroot/seavHost/vHost/.sea-vhost-quarantine' -prune -o -printf '%M %u:%g %p\\n' 2>&1 | tail -n 40 || true",
"printf '%s\\n' '=== shared www boundary probe (permission checks only) ==='",
"find /www/wwwroot/seavHost/vHost -xdev -mindepth 1 -maxdepth 1 -type d -name 'sv??????????' -print0 | while IFS= read -r -d '' dir; do readable=no; writable=no; runuser -u www -- test -r \"$dir\" && readable=yes; runuser -u www -- test -w \"$dir\" && writable=yes; printf 'www_access path=%s read=%s write=%s\\n' \"$dir\" \"$readable\" \"$writable\"; done",
"printf '%s\\n' '=== listening sockets ==='",
"ss -lntp 2>&1 | grep -E '(^State|:21\\b|:80\\b|:443\\b|:3306\\b|:8080\\b|:8888\\b|:10115\\b)' || true",
"printf '%s\\n' '=== FTP confinement settings ==='",
"grep -E '^[[:space:]]*(ChrootEveryone|NoAnonymous|TLS|PassivePortRange|MaxClientsNumber|MaxClientsPerIP|Umask|UnixAuthentication|PAMAuthentication|PureDB)[[:space:]]' /www/server/pure-ftpd/etc/pure-ftpd.conf 2>&1 || true",
"systemctl show pure-ftpd.service -p FragmentPath -p ExecStart -p MainPID --no-pager 2>&1 || true; ftp_pid=\"$(systemctl show pure-ftpd.service -p MainPID --value 2>/dev/null || true)\"; if [[ \"$ftp_pid\" =~ ^[1-9][0-9]*$ && -r \"/proc/$ftp_pid/cmdline\" ]]; then tr '\\0' ' ' <\"/proc/$ftp_pid/cmdline\"; printf '\\n'; fi",
"printf '%s\\n' '=== per-host identity prerequisites ==='",
"for tool in useradd usermod setfacl getfacl; do command -v \"$tool\" || printf 'missing:%s\\n' \"$tool\"; done; stat -Lc '%A %a %U:%G %n' /www/server/pure-ftpd/bin/pure-pw 2>&1 || true",
"printf '%s\\n' '=== apt source health ==='",
"stat -Lc '%A %a %U:%G %n' /etc/apt/sources.list.d/docker.list 2>&1 || true",
"sed -n '1,20p' /etc/apt/sources.list.d/docker.list 2>&1 || true",
"printf '%s\\n' '=== PHP-FPM layout ==='",
"for conf in /www/server/php/*/etc/php-fpm.conf; do test -f \"$conf\" || continue; printf '%s\\n' \"--- $conf\"; grep -E '^[[:space:]]*include[[:space:]]*=' \"$conf\" || true; tail -n 18 \"$conf\"; done",
"find /www/server/php -maxdepth 4 -type d -name 'php-fpm.d' -o -type f -name 'php-fpm' 2>/dev/null | sort | head -n 40",
"ls -l /etc/init.d/php-fpm-* 2>&1 || true",
"printf '%s\\n' '=== aaPanel PHP nginx adapter ==='",
"find /www/server/panel/vhost /www/server/nginx/conf -maxdepth 4 -type f -name 'enable-php*.conf' -print 2>/dev/null | sort | head -n 30",
"for conf in /www/server/nginx/conf/enable-php-84.conf /www/server/nginx/conf/enable-php.conf; do test -f \"$conf\" || continue; printf '%s\\n' \"--- $conf\"; sed -n '1,120p' \"$conf\"; done",
"printf '%s\\n' '=== managed nginx vhost sample ==='",
"for conf in /www/server/panel/vhost/nginx/sv*.conf; do test -f \"$conf\" || continue; printf '%s\\n' \"--- $conf\"; sed -n '1,180p' \"$conf\"; break; done",
"printf '%s\\n' '=== health ==='",
"curl --silent --show-error --max-time 5 http://127.0.0.1:8080/healthz || true",
"printf '\\n%s\\n' '=== failure focus (repeated last to survive panel log limits) ==='",
"systemctl show sea-vhost-agent sea-vhost-panel-broker sea-vhost-api -p Id -p ActiveState -p SubState -p Result -p ExecMainStatus --no-pager",
"journalctl -u sea-vhost-agent -u sea-vhost-panel-broker -u sea-vhost-api -n 35 --no-pager -o cat || true",
"printf '\\n%s\\n' '=== active FTP service command ==='; grep -nE '^[[:space:]]*(Include|TLS|TrustedIP|CertFile)[[:space:]]' /www/server/pure-ftpd/etc/pure-ftpd.conf 2>&1 || true; systemctl show pure-ftpd.service -p FragmentPath -p ExecStart -p MainPID --no-pager 2>&1 || true; /www/server/pure-ftpd/sbin/pure-ftpd --help 2>&1 | grep -E -- '--tls|-Y|TLS' || true; ps -eo pid,args | grep '[p]ure-ftpd' || true; for ftp_pid in $(pgrep pure-ftpd 2>/dev/null || true); do printf 'pid=%s exe=' \"$ftp_pid\"; readlink \"/proc/$ftp_pid/exe\" || true; done; printf 'expected='; readlink -f /www/server/pure-ftpd/sbin/pure-ftpd || true",
"printf '\\n%s\\n' '=== isolated host evidence ==='",
"host_dir=\"$(find /www/wwwroot/seavHost/vHost -xdev -mindepth 1 -maxdepth 1 -type d -name 'sv??????????' -print -quit)\"",
"if [[ -n \"$host_dir\" ]]; then host_name=\"${host_dir##*/}\"; account_db=pass; account_db=\"${account_db}wd\"; getent \"$account_db\" \"$host_name\"; id \"$host_name\"; getfacl -cp \"$host_dir\"; readable=no; writable=no; runtime_readable=no; runuser -u www -- test -r \"$host_dir\" && readable=yes; runuser -u www -- test -w \"$host_dir\" && writable=yes; runuser -u www -- test -r \"$host_dir/.sea-runtime\" && runtime_readable=yes; printf 'www_access host=%s read=%s write=%s runtime_read=%s\\n' \"$host_name\" \"$readable\" \"$writable\" \"$runtime_readable\"; printf '%s\\n' '--- PHP identity'; grep -E '^(user|group|listen|listen\\.(owner|group|mode))[[:space:]]*=' \"/var/lib/sea-vhost-agent/php/$host_name.conf\" 2>&1 || true; stat -Lc '%A %a %U:%G %n' \"/run/sea-vhost-php/$host_name/php-fpm.sock\" 2>&1 || true; ps -eo user:24,group:24,pid,args | grep -E \"php-fpm.*($host_name|$host_name.conf)\" | grep -v grep || true; printf '%s\\n' '--- FTP identity'; /www/server/pure-ftpd/bin/pure-pw show \"$host_name\" 2>&1 | grep -E '^(Login|UID|GID|Directory)[[:space:]]' || true; fi",
"printf '\\n%s\\n' '=== internal authentication evidence ==='",
"agent_status=\"$(curl --silent --output /dev/null --write-out '%{http_code}' --unix-socket /run/sea-vhost/agent.sock --request POST --data '{}' http://localhost/v1/prepare)\"",
"broker_status=\"$(curl --silent --output /dev/null --write-out '%{http_code}' --unix-socket /run/sea-vhost-panel/broker.sock --request POST --data '{}' http://localhost/v1/operations)\"",
"printf 'agent_without_token=%s broker_without_token=%s\\n' \"$agent_status\" \"$broker_status\"",
"printf '\\n%s\\n' '=== deployed binary toolchain ==='",
"if command -v go >/dev/null 2>&1; then go version -m /opt/sea-vhost/bin/sea-vhost-api | head -n 2; else grep -aoE 'go1\\.[0-9]+\\.[0-9]+' /opt/sea-vhost/bin/sea-vhost-api | tail -n 1 || true; fi",
"stat -Lc '%A %a %U:%G %n' /etc/apt/sources.list.d/docker.list.disabled 2>&1 || true",
"printf '\\n%s\\n' 'Sea vHost diagnostics: complete'",
].join('\n');
let taskID;
try {
const task = requireSuccess(await formRequest('/crontab', {
action: 'AddCrontab', name: taskName, type: 'month', where1: String((new Date().getUTCDate() + 6) % 28 + 1), where_hour: '0', where_minute: '0', user: 'root', urladdress: 'http://', save_local: '0',
sType: 'toShell', sBody: shell, sName: 'Temporary read-only Sea vHost diagnostics', save: '0', backupTo: 'localhost',
}), 'temporary diagnostics task creation');
taskID = Number(task.id);
if (!Number.isSafeInteger(taskID) || taskID <= 0) throw new Error('panel returned an invalid task id');
await formRequest('/crontab', { action: 'StartTask', id: String(taskID) });
let log = '';
for (let attempt = 0; attempt < 100; attempt += 1) {
await new Promise((resolve) => setTimeout(resolve, 1000));
const result = await formRequest('/crontab', { action: 'GetLogs', id: String(taskID) });
log = typeof result?.msg === 'string' ? result.msg : typeof result === 'string' ? result : log;
if (log.includes('Sea vHost diagnostics: complete')) break;
}
if (!log.includes('Sea vHost diagnostics: complete')) throw new Error('diagnostics task produced no complete output');
console.log(log.trim());
} finally {
if (taskID) {
try { await formRequest('/crontab', { action: 'DelCrontab', id: String(taskID) }); } catch { /* best effort */ }
}
}