-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
77 lines (72 loc) · 1.78 KB
/
client.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
package main
import (
"errors"
"fmt"
"net"
"sync"
"time"
)
type clientConfig struct {
host string
portRangeStart uint16
portRangeEnd uint16
networks []network
waitTime time.Duration
parallelisation uint
}
type triple struct {
network network
host string
port int
}
func run(config *clientConfig) (map[network][]int, error) {
failedPorts := make(map[network][]int)
if config.portRangeEnd < config.portRangeStart {
return nil, errors.New(fmt.Sprintf("Port range is invalid."))
}
if config.networks == nil || len(config.networks) == 0 {
return nil, errors.New("no network types provided")
}
channel := make(chan triple)
wg := sync.WaitGroup{}
for i := uint(0); i < config.parallelisation; i++ {
wg.Add(1)
go func() {
for triple := range channel {
isSuccessful := check(triple.network, triple.host, triple.port, config.waitTime)
if isSuccessful {
continue
}
failedPorts[triple.network] = append(failedPorts[triple.network], triple.port)
}
wg.Done()
}()
}
for _, network := range config.networks {
for port := config.portRangeStart; port <= config.portRangeEnd; port++ {
channel <- triple{
network: network,
host: config.host,
port: int(port),
}
}
}
close(channel)
wg.Wait()
return failedPorts, nil
}
func check(network network, host string, port int, waitTime time.Duration) bool {
conn, _ := net.Dial(string(network), fmt.Sprintf("%s:%d", host, port))
_ = conn.SetDeadline(time.Now().Add(waitTime))
_, _ = conn.Write(PING)
buf := make([]byte, len(PONG))
_, err := conn.Read(buf)
if err != nil {
log.Info("Error while reading server response", err)
}
if string(buf) != string(PONG) {
log.Info("Incorrect server response", string(buf), len(buf))
return false
}
return true
}