-
Notifications
You must be signed in to change notification settings - Fork 42
/
ping.go
237 lines (208 loc) · 5.7 KB
/
ping.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package yaklib
import (
"context"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/netx"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/utils/hostsparser"
"github.com/yaklang/yaklang/common/utils/network"
"github.com/yaklang/yaklang/common/utils/pingutil"
"strings"
"time"
)
type _pingConfig struct {
dnsTimeout time.Duration
timeout time.Duration
dnsServers []string
scanCClass bool
concurrent int
skipped bool
tcpPingPort string
proxies []string
excludeHostsFilter *hostsparser.HostsParser
_cancel func()
_onResult func(result *pingutil.PingResult)
}
type _pingConfigOpt func(config *_pingConfig)
func _pingConfigOpt_skipped(i bool) _pingConfigOpt {
return func(config *_pingConfig) {
config.skipped = i
}
}
func _pingConfigOpt_tcpPingPorts(i string) _pingConfigOpt {
return func(c *_pingConfig) {
c.tcpPingPort = i
}
}
func _pingConfigOpt_proxy(i ...string) _pingConfigOpt {
return func(config *_pingConfig) {
if len(utils.StringArrayFilterEmpty(i)) <= 0 {
return
}
config.proxies = i
}
}
func _pingConfigOpt_withDNSTimeout(i float64) _pingConfigOpt {
return func(config *_pingConfig) {
config.dnsTimeout = utils.FloatSecondDuration(i)
}
}
func _pingConfigOpt_withTimeout(i float64) _pingConfigOpt {
return func(config *_pingConfig) {
config.timeout = utils.FloatSecondDuration(i)
}
}
func _pingConfigOpt_dnsServers(i ...string) _pingConfigOpt {
return func(config *_pingConfig) {
config.dnsServers = i
}
}
func _pingConfigOpt_concurrent(i int) _pingConfigOpt {
return func(config *_pingConfig) {
config.concurrent = i
}
}
func _pingConfigOpt_scanCClass(i bool) _pingConfigOpt {
return func(config *_pingConfig) {
config.scanCClass = i
}
}
func _pingConfigOpt_cancel(f func()) _pingConfigOpt {
return func(config *_pingConfig) {
config._cancel = f
}
}
func _pingScan(target string, opts ..._pingConfigOpt) chan *pingutil.PingResult {
config := &_pingConfig{
dnsTimeout: 5 * time.Second,
timeout: 5 * time.Second,
scanCClass: false,
concurrent: 50,
}
for _, r := range opts {
r(config)
}
if config.scanCClass {
target = network.ParseStringToCClassHosts(target)
}
ctx, cancel := context.WithCancel(context.Background())
config._cancel = cancel
opts = append(opts, _pingConfigOpt_cancel(config._cancel))
var resultChan = make(chan *pingutil.PingResult)
go func() {
defer close(resultChan)
swg := utils.NewSizedWaitGroup(config.concurrent)
for _, hostRaw := range utils.ParseStringToHosts(target) {
hostOrigin := hostRaw
host := utils.ExtractHost(hostRaw)
err := swg.AddWithContext(ctx)
if err != nil {
log.Error("cancel pingscan from context")
return
}
targetHost := host
go func() {
defer swg.Done()
if config.skipped || config.IsFiltered(targetHost) {
resultChan <- &pingutil.PingResult{
IP: hostOrigin,
Ok: true,
Reason: "skipped",
}
return
}
result := _ping(targetHost, opts...)
//if utils.MatchAnyOfRegexp(result.Reason, "(?i)operation not permitted") {
// // 权限不足
// cancel()
//}
if config._onResult != nil {
config._onResult(result)
}
if result != nil {
if result.IP != hostOrigin {
result.IP = hostOrigin
}
resultChan <- result
}
}()
}
swg.Wait()
}()
return resultChan
}
func _ping(target string, opts ..._pingConfigOpt) *pingutil.PingResult {
config := &_pingConfig{
dnsTimeout: time.Second * 5,
timeout: 5 * time.Second,
proxies: []string{},
}
for _, r := range opts {
r(config)
}
if utils.IsIPv4(target) || utils.IsIPv6(target) {
return pingutil.PingAuto(target, config.tcpPingPort, config.timeout, config.proxies...)
} else if strings.HasPrefix(
target, "https://") || strings.HasPrefix(
target, "http://") {
result, _, err := utils.ParseStringToHostPort(target)
if err != nil {
return &pingutil.PingResult{
IP: target,
Ok: false,
RTT: 0,
Reason: utils.Errorf("parse host[%s] from url failed: %s", target, err.Error()).Error(),
}
}
return _ping(result, opts...)
} else {
result := netx.LookupFirst(target, netx.WithTimeout(config.dnsTimeout), netx.WithDNSServers(config.dnsServers...))
if result != "" && (utils.IsIPv4(result) || utils.IsIPv6(result)) {
return pingutil.PingAuto(result, config.tcpPingPort, config.timeout, config.proxies...)
}
return &pingutil.PingResult{
IP: target,
Ok: false,
RTT: 0,
Reason: utils.Errorf("parse/dns [%s] to ip failed", target).Error(),
}
}
}
func _pingConfigOpt_onResult(i func(result *pingutil.PingResult)) _pingConfigOpt {
return func(config *_pingConfig) {
config._onResult = i
}
}
func _pingConfigOpt_excludeHosts(host string) _pingConfigOpt {
return func(config *_pingConfig) {
if host == "" {
return
}
config.excludeHostsFilter = hostsparser.NewHostsParser(context.Background(), host)
}
}
func (c *_pingConfig) IsFiltered(host string) bool {
if c == nil {
return false
}
if c.excludeHostsFilter != nil {
if c.excludeHostsFilter.Contains(host) {
return true
}
}
return false
}
var PingExports = map[string]interface{}{
"Scan": _pingScan,
"Ping": _ping,
"excludeHosts": _pingConfigOpt_excludeHosts,
"onResult": _pingConfigOpt_onResult,
"dnsTimeout": _pingConfigOpt_withDNSTimeout,
"timeout": _pingConfigOpt_withTimeout,
"dnsServers": _pingConfigOpt_dnsServers,
"scanCClass": _pingConfigOpt_scanCClass,
"skip": _pingConfigOpt_skipped,
"concurrent": _pingConfigOpt_concurrent,
"tcpPingPorts": _pingConfigOpt_tcpPingPorts,
"proxy": _pingConfigOpt_proxy,
}