From 94405dd4677802e826284951c98fdf4aef53acbd Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 25 Apr 2017 01:56:08 +0200 Subject: [PATCH] fix snifer --- app/dispatcher/impl/default.go | 2 +- common/buf/multi_buffer.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/dispatcher/impl/default.go b/app/dispatcher/impl/default.go index 2d1564e2..32bba0e3 100644 --- a/app/dispatcher/impl/default.go +++ b/app/dispatcher/impl/default.go @@ -110,7 +110,7 @@ func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound if mb.IsEmpty() { continue } - nBytes, _ := mb.Read(payload) + nBytes := mb.Copy(payload) for _, protocol := range sniferList { var f func([]byte) (string, error) switch protocol { diff --git a/common/buf/multi_buffer.go b/common/buf/multi_buffer.go index 1562685d..30456f3c 100644 --- a/common/buf/multi_buffer.go +++ b/common/buf/multi_buffer.go @@ -31,6 +31,18 @@ func (mb *MultiBuffer) AppendMulti(buf MultiBuffer) { *mb = append(*mb, buf...) } +func (mb MultiBuffer) Copy(b []byte) int { + total := 0 + for _, bb := range mb { + nBytes := copy(b[total:], bb.Bytes()) + total += nBytes + if nBytes < bb.Len() { + break + } + } + return total +} + func (mb *MultiBuffer) Read(b []byte) (int, error) { endIndex := len(*mb) totalBytes := 0