slimming/main.go

83 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 10:03:34 +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-29 07:14:44 +00:00
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)
}
}
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
config := water.Config{
2022-08-29 10:01:29 +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)
}
2022-08-29 07:14:44 +00:00
startCard()
var frame ethernet.Frame
2022-08-25 17:14:32 +00:00
for {
2022-08-29 07:14:44 +00:00
2022-08-25 17:14:32 +00:00
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 10:01:29 +00:00
if frame.Ethertype() != ethernet.ARP {
log.Printf("Ethertype: % x\n", frame.Ethertype())
log.Printf("DstMac: %s SrcMac: %s\n", waterutil.IPv4Destination(frame), waterutil.IPv4Source(frame))
log.Printf("DstPort: %d SrcPort: %d\n", waterutil.IPv4DestinationPort(frame), waterutil.IPv4SourcePort(frame))
}
2022-08-25 17:14:32 +00:00
// log.Printf("Payload: % x\n", frame.Payload())
2022-08-26 08:53:05 +00:00
2022-08-29 01:33:06 +00:00
ifce.Write(frame)
2022-08-25 17:14:32 +00:00
}
}