/
netexec.go
671 lines (612 loc) · 22.3 KB
/
netexec.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
Copyright 2014 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 netexec
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/ishidawataru/sctp"
"github.com/spf13/cobra"
utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/sets"
)
var (
httpPort = 8080
udpPort = 8081
sctpPort = -1
shellPath = "/bin/sh"
serverReady = &atomicBool{0}
certFile = ""
privKeyFile = ""
httpOverride = ""
udpListenAddresses = ""
)
const bindToAny = ""
// CmdNetexec is used by agnhost Cobra.
var CmdNetexec = &cobra.Command{
Use: "netexec",
Short: "Creates HTTP(S), UDP, and (optionally) SCTP servers with various endpoints",
Long: `Starts a HTTP(S) server on given port with the following endpoints:
- /: Returns the request's timestamp.
- /clientip: Returns the request's IP address.
- /dial: Creates a given number of requests to the given host and port using the given protocol,
and returns a JSON with the fields "responses" (successful request responses) and "errors" (
failed request responses). Returns "200 OK" status code if the last request succeeded,
"417 Expectation Failed" if it did not, or "400 Bad Request" if any of the endpoint's parameters
is invalid. The endpoint's parameters are:
- "host": The host that will be dialed.
- "port": The port that will be dialed.
- "request": The HTTP endpoint or data to be sent through UDP. If not specified, it will result
in a "400 Bad Request" status code being returned.
- "protocol": The protocol which will be used when making the request. Default value: "http".
Acceptable values: "http", "udp", "sctp".
- "tries": The number of times the request will be performed. Default value: "1".
- "/echo": Returns the given "msg" ("/echo?msg=echoed_msg"), with the optional status "code".
- "/exit": Closes the server with the given code and graceful shutdown. The endpoint's parameters
are:
- "code": The exit code for the process. Default value: 0. Allows an integer [0-127].
- "timeout": The amount of time to wait for connections to close before shutting down.
Acceptable values are golang durations. If 0 the process will exit immediately without
shutdown.
- "wait": The amount of time to wait before starting shutdown. Acceptable values are
golang durations. If 0 the process will start shutdown immediately.
- "/healthz": Returns "200 OK" if the server is ready, "412 Status Precondition Failed"
otherwise. The server is considered not ready if the UDP server did not start yet or
it exited.
- "/hostname": Returns the server's hostname.
- "/hostName": Returns the server's hostname.
- "/redirect": Returns a redirect response to the given "location", with the optional status "code"
("/redirect?location=/echo%3Fmsg=foobar&code=307").
- "/shell": Executes the given "shellCommand" or "cmd" ("/shell?cmd=some-command") and
returns a JSON containing the fields "output" (command's output) and "error" (command's
error message). Returns "200 OK" if the command succeeded, "417 Expectation Failed" if not.
- "/shutdown": Closes the server with the exit code 0.
- "/upload": Accepts a file to be uploaded, writing it in the "/uploads" folder on the host.
Returns a JSON with the fields "output" (containing the file's name on the server) and
"error" containing any potential server side errors.
If "--tls-cert-file" is added (ideally in conjunction with "--tls-private-key-file", the HTTP server
will be upgraded to HTTPS. The image has default, "localhost"-based cert/privkey files at
"/localhost.crt" and "/localhost.key" (see: "porter" subcommand)
If "--http-override" is set, the HTTP(S) server will always serve the override path & options,
ignoring the request URL.
It will also start a UDP server on the indicated UDP port that responds to the following commands:
- "hostname": Returns the server's hostname
- "echo <msg>": Returns the given <msg>
- "clientip": Returns the request's IP address
Additionally, if (and only if) --sctp-port is passed, it will start an SCTP server on that port,
responding to the same commands as the UDP server.
`,
Args: cobra.MaximumNArgs(0),
Run: main,
}
func init() {
CmdNetexec.Flags().IntVar(&httpPort, "http-port", 8080, "HTTP Listen Port")
CmdNetexec.Flags().StringVar(&certFile, "tls-cert-file", "",
"File containing an x509 certificate for HTTPS. (CA cert, if any, concatenated after server cert)")
CmdNetexec.Flags().StringVar(&privKeyFile, "tls-private-key-file", "",
"File containing an x509 private key matching --tls-cert-file")
CmdNetexec.Flags().IntVar(&udpPort, "udp-port", 8081, "UDP Listen Port")
CmdNetexec.Flags().IntVar(&sctpPort, "sctp-port", -1, "SCTP Listen Port")
CmdNetexec.Flags().StringVar(&httpOverride, "http-override", "", "Override the HTTP handler to always respond as if it were a GET with this path & params")
CmdNetexec.Flags().StringVar(&udpListenAddresses, "udp-listen-addresses", "", "A comma separated list of ip addresses the udp servers listen from")
}
// atomicBool uses load/store operations on an int32 to simulate an atomic boolean.
type atomicBool struct {
v int32
}
// set sets the int32 to the given boolean.
func (a *atomicBool) set(value bool) {
if value {
atomic.StoreInt32(&a.v, 1)
return
}
atomic.StoreInt32(&a.v, 0)
}
// get returns true if the int32 == 1
func (a *atomicBool) get() bool {
return atomic.LoadInt32(&a.v) == 1
}
func main(cmd *cobra.Command, args []string) {
exitCh := make(chan shutdownRequest)
if httpOverride != "" {
mux := http.NewServeMux()
addRoutes(mux, exitCh)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
overrideReq, err := http.NewRequestWithContext(r.Context(), "GET", httpOverride, nil)
if err != nil {
http.Error(w, fmt.Sprintf("override request failed: %v", err), http.StatusInternalServerError)
return
}
mux.ServeHTTP(w, overrideReq)
})
} else {
addRoutes(http.DefaultServeMux, exitCh)
}
udpBindTo, err := parseAddresses(udpListenAddresses)
if err != nil {
log.Fatal(err)
}
for _, address := range udpBindTo {
go startUDPServer(address, udpPort)
}
if sctpPort != -1 {
go startSCTPServer(sctpPort)
}
server := &http.Server{Addr: fmt.Sprintf(":%d", httpPort)}
if len(certFile) > 0 {
startServer(server, exitCh, func() error { return server.ListenAndServeTLS(certFile, privKeyFile) })
} else {
startServer(server, exitCh, server.ListenAndServe)
}
}
func addRoutes(mux *http.ServeMux, exitCh chan shutdownRequest) {
mux.HandleFunc("/", rootHandler)
mux.HandleFunc("/clientip", clientIPHandler)
mux.HandleFunc("/dial", dialHandler)
mux.HandleFunc("/echo", echoHandler)
mux.HandleFunc("/exit", func(w http.ResponseWriter, req *http.Request) { exitHandler(w, req, exitCh) })
mux.HandleFunc("/healthz", healthzHandler)
mux.HandleFunc("/hostname", hostnameHandler)
mux.HandleFunc("/redirect", redirectHandler)
mux.HandleFunc("/shell", shellHandler)
mux.HandleFunc("/upload", uploadHandler)
// older handlers
mux.HandleFunc("/hostName", hostNameHandler)
mux.HandleFunc("/shutdown", shutdownHandler)
}
func startServer(server *http.Server, exitCh chan shutdownRequest, fn func() error) {
log.Printf("Started HTTP server on port %d", httpPort)
go func() {
re := <-exitCh
ctx, cancelFn := context.WithTimeout(context.Background(), re.timeout)
defer cancelFn()
err := server.Shutdown(ctx)
log.Printf("Graceful shutdown completed with: %v", err)
os.Exit(re.code)
}()
if err := fn(); err != nil {
if err == http.ErrServerClosed {
// wait until the goroutine calls os.Exit()
select {}
}
log.Fatal(err)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("GET /")
fmt.Fprintf(w, "NOW: %v", time.Now())
}
func echoHandler(w http.ResponseWriter, r *http.Request) {
msg := r.FormValue("msg")
codeString := r.FormValue("code")
log.Printf("GET /echo?msg=%s&code=%s", msg, codeString)
if codeString != "" {
code, err := strconv.Atoi(codeString)
if err != nil && codeString != "" {
fmt.Fprintf(w, "argument 'code' must be an integer or empty, got %q\n", codeString)
return
}
w.WriteHeader(code)
}
fmt.Fprintf(w, "%s", msg)
}
func clientIPHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("GET /clientip")
fmt.Fprintf(w, r.RemoteAddr)
}
type shutdownRequest struct {
code int
timeout time.Duration
}
func exitHandler(w http.ResponseWriter, r *http.Request, exitCh chan<- shutdownRequest) {
waitString := r.FormValue("wait")
timeoutString := r.FormValue("timeout")
codeString := r.FormValue("code")
log.Printf("GET /exit?code=%s&timeout=%s&wait=%s", codeString, timeoutString, waitString)
timeout, err := time.ParseDuration(timeoutString)
if err != nil && timeoutString != "" {
fmt.Fprintf(w, "argument 'timeout' must be a valid golang duration or empty, got %q\n", timeoutString)
return
}
wait, err := time.ParseDuration(waitString)
if err != nil && waitString != "" {
fmt.Fprintf(w, "argument 'wait' must be a valid golang duration or empty, got %q\n", waitString)
return
}
code, err := strconv.Atoi(codeString)
if err != nil && codeString != "" {
fmt.Fprintf(w, "argument 'code' must be an integer [0-127] or empty, got %q\n", codeString)
return
}
log.Printf("Will begin shutdown in %s, allowing %s for connections to close, then will exit with %d", wait, timeout, code)
time.Sleep(wait)
if timeout == 0 {
os.Exit(code)
}
exitCh <- shutdownRequest{code: code, timeout: timeout}
}
func hostnameHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("GET /hostname")
fmt.Fprint(w, getHostName())
}
// healthHandler response with a 200 if the UDP server is ready. It also serves
// as a health check of the HTTP server by virtue of being a HTTP handler.
func healthzHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("GET /healthz")
if serverReady.get() {
w.WriteHeader(200)
return
}
w.WriteHeader(http.StatusPreconditionFailed)
}
func shutdownHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("GET /shutdown")
os.Exit(0)
}
func dialHandler(w http.ResponseWriter, r *http.Request) {
values, err := url.Parse(r.URL.RequestURI())
if err != nil {
http.Error(w, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
host := values.Query().Get("host")
port := values.Query().Get("port")
request := values.Query().Get("request") // hostName
protocol := values.Query().Get("protocol")
tryParam := values.Query().Get("tries")
log.Printf("GET /dial?host=%s&protocol=%s&port=%s&request=%s&tries=%s", host, protocol, port, request, tryParam)
tries := 1
if len(tryParam) > 0 {
tries, err = strconv.Atoi(tryParam)
}
if err != nil {
http.Error(w, fmt.Sprintf("tries parameter is invalid. %v", err), http.StatusBadRequest)
return
}
if len(request) == 0 {
http.Error(w, fmt.Sprintf("request parameter not specified. %v", err), http.StatusBadRequest)
return
}
hostPort := net.JoinHostPort(host, port)
var addr net.Addr
var dialer func(string, net.Addr) (string, error)
switch strings.ToLower(protocol) {
case "", "http":
dialer = dialHTTP
addr, err = net.ResolveTCPAddr("tcp", hostPort)
case "udp":
dialer = dialUDP
addr, err = net.ResolveUDPAddr("udp", hostPort)
case "sctp":
dialer = dialSCTP
addr, err = sctp.ResolveSCTPAddr("sctp", hostPort)
default:
http.Error(w, fmt.Sprintf("unsupported protocol. %s", protocol), http.StatusBadRequest)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("host and/or port param are invalid. %v", err), http.StatusBadRequest)
return
}
errors := make([]string, 0)
responses := make([]string, 0)
var response string
for i := 0; i < tries; i++ {
response, err = dialer(request, addr)
if err != nil {
errors = append(errors, fmt.Sprintf("%v", err))
} else {
responses = append(responses, response)
}
}
output := map[string][]string{}
if len(response) > 0 {
output["responses"] = responses
}
if len(errors) > 0 {
output["errors"] = errors
}
bytes, err := json.Marshal(output)
if err == nil {
fmt.Fprint(w, string(bytes))
} else {
http.Error(w, fmt.Sprintf("response could not be serialized. %v", err), http.StatusExpectationFailed)
}
}
func dialHTTP(request string, addr net.Addr) (string, error) {
transport := utilnet.SetTransportDefaults(&http.Transport{})
httpClient := createHTTPClient(transport)
resp, err := httpClient.Get(fmt.Sprintf("http://%s/%s", addr.String(), request))
defer transport.CloseIdleConnections()
if err == nil {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
return string(body), nil
}
}
return "", err
}
func createHTTPClient(transport *http.Transport) *http.Client {
client := &http.Client{
Transport: transport,
Timeout: 5 * time.Second,
}
return client
}
func dialUDP(request string, addr net.Addr) (string, error) {
Conn, err := net.DialUDP("udp", nil, addr.(*net.UDPAddr))
if err != nil {
return "", fmt.Errorf("udp dial failed. err:%v", err)
}
defer Conn.Close()
buf := []byte(request)
_, err = Conn.Write(buf)
if err != nil {
return "", fmt.Errorf("udp connection write failed. err:%v", err)
}
udpResponse := make([]byte, 2048)
Conn.SetReadDeadline(time.Now().Add(5 * time.Second))
count, err := Conn.Read(udpResponse)
if err != nil || count == 0 {
return "", fmt.Errorf("reading from udp connection failed. err:'%v'", err)
}
return string(udpResponse[0:count]), nil
}
func dialSCTP(request string, addr net.Addr) (string, error) {
Conn, err := sctp.DialSCTP("sctp", nil, addr.(*sctp.SCTPAddr))
if err != nil {
return "", fmt.Errorf("sctp dial failed. err:%v", err)
}
defer Conn.Close()
buf := []byte(request)
_, err = Conn.Write(buf)
if err != nil {
return "", fmt.Errorf("sctp connection write failed. err:%v", err)
}
sctpResponse := make([]byte, 1024)
Conn.SetReadDeadline(time.Now().Add(5 * time.Second))
count, err := Conn.Read(sctpResponse)
if err != nil || count == 0 {
return "", fmt.Errorf("reading from sctp connection failed. err:'%v'", err)
}
return string(sctpResponse[0:count]), nil
}
func shellHandler(w http.ResponseWriter, r *http.Request) {
cmd := r.FormValue("shellCommand")
if cmd == "" {
cmd = r.FormValue("cmd")
}
log.Printf("GET /shell?cmd=%s", cmd)
cmdOut, err := exec.Command(shellPath, "-c", cmd).CombinedOutput()
output := map[string]string{}
if len(cmdOut) > 0 {
output["output"] = string(cmdOut)
}
if err != nil {
output["error"] = fmt.Sprintf("%v", err)
}
log.Printf("Output: %s", output)
bytes, err := json.Marshal(output)
if err == nil {
fmt.Fprint(w, string(bytes))
} else {
http.Error(w, fmt.Sprintf("response could not be serialized. %v", err), http.StatusExpectationFailed)
}
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("GET /upload")
result := map[string]string{}
file, _, err := r.FormFile("file")
if err != nil {
result["error"] = "Unable to upload file."
bytes, err := json.Marshal(result)
if err == nil {
fmt.Fprint(w, string(bytes))
} else {
http.Error(w, fmt.Sprintf("%s. Also unable to serialize output. %v", result["error"], err), http.StatusInternalServerError)
}
log.Printf("Unable to upload file: %s", err)
return
}
defer file.Close()
f, err := ioutil.TempFile("/uploads", "upload")
if err != nil {
result["error"] = "Unable to open file for write"
bytes, err := json.Marshal(result)
if err == nil {
fmt.Fprint(w, string(bytes))
} else {
http.Error(w, fmt.Sprintf("%s. Also unable to serialize output. %v", result["error"], err), http.StatusInternalServerError)
}
log.Printf("Unable to open file for write: %s", err)
return
}
defer f.Close()
if _, err = io.Copy(f, file); err != nil {
result["error"] = "Unable to write file."
bytes, err := json.Marshal(result)
if err == nil {
fmt.Fprint(w, string(bytes))
} else {
http.Error(w, fmt.Sprintf("%s. Also unable to serialize output. %v", result["error"], err), http.StatusInternalServerError)
}
log.Printf("Unable to write file: %s", err)
return
}
UploadFile := f.Name()
if err := os.Chmod(UploadFile, 0700); err != nil {
result["error"] = "Unable to chmod file."
bytes, err := json.Marshal(result)
if err == nil {
fmt.Fprint(w, string(bytes))
} else {
http.Error(w, fmt.Sprintf("%s. Also unable to serialize output. %v", result["error"], err), http.StatusInternalServerError)
}
log.Printf("Unable to chmod file: %s", err)
return
}
log.Printf("Wrote upload to %s", UploadFile)
result["output"] = UploadFile
w.WriteHeader(http.StatusCreated)
bytes, err := json.Marshal(result)
if err != nil {
http.Error(w, fmt.Sprintf("%s. Also unable to serialize output. %v", result["error"], err), http.StatusInternalServerError)
return
}
fmt.Fprint(w, string(bytes))
}
func hostNameHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("GET /hostName")
fmt.Fprint(w, getHostName())
}
func redirectHandler(w http.ResponseWriter, r *http.Request) {
location := r.FormValue("location")
codeString := r.FormValue("code")
log.Printf("%s /redirect?msg=%s&code=%s", r.Method, location, codeString)
code := http.StatusFound
if codeString != "" {
var err error
code, err = strconv.Atoi(codeString)
if err != nil && codeString != "" {
fmt.Fprintf(w, "argument 'code' must be an integer or empty, got %q\n", codeString)
return
}
}
http.Redirect(w, r, location, code)
}
// udp server supports the hostName, echo and clientIP commands.
func startUDPServer(address string, udpPort int) {
serverAddress, err := net.ResolveUDPAddr("udp", net.JoinHostPort(address, strconv.Itoa(udpPort)))
assertNoError(err, fmt.Sprintf("failed to resolve UDP address for port %d", sctpPort))
serverConn, err := net.ListenUDP("udp", serverAddress)
assertNoError(err, fmt.Sprintf("failed to create listener for UDP address %v", serverAddress))
defer serverConn.Close()
buf := make([]byte, 2048)
log.Printf("Started UDP server on port %s %d", address, udpPort)
// Start responding to readiness probes.
serverReady.set(true)
defer func() {
log.Printf("UDP server exited")
serverReady.set(false)
}()
for {
n, clientAddress, err := serverConn.ReadFromUDP(buf)
assertNoError(err, fmt.Sprintf("failed accepting UDP connections"))
receivedText := strings.ToLower(strings.TrimSpace(string(buf[0:n])))
if receivedText == "hostname" {
log.Println("Sending udp hostName response")
_, err = serverConn.WriteToUDP([]byte(getHostName()), clientAddress)
assertNoError(err, fmt.Sprintf("failed to write hostname to UDP client %s", clientAddress))
} else if strings.HasPrefix(receivedText, "echo ") {
parts := strings.SplitN(receivedText, " ", 2)
resp := ""
if len(parts) == 2 {
resp = parts[1]
}
log.Printf("Echoing %v to UDP client %s\n", resp, clientAddress)
_, err = serverConn.WriteToUDP([]byte(resp), clientAddress)
assertNoError(err, fmt.Sprintf("failed to echo to UDP client %s", clientAddress))
} else if receivedText == "clientip" {
log.Printf("Sending clientip back to UDP client %s\n", clientAddress)
_, err = serverConn.WriteToUDP([]byte(clientAddress.String()), clientAddress)
assertNoError(err, fmt.Sprintf("failed to write clientip to UDP client %s", clientAddress))
} else if len(receivedText) > 0 {
log.Printf("Unknown UDP command received from %s: %v\n", clientAddress, receivedText)
}
}
}
// sctp server supports the hostName, echo and clientIP commands.
func startSCTPServer(sctpPort int) {
serverAddress, err := sctp.ResolveSCTPAddr("sctp", fmt.Sprintf(":%d", sctpPort))
assertNoError(err, fmt.Sprintf("failed to resolve SCTP address for port %d", sctpPort))
listener, err := sctp.ListenSCTP("sctp", serverAddress)
assertNoError(err, fmt.Sprintf("failed to create listener for SCTP address %v", serverAddress))
defer listener.Close()
buf := make([]byte, 1024)
log.Printf("Started SCTP server")
// Start responding to readiness probes.
serverReady.set(true)
defer func() {
log.Printf("SCTP server exited")
serverReady.set(false)
}()
for {
conn, err := listener.AcceptSCTP()
assertNoError(err, fmt.Sprintf("failed accepting SCTP connections"))
clientAddress := conn.RemoteAddr().String()
n, err := conn.Read(buf)
assertNoError(err, fmt.Sprintf("failed to read from SCTP client %s", clientAddress))
receivedText := strings.ToLower(strings.TrimSpace(string(buf[0:n])))
if receivedText == "hostname" {
log.Println("Sending SCTP hostName response")
_, err = conn.Write([]byte(getHostName()))
assertNoError(err, fmt.Sprintf("failed to write hostname to SCTP client %s", clientAddress))
} else if strings.HasPrefix(receivedText, "echo ") {
parts := strings.SplitN(receivedText, " ", 2)
resp := ""
if len(parts) == 2 {
resp = parts[1]
}
log.Printf("Echoing %v to SCTP client %s\n", resp, clientAddress)
_, err = conn.Write([]byte(resp))
assertNoError(err, fmt.Sprintf("failed to echo to SCTP client %s", clientAddress))
} else if receivedText == "clientip" {
log.Printf("Sending clientip back to SCTP client %s\n", clientAddress)
_, err = conn.Write([]byte(clientAddress))
assertNoError(err, fmt.Sprintf("failed to write clientip to SCTP client %s", clientAddress))
} else if len(receivedText) > 0 {
log.Printf("Unknown SCTP command received from %s: %v\n", clientAddress, receivedText)
}
conn.Close()
}
}
func getHostName() string {
hostName, err := os.Hostname()
assertNoError(err, "failed to get hostname")
return hostName
}
func assertNoError(err error, detail string) {
if err != nil {
log.Fatalf("Error occurred: %s:%v", detail, err)
}
}
func parseAddresses(addresses string) ([]string, error) {
if addresses == "" {
return []string{bindToAny}, nil
}
// Using a set to remove duplicates
res := make([]string, 0)
split := strings.Split(addresses, ",")
for _, address := range split {
netAddr := net.ParseIP(address)
if netAddr == nil {
return nil, fmt.Errorf("parseAddress: invalid address %s", address)
}
res = append(res, address)
}
set := sets.NewString(res...)
return set.List(), nil
}