slimming/main.go
2022-08-29 09:15:33 +08:00

78 lines
1.3 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"os/exec"
"strings"
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
)
func init() {
log.SetFlags(log.Llongfile | log.LstdFlags)
}
//go:generate bash -c "protoc --go_out=plugins=grpc:. proto/*.proto"
func main() {
testmain()
// NewNetCard().Run()
}
var (
flagAddr = flag.String("addr", "", "addr set")
)
func testmain() {
flag.Parse()
var a chan bool = make(chan bool)
go func() {
<-a
cmdstr := fmt.Sprintf("ip addr add %s dev stun", *flagAddr)
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.TAP,
}
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 Src: %s\n", frame.Destination(), frame.Source())
log.Printf("Ethertype: % x\n", frame.Ethertype())
// log.Printf("Payload: % x\n", frame.Payload())
ifce.Write(frame)
}
}