aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarin Ivanov <[email protected]>2019-03-24 22:21:08 +0200
committerMarin Ivanov <[email protected]>2019-03-24 22:21:08 +0200
commitc9585c36afe0c502c91e5ad527af1c71a80feb0a (patch)
tree3a8adf46ced72bea7410ed377c116af4330f6883
parent6ab1613157a6a5d3eabf9d2fe2adf4699d29a5ba (diff)
Add Makefile and configurable delay
-rw-r--r--.gitignore1
-rw-r--r--Makefile8
-rw-r--r--handle.go7
-rw-r--r--main.go14
-rw-r--r--ssh.go6
5 files changed, 26 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..796b96d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/build
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..906b4bc
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,8 @@
+linux-amd64: build/linux-amd64
+windows-amd64: build/windows-amd64.exe
+
+build/linux-amd64: $(SRC)
+ GOOS=linux GOARCH=amd64 go build -o "$@"
+
+build/windows-amd64.exe: $(SRC)
+ GOOS=windows GOARCH=amd64 go build -o "$@"
diff --git a/handle.go b/handle.go
index 622b68e..918f3a8 100644
--- a/handle.go
+++ b/handle.go
@@ -6,7 +6,8 @@ import (
"time"
)
-type protoHandler func(net.Conn)
+type empty struct{}
+type protoHandler func(net.Conn, time.Duration)
func protocolHandler(proto string) (protoHandler, error) {
switch proto {
@@ -22,10 +23,10 @@ func logConn(conn net.Conn, msg string) {
fmt.Printf("%s, %s, %s\n", now.String(), conn.RemoteAddr().String(), msg)
}
-func connHandler(handler protoHandler, conn net.Conn) {
+func connHandler(handler protoHandler, conn net.Conn, delay time.Duration) {
defer conn.Close()
logConn(conn, "handling")
- handler(conn)
+ handler(conn, delay)
logConn(conn, "closing")
}
diff --git a/main.go b/main.go
index 8de0a5c..ac4a25a 100644
--- a/main.go
+++ b/main.go
@@ -13,9 +13,11 @@ import (
func main() {
var protocol string
var bindAddr string
+ var delayParam string
var port int
- flag.StringVarP(&protocol, "proto", "P", "ssh", "protocol to trap")
+ flag.StringVarP(&protocol, "proto", "P", "ssh", "protocol to tarpit")
+ flag.StringVarP(&delayParam, "delay", "d", "10s", "delay between the tarpit keep-alive data packets")
flag.StringVarP(&bindAddr, "bind-address", "b", "", "address to bind the socket to")
flag.IntVarP(&port, "port", "p", 22, "TCP port")
flag.Parse()
@@ -25,14 +27,20 @@ func main() {
fmt.Fprintln(os.Stderr, "Error: protocol handler;", err.Error())
os.Exit(1)
}
+ delay, err := time.ParseDuration(delayParam)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error: parse delay;", err.Error())
+ os.Exit(1)
+ }
- rand.Seed(time.Now().UnixNano())
bind := fmt.Sprintf("%s:%d", bindAddr, port)
ln, err := net.Listen("tcp", bind)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: server listen;", err.Error())
os.Exit(1)
}
+
+ rand.Seed(time.Now().UnixNano())
fmt.Fprintf(os.Stderr, "** Server listening on %s\n", bind)
for {
conn, err := ln.Accept()
@@ -40,6 +48,6 @@ func main() {
// handle error
continue
}
- go connHandler(handler, conn)
+ go connHandler(handler, conn, delay)
}
}
diff --git a/ssh.go b/ssh.go
index 3d43a6c..909dc13 100644
--- a/ssh.go
+++ b/ssh.go
@@ -9,16 +9,14 @@ import (
"time"
)
-type empty struct{}
-
-func sshHandler(conn net.Conn) {
+func sshHandler(conn net.Conn, delay time.Duration) {
eof := make(chan empty)
go func() {
io.Copy(ioutil.Discard, conn)
eof <- empty{}
}()
- tick := time.Tick(10 * time.Second)
+ tick := time.Tick(delay)
for {
select {
case <-eof: