slimming/netcard.go

175 lines
3.4 KiB
Go
Raw Normal View History

2022-08-29 06:54:52 +00:00
package main
import (
2022-08-31 09:51:42 +00:00
"fmt"
2022-08-29 06:54:52 +00:00
"log"
"net"
2022-08-31 10:08:59 +00:00
"os"
2022-08-31 09:51:42 +00:00
"os/exec"
2022-08-29 06:54:52 +00:00
gen "slimming/proto/gen"
2022-08-31 09:51:42 +00:00
"strings"
2022-08-29 06:54:52 +00:00
"sync"
2022-08-31 08:28:43 +00:00
"github.com/474420502/check"
"github.com/474420502/perfectshutdown"
2022-08-29 06:54:52 +00:00
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
"google.golang.org/grpc"
)
2022-08-31 08:28:43 +00:00
type NetTunnel struct {
ifce *water.Interface
shutdown *perfectshutdown.PerfectShutdown
writer chan []byte
reader chan []byte
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
clients sync.Map
2022-08-31 09:51:42 +00:00
ipv4key any
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
gen.UnimplementedFrameServiceServer
2022-08-29 06:54:52 +00:00
}
2022-08-31 08:28:43 +00:00
func NewNetTunnel() *NetTunnel {
var err error
nt := &NetTunnel{
writer: make(chan []byte, 1000),
reader: make(chan []byte, 1000),
shutdown: perfectshutdown.New(),
clients: sync.Map{},
2022-08-31 09:51:42 +00:00
ipv4key: config.CurrentNodeKey,
2022-08-29 06:54:52 +00:00
}
2022-08-31 10:08:59 +00:00
nt.shutdown.OnClose(func() {
os.Exit(0)
})
2022-08-31 08:28:43 +00:00
for ipv4, address := range config.IPv4Nodes {
nt.clients.Store(ipv4, NewRPCClient(address))
2022-08-29 06:54:52 +00:00
}
2022-08-31 08:28:43 +00:00
// log.Println(config)
nt.ifce, err = water.New(water.Config{
DeviceType: water.TAP,
2022-08-29 06:54:52 +00:00
PlatformSpecificParams: water.PlatformSpecificParams{
2022-08-29 07:25:47 +00:00
Name: "stun",
2022-08-29 06:54:52 +00:00
},
2022-08-31 08:28:43 +00:00
})
2022-08-29 06:54:52 +00:00
if err != nil {
log.Panic(err)
}
2022-08-31 09:51:42 +00:00
cmdstr := fmt.Sprintf("ip addr add %s dev stun", config.Network.Self.Virt)
log.Println(cmdstr)
cmd := strings.Split(cmdstr, " ")
err = exec.Command(cmd[0], cmd[1:]...).Run()
if err != nil {
log.Panic(err)
}
cmdstr = "ip link set dev stun up"
log.Println(cmdstr)
cmd = strings.Split(cmdstr, " ")
err = exec.Command(cmd[0], cmd[1:]...).Run()
if err != nil {
log.Panic(err)
}
2022-08-31 08:28:43 +00:00
go func() {
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
for rbuf := range nt.reader {
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
var frame ethernet.Frame = rbuf
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
switch frame.Ethertype() {
case ethernet.ARP:
2022-08-31 09:54:00 +00:00
log.Printf("bytes len: %d type: % x", len(rbuf), frame) // 数据长度
2022-08-31 09:51:42 +00:00
nt.clients.Range(func(key, value any) bool {
if key == nt.ipv4key {
return true
}
client := value.(*RPCClient)
client.CheckConnect()
client.Frame <- frame
return true
})
2022-08-31 08:28:43 +00:00
case ethernet.IPv4:
2022-08-31 09:54:00 +00:00
log.Printf("bytes len: %d type: % x", len(rbuf), frame)
2022-08-31 09:51:42 +00:00
log.Println(len(frame))
2022-08-29 06:54:52 +00:00
}
2022-08-31 08:28:43 +00:00
// TODO: 判断地址 转入对应的客户端发送数据
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
if nt.shutdown.IsClose() {
break
2022-08-29 06:54:52 +00:00
}
}
2022-08-31 08:28:43 +00:00
}()
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
go func() {
for wbuf := range nt.writer {
var frame ethernet.Frame = wbuf
_, err := nt.ifce.Write(frame)
if err != nil {
log.Println(err)
2022-08-29 06:54:52 +00:00
}
2022-08-31 08:28:43 +00:00
if nt.shutdown.IsClose() {
break
2022-08-29 06:54:52 +00:00
}
}
2022-08-31 08:28:43 +00:00
}()
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
return nt
2022-08-29 06:54:52 +00:00
}
2022-08-31 08:28:43 +00:00
func (nt *NetTunnel) Run() {
var (
frame ethernet.Frame
err error
n int
)
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
lis, err := net.Listen("tcp", config.Network.Self.Real)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
server := grpc.NewServer()
gen.RegisterFrameServiceServer(server, nt)
log.Printf("server listening at %v", lis.Addr())
defer server.Stop()
go func() {
if err := server.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
nt.shutdown.Loop(func(index int, ps *perfectshutdown.PerfectShutdown) {
frame.Resize(1500)
n, err = nt.ifce.Read(frame)
check.CheckPanic(err)
nt.reader <- frame[0:n] // 从网卡里读数据
})
}
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
func (nt *NetTunnel) SendFrames(stream gen.FrameService_SendFramesServer) error {
log.Printf("Start: %v", stream)
2022-08-30 03:36:13 +00:00
2022-08-31 08:28:43 +00:00
nt.shutdown.Loop(func(index int, ps *perfectshutdown.PerfectShutdown) {
request, err := stream.Recv()
2022-08-29 06:54:52 +00:00
if err != nil {
log.Panic(err)
}
2022-08-31 08:28:43 +00:00
log.Printf("request: %v", len(request.Frames))
frames := Decompress(request.GetFrames())
if len(frames) > 0 {
for _, frame := range frames {
nt.writer <- frame
2022-08-29 06:54:52 +00:00
}
}
2022-08-31 08:28:43 +00:00
})
2022-08-29 06:54:52 +00:00
2022-08-31 08:28:43 +00:00
return nil
2022-08-29 06:54:52 +00:00
}