|
| 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