-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (81 loc) · 1.62 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"flag"
"fmt"
"net"
"sync"
"time"
"golang.org/x/net/proxy"
)
const (
defaultAddress = "127.0.0.1"
)
var (
useProxy *bool
port *uint64
ipAddress string
)
func init() {
useProxy = flag.Bool("proxy", false, "use proxy ...")
port = flag.Uint64("port", 0, "port ...")
}
func main() {
flag.Parse()
ipAddress = IF[string](flag.Arg(0) == "", "127.0.0.1", flag.Arg(0))
fmt.Println("Ip Address : ", ipAddress)
ctx := context.TODO()
Scanner(ctx, &scanner{
IP: ipAddress,
UseProxy: *useProxy,
Port: *port,
})
}
// scanner d
type scanner struct {
IP string
UseProxy bool
Port uint64
}
// Scanner doc
func Scanner(ctx context.Context, sc *scanner) {
var begin = time.Now()
//wg
var wg sync.WaitGroup
do := func(address string, showClose bool) {
//释放wg
defer wg.Done()
var conn net.Conn
var err error
// 请求
if sc.UseProxy {
conn, err = proxy.Dial(ctx, "tcp4", address)
} else {
// conn, err = net.DialTimeout("tcp", address, time.Second*10)
conn, err = net.Dial("tcp4", address)
}
if err != nil {
if showClose {
fmt.Println(address, "关闭")
}
return
}
defer conn.Close()
fmt.Println(IF[string](conn.RemoteAddr().String() == address, "", "["+conn.RemoteAddr().String()+"]"), address, "打开")
}
if sc.Port == 0 {
//循环
for j := 21; j <= 65535; j++ {
//添加wg
wg.Add(1)
go do(fmt.Sprintf("%s:%d", sc.IP, j), false)
}
} else {
wg.Add(1)
go do(fmt.Sprintf("%s:%d", sc.IP, sc.Port), true)
}
//等待wg
wg.Wait()
var elapseTime = time.Since(begin)
fmt.Println("耗时:", elapseTime)
}