From f2f7c5f20d4b6a4d410c96a67ac8c19ecc31aaef Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Thu, 18 Aug 2022 02:31:41 +0800 Subject: [PATCH] update: daemon of xray --- cmd/network/main.go | 5 ++++- cmd/xproxy.go | 52 ++++++++++++++++++++++++++++----------------- src/main.go | 21 ------------------ src/process.go | 24 --------------------- 4 files changed, 36 insertions(+), 66 deletions(-) delete mode 100644 src/main.go delete mode 100644 src/process.go diff --git a/cmd/network/main.go b/cmd/network/main.go index a57cd57..6c5fff8 100644 --- a/cmd/network/main.go +++ b/cmd/network/main.go @@ -2,6 +2,7 @@ package network import ( "XProxy/cmd/common" + log "github.com/sirupsen/logrus" "time" ) @@ -16,11 +17,13 @@ type Config struct { var run = common.RunCommand func Load(dns []string, ipv4 Config, ipv6 Config) { + var delay time.Duration = 1 loadDns(dns) // init dns server flushNetwork() // clear network settings loadV4Network(ipv4) loadV6Network(ipv6) - time.Sleep(time.Second) // wait 1s for ipv6 (ND protocol) + log.Infof("Wait %ds for IPv6 setting up", delay) + time.Sleep(delay * time.Second) // wait for ipv6 setting up (ND protocol) loadV4TProxy(ipv4, getV4Cidr()) loadV6TProxy(ipv6, getV6Cidr()) } diff --git a/cmd/xproxy.go b/cmd/xproxy.go index 189e070..b30d93a 100644 --- a/cmd/xproxy.go +++ b/cmd/xproxy.go @@ -1,10 +1,13 @@ package main import ( + "XProxy/cmd/config" "XProxy/cmd/process" "fmt" log "github.com/sirupsen/logrus" - "time" + "os" + "os/signal" + "syscall" ) var version = "dev" @@ -20,29 +23,38 @@ var assetFile = "/assets.tar.xz" var assetDir = exposeDir + "/assets" var configFile = exposeDir + "/config.yml" -func main() { - log.SetLevel(log.DebugLevel) - fmt.Println("XProxy start -> version =", version) +var subProcess []*process.Process - //settings := config.Load(configFile) - //loadNetwork(&settings) - //loadProxy(&settings) - //loadAsset(&settings) - //runScript(&settings) - - xray := process.New("xray", "-confdir", configDir) - xray.Run(true) - xray.Daemon() +func runProxy() { + proxy := process.New("xray", "-confdir", configDir) + proxy.Run(true) + proxy.Daemon() + subProcess = append(subProcess, proxy) +} - sleep := process.New("sleep", "1001") - sleep.Run(true) - sleep.Daemon() +func blockWait() { + sigExit := make(chan os.Signal, 1) + signal.Notify(sigExit, syscall.SIGINT, syscall.SIGTERM) // wait until get exit signal + <-sigExit +} - empty := process.New("empty") - empty.Daemon() +func main() { + defer func() { + if err := recover(); err != nil { + log.Errorf("Unknown error -> %v", err) + } + }() - time.Sleep(5 * time.Second) + log.SetLevel(log.DebugLevel) + fmt.Println("XProxy start -> version =", version) - process.Exit(xray, sleep, empty) + settings := config.Load(configFile) + loadNetwork(&settings) + loadProxy(&settings) + loadAsset(&settings) + runScript(&settings) + runProxy() + blockWait() + process.Exit(subProcess...) } diff --git a/src/main.go b/src/main.go deleted file mode 100644 index 9e46b00..0000000 --- a/src/main.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - log "github.com/sirupsen/logrus" - "os" - "os/signal" - "syscall" -) - -func main() { - defer func() { - if err := recover(); err != nil { - log.Errorf("Unknown error -> %v", err) - } - }() - - sigExit := make(chan os.Signal, 1) - signal.Notify(sigExit, syscall.SIGINT, syscall.SIGTERM) - <-sigExit - exit() -} diff --git a/src/process.go b/src/process.go deleted file mode 100644 index 0e3139a..0000000 --- a/src/process.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import ( - "os/exec" -) - -var subProcess []*Process - -func killSub(sub *Process) { - defer func() { - recover() - }() - -} - -func waitSub(sub *exec.Cmd) { - defer func() { - recover() - }() -} - -func exit() { - -}