-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (66 loc) · 1.55 KB
/
Copy pathmain.go
File metadata and controls
84 lines (66 loc) · 1.55 KB
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
package main
import (
"net"
"fmt"
"flag"
"time"
"os"
"os/signal"
"math/rand"
"github.com/golang/glog"
)
var port int
var trappedCount int
func init() {
flag.IntVar(&port, "port", 22, "Port to listen to")
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func RandString() string {
n := rand.Intn(60) + 10
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func handleConnection(connection net.Conn) {
defer connection.Close()
trappedCount++
glog.Infof("Currently handling %+v trapped connections", trappedCount)
for {
_, err := connection.Write([]byte(RandString()))
if err != nil {
glog.Infof("Error writing: %+v, closing connection", err)
trappedCount--
glog.Infof("Currently handling %+v trapped connections", trappedCount)
return
}
time.Sleep(10 * time.Second)
}
}
func main() {
trappedCount = 0
flag.Parse()
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt)
go func() {
_ = <-sigc
glog.Info("Received interrupt, closing")
glog.Flush()
os.Exit(1)
}()
glog.Infof("Starting up ssh-trap on port %+v", port)
sock, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%v", port))
if err != nil {
glog.Fatalf("Error when opening socket: %+v", err)
}
glog.Info("Waiting for connections...")
for {
conn, err := sock.Accept()
if err != nil {
glog.Warningf("Error on accept: %+v", err)
}
glog.Infof("Connection accepted from %+v", conn.RemoteAddr())
go handleConnection(conn)
}
}