forked from mushorg/glutton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp.go
More file actions
28 lines (26 loc) · 648 Bytes
/
tcp.go
File metadata and controls
28 lines (26 loc) · 648 Bytes
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
package glutton
import (
"context"
"encoding/hex"
"fmt"
"net"
)
// HandleTCP takes a net.Conn and peeks at the data send
func (g *Glutton) HandleTCP(ctx context.Context, conn net.Conn) (err error) {
defer func() {
err = conn.Close()
if err != nil {
g.logger.Error(fmt.Sprintf("[log.tcp ] error: %v", err))
}
}()
host, _, _ := net.SplitHostPort(conn.RemoteAddr().String())
buffer := make([]byte, 1024)
n, err := conn.Read(buffer)
if err != nil {
g.logger.Error(fmt.Sprintf("[log.tcp ] error: %v", err))
}
if n > 0 && n < 1024 {
g.logger.Info(fmt.Sprintf("[log.tcp ] %s\n%s", host, hex.Dump(buffer[0:n])))
}
return err
}