Skip to content

Commit

Permalink
Merge pull request #416 from MortezaBashsiz/golang-v1.5-fix-zombie-le…
Browse files Browse the repository at this point in the history
…akproc

fixed zombie process leak
  • Loading branch information
SonyaCore authored Apr 12, 2023
2 parents 5c76a18 + 1e060c9 commit f018d6e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions golang/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// Program Info
var (
version = "1.4"
version = "1.5"
build = "Custom"
codename = "CFScanner , CloudFlare Scanner."
)
Expand Down
32 changes: 21 additions & 11 deletions golang/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strconv"
"strings"
"sync"
"syscall"
"time"
)

Expand Down Expand Up @@ -46,7 +47,6 @@ var (
)

// const WorkerCount = 48

func scanner(ip string, C config.Configuration, Worker config.Worker) *Result {

result := &Result{
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion golang/speedtest/fronting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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{
Expand Down

0 comments on commit f018d6e

Please sign in to comment.