2022-08-25 01:49:42 +00:00
|
|
|
package main
|
2022-08-23 09:58:07 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-25 01:49:42 +00:00
|
|
|
"context"
|
2022-08-23 09:58:07 +00:00
|
|
|
"log"
|
2022-08-25 01:49:42 +00:00
|
|
|
gen "slimming/proto/gen"
|
2022-08-23 09:58:07 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/songgao/packets/ethernet"
|
|
|
|
"github.com/songgao/water"
|
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 {
|
|
|
|
FrameChan chan [][]byte
|
|
|
|
ifce *water.Interface
|
2022-08-25 01:49:42 +00:00
|
|
|
cli *RPCClient
|
|
|
|
server *RPCServer
|
2022-08-23 09:58:07 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 01:49:42 +00:00
|
|
|
type RPCClient struct {
|
|
|
|
FrameChan chan [][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *RPCClient) run() {
|
|
|
|
log.Println("rpcclient start")
|
|
|
|
defer log.Println("rpcclient exit")
|
|
|
|
|
|
|
|
conn, err := grpc.Dial(*othersAddr,
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("did not connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
c := gen.NewFrameServiceClient(conn)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
stream, err := c.SendFrames(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Contact the server and print out its response.
|
|
|
|
|
|
|
|
// 发到对面的网卡
|
|
|
|
err = stream.Send(&gen.Request{
|
|
|
|
Frames: <-cli.FrameChan,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nc *NetCard) Run() {
|
|
|
|
go nc.runRead()
|
|
|
|
go nc.runWrite()
|
|
|
|
go nc.cli.run()
|
|
|
|
nc.server.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNetCard() *NetCard {
|
|
|
|
|
2022-08-23 09:58:07 +00:00
|
|
|
config := water.Config{
|
|
|
|
DeviceType: water.TAP,
|
|
|
|
}
|
|
|
|
config.Name = "stap"
|
|
|
|
|
|
|
|
ifce, err := water.New(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nc := &NetCard{
|
|
|
|
FrameChan: make(chan [][]byte, 2000),
|
|
|
|
ifce: ifce,
|
2022-08-25 01:49:42 +00:00
|
|
|
cli: &RPCClient{FrameChan: make(chan [][]byte, 2000)},
|
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-23 09:58:07 +00:00
|
|
|
|
|
|
|
var ifce *water.Interface = nc.ifce
|
|
|
|
var ticker time.Ticker = *time.NewTicker(time.Millisecond * 20)
|
|
|
|
for {
|
|
|
|
|
|
|
|
var framesBytes [][]byte
|
|
|
|
for range ticker.C {
|
|
|
|
var rframe ethernet.Frame
|
|
|
|
rframe.Resize(1500)
|
|
|
|
n, err := ifce.Read([]byte(rframe))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
rframe = rframe[:n]
|
|
|
|
framesBytes = append(framesBytes, []byte(rframe))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(framesBytes) > 0 {
|
2022-08-25 01:49:42 +00:00
|
|
|
nc.cli.FrameChan <- framesBytes // 网卡数据 发到对方
|
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
|
|
|
|
|
|
|
|
for wframes := range nc.FrameChan {
|
|
|
|
for _, wframe := range wframes {
|
|
|
|
_, err := ifce.Write(wframe)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|