Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Serious Performance Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
alecbcs committed Feb 2, 2022
1 parent 6c2a0d1 commit 20c306d
Show file tree
Hide file tree
Showing 5 changed files with 423 additions and 97 deletions.
3 changes: 1 addition & 2 deletions cli/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"os"
"path/filepath"

Expand Down Expand Up @@ -38,7 +37,7 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
}

// Create New Libp2p Node
host, err := libp2p.New(context.Background())
host, err := libp2p.New()
checkErr(err)

// Get Node's Private Key
Expand Down
40 changes: 33 additions & 7 deletions cli/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"bufio"
"context"
"encoding/binary"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -205,9 +206,12 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
dst := net.IPv4(packet[16], packet[17], packet[18], packet[19]).String()
stream, ok := activeStreams[dst]
if ok {
_, err = stream.Write(packet[:plen])
err = binary.Write(stream, binary.LittleEndian, uint16(plen))
if err == nil {
continue
_, err = stream.Write(packet[:plen])
if err == nil {
continue
}
}
stream.Close()
delete(activeStreams, dst)
Expand All @@ -216,10 +220,18 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
if peer, ok := peerTable[dst]; ok {
stream, err = host.NewStream(ctx, peer, p2p.Protocol)
if err != nil {
log.Println(err)
continue
}
stream.Write(packet[:plen])
err = binary.Write(stream, binary.LittleEndian, uint16(plen))
if err != nil {
stream.Close()
continue
}
_, err = stream.Write(packet[:plen])
if err != nil {
stream.Close()
continue
}
activeStreams[dst] = stream
}
}
Expand Down Expand Up @@ -265,14 +277,28 @@ func streamHandler(stream network.Stream) {
return
}
var packet = make([]byte, 1420)
var packetSize = make([]byte, 2)
for {
plen, err := stream.Read(packet)
_, err := stream.Read(packetSize)
if err != nil {
stream.Close()
return
}
size := binary.LittleEndian.Uint16(packetSize)
if err != nil {
stream.Close()
delete(activeStreams, RevLookup[stream.Conn().RemotePeer().Pretty()])
return
}
tunDev.Iface.Write(packet[:plen])
var plen uint16 = 0
for plen < size {
tmp, err := stream.Read(packet[plen:size])
plen += uint16(tmp)
if err != nil {
stream.Close()
return
}
}
tunDev.Iface.Write(packet[:size])
}
}

Expand Down
18 changes: 8 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ go 1.16

require (
github.com/DataDrake/cli-ng/v2 v2.0.2
github.com/hashicorp/go-version v1.2.1 // indirect
github.com/hashicorp/go-version v1.4.0 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
github.com/ipfs/go-datastore v0.4.5
github.com/ipfs/go-datastore v0.5.1
github.com/kr/text v0.2.0 // indirect
github.com/libp2p/go-libp2p v0.14.1
github.com/libp2p/go-libp2p-core v0.8.6
github.com/libp2p/go-libp2p-kad-dht v0.12.1
github.com/libp2p/go-libp2p-quic-transport v0.12.0
github.com/libp2p/go-tcp-transport v0.2.8
github.com/multiformats/go-multiaddr v0.3.3
github.com/libp2p/go-libp2p v0.17.0
github.com/libp2p/go-libp2p-core v0.13.0
github.com/libp2p/go-libp2p-kad-dht v0.15.0
github.com/libp2p/go-libp2p-quic-transport v0.15.2
github.com/libp2p/go-tcp-transport v0.4.0
github.com/multiformats/go-multiaddr v0.4.1
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e
github.com/vishvananda/netlink v1.1.0
golang.org/x/sys v0.0.0-20210603125802-9665404d3644 // indirect
gopkg.in/yaml.v2 v2.4.0
honnef.co/go/tools v0.0.1-2020.1.4 // indirect
)
Loading

0 comments on commit 20c306d

Please sign in to comment.