From 84557cef46935e1d240f8eaf679ec0ee582beecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=80=9D=E6=95=8F?= Date: Tue, 30 Aug 2022 18:08:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 8c7fdbc..ea6ee86 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,16 @@ package main import ( + "encoding/binary" "flag" "fmt" "log" + "net" "os/exec" "strings" "github.com/songgao/packets/ethernet" "github.com/songgao/water" - "github.com/songgao/water/waterutil" ) func init() { @@ -18,8 +19,8 @@ func init() { //go:generate bash -c "protoc --go_out=plugins=grpc:. proto/*.proto" func main() { - // testmain() - NewNetCard().Run() + testmain() + // NewNetCard().Run() } var ( @@ -47,7 +48,7 @@ func testmain() { flag.Parse() config := water.Config{ - DeviceType: water.TUN, + DeviceType: water.TAP, } config.Name = "stun" @@ -68,15 +69,29 @@ func testmain() { } frame = frame[:n] - if frame.Ethertype() != ethernet.ARP { - log.Printf("Ethertype: % x\n", frame.Ethertype()) + if frame.Ethertype() == ethernet.ARP || frame.Ethertype() == ethernet.AARP { - 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)) + 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()) - ifce.Write(frame) } }