feat: 支持多APK与渠道变体,增加自动定位的服务管理CLI

This commit is contained in:
tongtong committed 2026-11-25 16:10:05 +08:00
1 parent 7984785610
commit 87652364bc
33 files changed
+1110 -42

No files matched your search

+36 -7
View File
@@ -41,9 +41,10 @@ type SyncRequest struct {
Git *GitSnapshot `json:"git,omitempty"`
}
type Artifact struct {
Name string `json:"name"`
Hash string `json:"hash"`
Size int64 `json:"size"`
Name string `json:"name"`
Hash string `json:"hash"`
Size int64 `json:"size"`
SourcePath string `json:"sourcePath,omitempty"`
}
type Build struct {
JavaHome string `json:"javaHome,omitempty"`
@@ -78,6 +79,8 @@ type Server struct {
workspaceRoot string
workspaceRoots []string
environmentFile string
accessFile string
accessLoaded bool
}
var safeID = regexp.MustCompile(`^[a-f0-9]{32}$`)
@@ -163,10 +166,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
s.mu.Lock()
defer s.mu.Unlock()
if e := s.reloadAccessPolicy(); e != nil {
http.Error(w, e.Error(), 503)
return
}
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
fail := func(e error) { http.Error(w, e.Error(), 400) }
if r.URL.Path == "/api/v1/health" {
jsonOut(w, map[string]any{"version": "0.4.6", "preflight": true, "textComparison": true, "jdkSelection": true, "workspaceSelection": true, "os": runtime.GOOS, "sync": "http-manifest", "recovery": true})
jsonOut(w, map[string]any{"version": "0.4.9", "adminCLI": true, "preflight": true, "textComparison": true, "jdkSelection": true, "workspaceSelection": true, "os": runtime.GOOS, "sync": "http-manifest", "recovery": true})
return
}
if r.URL.Path == "/api/v1/environment" && r.Method == "GET" {
@@ -361,8 +368,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fail(e)
return
}
if !s.tasks[req.Task] || !safeTask.MatchString(req.Task) {
fail(errors.New("task not allowed"))
if !safeTask.MatchString(req.Task) {
fail(errors.New("任务名不合法:只允许单个 Gradle 任务,不允许命令行参数"))
return
}
if !s.taskAllowed(req.Task) {
fail(fmt.Errorf("task not allowed:服务端尚未允许任务 %s\n请在服务器本机以管理员权限执行:\n%s\n也可将精确任务名替换为限定模块的 :app:assemble* 规则;保存后无需重启", req.Task, s.adminCommand("task", req.Task)))
return
}
if req.RequestID != "" {
@@ -632,7 +643,7 @@ func (s *Server) run(b *Build) {
if e = os.WriteFile(filepath.Join(dest, name), data, 0600); e != nil {
return e
}
artifacts = append(artifacts, Artifact{name, digest(data), int64(len(data))})
artifacts = append(artifacts, Artifact{Name: name, Hash: digest(data), Size: int64(len(data)), SourcePath: artifactSourcePath(work, p)})
return nil
})
}
@@ -657,6 +668,12 @@ func (s *Server) run(b *Build) {
os.Remove(filepath.Join(s.root, "projects", b.Project, "active-build"))
}
func main() {
if len(os.Args) > 1 && os.Args[1] == "admin" {
if e := runAdmin(os.Args[2:], os.Stdout); e != nil && e != flag.ErrHelp {
log.Fatal(e)
}
return
}
addr := flag.String("listen", "127.0.0.1:8765", "HTTP bind address; use HTTPS proxy or SSH tunnel remotely")
root := flag.String("data", "./data", "service data directory")
tasks := flag.String("tasks", ":app:assembleDebug", "comma separated allowed Gradle tasks")
@@ -667,6 +684,7 @@ func main() {
var workspaceRoots workspaceRootFlags
flag.Var(&workspaceRoots, "workspace-root", "allowed existing-project root; repeat for multiple roots; defaults to data/projects")
environmentFile := flag.String("environment-file", "", "trusted build environment JSON; reloaded for each build")
accessFile := flag.String("access-file", "", "runtime access policy; defaults to <data>/access.json; CLI edits apply without restart")
flag.Parse()
if *initHost != "" {
if e := initCertificate(*initHost, *tlsDir); e != nil {
@@ -707,6 +725,17 @@ func main() {
}
s.tasks[t] = true
}
policyPath := *accessFile
if policyPath == "" {
policyPath = filepath.Join(abs, "access.json")
}
policyPath, e = filepath.Abs(policyPath)
if e != nil {
log.Fatal(e)
}
if e = s.initAccessPolicy(policyPath); e != nil {
log.Fatal(e)
}
entries, _ := os.ReadDir(filepath.Join(abs, "projects"))
for _, entry := range entries {
if !safeID.MatchString(entry.Name()) {