slimming/netcard.go

194 lines
4.1 KiB
Go
Raw Normal View History

2022-08-29 06:54:52 +00:00
package main
import (
2022-09-01 02:42:06 +00:00
"encoding/binary"
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 10:10:27 +00:00
"time"
2022-08-29 06:54:52 +00:00
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"
2022-09-01 02:58:34 +00:00
"github.com/songgao/water/waterutil"
2022-08-29 06:54:52 +00:00
"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() {
2022-08-31 10:17:33 +00:00
time.Sleep(time.Second * 1)
2022-08-31 10:10:27 +00:00
log.Println("OnClose")
2022-08-31 10:08:59 +00:00
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 10:18:40 +00:00
log.Printf("% x len: %d", frame.Ethertype(), len(frame))
2022-08-31 08:28:43 +00:00
switch frame.Ethertype() {
case ethernet.ARP:
2022-08-31 10:11:50 +00:00
log.Printf("bytes len: %d type(arp): % x", len(rbuf), frame.Ethertype()) // 数据长度
2022-09-01 02:42:06 +00:00
SwapBytes(frame, 0, frame, 6, 6)
binary.BigEndian.PutUint16(frame[20:22], 2)
SwapBytes(frame, 22, frame, 32, 10)
nt.ifce.Write(frame)
2022-08-31 10:23:59 +00:00
//TODO: 解析arp 回应arp
2022-09-01 02:42:06 +00:00
// nt.clients.Range(func(key, value any) bool {
// if key == nt.ipv4key {
// return true
// }
// client := value.(*RPCClient)
// client.CheckConnect()
// client.Frame <- frame
// log.Println(client.realAddr)
// return true
// })
2022-08-31 08:28:43 +00:00
case ethernet.IPv4:
2022-08-31 10:11:50 +00:00
log.Printf("bytes len: %d type(ipv4): % x", len(rbuf), frame.Ethertype())
2022-09-01 03:01:33 +00:00
log.Println(frame.Source(), frame.Destination())
2022-09-01 02:58:34 +00:00
log.Printf("src: %s(%d) dst: %s(%d)", waterutil.IPv4Source(frame), waterutil.IPv4SourcePort(frame), waterutil.IPv4Destination(frame), waterutil.IPv4DestinationPort(frame))
2022-08-31 09:51:42 +00:00
log.Println(len(frame))
2022-09-01 02:58:34 +00:00
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
2022-08-31 10:14:31 +00:00
log.Printf("ifce write %d", len(frame))
2022-08-31 08:28:43 +00:00
_, 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 {
2022-08-31 10:23:59 +00:00
log.Printf("Start: %v", stream.Context())
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 10:14:31 +00:00
2022-08-31 08:28:43 +00:00
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
}