mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-10-27 17:03:49 +08:00
系统配置页新增「版本与更新」卡片,顶栏版本号旁在有新版时亮出提示胶囊。 更新链路:下载发布包 → 比对 SHA256SUMS → -h 冒烟测试 → 暂存 artex.new → 以退出码 75 退出 → 守护脚本重新拉起并完成换装,页面轮询 /api/health 等版本号变化后自动刷新。 新增 start.sh / start.bat 作为正式启动入口,只按退出码决定是否重新拉起 (0 停止 / 75 应用新版 / 其余崩溃退避)。校验、冒烟、换装、回滚全部收在 selfupdate 包里而不写进脚本:这些逻辑在 sh 和 bat 上要写两套,而它们恰恰 最不能出错——换上跑不起来的二进制后守护进程会反复拉起它,只能上机器手工救。 Docker 的 ENTRYPOINT 一并改为该脚本,并由它转发 SIGTERM,否则 docker stop 只发给 PID 1,artex 做不了优雅关闭会被 SIGKILL 硬杀。 失败保护两道闸门互补:无法 exec 由换装前的冒烟挡住,能执行但初始化崩溃 由启动计数兜住(连续 3 次失败自动回滚 artex.old,坏的留作 artex.failed)。 GitHub 查询结果后端缓存 30 分钟(失败 2 分钟)。未认证 API 是每 IP 每小时 60 次,且这 60 次是服务器 IP 上所有用户共用的,顶栏每次整页加载都查一次, 不缓存会很快耗光配额导致真想更新时反而查不动。手动「检查更新」带 force=1 绕过缓存。 安全:只认 GitHub 域名且强制 HTTPS(逐跳校验重定向),发布源写死不可配置; 升级文件挂在可执行文件所在目录而非 CWD(服务化运行时 CWD 可能是 /); 开发构建版本号不可比较,禁用一键更新。 走完整 zip 而非裸二进制,因为现有 Release 的 SHA256SUMS 只覆盖 zip, 这样无需改 CI,也兼容已发布的历史版本。 已知限制:Docker 下只换程序不换镜像(工具链不同步、重建容器会退回镜像 版本);不同步发布包里的 skills/;更新即重启会中断运行中的任务。
161 lines
5.2 KiB
Go
161 lines
5.2 KiB
Go
// Command artex runs the ARTEX backend: the dual SQLite graph stores,
|
|
// the event-driven exploration engine, and the JSON HTTP API consumed by the
|
|
// shadcn/ui frontend.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"runtime"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/Autumn-27/artex/agent"
|
|
"github.com/Autumn-27/artex/config"
|
|
"github.com/Autumn-27/artex/selfupdate"
|
|
"github.com/Autumn-27/artex/server"
|
|
)
|
|
|
|
// version is the build version, injected at release time via
|
|
// -ldflags "-X main.version=<tag>". Defaults to "dev" for local builds.
|
|
var version = "dev"
|
|
|
|
const banner = `
|
|
_ ____ _____ _______ __
|
|
/ \ | _ \_ _| ____\ \/ /
|
|
/ _ \ | |_) || | | _| \ /
|
|
/ ___ \| _ < | | | |___ / \
|
|
/_/ \_\_| \_\|_| |_____/_/\_\
|
|
`
|
|
|
|
// printBanner writes the startup banner + version/runtime info to stdout.
|
|
func printBanner(addr string) {
|
|
fmt.Print(banner)
|
|
fmt.Println(" AI 自主渗透测试系统")
|
|
fmt.Printf(" 版本 %s · %s/%s · %s · 监听 %s\n\n",
|
|
version, runtime.GOOS, runtime.GOARCH, runtime.Version(), addr)
|
|
}
|
|
|
|
// main only maps run's result onto the process exit code. The exit code is part
|
|
// of the update protocol — the supervising start script reads it to decide
|
|
// whether to relaunch us (see selfupdate.ExitRestart) — so the body has to live
|
|
// in a function that can *return* rather than os.Exit past its own defers.
|
|
func main() {
|
|
os.Exit(run())
|
|
}
|
|
|
|
func run() int {
|
|
var (
|
|
addr = flag.String("addr", ":8787", "HTTP listen address")
|
|
dataDir = flag.String("data", filepath.Join(config.BaseDir(), "data"), "data directory for SQLite stores (default: data/ next to the executable)")
|
|
proxy = flag.String("proxy", ":8788", "traffic recording proxy address (empty to disable)")
|
|
)
|
|
flag.Parse()
|
|
|
|
// hand the build version to the server package so GET /api/health can report it
|
|
// to the frontend top bar.
|
|
server.BuildVersion = version
|
|
|
|
printBanner(*addr)
|
|
|
|
// capture backend logs into the in-memory sink (still to stderr) so the /logs
|
|
// page can show a live log stream. Do this first, to catch startup logs too.
|
|
server.StartLogCapture()
|
|
|
|
// Self-update bootstrap: swap in a staged binary, or count a post-swap boot
|
|
// attempt and roll back if the new build keeps dying. Must run before we open
|
|
// the stores or bind a port — this may end with "exit and let the start script
|
|
// relaunch me", and there is no point paying for either first.
|
|
action, upState := selfupdate.Bootstrap()
|
|
server.SetBootUpdateState(upState)
|
|
if action == selfupdate.Restart {
|
|
return selfupdate.ExitRestart
|
|
}
|
|
|
|
// surface which config file the binary reads (absolute, so `go run`'s relative
|
|
// "config.json" — resolved against the CWD — is unambiguous).
|
|
cfgPath := config.Path()
|
|
if abs, e := filepath.Abs(cfgPath); e == nil {
|
|
cfgPath = abs
|
|
}
|
|
if _, e := os.Stat(cfgPath); e == nil {
|
|
log.Printf("[config] 配置文件: %s", cfgPath)
|
|
} else {
|
|
log.Printf("[config] 配置文件: %s (不存在 — 将仅尝试环境变量 ARTEX_PG_DSN)", cfgPath)
|
|
}
|
|
|
|
sigCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
ctx, shutdown := shutdownContext(sigCtx)
|
|
defer shutdown(agent.AbortShutdown)
|
|
|
|
mgr, err := server.NewManager(*dataDir, *proxy)
|
|
if err != nil {
|
|
log.Fatalf("open stores: %v", err)
|
|
}
|
|
defer mgr.Close()
|
|
|
|
// Surviving this long means a freshly swapped-in build actually works, so drop
|
|
// the upgrade marker and stop counting attempts. Until it fires, every boot
|
|
// increments the count and a build that keeps dying gets rolled back.
|
|
settle := time.AfterFunc(selfupdate.SettleDelay, selfupdate.Settle)
|
|
defer settle.Stop()
|
|
|
|
skillDir := config.SkillDir()
|
|
if abs, err := filepath.Abs(skillDir); err == nil {
|
|
skillDir = abs
|
|
}
|
|
log.Printf("[config] skill 目录: %s", skillDir)
|
|
srv := server.New(ctx, mgr, skillDir, *dataDir, config.BaseDir())
|
|
httpSrv := &http.Server{
|
|
Addr: *addr,
|
|
Handler: srv.Handler(),
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
}
|
|
|
|
go func() {
|
|
log.Printf("ARTEX %s backend listening on %s (data=%s, workers=%d)", version, *addr, *dataDir, mgr.Workers())
|
|
if err := httpSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatalf("serve: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Two ways out: a signal (normal stop → exit 0, the start script stops looping)
|
|
// or a staged update / rollback (→ exit 75, the script relaunches us and the
|
|
// bootstrap above installs the new build).
|
|
code := 0
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-server.RestartRequested():
|
|
code = selfupdate.ExitRestart
|
|
shutdown(agent.AbortShutdown)
|
|
}
|
|
|
|
log.Println("shutting down...")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
_ = httpSrv.Shutdown(shutdownCtx)
|
|
return code
|
|
}
|
|
|
|
// shutdownContext deliberately does not derive from signalCtx. If it did, the
|
|
// parent's plain context.Canceled could win the race before AbortShutdown was
|
|
// attached to the child, losing the diagnostic cause in every running Agent.
|
|
func shutdownContext(signalCtx context.Context) (context.Context, context.CancelCauseFunc) {
|
|
ctx, shutdown := context.WithCancelCause(context.Background())
|
|
go func() {
|
|
select {
|
|
case <-signalCtx.Done():
|
|
shutdown(agent.AbortShutdown)
|
|
case <-ctx.Done():
|
|
}
|
|
}()
|
|
return ctx, shutdown
|
|
}
|