Skip to content

Commit

Permalink
p2p: add start timestamp, net speed and misc
Browse files Browse the repository at this point in the history
  • Loading branch information
inciner8r committed Apr 14, 2024
1 parent 81d6f98 commit 7caf34b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ jobs:
docker stop erebrus && docker rm erebrus && docker image rm ghcr.io/netsepio/erebrus:main
echo ${{ secrets.GHCR_TOKEN }} | docker login ghcr.io -u ${{ secrets.GHCR_USERNAME }} --password-stdin
docker pull ghcr.io/netsepio/erebrus:main
docker run -d -p 9080:9080/tcp -p 51820:51820/udp --cap-add=NET_ADMIN --cap-add=SYS_MODULE --sysctl="net.ipv4.conf.all.src_valid_mark=1" --sysctl="net.ipv6.conf.all.forwarding=1" --restart unless-stopped -v /home/ubuntu/erebrus/wireguard/:/etc/wireguard/ --name erebrus --env-file .env ghcr.io/netsepio/erebrus:main
docker run -d -p 9080:9080/tcp -p 9002:9002/tcp -p 51820:51820/udp --cap-add=NET_ADMIN --cap-add=SYS_MODULE --sysctl="net.ipv4.conf.all.src_valid_mark=1" --sysctl="net.ipv6.conf.all.forwarding=1" --restart unless-stopped -v /home/ubuntu/erebrus/wireguard/:/etc/wireguard/ --name erebrus --env-file .env ghcr.io/netsepio/erebrus:main
53 changes: 1 addition & 52 deletions p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"context"
"fmt"
"sync"
"time"

dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/discovery/routing"
discovery "github.com/libp2p/go-libp2p/p2p/discovery/util"
Expand All @@ -19,19 +17,7 @@ import (
// If you don't have any bootstrapPeers, you can use dht.DefaultBootstrapPeers
// or an empty list.
func NewDHT(ctx context.Context, host host.Host, bootstrapPeers []multiaddr.Multiaddr) (*dht.IpfsDHT, error) {
var options []dht.Option

// if no bootstrap peers, make this peer act as a bootstraping node
// other peers can use this peers ipfs address for peer discovery via dht
if len(bootstrapPeers) == 0 {
options = append(options, dht.Mode(dht.ModeServer))
}

// set our DiscoveryServiceTag as the protocol prefix so we can discover
// peers we're interested in.
options = append(options, dht.ProtocolPrefix("/"+DiscoveryServiceTag))

kdht, err := dht.New(ctx, host, options...)
kdht, err := dht.New(ctx, host)
if err != nil {
return nil, err
}
Expand All @@ -58,7 +44,6 @@ func NewDHT(ctx context.Context, host host.Host, bootstrapPeers []multiaddr.Mult
}()
}
wg.Wait()

return kdht, nil
}

Expand All @@ -68,40 +53,4 @@ func Discover(ctx context.Context, h host.Host, dht *dht.IpfsDHT, rendezvous str

// Advertise our addresses on rendezvous
discovery.Advertise(ctx, routingDiscovery, rendezvous)

// Search for peers every DiscoveryInterval
ticker := time.NewTicker(DiscoveryInterval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:

// Search for other peers advertising on rendezvous and
// connect to them.
peers, err := discovery.FindPeers(ctx, routingDiscovery, rendezvous)
if err != nil {
panic(err)
}

for _, p := range peers {
if p.ID == h.ID() {
continue
}
if h.Network().Connectedness(p.ID) != network.Connected {
_, err = h.Network().DialPeer(ctx, p.ID)
if err != nil {
fmt.Printf("Failed to connect to peer (%s): %s", p.ID, err.Error())
fmt.Println()
h.Network().ClosePeer(p.ID)
h.Peerstore().ClearAddrs(p.ID)
continue
}
fmt.Println("Connected to peer", p.ID.String())
}
}
}
}
}
7 changes: 6 additions & 1 deletion p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const DiscoveryInterval = time.Second * 10
// other peers.
const DiscoveryServiceTag = "erebrus"

var StartTimeStamp int64

func Init() {
StartTimeStamp = time.Now().Unix()
ctx := context.Background()

// create a new libp2p Host
Expand All @@ -32,6 +35,7 @@ func Init() {
fullAddr := getHostAddress(ha)
log.Printf("I am %s\n", fullAddr)

remoteAddr := "/ip4/" + os.Getenv("HOST_IP") + "/tcp/9002/p2p/" + ha.ID().String()
// Create a new PubSub service using the GossipSub router.
ps, err := pubsub.NewGossipSub(ctx, ha)
if err != nil {
Expand Down Expand Up @@ -61,7 +65,8 @@ func Init() {
}
go func() {
time.Sleep(5 * time.Second)
node_data := node.CreateNodeStatus(fullAddr, ha.ID().String())
fmt.Println("sending")
node_data := node.CreateNodeStatus(remoteAddr, ha.ID().String(), StartTimeStamp)
msgBytes, err := json.Marshal(node_data)
if err != nil {
panic(err)
Expand Down
35 changes: 23 additions & 12 deletions util/pkg/node/node.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package node

import "os"
import (
"os"

"github.com/NetSepio/erebrus/util/pkg/speedtest"
)

type NodeStatus struct {
Id string `json:"id"`
HttpPort string `json:"httpPort"`
Domain string `json:"domain"`
Address string `json:"address"`
Region string `json:"region"`
Id string `json:"id"`
HttpPort string `json:"httpPort"`
Domain string `json:"domain"`
Address string `json:"address"`
Region string `json:"region"`
DownloadSpeed float64 `json:"downloadSpeed"`
UploadSpeed float64 `json:"uploadSpeed"`
StartTimeStamp int64 `json:"startTimeStamp"`
}

func CreateNodeStatus(address string, id string) *NodeStatus {
func CreateNodeStatus(address string, id string, startTimeStamp int64) *NodeStatus {
speedtestResult, _ := speedtest.GetSpeedtestResults()
nodeStatus := &NodeStatus{
HttpPort: os.Getenv("HTTP_PORT"),
Domain: os.Getenv("DOMAIN"),
Address: address,
Region: os.Getenv("REGION"),
Id: id,
HttpPort: os.Getenv("HTTP_PORT"),
Domain: os.Getenv("DOMAIN"),
Address: address,
Region: os.Getenv("REGION"),
Id: id,
DownloadSpeed: speedtestResult.DownloadSpeed,
UploadSpeed: speedtestResult.UploadSpeed,
StartTimeStamp: startTimeStamp,
}
return nodeStatus
}

0 comments on commit 7caf34b

Please sign in to comment.