slimming/main.go
huangsimin 28df4d850d 成功
2022-08-26 01:14:32 +08:00

70 lines
1.3 KiB
Go

package main
import (
"fmt"
"log"
"os/exec"
"strings"
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
"github.com/songgao/water/waterutil"
)
func init() {
log.SetFlags(log.Llongfile | log.LstdFlags)
}
//go:generate bash -c "protoc --go_out=plugins=grpc:. proto/*.proto"
func main() {
// testmain()
NewNetCard().Run()
}
func testmain() {
var a chan bool = make(chan bool)
go func() {
<-a
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"
cmd = strings.Split(cmdstr, " ")
err = exec.Command(cmd[0], cmd[1:]...).Run()
if err != nil {
log.Panic(err)
}
}()
config := water.Config{
DeviceType: water.TUN,
}
config.Name = "stun"
ifce, err := water.New(config)
if err != nil {
log.Fatal(err)
}
a <- true
var frame ethernet.Frame
for {
frame.Resize(1500)
n, err := ifce.Read([]byte(frame))
if err != nil {
log.Fatal(err)
}
frame = frame[:n]
log.Printf("Dst: %s\n", waterutil.IPv4Destination(frame))
log.Printf("Src: %s\n", waterutil.IPv4Source(frame))
log.Printf("Ethertype: % x\n", frame.Ethertype())
// log.Printf("Payload: % x\n", frame.Payload())
}
}