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

Commit 08d495e

Browse files
committed
Create a webserver in node to configure when server is running
1 parent 512bc06 commit 08d495e

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

cli/up.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"syscall"
1717
"time"
1818

19+
"github.com/hyprspace/hyprspace/webserver"
20+
1921
"github.com/DataDrake/cli-ng/v2/cmd"
2022
"github.com/hyprspace/hyprspace/config"
2123
"github.com/hyprspace/hyprspace/p2p"
@@ -157,6 +159,11 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
157159
// Configure path for lock
158160
lockPath := filepath.Join(filepath.Dir(cfg.Path), cfg.Interface.Name+".lock")
159161

162+
// Start webserver
163+
fmt.Println("[+] Starting Web Server")
164+
165+
go webserver.CreateServer(cfg)
166+
160167
// Register the application to listen for SIGINT/SIGTERM
161168
go signalExit(host, lockPath)
162169

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/hashicorp/go-version v1.4.0 // indirect
88
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
99
github.com/ipfs/go-datastore v0.5.1
10+
github.com/julienschmidt/httprouter v1.3.0
1011
github.com/kr/text v0.2.0 // indirect
1112
github.com/libp2p/go-libp2p v0.17.0
1213
github.com/libp2p/go-libp2p-core v0.13.0

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
414414
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
415415
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
416416
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
417+
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
417418
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
418419
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d/go.mod h1:P2viExyCEfeWGU259JnaQ34Inuec4R38JCyBx2edgD0=
419420
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=

webserver/webserver.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package webserver
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
7+
"net/http"
8+
9+
"github.com/hyprspace/hyprspace/config"
10+
"github.com/julienschmidt/httprouter"
11+
)
12+
13+
type Server struct {
14+
router *httprouter.Router
15+
cfg *config.Config
16+
}
17+
18+
// Create a http server to read and edit the config file
19+
func CreateServer(cfg *config.Config) {
20+
// Create a new web server
21+
server := &Server{cfg: cfg}
22+
server.Init()
23+
server.Run()
24+
}
25+
26+
func (s *Server) Init() {
27+
s.router = httprouter.New()
28+
29+
s.router.GET("/", s.Index)
30+
s.router.GET("/routes", s.Routes)
31+
s.router.GET("/add-route/:net/:mask/:gateway", s.AddRoute)
32+
s.router.GET("/remove-route/:net/:mask/:gateway", s.RemoveRoute)
33+
}
34+
35+
func (s *Server) Run() {
36+
log.Fatal(http.ListenAndServe(":8080", s.router))
37+
}
38+
39+
func (s *Server) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
40+
var PrivateKey string
41+
w.WriteHeader(http.StatusOK)
42+
w.Header().Set("Content-Type", "application/json")
43+
PrivateKey = s.cfg.Interface.PrivateKey
44+
s.cfg.Interface.PrivateKey = "<hidden>"
45+
jsonResp, err := json.Marshal(s.cfg)
46+
s.cfg.Interface.PrivateKey = PrivateKey
47+
if err != nil {
48+
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
49+
}
50+
w.Write(jsonResp)
51+
}
52+
53+
func (s *Server) Routes(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
54+
w.WriteHeader(http.StatusOK)
55+
w.Header().Set("Content-Type", "application/json")
56+
jsonResp, err := json.Marshal(s.cfg.Routes)
57+
if err != nil {
58+
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
59+
}
60+
w.Write(jsonResp)
61+
}
62+
63+
func (s *Server) AddRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
64+
network := ps.ByName("net")
65+
mask := ps.ByName("mask")
66+
cidr := network + "/" + mask
67+
gateway := ps.ByName("gateway")
68+
s.cfg.Routes[cidr] = config.Route{IP: gateway}
69+
w.WriteHeader(http.StatusOK)
70+
w.Header().Set("Content-Type", "application/json")
71+
jsonResp, err := json.Marshal(s.cfg.Routes)
72+
if err != nil {
73+
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
74+
}
75+
w.Write(jsonResp)
76+
}
77+
78+
func (s *Server) RemoveRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
79+
network := ps.ByName("net")
80+
mask := ps.ByName("mask")
81+
cidr := network + "/" + mask
82+
delete(s.cfg.Routes, cidr)
83+
w.WriteHeader(http.StatusOK)
84+
w.Header().Set("Content-Type", "application/json")
85+
jsonResp, err := json.Marshal(s.cfg.Routes)
86+
if err != nil {
87+
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
88+
}
89+
w.Write(jsonResp)
90+
}

0 commit comments

Comments
 (0)