slimming/main.go

71 lines
1.4 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 (
"fmt"
"log"
"os/exec"
"strings"
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
"github.com/songgao/water/waterutil"
)
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-25 17:14:32 +00:00
// testmain()
2022-08-25 01:49:42 +00:00
NewNetCard().Run()
2022-08-23 09:58:07 +00:00
}
2022-08-25 17:14:32 +00:00
func testmain() {
var a chan bool = make(chan bool)
go func() {
<-a
2022-08-26 08:53:05 +00:00
cmdstr := fmt.Sprintf("ip addr add %s dev stun", "10.10.10.111")
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{
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]
2022-08-26 01:01:45 +00:00
log.Printf("Dst: %s %s\n", waterutil.IPv4Destination(frame), frame.Destination())
log.Printf("Src: %s %s\n", waterutil.IPv4Source(frame), frame.Source())
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
}
}