-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_connections.go
212 lines (197 loc) · 5.43 KB
/
test_connections.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"fmt"
"io"
"log"
"net"
"net/http"
"time"
)
var outConnections int
/*
Forwards the TCP to either TCP or HTTP,
but sometimes pauses output to test timeout recovery.
Combine with Ctrl-C to test reconnects.
Test all at once with go run server/*go -- http_timeout:8s=http://localhost:12340 tcp_timeout:2s=tcp://localhost:12341 redirect:0=http://localhost:12342 redirect_loop:0=http://localhost:12343 http_flood:2s=http://localhost:12344 tcp_flood:2s=tcp://localhost:12345
*/
func main() {
outConnections = 0
notPaused := true
ticker := time.NewTicker(8 * time.Second).C
go func() {
for range ticker {
notPaused = !notPaused
fmt.Printf("outConnections: %d\n", outConnections)
}
}()
go timeoutHTTP(¬Paused)
go timeoutTCP(¬Paused)
go redirectOnce()
go redirectLoop()
go floodHTTP()
go floodTCP()
time.Sleep(time.Hour)
}
func readPauseTCP(send chan<- []byte, notPaused, stop *bool) {
addr, err := net.ResolveTCPAddr("tcp", "153.44.253.27:5631")
CheckErr(err, "Resolve kystverket address")
conn, err := net.DialTCP("tcp", nil, addr)
CheckErr(err, "Connect to kystverket")
defer conn.Close() // FIXME can fail
buf := make([]byte, 4096)
for !*stop {
n, err := conn.Read(buf)
CheckErr(err, "read tcp")
if *notPaused && len(send) < cap(send) {
content := make([]byte, n)
copy(content, buf[:n])
send <- content
fmt.Println(string(buf[0:n]))
}
}
}
// Connect to with http_timeout:8s=http://localhost:12340
func timeoutHTTP(notPaused *bool) {
read := make(chan []byte, 200)
h := func(w http.ResponseWriter, _ *http.Request) {
// I guess the caller closes the connection...
outConnections++
defer func() { outConnections-- }()
stop := false
defer func() { stop = true }()
go readPauseTCP(read, notPaused, &stop)
//w.Header().Set("Content-Type", "text/plain; charset=utf-8") // normal header
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("Server", "test_connections")
w.WriteHeader(http.StatusOK)
//go func() {
for s := range read {
_, err := w.Write(s)
if err != nil {
fmt.Printf("write to HTTP error: %s\n", err)
return
}
w.(http.Flusher).Flush()
}
//}()
}
s := http.Server{
Addr: "localhost:12340",
Handler: http.HandlerFunc(h),
}
err := s.ListenAndServe()
CheckErr(err, "listen to HTTP")
}
// Connect to with tcp_timeout:2s=tcp://localhost:12341
func timeoutTCP(notPaused *bool) {
a, err := net.ResolveTCPAddr("tcp", "localhost:12341")
CheckErr(err, "resolve TCP address")
l, err := net.ListenTCP("tcp", a)
CheckErr(err, "listen for TCP")
defer closeAndCheck(l, "timeoutTCP server")
read := make(chan []byte, 200)
for {
c, err := l.AcceptTCP()
CheckErr(err, "accept TCP connection")
go func() {
defer closeAndCheck(c, "timeoutTCP connection")
outConnections++
defer func() { outConnections-- }()
stop := false
defer func() { stop = true }()
go readPauseTCP(read, notPaused, &stop)
for s := range read {
_, err := c.Write(s)
if err != nil {
fmt.Printf("write to TCP error: %s\n", err)
break
}
}
}()
}
}
// Connect to with redirect:2s=http://localhost:12342
func redirectOnce() {
h := http.RedirectHandler("http://localhost:12340", http.StatusMovedPermanently)
s := http.Server{
Addr: "localhost:12342",
Handler: h,
}
err := s.ListenAndServe()
CheckErr(err, "listen to HTTP")
}
// Connect to with redirect_loop:0=http://localhost:12343
func redirectLoop() {
h := http.RedirectHandler("http://localhost:12343", http.StatusMovedPermanently)
s := http.Server{
Addr: "localhost:12343",
Handler: h,
}
err := s.ListenAndServe()
CheckErr(err, "listen to HTTP")
}
const floodPacket = "!BSVDM,2,1,6,A,59NSF?02;Ic4DiPoP00i0Nt>0t@E8L5<0000001@:H@964Q60;lPASQDh000,0*11\r\n!BSVDM,2,2,6,A,00000000000,2*3B\r\n"
// Connect to with http_flood:2s=http://localhost:12344
func floodHTTP() {
h := func(w http.ResponseWriter, _ *http.Request) {
// I guess the caller closes the connection...
outConnections++
defer func() { outConnections-- }()
//w.Header().Set("Content-Type", "text/plain; charset=utf-8") // normal header
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("Server", "test_connections")
w.WriteHeader(http.StatusOK)
//go func() {
for {
_, err := w.Write([]byte(floodPacket))
if err != nil {
fmt.Printf("write to HTTP error: %s\n", err)
return
}
w.(http.Flusher).Flush()
}
//}()
}
s := http.Server{
Addr: "localhost:12344",
Handler: http.HandlerFunc(h),
}
err := s.ListenAndServe()
CheckErr(err, "listen to HTTP")
}
// Connect to with tcp_flood:2s=tcp://localhost:12345
func floodTCP() {
a, err := net.ResolveTCPAddr("tcp", "localhost:12345")
CheckErr(err, "resolve TCP address")
l, err := net.ListenTCP("tcp", a)
CheckErr(err, "listen for TCP")
defer closeAndCheck(l, "floodTCP server")
for {
c, err := l.AcceptTCP()
CheckErr(err, "accept TCP connection")
go func() {
outConnections++
defer closeAndCheck(c, "floodTcp connection")
defer func() { outConnections-- }()
for {
_, err := c.Write([]byte(floodPacket))
if err != nil {
fmt.Printf("write to TCP error: %s\n", err)
break
}
}
}()
}
}
func closeAndCheck(c io.Closer, name string) {
err := c.Close()
if err != nil {
log.Fatalf("error when closing %s: %s", name, err.Error())
}
}
// CheckErr aborts the process if err is not nil
func CheckErr(err error, msg string) {
if err != nil {
log.Fatalf("Failed to %s: %s\n", msg, err.Error())
}
}