Yeung Alan 5 лет назад
Родитель
Сommit
a755e42935
3 измененных файлов с 70 добавлено и 0 удалено
  1. 8 0
      go.mod
  2. 4 0
      go.sum
  3. 58 0
      main.go

+ 8 - 0
go.mod

@@ -0,0 +1,8 @@
+module imuslab.com/private/vnet
+
+go 1.13
+
+require (
+	github.com/inszva/tap0901 v0.0.0-20170111121940-193959613e53
+	golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
+)

+ 4 - 0
go.sum

@@ -0,0 +1,4 @@
+github.com/inszva/tap0901 v0.0.0-20170111121940-193959613e53 h1:bY9zyIfelc+4Js+JNcP8qH0GsWwYWTpYNpMjfTg+GU8=
+github.com/inszva/tap0901 v0.0.0-20170111121940-193959613e53/go.mod h1:6rmGIIoIhz1Donb5jwRpIIaL36DjMMqzNG0YSSZZb7g=
+golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o=
+golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

+ 58 - 0
main.go

@@ -0,0 +1,58 @@
+package main
+
+import (
+	"net"
+	"sync"
+	"github.com/inszva/tap0901"
+	"time"
+	"fmt"
+	"encoding/hex"
+)
+
+func main() {
+	//Create a new virtual network interface
+	tun, err := tap0901.OpenTun(net.IP([]byte{0, 0, 0, 0}), net.IP([]byte{0,0,0,0}), net.IP([]byte{0,0,0,0}))
+    if err != nil {
+        panic(err)
+    }
+	fmt.Println("Virtual Network Interface Created")
+	time.Sleep(2 * time.Second)
+
+	//create fucking DHCP IP
+	err = tun.SetDHCPMasq(net.IP([]byte{162, 169, 228, 206}), net.IP([]byte{255, 255, 255, 0}),
+        net.IP([]byte{0, 0, 0, 0}), net.IP([]byte{0, 0, 0, 0}))
+	if err != nil {
+        fmt.Println(err)
+    }
+	
+	tun.Connect()
+	time.Sleep(2 * time.Second)
+	szName := tun.GetNetworkName(false)
+	fmt.Println("Interface name: ", szName)
+	tun.SetReadHandler(func (tun *tap0901.Tun, data []byte) {
+	a := hex.EncodeToString(data)
+	 bs, _ := hex.DecodeString(a)
+		 fmt.Println(string(bs))
+	})
+	wp := sync.WaitGroup{}
+	wp.Add(1)
+	go func () {
+		tun.Listen(1)
+		wp.Done()
+	} ()
+	
+
+
+	fmt.Println(tun.GetMTU(false))
+	//fmt.Println(tun.Write([]byte{0x45, 0x00, 0x00, 0x23, 0x12, 0xa5, 0x00, 0x40, 0x11, 0xf3, 0x28, 0x7b, 0x02, 0x03, 0x04, 0x7b, 0x7b, 0x7b, 0x7b, 0x3d, 0x1d, 0x63, 0x71, 0x00, 0x0f, 0x59, 0x17, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67}))
+
+	for {
+		time.Sleep(3*time.Second)
+		tun.Write([]byte("Hello World"))
+		fmt.Println("Waiting")
+	}
+	time.Sleep(10*time.Second)
+	tun.SignalStop()
+	wp.Wait()
+
+}