-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from kazeburo/log-tcp-dump
Log tcp dump
- Loading branch information
Showing
6 changed files
with
152 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package dumper | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"strings" | ||
"sync" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
// Dumper dumper struct | ||
type Dumper struct { | ||
direction uint | ||
logger *zap.Logger | ||
buf *bytes.Buffer | ||
mu *sync.RWMutex | ||
} | ||
|
||
// New new handler | ||
func New(direction uint, logger *zap.Logger) *Dumper { | ||
d := &Dumper{ | ||
direction: direction, | ||
logger: logger, | ||
buf: new(bytes.Buffer), | ||
mu: new(sync.RWMutex), | ||
} | ||
return d | ||
} | ||
|
||
// Write to dump | ||
func (d *Dumper) Write(p []byte) (n int, err error) { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
d.buf.Write(p) | ||
return len(p), nil | ||
} | ||
|
||
// Flush flush buffer | ||
func (d *Dumper) Flush() { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
if d.buf.Len() == 0 { | ||
return | ||
} | ||
hexdump := strings.Split(hex.Dump(d.buf.Bytes()), "\n") | ||
d.buf.Truncate(0) | ||
byteString := []string{} | ||
ascii := []string{} | ||
for _, hd := range hexdump { | ||
if hd == "" { | ||
continue | ||
} | ||
byteString = append(byteString, strings.TrimRight(strings.Replace(hd[10:58], " ", " ", 1), " ")) | ||
ascii = append(ascii, hd[61:len(hd)-1]) | ||
} | ||
d.logger.Info("dump", | ||
zap.Uint("direction", d.direction), | ||
zap.String("hex", strings.Join(byteString, " ")), | ||
zap.String("ascii", strings.Join(ascii, "")), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package seq | ||
|
||
import "sync" | ||
|
||
// Seq struct | ||
type Seq struct { | ||
i uint64 | ||
mu *sync.RWMutex | ||
} | ||
|
||
// New create sequencer | ||
func New() *Seq { | ||
return &Seq{ | ||
i: 0, | ||
mu: new(sync.RWMutex), | ||
} | ||
} | ||
|
||
// Next fetch new sequence | ||
func (s *Seq) Next() uint64 { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
s.i = s.i + 1 | ||
return s.i | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters