-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
dumpingconn.go
74 lines (62 loc) · 1.57 KB
/
dumpingconn.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
package debugproxy
import (
"encoding/hex"
"io"
"net"
"os"
"time"
log "github.com/sirupsen/logrus"
)
type DumpingConn struct {
conn net.Conn
fileHandle *os.File
}
func NewDumpingConn(filePath string, conn net.Conn) *DumpingConn {
fileHandle, err := os.OpenFile(filePath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
dc := DumpingConn{fileHandle: fileHandle, conn: conn}
return &dc
}
func (d DumpingConn) Read(b []byte) (n int, err error) {
n, err = d.conn.Read(b)
if err != nil {
io.WriteString(d.fileHandle, "\n\nError Reading"+err.Error())
}
io.WriteString(d.fileHandle, "\n\nReading------------->\n")
d.fileHandle.Write([]byte(hex.Dump(b)))
return n, err
}
func (d DumpingConn) Write(b []byte) (n int, err error) {
n, err = d.conn.Write(b)
if err != nil {
io.WriteString(d.fileHandle, "\n\nError Sending"+err.Error())
}
io.WriteString(d.fileHandle, "\n\nSending------------->\n")
d.fileHandle.Write([]byte(hex.Dump(b)))
return n, err
}
func (d DumpingConn) Close() error {
err := d.fileHandle.Close()
if err != nil {
log.Warn("failed closing bin file handle", err)
}
return d.conn.Close()
}
func (d DumpingConn) LocalAddr() net.Addr {
return d.conn.LocalAddr()
}
func (d DumpingConn) RemoteAddr() net.Addr {
return d.conn.RemoteAddr()
}
func (d DumpingConn) SetDeadline(t time.Time) error {
return d.conn.SetDeadline(t)
}
func (d DumpingConn) SetReadDeadline(t time.Time) error {
return d.conn.SetReadDeadline(t)
}
func (d DumpingConn) SetWriteDeadline(t time.Time) error {
return d.conn.SetWriteDeadline(t)
}