forked from nxenon/port-scanner-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.go
60 lines (48 loc) · 1.13 KB
/
scanner.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
package main
import (
"context"
"fmt"
"net"
"strings"
"time"
)
type scanner struct {
ip string
port int
timeout time.Duration
debug bool
}
func (s *scanner) scan(ctx context.Context) error {
d := net.Dialer{Timeout: s.timeout}
address := fmt.Sprintf(`%s:%d`, s.ip, s.port)
if s.debug {
fmt.Printf("scanning %s ...\n", address)
}
_, err := d.Dial("tcp", address)
if err != nil {
if addrError, ok := err.(*net.AddrError); ok {
if addrError.Timeout() {
return nil
}
} else if opError, ok := err.(*net.OpError); ok {
// handle lacked sufficient buffer space error
if strings.TrimSpace(opError.Err.Error()) == "bind: An operation on a socket could not be performed because "+
"the system lacked sufficient buffer space or because a queue was full." {
time.Sleep(s.timeout + (3 * time.Second))
_, errAe := d.Dial("tcp", address)
if errAe != nil {
if addErr, ok := err.(*net.AddrError); ok {
if addErr.Timeout() {
return nil
}
}
}
}
} else {
return err
}
return nil
}
fmt.Printf("[+] Port %15s %5d/TCP is open\n", s.ip, s.port)
return nil
}