-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_check.go
92 lines (87 loc) · 2.13 KB
/
port_check.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
package main
import (
"fmt"
"net"
"net/url"
"strconv"
"strings"
"time"
)
func portCheck() int {
ports := make(map[string]*url.URL)
var err error
for _, serviceString := range servicesRaw {
uri, _ := url.Parse("noname://" + host + ":1")
ax.FatalIfError(err, "internal error: could not parse definition")
if strings.Contains(serviceString, "://") {
// This is the URI variant for the port definition
uri, err = url.Parse(serviceString)
ax.FatalIfError(err, "could not parse port definition")
} else {
var frag string
var parts []string
parts = strings.SplitN(serviceString, ":", 3)
if len(parts) == 3 {
_, err = strconv.Atoi(strings.TrimSuffix(strings.TrimSuffix(parts[2], "-udp"), "-tcp"))
ax.FatalIfError(err, "could not parse port information")
uri.Scheme = parts[0]
frag = parts[1] + ":" + parts[2]
} else if len(parts) == 2 {
_, err = strconv.Atoi(strings.TrimSuffix(strings.TrimSuffix(parts[1], "-udp"), "-tcp"))
ax.FatalIfError(err, "could not parse port information")
uri.Scheme = parts[0]
frag = parts[1]
} else {
_, err = strconv.Atoi(strings.TrimSuffix(strings.TrimSuffix(parts[0], "-udp"), "-tcp"))
ax.FatalIfError(err, "could not parse port as int")
uri.Scheme = parts[0]
frag = parts[0]
}
if !strings.Contains(frag, ":") {
frag = net.JoinHostPort(host, frag)
}
uri.Host = frag
}
if _, ok := ports[uri.String()]; ok {
ax.Fatalf("duplicate check %q", uri.String())
}
ports[uri.String()] = uri
}
startTime := time.Now()
for {
results := tcpChecks(ports)
allSuccess := true
startCheck := time.Now()
for port, resp := range results {
if resp != "found" && resp != "verified" {
allSuccess = false
} else {
delete(ports, port)
}
if !quiet {
if dots {
fmt.Print(".")
} else {
fmt.Printf("%s: %s\n", port, resp)
}
}
}
if allSuccess {
if !quiet && dots {
fmt.Println()
}
break
}
if time.Since(startTime) > wait {
if !quiet && dots {
fmt.Println()
}
return 1
}
since := time.Since(startCheck)
if since < delay {
time.Sleep(delay - since)
}
}
return 0
}