From c91c3ed362169d3dbc722a87fb75975d387e0089 Mon Sep 17 00:00:00 2001 From: smolgroot Date: Tue, 4 Jun 2024 00:28:47 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Improve=20structure=20of=20the?= =?UTF-8?q?=20code=20/=20VPN=20interface=20set=20up=20on=20request?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/skypier-vpn-node/main.go | 2 +- pkg/vpn/p2p.go | 51 +++++++++++++++++++++++++++---- pkg/vpn/tun.go | 58 +++--------------------------------- 3 files changed, 51 insertions(+), 60 deletions(-) diff --git a/cmd/skypier-vpn-node/main.go b/cmd/skypier-vpn-node/main.go index 47da743..219af1a 100644 --- a/cmd/skypier-vpn-node/main.go +++ b/cmd/skypier-vpn-node/main.go @@ -49,7 +49,7 @@ func main() { ProtocolVersion: "1.0", } - go vpn.SetInterfaceUp() + // go vpn.SetInterfaceUp() node, dht := vpn.SetNodeUp(ctx, innerConfig) gin.SetMode(gin.ReleaseMode) diff --git a/pkg/vpn/p2p.go b/pkg/vpn/p2p.go index 1d9923b..c25f1e0 100644 --- a/pkg/vpn/p2p.go +++ b/pkg/vpn/p2p.go @@ -27,6 +27,7 @@ import ( quic "github.com/libp2p/go-libp2p/p2p/transport/quic" "github.com/libp2p/go-libp2p/p2p/transport/tcp" "github.com/multiformats/go-multiaddr" + "golang.org/x/net/ipv4" ) type SkypierNode struct { @@ -143,16 +144,40 @@ func Connect(node host.Host, dht *dht.IpfsDHT) gin.HandlerFunc { if err != nil { log.Println(err) } - n, err := s.Write([]byte("Hello World!")) - if err != nil { - log.Println(err) - } - res := fmt.Sprintf("Created a stream to the remote node %v, and sent %d bytes. %v", dstPeer.ID, n, dstPeer.Addrs) + iface := SetInterfaceUp() + res := fmt.Sprintf("Created a stream to the remote node and created `%v` VPN interface.", iface.Name()) type Result struct { Res string `json:"result"` } r := &Result{Res: res} c.IndentedJSON(200, r) + packet := make([]byte, 1500) + for { + plen, err := iface.Read(packet) + if err != nil { + break + } + // debug + header, _ := ipv4.ParseHeader(packet[:plen]) + fmt.Printf("Sending to remote: %+v (%+v)\n", header, err) + // real send + n, err := s.Write(packet[:plen]) + if err != nil { + log.Println(err) + } + fmt.Printf("Connected to the remote node %v, and sent %d bytes. %v\n", dstPeer.ID, n, dstPeer.Addrs) + } + + // n, err := s.Write([]byte("Hello there frens! WAGMI!")) + // if err != nil { + // log.Println(err) + // } + // res := fmt.Sprintf("Created a stream to the remote node %v, and sent %d bytes. %v", dstPeer.ID, n, dstPeer.Addrs) + // type Result struct { + // Res string `json:"result"` + // } + // r := &Result{Res: res} + // c.IndentedJSON(200, r) } return gin.HandlerFunc(fn) @@ -395,4 +420,20 @@ func streamHandler(stream network.Stream) { } // tun.Iface.Write(packet[:size]) } + + // go func() { + // buf := make([]byte, BUFFERSIZE) + // for { + // n, addr, err := lstnConn.ReadFromUDP(buf) + // // just debug + // header, _ := ipv4.ParseHeader(buf[:n]) + // fmt.Printf("Received %d bytes from %v: %+v\n", n, addr, header) + // if err != nil || n == 0 { + // fmt.Println("Error: ", err) + // continue + // } + // // write to TUN interface + // iface.Write(buf[:n]) + // } + // }() } diff --git a/pkg/vpn/tun.go b/pkg/vpn/tun.go index 8b8a5a3..f34cca9 100644 --- a/pkg/vpn/tun.go +++ b/pkg/vpn/tun.go @@ -2,10 +2,7 @@ package vpn import ( "log" - "net" - "github.com/SkyPierIO/skypier-vpn/pkg/utils" - "github.com/songgao/packets/ethernet" "github.com/songgao/water" "github.com/vishvananda/netlink" ) @@ -19,10 +16,7 @@ const ( var interfaceName = "skypier0" -func SetInterfaceUp() { - - log.Println("Set TUN interface up") - +func SetInterfaceUp() *water.Interface { config := water.Config{ DeviceType: water.TUN, } @@ -34,7 +28,7 @@ func SetInterfaceUp() { log.Fatal(err) } - log.Println("New interface OK") + log.Println("Set TUN interface up") // Configure the network interface pierIface, _ := netlink.LinkByName(interfaceName) @@ -42,6 +36,8 @@ func SetInterfaceUp() { netlink.AddrAdd(pierIface, addr) netlink.LinkSetUp(pierIface) + return iface + // resolve remote addr // remoteAddr, err := net.ResolveUDPAddr("udp", "89.89.226.3:4321") // remoteAddr, err := net.ResolveUDPAddr("udp", "136.244.105.166:4321") @@ -88,52 +84,6 @@ func SetInterfaceUp() { // lstnConn.WriteToUDP(packet[:plen], remoteAddr) // } - isDebugEnabled := utils.IsDebugEnabled() - var frame ethernet.Frame - for { - frame.Resize(1500) // MTU - packet := []byte(frame) - n, err := iface.Read(packet) - if err != nil { - log.Fatal(err) - } - frame = frame[:n] - if isDebugEnabled { - log.Printf("\n────────────── ETHERNET TYPE II ──────────────────") - log.Printf("Dst MAC addr: %s\n", frame.Destination()) - log.Printf("Src MAC addr: %s\n", frame.Source()) - log.Printf("EtherType: % x\n", frame.Ethertype()) - log.Printf("Payload: % x\n", frame.Payload()) - } - if frame.Ethertype() == ethernet.IPv4 { - // Example Internet Datagram Header - // - // 0 1 2 3 - // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // |Version| IHL |Type of Service| Total Length | - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // | Identification |Flags| Fragment Offset | - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // | Time to Live | Protocol | Header Checksum | - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // | Source Address | - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // | Destination Address | - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // | Options | Padding | - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - - ipDst := net.IPv4(packet[16], packet[17], packet[18], packet[19]).String() - ipSrc := net.IPv4(packet[20], packet[21], packet[22], packet[23]).String() - - if isDebugEnabled { - log.Printf("─────────────────── IP packet ────────────────────") - log.Printf("IP dst: %s\n", ipDst) - log.Printf("IP src: %s\n", ipSrc) - } - } - } } func SetInterfaceDown() error {