This commit is contained in:
黄思敏 2022-08-30 18:08:03 +08:00
parent ba3d6a6799
commit 84557cef46

33
main.go
View File

@ -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)
}
}