-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (64 loc) · 1.27 KB
/
main.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
package main
import (
"flag"
"fmt"
"net"
"os"
"time"
)
func main() {
hostPtr := flag.String("host", "", "hostname")
portPtr := flag.Uint64("port", 0, "port")
timeoutPtr := flag.Float64("timeout", 1, "timeout")
retriesPtr := flag.Int("retries", 3, "retries")
sleepPtr := flag.Float64("sleep", 1, "sleep")
flag.Parse()
host := *hostPtr
port := *portPtr
timeout := *timeoutPtr
retries := *retriesPtr
sleep := *sleepPtr
// Sanity check
if timeout < 0.1 {
timeout = 0.1
}
if sleep < 0.5 {
sleep = 0.5
}
if retries < 1 {
retries = 1
}
if host == "" || port == 0 {
os.Exit(1)
}
if probePort(host, port, timeout, retries, sleep) {
os.Exit(0)
} else {
os.Exit(1)
}
}
func scanPort(host string, port uint64, timeout float64) bool {
d := &net.Dialer{Timeout: time.Duration(uint64(timeout*1000)) * time.Millisecond}
conn, _ := d.Dial("tcp", fmt.Sprintf("%v:%v", host, port))
if conn != nil {
conn.Close()
return true
} else {
return false
}
}
func probePort(host string, port uint64, timeout float64, retries int, sleep float64) bool {
i := 0
for {
if scanPort(host, port, timeout) {
return true
} else {
i++
if i == retries {
return false
}
time.Sleep(time.Duration(uint64(sleep*1000)) * time.Millisecond)
}
}
return false
}