-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-port.go
126 lines (108 loc) · 2.5 KB
/
check-port.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bufio"
"context"
"flag"
"fmt"
"net"
"os"
"sync"
"time"
"modular-netstalking/lib"
)
var (
port string
workers int
timeout time.Duration
)
func init() {
flag.StringVar(&port, "p", "", "port")
flag.IntVar(&workers, "w", 256, "workers count")
flag.DurationVar(&timeout, "t", 750*time.Millisecond, "connection timeout")
flag.Uint64Var(&lib.OutputCount, "c", 0, "count of IPs to gather (0 = infinite)")
}
func CheckPort(host string, toGatherIPsChan chan<- lib.HostInfo) {
if conn, err := net.DialTimeout("tcp", host, timeout); err == nil {
conn.Close()
toGatherIPsChan <- lib.NewHostInfo(host)
}
}
func Work(toCheckIPsChan <-chan string, toGatherIPsChan chan<- lib.HostInfo, wg *sync.WaitGroup) {
defer wg.Done()
for {
if host, ok := <-toCheckIPsChan; ok {
CheckPort(host, toGatherIPsChan)
continue
}
return
}
}
func Gather(toGatherIPsChan <-chan lib.HostInfo, wg *sync.WaitGroup) {
defer wg.Done()
limited := lib.OutputCount > 0
rest := lib.OutputCount
for {
ip, ok := <-toGatherIPsChan
if !ok {
break
}
fmt.Println(ip.String(!lib.GreppableOutput))
if limited {
rest--
if rest == 0 {
break
}
}
}
}
func ReadStdin(toCheckIPsChan chan<- string, ctx context.Context) {
scanner := bufio.NewScanner(os.Stdin)
noPortSpecified := port == ""
if noPortSpecified {
scanner.Scan()
host := scanner.Text()
if _, _, err := net.SplitHostPort(host); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(255)
}
toCheckIPsChan <- host
}
for scanner.Scan() {
select {
case <-ctx.Done():
return
default:
if noPortSpecified {
toCheckIPsChan <- scanner.Text()
} else {
toCheckIPsChan <- net.JoinHostPort(scanner.Text(), port)
}
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
close(toCheckIPsChan)
}
func RunWorkers(toCheckIPsChan <-chan string, toGatherIPsChan chan<- lib.HostInfo) {
var wg_workers sync.WaitGroup
for i := 0; i < workers; i++ {
wg_workers.Add(1)
go Work(toCheckIPsChan, toGatherIPsChan, &wg_workers)
}
wg_workers.Wait()
close(toGatherIPsChan)
}
func main() {
var wgGather sync.WaitGroup
flag.Parse()
toCheckIPsChan := make(chan string, workers*2)
toGatherIPsChan := make(chan lib.HostInfo)
readerCtx, readerCancel := context.WithCancel(context.Background())
wgGather.Add(1)
go Gather(toGatherIPsChan, &wgGather)
go RunWorkers(toCheckIPsChan, toGatherIPsChan)
go ReadStdin(toCheckIPsChan, readerCtx)
wgGather.Wait()
readerCancel()
}