slimming/tap.go

241 lines
4.5 KiB
Go
Raw Normal View History

2022-08-25 01:49:42 +00:00
package main
2022-08-23 09:58:07 +00:00
import (
2022-08-25 07:31:51 +00:00
"bytes"
2022-08-25 01:49:42 +00:00
"context"
2022-08-25 07:31:51 +00:00
"encoding/gob"
2022-08-25 04:24:57 +00:00
"flag"
2022-08-25 04:21:50 +00:00
"fmt"
2022-08-23 09:58:07 +00:00
"log"
2022-08-25 02:24:31 +00:00
"os/exec"
2022-08-25 01:49:42 +00:00
gen "slimming/proto/gen"
2022-08-25 02:24:31 +00:00
"strings"
2022-08-25 03:54:48 +00:00
"sync/atomic"
2022-08-23 09:58:07 +00:00
"time"
2022-08-25 07:31:51 +00:00
"github.com/klauspost/compress/zstd"
2022-08-23 09:58:07 +00:00
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
2022-08-25 17:14:32 +00:00
"github.com/songgao/water/waterutil"
2022-08-25 01:49:42 +00:00
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
2022-08-23 09:58:07 +00:00
)
type NetCard struct {
2022-08-25 07:31:51 +00:00
FrameChan chan []byte
2022-08-23 09:58:07 +00:00
ifce *water.Interface
2022-08-25 01:49:42 +00:00
server *RPCServer
2022-08-25 07:31:51 +00:00
clientMap map[string]*RPCClient
2022-08-23 09:58:07 +00:00
}
2022-08-25 01:49:42 +00:00
type RPCClient struct {
FrameChan chan [][]byte
2022-08-25 02:12:43 +00:00
conn *grpc.ClientConn
2022-08-25 01:49:42 +00:00
}
2022-08-25 07:31:51 +00:00
func (cli *RPCClient) connect(addr string) {
2022-08-25 01:49:42 +00:00
log.Println("rpcclient start")
2022-08-25 04:09:48 +00:00
// defer log.Println("rpcclient exit")
2022-08-25 01:49:42 +00:00
2022-08-25 07:31:51 +00:00
conn, err := grpc.Dial(addr,
2022-08-25 01:49:42 +00:00
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
2022-08-25 02:12:43 +00:00
log.Printf("did not connect: %v", err)
return
2022-08-25 01:49:42 +00:00
}
2022-08-25 03:54:48 +00:00
2022-08-25 02:12:43 +00:00
cli.conn = conn
go cli.run()
2022-08-25 03:54:48 +00:00
time.Sleep(time.Millisecond)
2022-08-25 02:12:43 +00:00
}
func (cli *RPCClient) run() {
2022-08-25 04:09:48 +00:00
defer log.Println("rpcclient exit")
2022-08-25 03:54:48 +00:00
defer func() {
if err := recover(); err != nil {
log.Println("recover")
cli.conn = nil
}
}()
2022-08-25 02:12:43 +00:00
c := gen.NewFrameServiceClient(cli.conn)
2022-08-25 04:05:24 +00:00
stream, err := c.SendFrames(context.Background())
2022-08-25 01:49:42 +00:00
if err != nil {
2022-08-25 03:54:48 +00:00
log.Panic(err)
2022-08-25 01:49:42 +00:00
}
2022-08-25 07:31:51 +00:00
buf := bytes.NewBuffer(nil)
2022-08-25 01:49:42 +00:00
for {
// Contact the server and print out its response.
2022-08-25 07:31:51 +00:00
buf.Reset()
zenc, err := zstd.NewWriter(buf)
if err != nil {
log.Panic(err)
}
enc := gob.NewEncoder(zenc)
enc.Encode(<-cli.FrameChan)
2022-08-25 01:49:42 +00:00
// 发到对面的网卡
err = stream.Send(&gen.Request{
2022-08-25 07:31:51 +00:00
Frames: buf.Bytes(),
2022-08-25 01:49:42 +00:00
})
if err != nil {
2022-08-25 03:54:48 +00:00
log.Panic(err)
2022-08-25 01:49:42 +00:00
}
}
}
func (nc *NetCard) Run() {
go nc.runRead()
go nc.runWrite()
2022-08-25 02:12:43 +00:00
// go nc.cli.run()
2022-08-25 02:24:31 +00:00
time.Sleep(time.Second)
2022-08-25 07:31:51 +00:00
2022-08-25 17:14:32 +00:00
cmdstr := fmt.Sprintf("ip addr add %s dev stun", config.Network.Self.Virt)
2022-08-25 07:37:55 +00:00
log.Println(cmdstr)
cmd := strings.Split(cmdstr, " ")
2022-08-25 02:24:31 +00:00
err := exec.Command(cmd[0], cmd[1:]...).Run()
if err != nil {
2022-08-25 03:54:48 +00:00
log.Panic(err)
2022-08-25 02:24:31 +00:00
}
2022-08-25 07:37:55 +00:00
2022-08-25 17:14:32 +00:00
cmdstr = "ip link set dev stun up"
2022-08-25 07:37:55 +00:00
cmd = strings.Split(cmdstr, " ")
2022-08-25 02:24:31 +00:00
err = exec.Command(cmd[0], cmd[1:]...).Run()
if err != nil {
2022-08-25 03:54:48 +00:00
log.Panic(err)
2022-08-25 02:24:31 +00:00
}
2022-08-25 01:49:42 +00:00
nc.server.run()
}
func NewNetCard() *NetCard {
2022-08-25 04:24:57 +00:00
flag.Parse()
2022-08-25 01:49:42 +00:00
2022-08-23 09:58:07 +00:00
config := water.Config{
2022-08-25 10:21:41 +00:00
DeviceType: water.TUN,
2022-08-23 09:58:07 +00:00
}
2022-08-25 17:14:32 +00:00
config.Name = "stun"
2022-08-23 09:58:07 +00:00
ifce, err := water.New(config)
if err != nil {
2022-08-25 03:54:48 +00:00
log.Panic(err)
2022-08-23 09:58:07 +00:00
}
nc := &NetCard{
2022-08-25 07:31:51 +00:00
FrameChan: make(chan []byte, 3000),
2022-08-23 09:58:07 +00:00
ifce: ifce,
2022-08-25 07:31:51 +00:00
clientMap: map[string]*RPCClient{},
2022-08-23 09:58:07 +00:00
}
2022-08-25 01:49:42 +00:00
nc.server = newRPCServer(nc)
2022-08-23 09:58:07 +00:00
return nc
}
2022-08-25 01:49:42 +00:00
func (nc *NetCard) runRead() {
2022-08-25 03:54:48 +00:00
log.Println("start netcard read")
2022-08-23 09:58:07 +00:00
var ifce *water.Interface = nc.ifce
2022-08-25 03:54:48 +00:00
2022-08-23 09:58:07 +00:00
for {
2022-08-25 07:31:51 +00:00
var bytesMap map[string]*[][]byte = make(map[string]*[][]byte)
var ok bool
2022-08-25 03:54:48 +00:00
var isLoop int32 = 1
go func() {
var after = time.NewTimer(time.Millisecond * 20)
<-after.C
atomic.StoreInt32(&isLoop, 0)
}()
for atomic.LoadInt32(&isLoop) > 0 {
2022-08-23 09:58:07 +00:00
var rframe ethernet.Frame
rframe.Resize(1500)
2022-08-25 17:14:32 +00:00
2022-08-23 09:58:07 +00:00
n, err := ifce.Read([]byte(rframe))
if err != nil {
log.Fatal(err)
}
2022-08-25 17:14:32 +00:00
rframe = rframe[:n]
2022-08-25 07:31:51 +00:00
2022-08-25 17:14:32 +00:00
// log.Printf("Payload: % x\n", rframe.Payload())
2022-08-25 10:03:44 +00:00
2022-08-25 17:14:32 +00:00
log.Printf("Dst: %s\n", waterutil.IPv4Destination(rframe))
log.Printf("Src: %s\n", waterutil.IPv4Source(rframe))
log.Printf("Ethertype: % x\n", rframe.Ethertype())
2022-08-25 10:03:44 +00:00
2022-08-25 07:31:51 +00:00
var buffer *[][]byte
2022-08-25 17:14:32 +00:00
var key = waterutil.IPv4Destination(rframe).String()
if buffer, ok = bytesMap[key]; !ok {
mbuffer := make([][]byte, 0, 100)
2022-08-25 09:42:58 +00:00
buffer = &mbuffer
2022-08-25 17:14:32 +00:00
bytesMap[key] = buffer
2022-08-25 07:31:51 +00:00
}
*buffer = append(*buffer, []byte(rframe))
2022-08-23 09:58:07 +00:00
}
2022-08-25 07:31:51 +00:00
for dst, buffer := range bytesMap {
var cli *RPCClient
if cli, ok = nc.clientMap[dst]; !ok {
cli = &RPCClient{FrameChan: make(chan [][]byte, 1000)}
nc.clientMap[dst] = cli
2022-08-25 02:12:43 +00:00
}
2022-08-25 07:31:51 +00:00
if cli.conn == nil {
cli.connect(dst)
} else {
cli.FrameChan <- *buffer // 网卡数据 发到对方
2022-08-25 17:14:32 +00:00
*buffer = (*buffer)[0:]
2022-08-25 02:12:43 +00:00
}
2022-08-25 07:31:51 +00:00
2022-08-23 09:58:07 +00:00
}
// 写到grpc服务
// log.Printf("Dst: %s\n", rframe.Destination()[0:4])
// log.Printf("Src: %s\n", rframe.Source()[0:4])
// log.Printf("Ethertype: % x\n", rframe.Ethertype())
// log.Printf("Payload: % x\n", rframe.Payload())
}
}
2022-08-25 01:49:42 +00:00
func (nc *NetCard) runWrite() {
2022-08-23 09:58:07 +00:00
var ifce *water.Interface = nc.ifce
2022-08-25 07:31:51 +00:00
var buf = bytes.NewBuffer(nil)
for wframe := range nc.FrameChan {
log.Printf("get wframes bytes len: %d", len(wframe))
buf.Reset()
buf.Write(wframe)
zdec, err := zstd.NewReader(buf)
if err != nil {
log.Panic(err)
}
dec := gob.NewDecoder(zdec)
var bufs [][]byte
err = dec.Decode(&bufs)
if err != nil {
log.Panic(err)
}
for _, buf := range bufs {
_, err := ifce.Write(buf)
2022-08-23 09:58:07 +00:00
if err != nil {
2022-08-25 07:31:51 +00:00
log.Panic(err)
2022-08-23 09:58:07 +00:00
}
}
}
}