From febf075f8d874ed2f844193517be8ae539715ea7 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Sun, 19 Apr 2020 15:50:59 +0700 Subject: [PATCH] Add Server config page Handle server ip addresses input and store TODO: Key pair form --- client/khanh.json | 4 - handler/routes.go | 59 ++++++++- main.go | 5 +- model/server.go | 25 ++++ router/router.go | 3 +- templates/base.html | 18 ++- templates/{home.html => clients.html} | 6 +- templates/server.html | 165 ++++++++++++++++++++++++++ util/util.go | 22 +++- 9 files changed, 285 insertions(+), 22 deletions(-) delete mode 100644 client/khanh.json create mode 100644 model/server.go rename templates/{home.html => clients.html} (97%) create mode 100644 templates/server.html diff --git a/client/khanh.json b/client/khanh.json deleted file mode 100644 index ef5f7c7..0000000 --- a/client/khanh.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "privateKey": "xyz", - "pulbicKey": "123" -} \ No newline at end of file diff --git a/handler/routes.go b/handler/routes.go index 959a131..03a51b4 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -16,8 +16,8 @@ import ( "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) -// Home handler -func Home() echo.HandlerFunc { +// WireGuardClients handler +func WireGuardClients() echo.HandlerFunc { return func(c echo.Context) error { // initialize database directory dir := "./db" @@ -53,7 +53,7 @@ func Home() echo.HandlerFunc { clientDataList = append(clientDataList, clientData) } - return c.Render(http.StatusOK, "home.html", map[string]interface{}{ + return c.Render(http.StatusOK, "clients.html", map[string]interface{}{ "name": "Khanh", "clientDataList": clientDataList, }) @@ -68,7 +68,7 @@ func NewClient() echo.HandlerFunc { // validate the input AllowedIPs if util.ValidateAllowedIPs(client.AllowedIPs) == false { - log.Warn("Invalid Allowed IPs input from user: %v", client.AllowedIPs) + log.Warnf("Invalid Allowed IPs input from user: %v", client.AllowedIPs) return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Allowed IPs must be in CIDR format"}) } @@ -122,3 +122,54 @@ func RemoveClient() echo.HandlerFunc { return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Client removed"}) } } + +// WireGuardServer handler +func WireGuardServer() echo.HandlerFunc { + return func(c echo.Context) error { + + // initialize database directory + dir := "./db" + db, err := scribble.New(dir, nil) + if err != nil { + log.Error("Cannot initialize the database: ", err) + } + + serverInterface := model.ServerInterface{} + if err := db.Read("server", "interfaces", &serverInterface); err != nil { + log.Error("Cannot fetch server interface config from database: ", err) + } + + return c.Render(http.StatusOK, "server.html", map[string]interface{}{ + "name": "Khanh", + "serverInterface": serverInterface, + }) + } +} + +// WireGuardServerInterfaces handler +func WireGuardServerInterfaces() echo.HandlerFunc { + return func(c echo.Context) error { + serverInterface := new(model.ServerInterface) + c.Bind(serverInterface) + + // validate the input addresses + if util.ValidateServerAddresses(serverInterface.Addresses) == false { + log.Warnf("Invalid server interface addresses input from user: %v", serverInterface.Addresses) + return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Interface IP addresses must be in CIDR format"}) + } + + serverInterface.UpdatedAt = time.Now().UTC() + + // write config to the database + dir := "./db" + db, err := scribble.New(dir, nil) + if err != nil { + log.Error("Cannot initialize the database: ", err) + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot access database"}) + } + db.Write("server", "interfaces", serverInterface) + log.Infof("Updated wireguard server interfaces settings: %v", serverInterface) + + return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Updated interface addresses successfully"}) + } +} diff --git a/main.go b/main.go index 95d1d20..3334bdb 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,10 @@ import ( func main() { app := router.New() - app.GET("/", handler.Home()) + app.GET("/", handler.WireGuardClients()) app.POST("/new-client", handler.NewClient()) app.POST("/remove-client", handler.RemoveClient()) - + app.GET("/wg-server", handler.WireGuardServer()) + app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces()) app.Logger.Fatal(app.Start("127.0.0.1:5000")) } diff --git a/model/server.go b/model/server.go new file mode 100644 index 0000000..06ab21a --- /dev/null +++ b/model/server.go @@ -0,0 +1,25 @@ +package model + +import ( + "time" +) + +// Server model +type Server struct { + KeyPair *ServerKeypair + Interface *ServerInterface +} + +// ServerKeypair model +type ServerKeypair struct { + PrivateKey string `json:"private_key"` + PublicKey string `json:"pulbic_key"` + UpdatedAt time.Time `json:"updated_at"` +} + +// ServerInterface model +type ServerInterface struct { + Addresses []string `json:"addresses"` + ListenPort int `json:"listen_port,string"` // ,string to get listen_port string input as int + UpdatedAt time.Time `json:"updated_at"` +} diff --git a/router/router.go b/router/router.go index d30d710..c2d6bcc 100644 --- a/router/router.go +++ b/router/router.go @@ -29,7 +29,8 @@ func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c func New() *echo.Echo { e := echo.New() templates := make(map[string]*template.Template) - templates["home.html"] = template.Must(template.ParseFiles("templates/home.html", "templates/base.html")) + templates["clients.html"] = template.Must(template.ParseFiles("templates/clients.html", "templates/base.html")) + templates["server.html"] = template.Must(template.ParseFiles("templates/server.html", "templates/base.html")) e.Logger.SetLevel(log.DEBUG) e.Pre(middleware.RemoveTrailingSlash()) diff --git a/templates/base.html b/templates/base.html index 834a04b..14ef39c 100644 --- a/templates/base.html +++ b/templates/base.html @@ -86,18 +86,26 @@