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: 判断地址 转入对应的客户端发送数据