slimming/main.go
2022-08-29 09:26:16 +08:00

80 lines
1.5 KiB
Go

package main
import (
"flag"
"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()
}
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.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("DstMac: %s SrcMac: %s\n", waterutil.MACDestination(frame), waterutil.MACSource(frame))
log.Printf("DstIP: %s SrcIP: %s\n", waterutil.IPv4Destination(frame), waterutil.IPv4Source(frame))
log.Printf("Ethertype: % x\n", frame.Ethertype())
// log.Printf("Payload: % x\n", frame.Payload())
ifce.Write(frame)
}
}