From 8cc6d2ab29185bed1e37a53fae00838420826fcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=80=9D=E6=95=8F?= Date: Wed, 31 Aug 2022 17:51:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0TAP=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 13 ++++++++++--- netcard.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 49fd6b8..59f6767 100644 --- a/config.go +++ b/config.go @@ -22,8 +22,9 @@ type Config struct { Nodes []Node `yaml:"nodes"` } `yaml:"network"` - IPv4Nodes map[uint32]string - lock sync.Mutex + CurrentNodeKey uint32 + IPv4Nodes map[uint32]string + lock sync.Mutex } var config = NewConfig() @@ -50,10 +51,16 @@ func NewConfig() *Config { log.Panic(err) } - cnf.IPv4Nodes[binary.LittleEndian.Uint32([]byte(inet.To4()))] = node.Real + cnf.IPv4Nodes[binary.BigEndian.Uint32([]byte(inet.To4()))] = node.Real log.Printf("real %v virt %v %v", node.Real, inet, net) } + inet, _, err := net.ParseCIDR(cnf.Network.Self.Virt) + if err != nil { + log.Panic(err) + } + cnf.CurrentNodeKey = binary.BigEndian.Uint32([]byte(inet.To4())) + return cnf } diff --git a/netcard.go b/netcard.go index 6248acf..be88d4a 100644 --- a/netcard.go +++ b/netcard.go @@ -1,9 +1,12 @@ package main import ( + "fmt" "log" "net" + "os/exec" gen "slimming/proto/gen" + "strings" "sync" "github.com/474420502/check" @@ -20,6 +23,7 @@ type NetTunnel struct { reader chan []byte clients sync.Map + ipv4key any gen.UnimplementedFrameServiceServer } @@ -31,6 +35,7 @@ func NewNetTunnel() *NetTunnel { reader: make(chan []byte, 1000), shutdown: perfectshutdown.New(), clients: sync.Map{}, + ipv4key: config.CurrentNodeKey, } for ipv4, address := range config.IPv4Nodes { @@ -48,6 +53,22 @@ func NewNetTunnel() *NetTunnel { log.Panic(err) } + 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) + } + go func() { for rbuf := range nt.reader { @@ -57,9 +78,18 @@ func NewNetTunnel() *NetTunnel { switch frame.Ethertype() { case ethernet.ARP: + nt.clients.Range(func(key, value any) bool { + if key == nt.ipv4key { + return true + } + client := value.(*RPCClient) + client.CheckConnect() + client.Frame <- frame + return true + }) case ethernet.IPv4: - + log.Println(len(frame)) } // TODO: 判断地址 转入对应的客户端发送数据