mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-06 16:29:24 +07:00
feat: dns routing (#26)
This commit is contained in:
@ -157,7 +157,7 @@ func (r *CryptoFrameRelocation) BytesFromPool() []byte {
|
||||
return pool.Get(0)
|
||||
}
|
||||
right := r.o[len(r.o)-1]
|
||||
return r.copyBytes(0, 0, len(r.o)-1, len(right.Data)-1, r.length)
|
||||
return r.copyBytesToPool(0, 0, len(r.o)-1, len(right.Data)-1, r.length)
|
||||
}
|
||||
|
||||
// RangeFromPool copy bytes from iUpperAppOffset to jUpperAppOffset.
|
||||
@ -191,11 +191,11 @@ func (r *CryptoFrameRelocation) RangeFromPool(i, j int) []byte {
|
||||
}
|
||||
}
|
||||
|
||||
return r.copyBytes(iOuter, iInner, jOuter, jInner, j-i+1)
|
||||
return r.copyBytesToPool(iOuter, iInner, jOuter, jInner, j-i+1)
|
||||
}
|
||||
|
||||
// copyBytes copy bytes including i and j.
|
||||
func (r *CryptoFrameRelocation) copyBytes(iOuter, iInner, jOuter, jInner, size int) []byte {
|
||||
// copyBytesToPool copy bytes including i and j.
|
||||
func (r *CryptoFrameRelocation) copyBytesToPool(iOuter, iInner, jOuter, jInner, size int) []byte {
|
||||
b := pool.Get(size)
|
||||
//io := r.o[iOuter]
|
||||
k := 0
|
||||
|
@ -8,6 +8,7 @@ package sniffing
|
||||
import (
|
||||
"github.com/mzz2017/softwind/pool"
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Sniffer struct {
|
||||
@ -15,12 +16,13 @@ type Sniffer struct {
|
||||
buf []byte
|
||||
bufAt int
|
||||
stream bool
|
||||
readMu sync.Mutex
|
||||
}
|
||||
|
||||
func NewStreamSniffer(r io.Reader, bufSize int) *Sniffer {
|
||||
s := &Sniffer{
|
||||
r: r,
|
||||
buf: pool.Get(bufSize),
|
||||
buf: make([]byte, bufSize),
|
||||
stream: true,
|
||||
}
|
||||
return s
|
||||
@ -37,6 +39,8 @@ func NewPacketSniffer(data []byte) *Sniffer {
|
||||
type sniff func() (d string, err error)
|
||||
|
||||
func (s *Sniffer) SniffTcp() (d string, err error) {
|
||||
s.readMu.Lock()
|
||||
defer s.readMu.Unlock()
|
||||
if s.stream {
|
||||
n, err := s.r.Read(s.buf)
|
||||
if err != nil {
|
||||
@ -65,6 +69,8 @@ func (s *Sniffer) SniffTcp() (d string, err error) {
|
||||
}
|
||||
|
||||
func (s *Sniffer) Read(p []byte) (n int, err error) {
|
||||
s.readMu.Lock()
|
||||
defer s.readMu.Unlock()
|
||||
if s.buf != nil && s.bufAt < len(s.buf) {
|
||||
// Read buf first.
|
||||
n = copy(p, s.buf[s.bufAt:])
|
||||
@ -84,6 +90,5 @@ func (s *Sniffer) Read(p []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (s *Sniffer) Close() (err error) {
|
||||
// DO NOT use pool.Put() here because Close() may not interrupt the reading, which will modify the value of the pool buffer.
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user