slimming/main.go
2022-08-26 16:53:05 +08:00

71 lines
1.4 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", "10.10.10.111")
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 %s\n", waterutil.IPv4Destination(frame), frame.Destination())
log.Printf("Src: %s %s\n", waterutil.IPv4Source(frame), frame.Source())
log.Printf("Ethertype: % x\n", frame.Ethertype())
// log.Printf("Payload: % x\n", frame.Payload())
ifce.Write(frame)
}
}