From 1e060c9ab361c3e263ade008ad7e50ccfd3ad1b3 Mon Sep 17 00:00:00 2001 From: Sonya Date: Wed, 12 Apr 2023 20:58:34 +0330 Subject: [PATCH] fixed zombie process leak revert config & result dir to current executable binary --- golang/configuration/config.go | 4 ++-- golang/main.go | 2 +- golang/scanner/scanner.go | 32 +++++++++++++++++++++----------- golang/speedtest/fronting.go | 8 +++++++- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/golang/configuration/config.go b/golang/configuration/config.go index 0caead48..ef98f97d 100644 --- a/golang/configuration/config.go +++ b/golang/configuration/config.go @@ -15,8 +15,8 @@ import ( var ( PROGRAMDIR = filepath.Dir(os.Args[0]) BIN = filepath.Join(PROGRAMDIR, "..", "bin", "v2ray") - DIR = filepath.Join(PROGRAMDIR, "..", "config") - RESULTDIR = filepath.Join(PROGRAMDIR, "..", "result") + DIR = filepath.Join(PROGRAMDIR, "config") + RESULTDIR = filepath.Join(PROGRAMDIR, "result") StartDtStr = time.Now().Format("2006-01-02_15:04:05") CSVInterimResultsPath = filepath.Join(RESULTDIR, StartDtStr+"_result.csv") JSONInterimResultsPath = filepath.Join(RESULTDIR, StartDtStr+"_result.json") diff --git a/golang/main.go b/golang/main.go index 3b21e38f..6f1a1d68 100644 --- a/golang/main.go +++ b/golang/main.go @@ -9,7 +9,7 @@ import ( // Program Info var ( - version = "1.4" + version = "1.5" build = "Custom" codename = "CFScanner , CloudFlare Scanner." ) diff --git a/golang/scanner/scanner.go b/golang/scanner/scanner.go index 0995dffb..7a15fbd5 100644 --- a/golang/scanner/scanner.go +++ b/golang/scanner/scanner.go @@ -15,6 +15,7 @@ import ( "strconv" "strings" "sync" + "syscall" "time" ) @@ -46,7 +47,6 @@ var ( ) // const WorkerCount = 48 - func scanner(ip string, C config.Configuration, Worker config.Worker) *Result { result := &Result{ @@ -64,29 +64,37 @@ func scanner(ip string, C config.Configuration, Worker config.Worker) *Result { var err error process, proxies, err = v2raysvc.StartV2RayService(v2rayConfigPath, time.Duration(Worker.StartProcessTimeout)) if err != nil { - log.Printf("%vERROR - %vCould not start v2ray service%v\n", + log.Printf("%vERROR - %vCould not start vpn service%v\n", utils.Colors.FAIL, utils.Colors.WARNING, utils.Colors.ENDC) log.Fatal(err) return nil } - defer func(Process *os.Process) { - err := Process.Kill() - if err != nil { - _ = fmt.Errorf("could not kill the process %v", process.Process.Pid) - } - }(process.Process) defer func() { - if r := recover(); r != nil { - log.Printf("%sFAIL %v%15s Panic: %v%v\n", utils.Colors.FAIL, utils.Colors.WARNING, ip, r, utils.Colors.ENDC) + if process != nil && process.Process != nil { + // terminate process + err = process.Process.Kill() + if err != nil { + log.Printf("%vERROR - %vFailed to kill process%v\n", + utils.Colors.FAIL, utils.Colors.WARNING, utils.Colors.ENDC) + } + + // using Wait for clean up zombie process after Kill func + process.Process.Signal(syscall.SIGCHLD) + _, err := process.Process.Wait() + if err != nil { + log.Printf("%vERROR - %vFailed to wait for process to exit%v\n", + utils.Colors.FAIL, utils.Colors.WARNING, utils.Colors.ENDC) + } } }() } for tryIdx := 0; tryIdx < C.Config.NTries; tryIdx++ { // Fronting test + if C.Config.DoFrontingTest { - fronting := speedtest.FrontingTest(ip, time.Duration(C.Config.FrontingTimeout)) + fronting := speedtest.FrontingTest(ip, proxies, time.Duration(C.Config.FrontingTimeout)) if !fronting { return nil @@ -114,6 +122,7 @@ func scanner(ip string, C config.Configuration, Worker config.Worker) *Result { return result } + func uploader(ip string, Upload *config.Upload, proxies map[string]string, result *Result) (*Result, bool) { var err error nBytes := Upload.MinUlSpeed * 1000 * Upload.MaxUlTime @@ -180,6 +189,7 @@ func downloader(ip string, Download *config.Download, proxies map[string]string, func scan(C *config.Configuration, worker *config.Worker, ip string) { res := scanner(ip, *C, *worker) + if res == nil { return } diff --git a/golang/speedtest/fronting.go b/golang/speedtest/fronting.go index 27a92be2..68498e94 100644 --- a/golang/speedtest/fronting.go +++ b/golang/speedtest/fronting.go @@ -13,7 +13,7 @@ import ( ) // FrontingTest conducts a fronting test on an ip and return true if status 200 is received -func FrontingTest(ip string, timeout time.Duration) bool { +func FrontingTest(ip string, proxies map[string]string, timeout time.Duration) bool { var success = false @@ -30,6 +30,12 @@ func FrontingTest(ip string, timeout time.Duration) bool { return false } req.Host = "speed.cloudflare.com" + + // set proxies to req header + for k, v := range proxies { + req.Header.Set(k, v) + } + client := &http.Client{ Timeout: timeout * time.Second, Transport: &http.Transport{