-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidecar.go
More file actions
229 lines (206 loc) · 5.15 KB
/
Copy pathsidecar.go
File metadata and controls
229 lines (206 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"errors"
"io"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
)
type SidecarProcess struct {
cmd *exec.Cmd
}
type sidecarCommand struct {
command string
args []string
dir string
mode string
}
func spawnNodeSidecar(port int, token string) (*SidecarProcess, error) {
spec, err := resolveSidecarCommand()
if err != nil {
return nil, err
}
cmd := exec.Command(spec.command, spec.args...)
cmd.Dir = spec.dir
cmd.Env = sidecarEnv(port, token)
output := processOutput()
cmd.Stdout = output
cmd.Stderr = output
configureCommand(cmd)
if err := cmd.Start(); err != nil {
return nil, userError("启动本地服务失败:" + err.Error())
}
logLine("CodexPanel sidecar started:", spec.mode, "pid", cmd.Process.Pid)
return &SidecarProcess{cmd: cmd}, nil
}
func (process *SidecarProcess) Wait() error {
if process == nil || process.cmd == nil {
return nil
}
return process.cmd.Wait()
}
func stopSidecar(process *SidecarProcess, port int) {
if process != nil && process.cmd != nil && process.cmd.Process != nil {
killProcessTree(process.cmd.Process.Pid)
_ = process.cmd.Process.Kill()
}
killSidecarOrphans()
if port > 0 {
waitForPortRelease(port, 5*time.Second)
}
}
func sidecarEnv(port int, token string) []string {
env := os.Environ()
env = appendEnv(env, "PORT", intString(port))
env = appendEnv(env, "MOBILE_TYPER_TOKEN", token)
env = appendEnv(env, "CODEX_APP_NAME", "CodexPanel")
env = appendEnv(env, "CODEX_OPEN_BROWSER", "0")
if remoteKey := configuredRemoteKey(); remoteKey != "" {
env = appendEnv(env, "CODEX_REMOTE_KEY", remoteKey)
}
if relayURL := configuredRelayURL(); relayURL != "" {
env = appendEnv(env, "CODEX_RELAY_URL", relayURL)
}
return env
}
func appendEnv(env []string, key string, value string) []string {
prefix := key + "="
filtered := env[:0]
for _, item := range env {
if !strings.HasPrefix(item, prefix) {
filtered = append(filtered, item)
}
}
return append(filtered, prefix+value)
}
func resolveSidecarCommand() (sidecarCommand, error) {
if configured := strings.TrimSpace(os.Getenv("CODEXPANEL_NODE_SIDECAR")); configured != "" {
if exists(configured) {
return sidecarCommand{
command: configured,
dir: filepath.Dir(configured),
mode: "configured sidecar",
}, nil
}
}
sidecarName := "codexpanel-node-sidecar"
if runtime.GOOS == "windows" {
sidecarName += ".exe"
}
for _, dir := range candidateDirs() {
candidate := filepath.Join(dir, sidecarName)
if exists(candidate) {
return sidecarCommand{
command: candidate,
dir: dir,
mode: "portable sidecar",
}, nil
}
}
node, err := exec.LookPath("node")
if err != nil {
return sidecarCommand{}, userError("未找到 Node.js,也没有找到 codexpanel-node-sidecar。请先安装 Node.js 或使用完整便携包。")
}
for _, dir := range candidateDirs() {
serverPath := filepath.Join(dir, "server.js")
if exists(serverPath) {
return sidecarCommand{
command: node,
args: []string{serverPath},
dir: dir,
mode: "node server.js",
}, nil
}
}
return sidecarCommand{}, userError("未找到 server.js 或 codexpanel-node-sidecar,本地服务无法启动。")
}
func candidateDirs() []string {
var dirs []string
add := func(dir string) {
if dir == "" {
return
}
abs, err := filepath.Abs(dir)
if err != nil {
return
}
for _, existing := range dirs {
if samePath(existing, abs) {
return
}
}
dirs = append(dirs, abs)
}
if cwd, err := os.Getwd(); err == nil {
add(cwd)
add(filepath.Join(cwd, "build", "bin"))
}
if exe, err := os.Executable(); err == nil {
exeDir := filepath.Dir(exe)
add(exeDir)
add(filepath.Dir(exeDir))
add(filepath.Dir(filepath.Dir(exeDir)))
}
return dirs
}
func exists(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}
func samePath(left string, right string) bool {
leftAbs, leftErr := filepath.Abs(left)
rightAbs, rightErr := filepath.Abs(right)
if leftErr != nil || rightErr != nil {
return left == right
}
if runtime.GOOS == "windows" {
return strings.EqualFold(leftAbs, rightAbs)
}
return leftAbs == rightAbs
}
func waitForHealth(port int, token string, timeout time.Duration) bool {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if healthOK(port, token) {
return true
}
time.Sleep(250 * time.Millisecond)
}
return false
}
func healthOK(port int, token string) bool {
client := &http.Client{
Timeout: 700 * time.Millisecond,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 500 * time.Millisecond,
}).DialContext,
DisableKeepAlives: true,
},
}
url := "http://127.0.0.1:" + intString(port) + "/codex/health?token=" + token
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return false
}
req.Header.Set("Connection", "close")
resp, err := client.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, resp.Body)
return resp.StatusCode == http.StatusOK
}
func commandExited(err error) bool {
if err == nil {
return true
}
var exitErr *exec.ExitError
return errors.As(err, &exitErr)
}