From e832536afc5913c8b17c7a644b8c8683cc99f9da Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sat, 20 Aug 2022 16:21:23 +0800 Subject: [PATCH] update: env with asset folder --- Dockerfile | 3 +-- cmd/controller.go | 12 ++++++------ cmd/process/daemon.go | 2 +- cmd/process/main.go | 8 +++++++- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 50ab3fe..9e0a0df 100644 --- a/Dockerfile +++ b/Dockerfile @@ -53,8 +53,7 @@ COPY --from=radvd /tmp/radvd* /asset/usr/sbin/ COPY --from=proxy /tmp/*ray /asset/usr/bin/ FROM alpine:3.16 -ENV XRAY_LOCATION_ASSET=/xproxy/assets RUN apk add --no-cache iptables ip6tables COPY --from=asset /asset/ / WORKDIR /xproxy -CMD ["xproxy"] +ENTRYPOINT ["xproxy"] diff --git a/cmd/controller.go b/cmd/controller.go index f693a73..7dcf40b 100644 --- a/cmd/controller.go +++ b/cmd/controller.go @@ -17,9 +17,9 @@ import ( "time" ) -func runProcess(command ...string) { +func runProcess(env []string, command ...string) { sub := process.New(command...) - sub.Run(true) + sub.Run(true, env) sub.Daemon() subProcess = append(subProcess, sub) } @@ -62,11 +62,11 @@ func runScript(settings *config.Config) { func runProxy(settings *config.Config) { if settings.Proxy.Core == "xray" { // xray-core - runProcess("xray", "-confdir", configDir) + runProcess([]string{"XRAY_LOCATION_ASSET=" + assetDir}, "xray", "-confdir", configDir) } else if settings.Proxy.Core == "v2ray" { // v2fly-core - runProcess("v2ray", "-confdir", configDir) + runProcess([]string{"V2RAY_LOCATION_ASSET=" + assetDir}, "v2ray", "-confdir", configDir) } else if settings.Proxy.Core == "sagray" { // sager-core - runProcess("sagray", "run", "-confdir", configDir) + runProcess([]string{"V2RAY_LOCATION_ASSET=" + assetDir}, "sagray", "run", "-confdir", configDir) } else { log.Panicf("Unknown core type -> %s", settings.Proxy.Core) } @@ -81,6 +81,6 @@ func runRadvd(settings *config.Config) { radvdCmd = append(radvdCmd, "--debug", strconv.Itoa(settings.Radvd.Log)) time.Sleep(time.Second) // radvd will crash on first boot without delay (enable debug), why??? } - runProcess(radvdCmd...) + runProcess(nil, radvdCmd...) } } diff --git a/cmd/process/daemon.go b/cmd/process/daemon.go index a05d6da..5721368 100644 --- a/cmd/process/daemon.go +++ b/cmd/process/daemon.go @@ -12,7 +12,7 @@ func daemonSub(sub *Process) { log.Warningf("Catch process %s exit", sub.name) time.Sleep(3 * time.Second) // delay 3s -> try to restart if !exitFlag { - sub.Run(true) + sub.Run(true, sub.env) log.Infof("Process %s restart success", sub.name) daemonSub(sub) } diff --git a/cmd/process/main.go b/cmd/process/main.go index adc04d0..dcead77 100644 --- a/cmd/process/main.go +++ b/cmd/process/main.go @@ -9,6 +9,7 @@ import ( type Process struct { name string + env []string command []string process *exec.Cmd } @@ -21,12 +22,17 @@ func New(command ...string) *Process { return process } -func (p *Process) Run(isOutput bool) { +func (p *Process) Run(isOutput bool, env []string) { p.process = exec.Command(p.command[0], p.command[1:]...) if isOutput { p.process.Stdout = os.Stdout p.process.Stderr = os.Stderr } + p.env = env + if len(p.env) != 0 { + p.process.Env = p.env + log.Infof("Process %s with env -> %v", p.name, p.env) + } err := p.process.Start() if err != nil { log.Errorf("Failed to start %s -> %v", p.name, err)