예제 #1
0
func main() {
	if len(os.Args) != 3 {
		fmt.Println("syntax:", os.Args[0], "tun|tap", "<device name>")
		return
	}

	var typ tuntap.DevKind
	switch os.Args[1] {
	case "tun":
		typ = tuntap.DevTun
	case "tap":
		typ = tuntap.DevTap
	default:
		fmt.Println("Unknown device type", os.Args[1])
		return
	}

	tun, err := tuntap.Open(os.Args[2], typ)
	if err != nil {
		fmt.Println("Error opening tun/tap device:", err)
		return
	}

	fmt.Println("Listening on", string(tun.Name()))
	buf := make([]byte, 1522)
	for {
		n, err := tun.Read(buf)
		if err != nil {
			fmt.Println("Read error:", err)
		} else {
			if util.IsIPv4(buf) {
				fmt.Printf("%d bytes from iface, IHL:%02X, TTL:%d\n", n, buf[0], buf[8])
				fmt.Printf("from %s to %s\n", util.IPv4Source(buf).String(), util.IPv4Destination(buf).String())
				fmt.Printf("protocol %02x checksum %02x\n", util.IPv4Protocol(buf), binary.BigEndian.Uint16(buf[22:24]))
				if util.IPv4Protocol(buf) == util.ICMP {
					srcip := make([]byte, 4)
					copy(srcip, buf[12:16])
					copy(buf[12:16], buf[16:20])
					copy(buf[16:20], srcip)
					buf[20] = 0x00
					buf[21] = 0x00
					buf[22] = 0x00
					buf[23] = 0x00
					cksum := util.Checksum(buf[20:n])
					fmt.Printf("my checksum:%02x\n", uint16(cksum))
					buf[22] = byte((cksum & 0xff00) >> 8)
					buf[23] = byte(cksum & 0xff)
					fmt.Printf("rsp: from %s to %s\n", util.IPv4Source(buf).String(), util.IPv4Destination(buf).String())
					_, err = tun.Write(buf)
					if err != nil {
						fmt.Println(err)
					}
				}
			}
		}
	}
}
예제 #2
0
func main() {
	tun, err := tuntap.Open("tun0", tuntap.DevTun)
	checkError(err)
	switch runtime.GOOS {
	case "linux":
		setTunLinux()
	case "darwin":
		setTunDarwin()
	default:
		fmt.Println("OS NOT supported")
		os.Exit(1)
	}
	addr, err := net.ResolveUDPAddr("udp", ":9826")
	checkError(err)
	conn, err := net.ListenUDP("udp", addr)
	checkError(err)
	defer conn.Close()
	raddr := &net.UDPAddr{}
	fmt.Println("Waiting IP Packet from UDP")
	go func() {
		buf := make([]byte, 10000)
		for {
			n, fromAddr, err := conn.ReadFromUDP(buf)
			if err != nil {
				fmt.Println("ReadFromUDP error:", err)
				continue
			}
			raddr = fromAddr
			fmt.Printf("receive %d bytes from %s\n", n, fromAddr.String())
			n, _ = tun.Write(buf[:n])
			fmt.Printf("write %d bytes to tun interface\n", n)
		}
	}()

	buf := make([]byte, 10000)
	for {
		n, err := tun.Read(buf)
		if err != nil {
			fmt.Println("run read error:", err)
			continue
		}
		n, err = conn.WriteTo(buf[:n], raddr)
		// n, err = conn.Write(buf[:n])
		fmt.Printf("write %d bytes to udp network\n", n)
	}
}
예제 #3
0
func main() {
	lip := flag.String("l", "192.168.102.32", "client local ip")
	rip := flag.String("r", "192.168.102.31", "server remote ip")
	flag.Parse()

	tun, err := tuntap.Open("tun0", tuntap.DevTun)
	checkError(err)
	switch runtime.GOOS {
	case "linux":
		setTunLinux()
	case "darwin":
		setTunDarwin()
	default:
		fmt.Println("OS NOT supported")
		os.Exit(1)
	}

	laddr, err := net.ResolveUDPAddr("udp", *lip+":0")
	checkError(err)
	raddr, err := net.ResolveUDPAddr("udp", *rip+":9826")
	checkError(err)

	conn, err := net.DialUDP("udp", laddr, raddr)
	checkError(err)
	defer conn.Close()

	fmt.Println("Waiting IP Packet from tun interface")
	go func() {
		buf := make([]byte, 10000)
		for {
			n, err := tun.Read(buf)
			if err != nil {
				fmt.Println("tun Read error:", err)
				continue
			}
			fmt.Printf("receive %d bytes, from %s to %s, \n", n, util.IPv4Source(buf).String(), util.IPv4Destination(buf).String())
			n, err = conn.Write(buf[:n])
			if err != nil {
				fmt.Println("udp write error:", err)
				continue
			}
			fmt.Printf("write %d bytes to udp network\n", n)
		}
	}()

	buf := make([]byte, 10000)
	for {
		n, _, err := conn.ReadFromUDP(buf)
		if err != nil {
			fmt.Println("udp Read error:", err)
			continue
		}
		fmt.Sprintf("receive %d bytes, from %s to %s, \n", n, util.IPv4Source(buf).String(), util.IPv4Destination(buf).String())
		n, err = tun.Write(buf[:n])
		if err != nil {
			fmt.Println("udp write error:", err)
			continue
		}
		fmt.Printf("write %d bytes to tun interface\n", n)
	}
}