新TAP测试(TODO 解析arp)

This commit is contained in:
黄思敏 2022-09-01 16:19:32 +08:00
parent 81aa28f1cb
commit 214e1637fe
3 changed files with 16 additions and 46 deletions

View File

@ -209,7 +209,8 @@ func (nt *NetTunnel) SendFrames(stream gen.FrameService_SendFramesServer) error
log.Panic(err) log.Panic(err)
} }
nt.writer <- Decompress(request.GetFrames()) buf, _ := Decompress(request.GetFrames())
nt.writer <- buf
// frames := Decompress(request.GetFrames()) // frames := Decompress(request.GetFrames())
// if len(frames) > 0 { // if len(frames) > 0 {

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"context" "context"
"log" "log"
gen "slimming/proto/gen" gen "slimming/proto/gen"
@ -102,7 +101,7 @@ func (cli *RPCClient) run() {
// var ticker = time.NewTicker(time.Millisecond * 1) // var ticker = time.NewTicker(time.Millisecond * 1)
buffer := bytes.NewBuffer(nil) // buffer := bytes.NewBuffer(nil)
for buf := range cli.Frames { for buf := range cli.Frames {
// select { // select {
@ -131,11 +130,9 @@ func (cli *RPCClient) run() {
// continue // continue
// } // }
Compress(buffer, buf)
// 发到对面的网卡 // 发到对面的网卡
err = stream.Send(&gen.RequestFrames{ err = stream.Send(&gen.RequestFrames{
Frames: buffer.Bytes(), Frames: Compress(buf),
}) })
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)

View File

@ -1,56 +1,28 @@
package main package main
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"io"
"log"
"net" "net"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
) )
func Compress(writer io.Writer, data []byte) { var encoder, _ = zstd.NewWriter(nil)
// encode gob
// enc := gob.NewEncoder(writer)
// err := enc.Encode(data)
// if err != nil {
// log.Panic(err)
// }
// zstd compress
zenc, err := zstd.NewWriter(writer)
if err != nil {
log.Panic(err)
}
zenc.Write(data)
err = zenc.Flush()
if err != nil {
log.Println(err)
}
// Compress a buffer.
// If you have a destination buffer, the allocation in the call can also be eliminated.
func Compress(src []byte) []byte {
return encoder.EncodeAll(src, make([]byte, 0, len(src)))
} }
func Decompress(data []byte) (frames []byte) { // Create a reader that caches decompressors.
var err error // For this operation type we supply a nil Reader.
var decoder, _ = zstd.NewReader(nil, zstd.WithDecoderConcurrency(0))
var buf = bytes.NewBuffer(data) // Decompress a buffer. We don't supply a destination buffer,
var zdec *zstd.Decoder // so it will be allocated by the decoder.
zdec, err = zstd.NewReader(buf) func Decompress(src []byte) ([]byte, error) {
if err != nil { return decoder.DecodeAll(src, nil)
log.Panic(err)
}
zdec.Close()
frames = buf.Bytes()
// dec := gob.NewDecoder(buf)
// err = dec.Decode(&frames)
// if err != nil {
// log.Panic(err)
// }
return
} }
func SwapBytes(frame1 []byte, start1 int, frame2 []byte, start2 int, size int) { func SwapBytes(frame1 []byte, start1 int, frame2 []byte, start2 int, size int) {