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

Commit 6f868c0

Browse files
committed
Add peer from api server
1 parent 08d495e commit 6f868c0

File tree

3 files changed

+71
-31
lines changed

3 files changed

+71
-31
lines changed

cli/up.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
154154

155155
// Setup P2P Discovery
156156
go p2p.Discover(ctx, host, dht, peerTable)
157-
go prettyDiscovery(ctx, host, peerTable)
157+
go p2p.PrettyDiscovery(ctx, host, peerTable)
158158

159159
// Configure path for lock
160160
lockPath := filepath.Join(filepath.Dir(cfg.Path), cfg.Interface.Name+".lock")
161161

162162
// Start webserver
163163
fmt.Println("[+] Starting Web Server")
164164

165-
go webserver.CreateServer(cfg)
165+
go webserver.CreateServer(ctx, host, dht, cfg, peerTable)
166166

167167
// Register the application to listen for SIGINT/SIGTERM
168168
go signalExit(host, lockPath)
@@ -237,6 +237,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
237237
// Check if the destination of the packet is a known peer to
238238
// the interface.
239239
if peer, ok := peerTable[dst]; ok {
240+
fmt.Println("[+] New Packet from", peer.String())
240241
stream, err = host.NewStream(ctx, peer, p2p.Protocol)
241242
if err != nil {
242243
continue
@@ -383,31 +384,6 @@ func streamHandler(stream network.Stream) {
383384
}
384385
}
385386

386-
func prettyDiscovery(ctx context.Context, node host.Host, peerTable map[string]peer.ID) {
387-
// Build a temporary map of peers to limit querying to only those
388-
// not connected.
389-
tempTable := make(map[string]peer.ID, len(peerTable))
390-
for ip, id := range peerTable {
391-
tempTable[ip] = id
392-
}
393-
for len(tempTable) > 0 {
394-
for ip, id := range tempTable {
395-
stream, err := node.NewStream(ctx, id, p2p.Protocol)
396-
if err != nil && (strings.HasPrefix(err.Error(), "failed to dial") ||
397-
strings.HasPrefix(err.Error(), "no addresses")) {
398-
// Attempt to connect to peers slowly when they aren't found.
399-
time.Sleep(5 * time.Second)
400-
continue
401-
}
402-
if err == nil {
403-
fmt.Printf("[+] Connection to %s Successful. Network Ready.\n", ip)
404-
stream.Close()
405-
}
406-
delete(tempTable, ip)
407-
}
408-
}
409-
}
410-
411387
func verifyPort(port int) (int, error) {
412388
var ln net.Listener
413389
var err error

p2p/discover.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package p2p
22

33
import (
44
"context"
5+
"fmt"
6+
"strings"
57
"time"
68

79
"github.com/libp2p/go-libp2p-core/host"
@@ -35,3 +37,28 @@ func Discover(ctx context.Context, h host.Host, dht *dht.IpfsDHT, peerTable map[
3537
}
3638
}
3739
}
40+
41+
func PrettyDiscovery(ctx context.Context, node host.Host, peerTable map[string]peer.ID) {
42+
// Build a temporary map of peers to limit querying to only those
43+
// not connected.
44+
tempTable := make(map[string]peer.ID, len(peerTable))
45+
for ip, id := range peerTable {
46+
tempTable[ip] = id
47+
}
48+
for len(tempTable) > 0 {
49+
for ip, id := range tempTable {
50+
stream, err := node.NewStream(ctx, id, Protocol)
51+
if err != nil && (strings.HasPrefix(err.Error(), "failed to dial") ||
52+
strings.HasPrefix(err.Error(), "no addresses")) {
53+
// Attempt to connect to peers slowly when they aren't found.
54+
time.Sleep(5 * time.Second)
55+
continue
56+
}
57+
if err == nil {
58+
fmt.Printf("[+] Connection to %s Successful. Network Ready.\n", ip)
59+
stream.Close()
60+
}
61+
delete(tempTable, ip)
62+
}
63+
}
64+
}

webserver/webserver.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
package webserver
22

33
import (
4+
"context"
45
"encoding/json"
56
"log"
67

78
"net/http"
89

910
"github.com/hyprspace/hyprspace/config"
11+
"github.com/hyprspace/hyprspace/p2p"
1012
"github.com/julienschmidt/httprouter"
13+
"github.com/libp2p/go-libp2p-core/host"
14+
"github.com/libp2p/go-libp2p-core/peer"
15+
dht "github.com/libp2p/go-libp2p-kad-dht"
1116
)
1217

1318
type Server struct {
14-
router *httprouter.Router
15-
cfg *config.Config
19+
router *httprouter.Router
20+
ctx context.Context
21+
host host.Host
22+
dht *dht.IpfsDHT
23+
cfg *config.Config
24+
peerTable map[string]peer.ID
1625
}
1726

1827
// Create a http server to read and edit the config file
19-
func CreateServer(cfg *config.Config) {
28+
func CreateServer(ctx context.Context, host host.Host, dht *dht.IpfsDHT, cfg *config.Config, peersTable map[string]peer.ID) {
2029
// Create a new web server
21-
server := &Server{cfg: cfg}
30+
server := &Server{ctx: ctx, host: host, dht: dht, cfg: cfg, peerTable: peersTable}
2231
server.Init()
2332
server.Run()
2433
}
@@ -30,6 +39,7 @@ func (s *Server) Init() {
3039
s.router.GET("/routes", s.Routes)
3140
s.router.GET("/add-route/:net/:mask/:gateway", s.AddRoute)
3241
s.router.GET("/remove-route/:net/:mask/:gateway", s.RemoveRoute)
42+
s.router.GET("/add-peer/:ip/:peerid", s.AddPeer)
3343
}
3444

3545
func (s *Server) Run() {
@@ -88,3 +98,30 @@ func (s *Server) RemoveRoute(w http.ResponseWriter, r *http.Request, ps httprout
8898
}
8999
w.Write(jsonResp)
90100
}
101+
102+
func (s *Server) AddPeer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
103+
var err error
104+
ip := ps.ByName("ip")
105+
peerid := ps.ByName("peerid")
106+
s.peerTable[ip], err = peer.Decode(peerid)
107+
if err != nil {
108+
w.WriteHeader(http.StatusBadRequest)
109+
w.Header().Set("Content-Type", "application/json")
110+
jsonResp, err := json.Marshal(err)
111+
if err != nil {
112+
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
113+
}
114+
w.Write(jsonResp)
115+
}
116+
s.cfg.Peers[ip] = config.Peer{ID: peerid}
117+
w.WriteHeader(http.StatusOK)
118+
w.Header().Set("Content-Type", "application/json")
119+
jsonResp, err := json.Marshal(s.cfg.Peers)
120+
if err != nil {
121+
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
122+
}
123+
w.Write(jsonResp)
124+
// Setup P2P Discovery
125+
go p2p.Discover(s.ctx, s.host, s.dht, s.peerTable)
126+
go p2p.PrettyDiscovery(s.ctx, s.host, s.peerTable)
127+
}

0 commit comments

Comments
 (0)