6
6
"flag"
7
7
"fmt"
8
8
"os"
9
- "regexp"
10
9
"sync"
11
10
12
11
"github.com/octoman90/proxyshiva/inputParser"
@@ -15,69 +14,58 @@ import (
15
14
16
15
var wg sync.WaitGroup
17
16
18
- func validateScanned (str string ) bool {
19
- ipRegex := `((?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))`
20
- protocolRegex := `(http|https|socks4|socks5)`
21
- portRegex := `\d+`
22
-
23
- regex := regexp .MustCompile (`` +
24
- // Match protocol or protocol list
25
- `^(` + protocolRegex + `(,` + protocolRegex + `)*)+` +
26
-
27
- // Match "://"
28
- `:\/\/` +
29
-
30
- // Match IP or IP range
31
- ipRegex + `(-` + ipRegex + `)?` +
32
-
33
- // Match ":"
34
- `:` +
17
+ func worker (taskQueue <- chan proxy.Proxy , resultQueue chan <- * proxy.Proxy , timeout * int , skipCert * bool ) {
18
+ for proxy := range taskQueue {
19
+ proxy .Check (resultQueue , timeout , skipCert )
20
+ }
21
+ }
35
22
36
- // Match port or port range
37
- `(` + portRegex + `)(-` + portRegex + `)?$` ,
38
- )
23
+ func printer (resultQueue <- chan * proxy.Proxy , JSON * bool ) {
24
+ for result := range resultQueue {
25
+ if * JSON { // Print out every result in JSON format
26
+ jr , _ := json .Marshal (* result )
27
+ fmt .Println (string (jr ))
28
+ } else if result .Good { // Print out good proxies in short format
29
+ fmt .Printf ("%v://%v:%v\n " , result .Scheme , result .Address , result .Port )
30
+ }
39
31
40
- return regex .MatchString (str )
32
+ wg .Done ()
33
+ }
41
34
}
42
35
43
36
func main () {
37
+ // Parse flags
44
38
flagJSON := flag .Bool ("json" , false , "Output full data in JSON format" )
45
39
flagInteractive := flag .Bool ("interactive" , false , "Don't exit after completing the task and wait for more input" )
46
40
flagSkipCert := flag .Bool ("skipcert" , false , "Skip the TLS certificate verification" )
47
41
flagTimeout := flag .Int ("timeout" , 15 , "Request timeout in seconds" )
48
42
flagSkipRes := flag .Bool ("skipres" , false , "Skip reserved IP addresses" )
43
+ flagParallel := flag .Int ("parallel" , 100 , "How many requests to make simultaneously" )
49
44
flag .Parse ()
50
45
51
- resultQueue := make (chan * proxy.Proxy )
46
+ // Create communication queues
47
+ taskQueue := make (chan proxy.Proxy , 100 )
48
+ resultQueue := make (chan * proxy.Proxy , 100 )
49
+ defer close (taskQueue )
52
50
defer close (resultQueue )
53
51
54
- // Receive and print out completed checks
55
- go func () {
56
- for result := range resultQueue {
57
- if * flagJSON { // Print out every result in JSON format
58
- jr , _ := json .Marshal (* result )
59
- fmt .Println (string (jr ))
60
- } else if result .Good { // Print out good proxies in short format
61
- fmt .Printf ("%v://%v:%v\n " , result .Scheme , result .Address , result .Port )
62
- }
52
+ // Spawn a printer goroutine
53
+ go printer (resultQueue , flagJSON )
63
54
64
- wg .Done ()
65
- }
66
- }()
55
+ // Spawn worker goroutines
56
+ for i := 0 ; i < * flagParallel ; i ++ {
57
+ go worker (taskQueue , resultQueue , flagTimeout , flagSkipCert )
58
+ }
67
59
68
60
// Scan for input
69
61
scanner := bufio .NewScanner (os .Stdin )
70
62
for {
71
63
if scanner .Scan () {
72
- scanned := scanner .Text ()
73
- if valid := validateScanned (scanned ); ! valid {
74
- continue
75
- }
76
-
77
- for proxy := range inputParser .RequestGenerator (scanned ) {
64
+ for proxy := range inputParser .RequestGenerator (scanner .Text ()) {
65
+ // Copy tasks from individual input channels to a single task queue
78
66
if ! * flagSkipRes || ! proxy .IsReserved () {
79
67
wg .Add (1 )
80
- go proxy . Check ( resultQueue , flagTimeout , flagSkipCert )
68
+ taskQueue <- proxy
81
69
}
82
70
}
83
71
}
0 commit comments