packages/hyprspace: log connects and disconnects properly
This commit is contained in:
parent
f0aeeb78a1
commit
16bdf7348e
2 changed files with 30 additions and 4 deletions
|
@ -21,6 +21,7 @@ import (
|
|||
"github.com/hyprspace/hyprspace/p2p"
|
||||
"github.com/hyprspace/hyprspace/tun"
|
||||
dht "github.com/libp2p/go-libp2p-kad-dht"
|
||||
"github.com/libp2p/go-libp2p/core/event"
|
||||
"github.com/libp2p/go-libp2p/core/host"
|
||||
"github.com/libp2p/go-libp2p/core/network"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
|
@ -168,6 +169,9 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
|
|||
// Register the application to listen for signals
|
||||
go signalHandler(host, lockPath, dht)
|
||||
|
||||
// Log about various events
|
||||
go eventLogger(host, cfg)
|
||||
|
||||
// Write lock to filesystem to indicate an existing running daemon.
|
||||
err = os.WriteFile(lockPath, []byte(fmt.Sprint(os.Getpid())), os.ModePerm)
|
||||
checkErr(err)
|
||||
|
@ -296,6 +300,29 @@ func signalHandler(host host.Host, lockPath string, dht *dht.IpfsDHT) {
|
|||
}
|
||||
}
|
||||
|
||||
func eventLogger(host host.Host, cfg *config.Config) {
|
||||
subCon, err := host.EventBus().Subscribe(new(event.EvtPeerConnectednessChanged))
|
||||
checkErr(err)
|
||||
for {
|
||||
select {
|
||||
case ev := <-subCon.Out():
|
||||
evt := ev.(event.EvtPeerConnectednessChanged)
|
||||
for vpnIp, vpnPeer := range cfg.Peers {
|
||||
if vpnPeer.ID == evt.Peer.Pretty() {
|
||||
if evt.Connectedness == network.Connected {
|
||||
for _, c := range host.Network().ConnsToPeer(evt.Peer) {
|
||||
fmt.Printf("[+] Connected to %s at %s/p2p/%s\n", vpnIp, c.RemoteMultiaddr().String(), evt.Peer.Pretty())
|
||||
}
|
||||
} else if evt.Connectedness == network.NotConnected {
|
||||
fmt.Printf("[!] Disconnected from %s\n", vpnIp)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// createDaemon handles creating an independent background process for a
|
||||
// Hyprspace daemon from the original parent process.
|
||||
func createDaemon(cfg *config.Config) error {
|
||||
|
|
|
@ -5,10 +5,10 @@ import (
|
|||
"fmt"
|
||||
"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"
|
||||
dht "github.com/libp2p/go-libp2p-kad-dht"
|
||||
)
|
||||
|
||||
var discoverNow = make(chan bool)
|
||||
|
@ -29,17 +29,16 @@ func Discover(ctx context.Context, h host.Host, dht *dht.IpfsDHT, peerTable map[
|
|||
ticker.Reset(time.Millisecond * 1)
|
||||
case <-ticker.C:
|
||||
connectedToAny := false
|
||||
for nd, id := range peerTable {
|
||||
for _, id := range peerTable {
|
||||
if h.Network().Connectedness(id) != network.Connected {
|
||||
addrs, err := dht.FindPeer(ctx, id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
conn, err := h.Network().DialPeer(ctx, addrs.ID)
|
||||
_, err = h.Network().DialPeer(ctx, addrs.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
fmt.Printf("[+] Connected to %s at %s\n", nd, conn.RemoteMultiaddr())
|
||||
connectedToAny = true
|
||||
} else {
|
||||
connectedToAny = true
|
||||
|
|
Loading…
Reference in a new issue