slimming/main.go

98 lines
2.3 KiB
Go
Raw Permalink 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-30 10:08:03 +00:00
"encoding/binary"
2022-08-29 01:11:46 +00:00
"flag"
2022-08-25 17:14:32 +00:00
"fmt"
"log"
2022-08-30 10:08:03 +00:00
"net"
2022-08-25 17:14:32 +00:00
"os/exec"
"strings"
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
)
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-31 08:28:43 +00:00
// testmain()
NewNetTunnel().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-30 10:08:03 +00:00
DeviceType: water.TAP,
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-30 10:08:03 +00:00
if frame.Ethertype() == ethernet.ARP || frame.Ethertype() == ethernet.AARP {
2022-08-29 10:01:29 +00:00
2022-08-30 10:08:03 +00:00
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)
2022-08-29 10:01:29 +00:00
}
2022-08-30 10:08:03 +00:00
// 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]))
// }
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-25 17:14:32 +00:00
}
}