main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "github.com/inszva/tap0901"
  6. "net"
  7. "net/http"
  8. //"net/url"
  9. "os"
  10. "sync"
  11. "time"
  12. )
  13. //Create a new virtual network interface
  14. var tun, _ = tap0901.OpenTun(net.IP([]byte{0, 0, 0, 0}), net.IP([]byte{0, 0, 0, 0}), net.IP([]byte{0, 0, 0, 0}))
  15. func main() {
  16. //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}))
  17. /*
  18. if err != nil {
  19. panic(err)
  20. }
  21. */
  22. fmt.Println("Virtual Network Interface Created")
  23. time.Sleep(2 * time.Second)
  24. // type IP
  25. IPAddress := net.ParseIP(os.Args[2])
  26. //create fucking DHCP IP
  27. err := tun.SetDHCPMasq(IPAddress, net.IP([]byte{255, 0, 0, 0}),
  28. net.IP([]byte{0, 0, 0, 0}), net.IP([]byte{0, 0, 0, 0}))
  29. if err != nil {
  30. fmt.Println(err)
  31. }
  32. //create http server
  33. http.HandleFunc("/rcv", rcv)
  34. go func() {
  35. http.ListenAndServe(":8181", nil)
  36. }()
  37. tun.Connect()
  38. time.Sleep(2 * time.Second)
  39. szName := tun.GetNetworkName(false)
  40. fmt.Println("Interface name: ", szName)
  41. fmt.Println("Dest IP:" + os.Args[1])
  42. tun.SetReadHandler(func(tun *tap0901.Tun, data []byte) {
  43. a := hex.EncodeToString(data)
  44. //bs, _ := hex.DecodeString(a)
  45. //fmt.Println(string(bs))
  46. //fmt.Println(a)
  47. resp, err := http.Get("http://" + os.Args[1] + "/rcv?packet=" + a)
  48. if err != nil {
  49. fmt.Println(err)
  50. } else {
  51. defer resp.Body.Close()
  52. }
  53. })
  54. wp := sync.WaitGroup{}
  55. wp.Add(1)
  56. go func() {
  57. tun.Listen(1)
  58. wp.Done()
  59. }()
  60. fmt.Println(tun.GetMTU(false))
  61. //fmt.Println()
  62. /*
  63. for {
  64. time.Sleep(3 * time.Second)
  65. tun.Write([]byte("Hello World"))
  66. fmt.Println("Waiting")
  67. }
  68. time.Sleep(10 * time.Second)
  69. tun.SignalStop()
  70. */
  71. wp.Wait()
  72. }
  73. func rcv(w http.ResponseWriter, req *http.Request) {
  74. fmt.Fprintf(w, "hello\n")
  75. x, ok := req.URL.Query()["packet"]
  76. if !ok || len(x[0]) < 1 {
  77. fmt.Println("Url Param 'packet' is missing")
  78. return
  79. } else {
  80. fmt.Println("Reciving data!!!")
  81. }
  82. data, err := hex.DecodeString(x[0])
  83. if err != nil {
  84. panic(err)
  85. }
  86. //fmt.Printf("% x", data)
  87. tun.Write(data)
  88. }