slimming/main.go

80 lines
1.5 KiB
Go
Raw Normal View History

2022-08-25 01:49:42 +00:00
package main
2022-08-23 09:58:07 +00:00
2022-08-25 17:14:32 +00:00
import (
2022-08-29 01:11:46 +00:00
"flag"
2022-08-25 17:14:32 +00:00
"fmt"
"log"
"os/exec"
"strings"
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
2022-08-29 01:19:16 +00:00
"github.com/songgao/water/waterutil"
2022-08-25 17:14:32 +00:00
)
2022-08-25 02:02:19 +00:00
func init() {
log.SetFlags(log.Llongfile | log.LstdFlags)
}
2022-08-23 09:58:07 +00:00
//go:generate bash -c "protoc --go_out=plugins=grpc:. proto/*.proto"
func main() {
2022-08-29 01:11:46 +00:00
testmain()
// NewNetCard().Run()
2022-08-23 09:58:07 +00:00
}
2022-08-25 17:14:32 +00:00
2022-08-29 01:11:46 +00:00
var (
flagAddr = flag.String("addr", "", "addr set")
)
2022-08-25 17:14:32 +00:00
func testmain() {
2022-08-29 01:11:46 +00:00
flag.Parse()
2022-08-25 17:14:32 +00:00
var a chan bool = make(chan bool)
go func() {
<-a
2022-08-29 01:11:46 +00:00
cmdstr := fmt.Sprintf("ip addr add %s dev stun", *flagAddr)
2022-08-25 17:14:32 +00:00
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{
2022-08-29 01:26:16 +00:00
DeviceType: water.TUN,
2022-08-25 17:14:32 +00:00
}
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]
2022-08-29 01:15:33 +00:00
2022-08-29 01:19:16 +00:00
log.Printf("DstMac: %s SrcMac: %s\n", waterutil.MACDestination(frame), waterutil.MACSource(frame))
2022-08-29 01:26:16 +00:00
log.Printf("DstIP: %s SrcIP: %s\n", waterutil.IPv4Destination(frame), waterutil.IPv4Source(frame))
2022-08-25 17:14:32 +00:00
log.Printf("Ethertype: % x\n", frame.Ethertype())
// log.Printf("Payload: % x\n", frame.Payload())
2022-08-26 08:53:05 +00:00
ifce.Write(frame)
2022-08-25 17:14:32 +00:00
}
}