新TAP测试
This commit is contained in:
parent
41549b64a5
commit
8cc6d2ab29
|
@ -22,6 +22,7 @@ type Config struct {
|
||||||
Nodes []Node `yaml:"nodes"`
|
Nodes []Node `yaml:"nodes"`
|
||||||
} `yaml:"network"`
|
} `yaml:"network"`
|
||||||
|
|
||||||
|
CurrentNodeKey uint32
|
||||||
IPv4Nodes map[uint32]string
|
IPv4Nodes map[uint32]string
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
@ -50,10 +51,16 @@ func NewConfig() *Config {
|
||||||
log.Panic(err)
|
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)
|
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
|
return cnf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
32
netcard.go
32
netcard.go
|
@ -1,9 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"os/exec"
|
||||||
gen "slimming/proto/gen"
|
gen "slimming/proto/gen"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/474420502/check"
|
"github.com/474420502/check"
|
||||||
|
@ -20,6 +23,7 @@ type NetTunnel struct {
|
||||||
reader chan []byte
|
reader chan []byte
|
||||||
|
|
||||||
clients sync.Map
|
clients sync.Map
|
||||||
|
ipv4key any
|
||||||
|
|
||||||
gen.UnimplementedFrameServiceServer
|
gen.UnimplementedFrameServiceServer
|
||||||
}
|
}
|
||||||
|
@ -31,6 +35,7 @@ func NewNetTunnel() *NetTunnel {
|
||||||
reader: make(chan []byte, 1000),
|
reader: make(chan []byte, 1000),
|
||||||
shutdown: perfectshutdown.New(),
|
shutdown: perfectshutdown.New(),
|
||||||
clients: sync.Map{},
|
clients: sync.Map{},
|
||||||
|
ipv4key: config.CurrentNodeKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
for ipv4, address := range config.IPv4Nodes {
|
for ipv4, address := range config.IPv4Nodes {
|
||||||
|
@ -48,6 +53,22 @@ func NewNetTunnel() *NetTunnel {
|
||||||
log.Panic(err)
|
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() {
|
go func() {
|
||||||
|
|
||||||
for rbuf := range nt.reader {
|
for rbuf := range nt.reader {
|
||||||
|
@ -57,9 +78,18 @@ func NewNetTunnel() *NetTunnel {
|
||||||
|
|
||||||
switch frame.Ethertype() {
|
switch frame.Ethertype() {
|
||||||
case ethernet.ARP:
|
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:
|
case ethernet.IPv4:
|
||||||
|
log.Println(len(frame))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: 判断地址 转入对应的客户端发送数据
|
// TODO: 判断地址 转入对应的客户端发送数据
|
||||||
|
|
Loading…
Reference in New Issue
Block a user