slimming/main.go
2022-08-29 15:14:44 +08:00

79 lines
1.4 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 startCard() {
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)
}
}
func testmain() {
flag.Parse()
config := water.Config{
DeviceType: water.TAP,
}
config.Name = "stun"
ifce, err := water.New(config)
if err != nil {
log.Fatal(err)
}
startCard()
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)
}
}