forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcp.go
119 lines (102 loc) · 2.38 KB
/
tcp.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
package tcp
import (
"fmt"
"net/url"
"strconv"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/heartbeat/monitors"
)
func init() {
monitors.RegisterActive("tcp", create)
}
var debugf = logp.MakeDebug("tcp")
type connURL struct {
Scheme string
Host string
Ports []uint16
}
func create(
info monitors.Info,
cfg *common.Config,
) ([]monitors.Job, error) {
config := DefaultConfig
if err := cfg.Unpack(&config); err != nil {
return nil, err
}
tls, err := outputs.LoadTLSConfig(config.TLS)
if err != nil {
return nil, err
}
defaultScheme := "tcp"
if tls != nil {
defaultScheme = "ssl"
}
addrs, err := collectHosts(&config, defaultScheme)
if err != nil {
return nil, err
}
if config.Socks5.URL != "" && !config.Socks5.LocalResolve {
var jobs []monitors.Job
for _, addr := range addrs {
scheme, host := addr.Scheme, addr.Host
for _, port := range addr.Ports {
job, err := newTCPMonitorHostJob(scheme, host, port, tls, &config)
if err != nil {
return nil, err
}
jobs = append(jobs, job)
}
}
return jobs, nil
}
jobs := make([]monitors.Job, len(addrs))
for i, addr := range addrs {
jobs[i], err = newTCPMonitorIPsJob(addr, tls, &config)
if err != nil {
return nil, err
}
}
return jobs, nil
}
func collectHosts(config *Config, defaultScheme string) ([]connURL, error) {
var addrs []connURL
for _, h := range config.Hosts {
scheme := defaultScheme
host := ""
u, err := url.Parse(h)
if err != nil || u.Host == "" {
host = h
} else {
scheme = u.Scheme
host = u.Host
}
debugf("Add tcp endpoint '%v://%v'.", scheme, host)
switch scheme {
case "tcp", "plain", "tls", "ssl":
default:
err := fmt.Errorf("'%v' is no supported connection scheme in '%v'", scheme, h)
return nil, err
}
pair := strings.SplitN(host, ":", 2)
ports := config.Ports
if len(pair) == 2 {
port, err := strconv.ParseUint(pair[1], 10, 16)
if err != nil {
return nil, fmt.Errorf("'%v' is no valid port number in '%v'", pair[1], h)
}
ports = []uint16{uint16(port)}
host = pair[0]
} else if len(config.Ports) == 0 {
return nil, fmt.Errorf("host '%v' missing port number", h)
}
addrs = append(addrs, connURL{
Scheme: scheme,
Host: host,
Ports: ports,
})
}
return addrs, nil
}