-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (77 loc) · 2.13 KB
/
main.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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"log"
"math/rand"
"net"
"time"
"github.com/adrianosela/authio"
)
const (
flagNameProtocol = "protocol"
flagNameAddress = "address"
flagNameKey = "key"
defaultProtocol = "tcp"
defaultAddress = "localhost:1234"
defaultKey = "mysupersecretstring"
)
var (
protocol string
address string
key string
)
func main() {
rand.Seed(time.Now().Unix())
// initialize flags
flag.StringVar(&protocol, flagNameProtocol, defaultProtocol, "listener protocol to use")
flag.StringVar(&address, flagNameAddress, defaultAddress, "listener address (i.e. HOST:PORT) to use")
flag.StringVar(&key, flagNameKey, defaultKey, "key to use for message authentication codes")
flag.Parse()
// start server
l, err := net.Listen(protocol, address)
if err != nil {
log.Fatalf("could not start %s listener on %s: %s", protocol, address, err)
}
defer l.Close()
// accept and handle new inbound connections
for {
c, err := l.Accept()
if err != nil {
log.Fatalf("could not accept new connection %s listener on %s", protocol, address)
}
go handleConn(rand.Intn(1000), c, key)
}
}
func handleConn(clientID int, conn net.Conn, key string) {
defer conn.Close()
// initialize authenticated reader and writer
authedReader := authio.NewReader(conn, []byte(key))
authedWriter := authio.NewWriter(conn, []byte(key))
for {
// read input from authenticated reader
data, err := bufio.NewReader(authedReader).ReadString('\n')
if err != nil {
if !errors.Is(err, io.EOF) {
log.Printf("failed to read authed reader for client id %d: %s", clientID, err)
}
return
}
// write input to stdout
fmt.Printf("[%d] %s", clientID, data)
// echo back input with a timestamp on authenticated writer
writer := bufio.NewWriter(authedWriter)
_, err = writer.WriteString(fmt.Sprintf("[%s] %s", time.Now().Format(time.RFC3339), data))
if err != nil {
log.Printf("failed to write to writer for client id %d: %s", clientID, err)
return
}
if err = writer.Flush(); err != nil {
log.Printf("failed to flush writer for client id %d: %s", clientID, err)
return
}
}
}