package main import ( "encoding/binary" "flag" "fmt" "log" "net" "os/exec" "strings" "github.com/songgao/packets/ethernet" "github.com/songgao/water" ) func init() { log.SetFlags(log.Llongfile | log.LstdFlags) } //go:generate bash -c "protoc --go_out=plugins=grpc:. proto/*.proto" func main() { // testmain() NewNetTunnel().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] if frame.Ethertype() == ethernet.ARP || frame.Ethertype() == ethernet.AARP { log.Println(binary.BigEndian.Uint16(frame[20:22]), net.IP(frame[28:32]).To4(), net.HardwareAddr(frame[22:28]), net.IP(frame[38:42]).To4(), net.HardwareAddr(frame[32:38])) binary.BigEndian.PutUint16(frame[20:22], 2) copy(frame[32:38], frame[22:28]) log.Println(binary.BigEndian.Uint16(frame[20:22]), net.IP(frame[28:32]).To4(), net.HardwareAddr(frame[22:28]), net.IP(frame[38:42]).To4(), net.HardwareAddr(frame[32:38])) log.Printf("type: % x src %s dst %s len %d", frame.Ethertype(), frame.Source(), frame.Destination(), len(frame)) ifce.Write(frame) } // if waterutil.IsIPv4(frame) && strings.HasPrefix(waterutil.IPv4Destination(frame).String(), "10.10") { // log.Printf("Src: %s(%d) Dst: %s(%d) \n", waterutil.IPv4Source(frame), waterutil.IPv4SourcePort(frame), waterutil.IPv4Destination(frame), waterutil.IPv4DestinationPort(frame)) // waterutil.SetIPv4Destination(frame, waterutil.IPv4Source(frame)) // // ifce.Write(frame) // // log.Printf("DstPort: %d SrcPort: %d\n", (uint16(payload[2])<<8)|uint16(payload[3]), (uint16(payload[0])<<8)|uint16(payload[1])) // } // log.Printf("Payload: % x\n", frame.Payload()) } }