-
Notifications
You must be signed in to change notification settings - Fork 38.9k
/
connect.go
204 lines (176 loc) · 5.05 KB
/
connect.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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package connect
import (
"fmt"
"net"
"os"
"strings"
"syscall"
"time"
"github.com/ishidawataru/sctp"
"github.com/spf13/cobra"
)
// CmdConnect is used by agnhost Cobra.
var CmdConnect = &cobra.Command{
Use: "connect [host:port]",
Short: "Attempts a TCP, UDP or SCTP connection and returns useful errors",
Long: `Tries to open a TCP, UDP or SCTP connection to the given host and port. On error it prints an error message prefixed with a specific fixed string that test cases can check for:
* UNKNOWN - Generic/unknown (non-network) error (eg, bad arguments)
* TIMEOUT - The connection attempt timed out
* DNS - An error in DNS resolution
* REFUSED - Connection refused
* OTHER - Other networking error (eg, "no route to host")`,
Args: cobra.ExactArgs(1),
Run: main,
}
var (
timeout time.Duration
protocol string
udpData string
sctpData string
)
func init() {
CmdConnect.Flags().DurationVar(&timeout, "timeout", time.Duration(0), "Maximum time before returning an error")
CmdConnect.Flags().StringVar(&protocol, "protocol", "tcp", "The protocol to use to perform the connection, can be tcp, udp or sctp")
CmdConnect.Flags().StringVar(&udpData, "udp-data", "hostname", "The UDP payload send to the server")
CmdConnect.Flags().StringVar(&sctpData, "sctp-data", "hostname", "The SCTP payload send to the server")
}
func main(cmd *cobra.Command, args []string) {
dest := args[0]
switch protocol {
case "", "tcp":
connectTCP(dest, timeout)
case "udp":
connectUDP(dest, timeout, udpData)
case "sctp":
connectSCTP(dest, timeout, sctpData)
default:
fmt.Fprint(os.Stderr, "Unsupported protocol\n", protocol)
os.Exit(1)
}
}
func connectTCP(dest string, timeout time.Duration) {
// Redundantly parse and resolve the destination so we can return the correct
// errors if there's a problem.
if _, _, err := net.SplitHostPort(dest); err != nil {
fmt.Fprintf(os.Stderr, "UNKNOWN: %v\n", err)
os.Exit(1)
}
if _, err := net.ResolveTCPAddr("tcp", dest); err != nil {
fmt.Fprintf(os.Stderr, "DNS: %v\n", err)
os.Exit(1)
}
conn, err := net.DialTimeout("tcp", dest, timeout)
if err == nil {
conn.Close()
os.Exit(0)
}
if opErr, ok := err.(*net.OpError); ok {
if opErr.Timeout() {
fmt.Fprintf(os.Stderr, "TIMEOUT\n")
os.Exit(1)
} else if syscallErr, ok := opErr.Err.(*os.SyscallError); ok {
if syscallErr.Err == syscall.ECONNREFUSED {
fmt.Fprintf(os.Stderr, "REFUSED\n")
os.Exit(1)
}
}
}
fmt.Fprintf(os.Stderr, "OTHER: %v\n", err)
os.Exit(1)
}
func connectSCTP(dest string, timeout time.Duration, data string) {
var (
buf = make([]byte, 1024)
conn *sctp.SCTPConn
)
addr, err := sctp.ResolveSCTPAddr("sctp", dest)
if err != nil {
fmt.Fprintf(os.Stderr, "DNS: %v\n", err)
os.Exit(1)
}
timeoutCh := time.After(timeout)
errCh := make(chan error)
go func() {
conn, err = sctp.DialSCTP("sctp", nil, addr)
if err != nil {
errCh <- err
return
}
defer func() {
errCh <- conn.Close()
}()
if _, err = conn.Write([]byte(fmt.Sprintf("%s\n", data))); err != nil {
errCh <- err
return
}
if _, err = conn.Read(buf); err != nil {
errCh <- err
return
}
}()
select {
case err := <-errCh:
if err != nil {
fmt.Fprintf(os.Stderr, "OTHER: %v\n", err)
os.Exit(1)
}
case <-timeoutCh:
fmt.Fprint(os.Stderr, "TIMEOUT\n")
os.Exit(1)
}
}
func connectUDP(dest string, timeout time.Duration, data string) {
var (
readBytes int
buf = make([]byte, 1024)
)
if _, err := net.ResolveUDPAddr("udp", dest); err != nil {
fmt.Fprintf(os.Stderr, "DNS: %v\n", err)
os.Exit(1)
}
conn, err := net.Dial("udp", dest)
if err != nil {
fmt.Fprintf(os.Stderr, "OTHER: %v\n", err)
os.Exit(1)
}
if timeout > 0 {
if err = conn.SetDeadline(time.Now().Add(timeout)); err != nil {
fmt.Fprintf(os.Stderr, "OTHER: %v\n", err)
os.Exit(1)
}
}
if _, err = conn.Write([]byte(fmt.Sprintf("%s\n", data))); err != nil {
parseUDPErrorAndExit(err)
}
if readBytes, err = conn.Read(buf); err != nil {
parseUDPErrorAndExit(err)
}
// ensure the response from UDP server
if readBytes == 0 {
fmt.Fprintf(os.Stderr, "OTHER: No data received from the server. Cannot guarantee the server received the request.\n")
os.Exit(1)
}
}
func parseUDPErrorAndExit(err error) {
neterr, ok := err.(net.Error)
if ok && neterr.Timeout() {
fmt.Fprintf(os.Stderr, "TIMEOUT: %v\n", err)
} else if strings.Contains(err.Error(), "connection refused") {
fmt.Fprintf(os.Stderr, "REFUSED: %v\n", err)
} else {
fmt.Fprintf(os.Stderr, "UNKNOWN: %v\n", err)
}
os.Exit(1)
}