-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrecord.go
156 lines (124 loc) · 3.49 KB
/
record.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
// Wrapper for net.Conn which supports recording and replaying received data
package main
import (
"io"
"os"
"net"
"log"
"flag"
"time"
"encoding/binary"
)
// Log record header
type header struct {
Timestamp int64 // delay since last packet, in nanoseconds
Length int32 // length of data bytes
}
type recorder struct {
conn net.Conn
log io.WriteCloser
lastTimestamp int64
}
func (recorder *recorder) Read(b []byte) (n int, err os.Error) {
n, err = recorder.conn.Read(b)
if err == nil {
now := time.Nanoseconds()
binary.Write(recorder.log, binary.BigEndian, &header{
now - recorder.lastTimestamp,
int32(n),
})
binary.Write(recorder.log, binary.BigEndian, b[:n])
recorder.lastTimestamp = now
}
return
}
func (recorder *recorder) Write(b []byte) (n int, err os.Error) {
return recorder.conn.Write(b)
}
func (recorder *recorder) Close() os.Error {
recorder.log.Close()
return recorder.conn.Close()
}
func (recorder *recorder) LocalAddr() net.Addr {
return recorder.conn.LocalAddr()
}
func (recorder *recorder) RemoteAddr() net.Addr {
return recorder.conn.RemoteAddr()
}
func (recorder *recorder) SetTimeout(nsec int64) os.Error {
return recorder.conn.SetTimeout(nsec)
}
func (recorder *recorder) SetReadTimeout(nsec int64) os.Error {
return recorder.conn.SetReadTimeout(nsec)
}
func (recorder *recorder) SetWriteTimeout(nsec int64) os.Error {
return recorder.conn.SetWriteTimeout(nsec)
}
type replayer struct {
conn net.Conn
log io.ReadCloser
lastTimestamp int64
}
func (replayer *replayer) Read(b []byte) (n int, err os.Error) {
var header header
err = binary.Read(replayer.log, binary.BigEndian, &header)
if err != nil {
return 0, err
}
if int32(len(b)) < header.Length {
return 0, os.NewError("replay read length too small")
}
// Wait until recorded time has passed
now := time.Nanoseconds()
delta := now - replayer.lastTimestamp
if delta < header.Timestamp {
time.Sleep(header.Timestamp - delta)
}
replayer.lastTimestamp = now
return replayer.log.Read(b[:header.Length])
}
func (replayer *replayer) Write(b []byte) (n int, err os.Error) {
return replayer.conn.Write(b)
}
func (replayer *replayer) Close() os.Error {
replayer.log.Close()
return replayer.conn.Close()
}
func (replayer *replayer) LocalAddr() net.Addr {
return replayer.conn.LocalAddr()
}
func (replayer *replayer) RemoteAddr() net.Addr {
return replayer.conn.RemoteAddr()
}
func (replayer *replayer) SetTimeout(nsec int64) os.Error {
return replayer.conn.SetTimeout(nsec)
}
func (replayer *replayer) SetReadTimeout(nsec int64) os.Error {
return replayer.conn.SetReadTimeout(nsec)
}
func (replayer *replayer) SetWriteTimeout(nsec int64) os.Error {
return replayer.conn.SetWriteTimeout(nsec)
}
var record = flag.String("record", "", "record received packets to file")
var replay = flag.String("replay", "", "replay received packets from file")
var connections = 0
// Interpose a recorder or replayer onto a network connection
func WrapConn(raw net.Conn) (wrapped net.Conn) {
if *record != "" {
file, err := os.Open(*record, os.O_CREAT|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
log.Exit("WrapConn: ", err.String())
}
return &recorder{raw, file, time.Nanoseconds()}
}
// The second client connection will replay the log file
if connections == 1 && *replay != "" {
file, err := os.Open(*replay, os.O_RDONLY, 0)
if err != nil {
log.Exit("WrapConn: ", err.String())
}
return &replayer{raw, file, time.Nanoseconds()}
}
connections++
return raw
}