Skip to content

Commit 299a570

Browse files
authored
Merge pull request #4 from octoman90/workerPool
2 parents c942306 + 916ecdd commit 299a570

File tree

3 files changed

+69
-49
lines changed

3 files changed

+69
-49
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ cat addresses.txt | ./proxyshiva
3737

3838
## Flags
3939

40-
| Flag | Description |
41-
| ------------ | ------------------------------------------------------------ |
42-
| -json | Output full data in JSON format |
43-
| -interactive | Don't exit after completing the task and wait for more input |
44-
| -skipcert | Skip the TLS certificate verification |
45-
| -skipres | Skip reserved IP addresses |
46-
| -timeout=15 | Request timeout in seconds (15 by default) |
40+
| Flag | Description |
41+
| ------------- | ------------------------------------------------------------ |
42+
| -json | Output full data in JSON format |
43+
| -interactive | Don't exit after completing the task and wait for more input |
44+
| -skipcert | Skip the TLS certificate verification |
45+
| -skipres | Skip reserved IP addresses |
46+
| -parallel=100 | How many requests to make simultaneously (100 by default) |
47+
| -timeout=15 | Request timeout in seconds (15 by default) |

inputParser/inputParser.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
11
package inputParser
22

33
import (
4+
"regexp"
45
"strconv"
56
"strings"
67

78
"github.com/octoman90/proxyshiva/proxy"
89
"inet.af/netaddr"
910
)
1011

12+
func validateScanned(str string) bool {
13+
ipRegex := `((?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))`
14+
protocolRegex := `(http|https|socks4|socks5)`
15+
portRegex := `\d+`
16+
17+
regex := regexp.MustCompile(`` +
18+
// Match protocol or protocol list
19+
`^(` + protocolRegex + `(,` + protocolRegex + `)*)+` +
20+
21+
// Match "://"
22+
`:\/\/` +
23+
24+
// Match IP or IP range
25+
ipRegex + `(-` + ipRegex + `)?` +
26+
27+
// Match ":"
28+
`:` +
29+
30+
// Match port or port range
31+
`(` + portRegex + `)(-` + portRegex + `)?$`,
32+
)
33+
34+
return regex.MatchString(str)
35+
}
36+
1137
func RequestGenerator(in string) chan proxy.Proxy {
1238
out := make(chan proxy.Proxy)
1339

40+
if !validateScanned(in) {
41+
defer close(out)
42+
return out
43+
}
44+
1445
schemeEndIndex := strings.Index(in, "://")
1546
addressStartIndex := schemeEndIndex + 3
1647
addressEndIndex := strings.LastIndex(in, ":")

main.go

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"flag"
77
"fmt"
88
"os"
9-
"regexp"
109
"sync"
1110

1211
"github.com/octoman90/proxyshiva/inputParser"
@@ -15,69 +14,58 @@ import (
1514

1615
var wg sync.WaitGroup
1716

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+
}
3522

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+
}
3931

40-
return regex.MatchString(str)
32+
wg.Done()
33+
}
4134
}
4235

4336
func main() {
37+
// Parse flags
4438
flagJSON := flag.Bool("json", false, "Output full data in JSON format")
4539
flagInteractive := flag.Bool("interactive", false, "Don't exit after completing the task and wait for more input")
4640
flagSkipCert := flag.Bool("skipcert", false, "Skip the TLS certificate verification")
4741
flagTimeout := flag.Int("timeout", 15, "Request timeout in seconds")
4842
flagSkipRes := flag.Bool("skipres", false, "Skip reserved IP addresses")
43+
flagParallel := flag.Int("parallel", 100, "How many requests to make simultaneously")
4944
flag.Parse()
5045

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)
5250
defer close(resultQueue)
5351

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)
6354

64-
wg.Done()
65-
}
66-
}()
55+
// Spawn worker goroutines
56+
for i := 0; i < *flagParallel; i++ {
57+
go worker(taskQueue, resultQueue, flagTimeout, flagSkipCert)
58+
}
6759

6860
// Scan for input
6961
scanner := bufio.NewScanner(os.Stdin)
7062
for {
7163
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
7866
if !*flagSkipRes || !proxy.IsReserved() {
7967
wg.Add(1)
80-
go proxy.Check(resultQueue, flagTimeout, flagSkipCert)
68+
taskQueue <- proxy
8169
}
8270
}
8371
}

0 commit comments

Comments
 (0)