Skip to content

Commit

Permalink
🎨 Improve structure of the code / VPN interface set up on request
Browse files Browse the repository at this point in the history
  • Loading branch information
smolgroot committed Jun 3, 2024
1 parent d24150e commit c91c3ed
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 60 deletions.
2 changes: 1 addition & 1 deletion cmd/skypier-vpn-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 46 additions & 5 deletions pkg/vpn/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
// }
// }()
}
58 changes: 4 additions & 54 deletions pkg/vpn/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
}
Expand All @@ -34,14 +28,16 @@ func SetInterfaceUp() {
log.Fatal(err)
}

log.Println("New interface OK")
log.Println("Set TUN interface up")

// Configure the network interface
pierIface, _ := netlink.LinkByName(interfaceName)
addr, _ := netlink.ParseAddr("10.1.1.1/24") // TODO remove static IP
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")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c91c3ed

Please sign in to comment.