-
Notifications
You must be signed in to change notification settings - Fork 1
/
connector-tcp.go
93 lines (79 loc) · 1.7 KB
/
connector-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
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"
import (
"context"
"net"
"strconv"
"time"
)
type (
tcpConn struct {
network, address string
}
)
var resolver = &net.Resolver{
PreferGo: true,
}
// TCPConnector is a dialer that forward to a local RFC5424 syslog receiver
func TCPConnector(network, address string) Connector {
if network == "" || address == "" {
return InvalidConnector{ErrEmptyNetworkAddress}
}
if network != "tcp" && network != "tcp4" && network != "tcp6" {
return InvalidConnector{ErrInvalidNetwork}
}
return &tcpConn{network, address}
}
func (c *tcpConn) Connect() (conn WriteCloser, err error) {
port := 514
addr, portStr, err := net.SplitHostPort(c.address)
if err != nil {
return nil, err
}
if portStr != "" {
port, err = strconv.Atoi(portStr)
if err != nil {
return nil, err
}
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
rawips, err := resolver.LookupIPAddr(ctx, addr)
cancel()
if err != nil {
return nil, err
}
ips := make([]net.IPAddr, 0, len(rawips))
switch c.network {
case "tcp4":
for _, ip := range rawips {
if ip.IP.To4() != nil {
ips = append(ips, ip)
}
}
case "tcp6":
for _, ip := range rawips {
if ip.IP.To4() == nil {
ips = append(ips, ip)
}
}
default:
ips = rawips
}
var contcp *net.TCPConn
for _, ip := range ips {
addr := &net.TCPAddr{IP: ip.IP, Port: port, Zone: ip.Zone}
contcp, err = net.DialTCP(c.network, nil, addr)
if err == nil {
break
}
}
if err != nil {
return nil, err
}
contcp.CloseRead()
contcp.SetNoDelay(false)
contcp.SetKeepAlive(true)
contcp.SetKeepAlivePeriod(2 * time.Minute)
contcp.SetLinger(-1)
conn = contcp
return
}