forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netkitten.go
91 lines (83 loc) · 2.24 KB
/
netkitten.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
// Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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.
// Netkitten is a slimmed down netcat intended to make our integ tests able to
// run containers lighter than busybox+netcat, but still be able to do suitably
// complex network testing. This program is only intended for testing. It
// implements cli functionality to read data from a given tcp address to stdout,
// serve data on
package main
import (
"flag"
"io"
"log"
"net"
"os"
"strconv"
"sync"
)
func main() {
var listen = flag.Int("l", -1, "Port to listen on and write data to")
var data = flag.String("write", "", "Data to write to the server connected to")
var serve = flag.String("serve", "", "Data to serve to clients when listening")
var loop = flag.Bool("loop", false, "Loop forever for reading or writing")
flag.Parse()
lport := *listen
target := flag.Arg(0)
once := sync.Once{}
var ln net.Listener
var err error
for {
// If given a target, work with it first
var cconn net.Conn
defer func() {
if cconn != nil {
cconn.Close()
}
}()
if target != "" {
var err error
cconn, err = net.Dial("tcp", target)
if err != nil {
log.Fatal("Error connecting to target: ", err)
}
if *data != "" {
cconn.Write([]byte(*data))
}
}
var conn net.Conn
if lport >= 0 {
once.Do(func() {
ln, err = net.Listen("tcp", ":"+strconv.Itoa(lport))
})
if err != nil {
log.Fatal("Error listening: ", err)
}
conn, err = ln.Accept()
if err != nil {
log.Fatal("Error accepting connection: ", err)
}
if *serve != "" {
conn.Write([]byte(*serve))
}
if target != "" && cconn != nil {
io.Copy(conn, cconn)
}
conn.Close()
} else if cconn != nil {
io.Copy(os.Stdout, cconn)
}
if !*loop {
break
}
}
}