feat: upadte

This commit is contained in:
2026-10-12 12:53:10 +08:00
parent 51f7c2e60c
commit 32da57d19b
5 changed files with 355 additions and 208 deletions
+310
View File
@@ -0,0 +1,310 @@
#!/usr/bin/env node
/*
* Start a tiny local static server and expose it through Tailscale Serve.
* Node is used for the local server so the Tailscale route only needs to
* proxy a localhost port.
*/
'use strict';
const fs = require('node:fs');
const http = require('node:http');
const net = require('node:net');
const path = require('node:path');
const { spawn, spawnSync } = require('node:child_process');
class PreviewError extends Error {}
function usage() {
return `Usage: node serve_file.js FILE [--mount-path PATH] [--port PORT] [--ttl SECONDS]
Expose FILE through Tailscale Serve. Node serves FILE's parent directory on
localhost; Tailscale proxies that local server to the tailnet.
`;
}
function parseArgs(argv) {
const args = {
file: null, root: null, mountPath: null, port: 80, localPort: 0,
ttl: 3600, tailscalePort: null, server: false,
};
for (let i = 0; i < argv.length; i += 1) {
const value = argv[i];
if (value === '--help' || value === '-h') {
console.log(usage());
process.exit(0);
}
if (value === '--server') {
args.server = true;
continue;
}
if (value === '--root') {
args.root = argv[++i];
continue;
}
if (value === '--mount-path') {
args.mountPath = argv[++i];
continue;
}
if (value === '--port') {
args.port = Number(argv[++i]);
continue;
}
if (value === '--local-port') {
args.localPort = Number(argv[++i]);
continue;
}
if (value === '--ttl') {
args.ttl = Number(argv[++i]);
continue;
}
if (value === '--tailscale-port') {
args.tailscalePort = Number(argv[++i]);
continue;
}
if (value.startsWith('-')) {
throw new PreviewError(`未知参数:${value}`);
}
if (args.file === null) {
args.file = value;
} else {
throw new PreviewError(`多余参数:${value}`);
}
}
return args;
}
function validPort(port, name) {
if (!Number.isInteger(port) || port < 0 || port > 65535 || (name === 'port' && port === 0)) {
throw new PreviewError(`${name} 必须是有效端口`);
}
}
function validTtl(ttl) {
if (!Number.isInteger(ttl) || ttl <= 0) {
throw new PreviewError('ttl 必须是正整数秒数');
}
}
function normalizeMountPath(value) {
const mountPath = value.trim();
if (!mountPath.startsWith('/') || mountPath === '/') {
throw new PreviewError('mount path 必须以 / 开头且不能是根路径');
}
if (mountPath.includes('?') || mountPath.includes('#') || mountPath.includes('//')) {
throw new PreviewError('mount path 不能包含 query、fragment 或空路径段');
}
const parts = mountPath.split('/');
if (parts.some((part) => part === '.' || part === '..')) {
throw new PreviewError('mount path 不能包含 . 或 ..');
}
return mountPath.replace(/\/$/, '');
}
function defaultMountPath(directory) {
const slug = path.basename(directory).replace(/[^a-zA-Z0-9._-]+/g, '-').replace(/^-+|-+$/g, '');
if (!slug) {
throw new PreviewError('无法从文件目录生成 mount path,请显式提供 --mount-path');
}
return `/preview/${slug}`;
}
function run(command, commandArgs) {
const result = spawnSync(command, commandArgs, { encoding: 'utf8' });
if (result.error) {
throw new PreviewError(`无法运行 ${command}:${result.error.message}`);
}
return result;
}
function tailscaleIPv4() {
const result = run('tailscale', ['ip', '-4']);
if (result.status !== 0) {
throw new PreviewError(`Tailscale 节点不可用:${(result.stderr || result.stdout).trim() || 'tailscale ip -4 失败'}`);
}
const address = result.stdout.split(/\s+/).find((candidate) => net.isIPv4(candidate));
if (!address) {
throw new PreviewError('tailscale 没有返回 IPv4 地址;请确认节点已连接 tailnet');
}
return address;
}
function checkTailscaleServe() {
const result = run('tailscale', ['serve', 'status', '--json']);
if (result.status !== 0) {
throw new PreviewError(`无法读取 Tailscale Serve 状态:${(result.stderr || result.stdout).trim() || 'status 失败'}`);
}
}
function serveStatic(root, mountPath, port, ttl, tailscalePort) {
const contentTypes = {
'.css': 'text/css; charset=utf-8',
'.gif': 'image/gif',
'.html': 'text/html; charset=utf-8',
'.jpeg': 'image/jpeg',
'.jpg': 'image/jpeg',
'.js': 'text/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.txt': 'text/plain; charset=utf-8',
'.webp': 'image/webp',
};
const server = http.createServer((request, response) => {
let pathname;
try {
pathname = decodeURIComponent(new URL(request.url, 'http://127.0.0.1').pathname);
} catch {
response.writeHead(400).end('Bad request');
return;
}
if (pathname === mountPath || pathname.startsWith(`${mountPath}/`)) {
pathname = pathname.slice(mountPath.length) || '/';
}
const relativePath = pathname.replace(/^\/+/, '');
const candidate = path.resolve(root, relativePath);
if (candidate !== root && !candidate.startsWith(`${root}${path.sep}`)) {
response.writeHead(403).end('Forbidden');
return;
}
fs.stat(candidate, (statError, info) => {
if (statError || !info.isFile()) {
response.writeHead(404).end('Not found');
return;
}
response.setHeader('Content-Type', contentTypes[path.extname(candidate).toLowerCase()] || 'application/octet-stream');
fs.createReadStream(candidate).on('error', () => response.destroy()).pipe(response);
});
});
server.listen(port, '127.0.0.1', () => {
const address = server.address();
console.log(`READY ${address.port}`);
});
setTimeout(() => {
spawnSync('tailscale', [
'serve', `--http=${tailscalePort}`, `--set-path=${mountPath}`, 'off',
], { stdio: 'ignore' });
server.close(() => process.exit(0));
setTimeout(() => process.exit(0), 1000);
}, ttl * 1000);
}
function startDetachedServer(root, mountPath, localPort, ttl, tailscalePort) {
const child = spawn(process.execPath, [
__filename, '--server', '--root', root, '--mount-path', mountPath,
'--port', String(localPort), '--ttl', String(ttl), '--tailscale-port', String(tailscalePort),
], {
detached: true,
stdio: ['ignore', 'pipe', 'pipe'],
});
return new Promise((resolve, reject) => {
let output = '';
const timeout = setTimeout(() => {
child.kill();
reject(new PreviewError('Node 静态服务启动超时'));
}, 5000);
child.stdout.on('data', (chunk) => {
output += chunk.toString();
const match = output.match(/READY (\d+)/);
if (!match) return;
clearTimeout(timeout);
child.stdout.destroy();
child.stderr.destroy();
child.unref();
resolve({ pid: child.pid, port: Number(match[1]) });
});
child.on('error', (error) => {
clearTimeout(timeout);
reject(new PreviewError(`Node 静态服务启动失败:${error.message}`));
});
child.on('exit', (code) => {
if (code !== null && code !== 0) {
clearTimeout(timeout);
reject(new PreviewError(`Node 静态服务退出,code=${code}`));
}
});
});
}
function stopChild(pid) {
if (!pid) return;
try {
process.kill(pid, 'SIGTERM');
} catch {
// The child may have exited while Tailscale was being configured.
}
}
async function launch(args) {
if (!args.file) throw new PreviewError('缺少要展示的文件');
validPort(args.port, 'port');
validPort(args.localPort, 'local-port');
validTtl(args.ttl);
const filePath = path.resolve(args.file);
let info;
try {
info = fs.statSync(filePath);
} catch {
throw new PreviewError(`输入文件不存在:${filePath}`);
}
if (!info.isFile()) throw new PreviewError(`输入不是普通文件:${filePath}`);
const root = path.dirname(filePath);
const mountPath = args.mountPath ? normalizeMountPath(args.mountPath) : defaultMountPath(root);
const ip = tailscaleIPv4();
checkTailscaleServe();
const child = await startDetachedServer(root, mountPath, args.localPort, args.ttl, args.port);
try {
const result = run('tailscale', [
'serve', '--bg', `--http=${args.port}`, `--set-path=${mountPath}`, '--yes',
`http://127.0.0.1:${child.port}`,
]);
if (result.status !== 0) {
throw new PreviewError(`Tailscale Serve 配置失败:${(result.stderr || result.stdout).trim() || 'serve 命令失败'}`);
}
} catch (error) {
stopChild(child.pid);
throw error;
}
const relative = path.relative(root, filePath).split(path.sep).map(encodeURIComponent).join('/');
const suffix = args.port === 80 ? '' : `:${args.port}`;
const url = `http://${ip}${suffix}${mountPath}/${relative}`;
console.log(JSON.stringify({
file: filePath,
directory: root,
mountPath,
ip,
port: args.port,
localPort: child.port,
pid: child.pid,
ttl: args.ttl,
expiresAt: new Date(Date.now() + args.ttl * 1000).toISOString(),
url,
stopCommand: `kill ${child.pid} 2>/dev/null || true; tailscale serve --http=${args.port} --set-path=${mountPath} off`,
}));
}
async function main() {
const args = parseArgs(process.argv.slice(2));
if (args.server) {
if (!args.mountPath || !args.root || !args.tailscalePort) throw new PreviewError('内部参数不完整');
validPort(args.port, 'local-port');
validPort(args.tailscalePort, 'port');
validTtl(args.ttl);
serveStatic(path.resolve(args.root), args.mountPath, args.port, args.ttl, args.tailscalePort);
return;
}
await launch(args);
}
main().catch((error) => {
console.error(`previewer: ${error.message}`);
process.exitCode = 2;
});
-177
View File
@@ -1,177 +0,0 @@
#!/usr/bin/env python3
"""Expose a static file's parent directory through a Tailscale Serve HTTP route."""
from __future__ import annotations
import argparse
import ipaddress
import json
import re
import shutil
import subprocess
import sys
from pathlib import Path, PurePosixPath
from urllib.parse import quote
class PreviewError(RuntimeError):
pass
def run_tailscale(*args: str) -> subprocess.CompletedProcess[str]:
try:
return subprocess.run(
["tailscale", *args],
check=False,
capture_output=True,
text=True,
)
except OSError as exc:
raise PreviewError(f"无法运行 tailscale:{exc}") from exc
def require_tailscale() -> None:
if shutil.which("tailscale") is None:
raise PreviewError("未找到 tailscale,请先安装并登录 Tailscale")
def tailscale_ipv4() -> str:
result = run_tailscale("ip", "-4")
if result.returncode != 0:
detail = (result.stderr or result.stdout).strip()
raise PreviewError(f"Tailscale 节点不可用:{detail or 'tailscale ip -4 失败'}")
for line in result.stdout.splitlines():
candidate = line.strip()
try:
address = ipaddress.ip_address(candidate)
except ValueError:
continue
if address.version == 4:
return candidate
raise PreviewError("tailscale 没有返回 IPv4 地址;请确认节点已连接 tailnet")
def serve_status() -> tuple[dict, str]:
result = run_tailscale("serve", "status", "--json")
if result.returncode != 0:
detail = (result.stderr or result.stdout).strip()
raise PreviewError(f"无法读取 Tailscale Serve 状态:{detail or 'status 失败'}")
try:
data = json.loads(result.stdout or "{}")
except json.JSONDecodeError as exc:
raise PreviewError("Tailscale Serve 返回了无法解析的 JSON 状态") from exc
if not isinstance(data, dict):
raise PreviewError("Tailscale Serve 状态不是 JSON 对象")
plain = run_tailscale("serve", "status")
if plain.returncode != 0:
detail = (plain.stderr or plain.stdout).strip()
raise PreviewError(f"无法读取 Tailscale Serve 文本状态:{detail or 'status 失败'}")
return data, plain.stdout
def status_blob(data: dict, plain: str) -> str:
return json.dumps(data, ensure_ascii=False, sort_keys=True) + "\n" + plain
def normalize_mount_path(value: str) -> str:
path = value.strip()
if not path.startswith("/") or path == "/":
raise PreviewError("mount path 必须以 / 开头且不能是根路径")
if "?" in path or "#" in path or "//" in path:
raise PreviewError("mount path 不能包含 query、fragment 或空路径段")
parts = PurePosixPath(path).parts
if any(part in {".", ".."} for part in parts):
raise PreviewError("mount path 不能包含 . 或 ..")
return "/" + "/".join(parts[1:])
def default_mount_path(directory: Path) -> str:
slug = re.sub(r"[^a-zA-Z0-9._-]+", "-", directory.name).strip("-")
if not slug:
raise PreviewError("无法从文件目录生成 mount path,请显式提供 --mount-path")
return f"/preview/{slug}"
def route_matches(blob: str, mount_path: str, directory: Path) -> bool:
return mount_path in blob and str(directory) in blob
def route_conflicts(blob: str, mount_path: str, directory: Path) -> bool:
return mount_path in blob and str(directory) not in blob
def configure_route(directory: Path, mount_path: str, port: int, data: dict, plain: str) -> None:
blob = status_blob(data, plain)
if route_conflicts(blob, mount_path, directory):
raise PreviewError(
f"mount path {mount_path} 已被其它目录占用;请改用唯一的 --mount-path,不会覆盖已有路由"
)
if route_matches(blob, mount_path, directory):
return
result = run_tailscale(
"serve",
"--bg",
f"--http={port}",
f"--set-path={mount_path}",
"--yes",
str(directory),
)
if result.returncode != 0:
detail = (result.stderr or result.stdout).strip()
raise PreviewError(f"Tailscale Serve 配置失败:{detail or 'serve 命令失败'}")
data, plain = serve_status()
blob = status_blob(data, plain)
if route_conflicts(blob, mount_path, directory) or not route_matches(blob, mount_path, directory):
raise PreviewError("Serve 命令返回成功,但状态中没有确认目标路由;停止输出 URL")
def build_result(file_path: Path, directory: Path, mount_path: str, ip: str, port: int) -> dict[str, object]:
relative = file_path.relative_to(directory).as_posix()
suffix = f":{port}" if port != 80 else ""
url = f"http://{ip}{suffix}{mount_path}/{quote(relative, safe='/')}"
return {
"file": str(file_path),
"directory": str(directory),
"mountPath": mount_path,
"ip": ip,
"port": port,
"url": url,
"stopCommand": f"tailscale serve --http={port} --set-path={mount_path} off",
}
def parse_args(argv: list[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("file", type=Path, help="要展示的静态文件")
parser.add_argument("--mount-path", help="Tailscale Serve 的 URL 路径")
parser.add_argument("--port", type=int, default=80, help="HTTP 端口,默认 80")
return parser.parse_args(argv)
def main(argv: list[str] | None = None) -> int:
args = parse_args(argv or sys.argv[1:])
try:
if not 1 <= args.port <= 65535:
raise PreviewError("port 必须在 1 到 65535 之间")
require_tailscale()
file_path = args.file.expanduser().resolve()
if not file_path.is_file():
raise PreviewError(f"输入不是普通文件或不存在:{file_path}")
directory = file_path.parent
mount_path = normalize_mount_path(args.mount_path) if args.mount_path else default_mount_path(directory)
ip = tailscale_ipv4()
data, plain = serve_status()
configure_route(directory, mount_path, args.port, data, plain)
print(json.dumps(build_result(file_path, directory, mount_path, ip, args.port), ensure_ascii=False))
return 0
except PreviewError as exc:
print(f"previewer: {exc}", file=sys.stderr)
return 2
if __name__ == "__main__":
raise SystemExit(main())