-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
133 lines (120 loc) · 2.6 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
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
package main
import (
"bufio"
"flag"
"fmt"
"net"
"os"
"strings"
"time"
log "github.com/Sirupsen/logrus"
)
type config struct {
timeout time.Duration
retryInterval time.Duration
sendInterval time.Duration
}
func main() {
log.SetOutput(os.Stderr)
var (
listen bool
laddr string
c config
)
flag.BoolVar(&listen, "l", false, "listen")
flag.StringVar(&laddr, "a", "", "listening address")
flag.DurationVar(&c.timeout, "t", 5*time.Second, "connect/send/recv timeout")
flag.DurationVar(&c.retryInterval, "r", 1*time.Second, "connection retry interval")
flag.DurationVar(&c.sendInterval, "i", 2*time.Second, "message sending interval")
flag.Parse()
addrs := flag.Args()
if listen {
laddr = normalizeAddr(laddr)
log.Infof("listening on %s", laddr)
ln, err := net.Listen("tcp", laddr)
if err != nil {
log.Fatal(err)
}
go accept(ln, c)
}
for _, addr := range addrs {
go send(normalizeAddr(addr), c)
}
select {}
}
func normalizeAddr(addr string) string {
parts := strings.Split(addr, ":")
if len(parts) == 1 {
return addr + ":8080"
}
return addr
}
func accept(ln net.Listener, c config) {
for {
conn, err := ln.Accept()
if err != nil {
log.Errorf("accept failed: %s", err)
continue
}
go recv(conn, c)
}
}
func recv(conn net.Conn, c config) {
log.Infof("accepted connection from %s", remoteAddr(conn))
r := bufio.NewReader(conn)
for {
conn.SetDeadline(time.Now().Add(c.timeout))
s, err := r.ReadString('\n')
parts := strings.Split(s, "\n")
msg := parts[0]
if err != nil {
log.Errorln(err)
break
}
if _, err := conn.Write([]byte("\n")); err != nil {
log.Errorln(err)
break
}
fmt.Printf("%s <-- %s\n", msg, remoteAddr(conn))
}
conn.Close()
}
func send(addr string, c config) {
i := 1
for {
conn, err := net.DialTimeout("tcp", addr, c.timeout)
if err != nil {
log.Errorln(err)
time.Sleep(c.retryInterval)
continue
}
log.Infof("connected to %s", remoteAddr(conn))
r := bufio.NewReader(conn)
for {
conn.SetDeadline(time.Now().Add(c.timeout))
msg := fmt.Sprintf("%d", i)
if _, err := conn.Write([]byte(msg + "\n")); err != nil {
log.Errorln(err)
break
}
_, err := r.ReadString('\n')
if err != nil {
log.Errorln(err)
break
}
fmt.Printf("%s --> %s\n", msg, remoteAddr(conn))
i++
time.Sleep(c.sendInterval)
}
conn.Close()
}
}
func remoteAddr(conn net.Conn) string {
ip := conn.RemoteAddr().(*net.TCPAddr).IP.String()
names, err := net.LookupAddr(ip)
if err == nil && len(names) > 0 {
parts := strings.Split(names[0], ".")
return parts[0] + " (" + ip + ")"
}
return ip
}