-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
78 lines (67 loc) · 1.77 KB
/
Copy pathcli.go
File metadata and controls
78 lines (67 loc) · 1.77 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
package proxycheck
import (
"fmt"
cli "github.com/urfave/cli/v2"
"os"
"strings"
"sync"
)
// NewApp builds the proxycheck CLI application. It is shared by the cmd binary
// and the tests so both exercise the same flag wiring.
func NewApp() *cli.App {
return &cli.App{
Name: "proxycheck",
Description: "Proxy checker tool",
Version: "0.0.6",
Flags: []cli.Flag{
&cli.StringFlag{Name: "judge", Usage: "Set judge", Value: "proxyjudge.us"},
&cli.IntFlag{Name: "threads", Usage: "Count of threads", Value: 10},
},
Action: Action,
}
}
func Action(c *cli.Context) error {
// Resolve the judge from the --judge flag before doing any work; an unknown
// name aborts here, so no proxy is checked.
judge, err := ResolveJudge(c.String("judge"))
if err != nil {
return err
}
// If pass args, use it or read stdin as input file with proxies
var feed Feed
if c.Args().Len() > 0 {
feed = NewSliceFeed(c.Args().Slice())
} else {
feed = NewFileFeed(os.Stdin)
}
// Worker pool
poolSize := c.Int("threads")
proxyAddrs := make(chan string, poolSize)
var wg sync.WaitGroup
for i := 0; i < poolSize; i++ {
wg.Add(1)
go func(proxyAddrs chan string) {
defer wg.Done()
for proxyAddr := range proxyAddrs {
if res := Check(proxyAddr, judge); res.Online {
fmt.Printf("%s\t%s\t%s\n", proxyAddr, strings.Join(res.Protocols, ","), res.Speed.String())
} else {
fmt.Fprintf(os.Stderr, "invalid proxy %s: %v\n", proxyAddr, res.Err)
}
}
}(proxyAddrs)
}
// Start one thread proxy check
for {
proxyAddr, err := feed.Next()
if err == FeedEnd {
break
}
proxyAddrs <- proxyAddr
}
// Closing the channel lets the workers' range loops finish so wg.Wait
// returns once the feed is exhausted.
close(proxyAddrs)
wg.Wait()
return nil
}