diff --git a/README.md b/README.md index 2dad719..598a994 100644 --- a/README.md +++ b/README.md @@ -49,10 +49,10 @@ Note: | Variable | Description | Default | |-----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------| | `BASE_PATH` | Set this variable if you run wireguard-ui under a subpath of your reverse proxy virtual host (e.g. /wireguard)) | N/A | -| `SESSION_SECRET` | The secret key used to encrypt the session cookies. Set this to a random value. | N/A | -| `WGUI_USERNAME` | The username for the login page | `admin` | -| `WGUI_PASSWORD` | The password for the user on the login page. Will be hashed automatically | `admin` | -| `WGUI_PASSWORD_HASH` | The password hash for the user on the login page. (alternative to `WGUI_PASSWORD`) | N/A | +| `SESSION_SECRET` | The secret key used to encrypt the session cookies. Set this to a random value | N/A | +| `WGUI_USERNAME` | The username for the login page. Used for db initialization only | `admin` | +| `WGUI_PASSWORD` | The password for the user on the login page. Will be hashed automatically. Used for db initialization only | `admin` | +| `WGUI_PASSWORD_HASH` | The password hash for the user on the login page. (alternative to `WGUI_PASSWORD`). Used for db initialization only | N/A | | `WGUI_ENDPOINT_ADDRESS` | The default endpoint address used in global settings | Resolved to your public ip address | | `WGUI_DNS` | The default DNS servers (comma-separated-list) used in the global settings | `1.1.1.1` | | `WGUI_MTU` | The default MTU used in global settings | `1450` | @@ -98,7 +98,7 @@ These environment variables only apply to the docker container. | Variable | Description | Default | |-----------------------|---------------------------------------------------------------|---------| -| `WGUI_MANAGE_START` | Start/stop WireGaurd when the container is started/stopped | `false` | +| `WGUI_MANAGE_START` | Start/stop WireGuard when the container is started/stopped | `false` | | `WGUI_MANAGE_RESTART` | Auto restart WireGuard when we Apply Config changes in the UI | `false` | ## Auto restart WireGuard daemon diff --git a/handler/routes.go b/handler/routes.go index 6c1bafc..8cfb9ef 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -100,6 +100,63 @@ func Logout() echo.HandlerFunc { } } +// LoadProfile to load user information +func LoadProfile(db store.IStore) echo.HandlerFunc { + return func(c echo.Context) error { + + userInfo, err := db.GetUser() + if err != nil { + log.Error("Cannot get user information: ", err) + } + + return c.Render(http.StatusOK, "profile.html", map[string]interface{}{ + "baseData": model.BaseData{Active: "profile", CurrentUser: currentUser(c)}, + "userInfo": userInfo, + }) + } +} + +// UpdateProfile to update user information +func UpdateProfile(db store.IStore) echo.HandlerFunc { + return func(c echo.Context) error { + data := make(map[string]interface{}) + err := json.NewDecoder(c.Request().Body).Decode(&data) + + if err != nil { + return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"}) + } + + username := data["username"].(string) + password := data["password"].(string) + + user, err := db.GetUser() + if err != nil { + return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, err.Error()}) + } + + if username == "" { + return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid username"}) + } else { + user.Username = username + } + + if password != "" { + hash, err := util.HashPassword(password) + if err != nil { + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, err.Error()}) + } + user.PasswordHash = hash + } + + if err := db.SaveUser(user); err != nil { + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, err.Error()}) + } + log.Infof("Updated admin user information successfully") + + return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Updated admin user information successfully"}) + } +} + // WireGuardClients handler func WireGuardClients(db store.IStore) echo.HandlerFunc { return func(c echo.Context) error { @@ -118,7 +175,7 @@ func WireGuardClients(db store.IStore) echo.HandlerFunc { } } -// GetClients handler return a list of Wireguard client data +// GetClients handler return a JSON list of Wireguard client data func GetClients(db store.IStore) echo.HandlerFunc { return func(c echo.Context) error { @@ -133,7 +190,7 @@ func GetClients(db store.IStore) echo.HandlerFunc { } } -// GetClient handler return a of Wireguard client data +// GetClient handler returns a JSON object of Wireguard client data func GetClient(db store.IStore) echo.HandlerFunc { return func(c echo.Context) error { diff --git a/main.go b/main.go index 2887fad..3f0cd13 100644 --- a/main.go +++ b/main.go @@ -135,6 +135,9 @@ func main() { if !util.DisableLogin { app.GET(util.BasePath+"/login", handler.LoginPage()) app.POST(util.BasePath+"/login", handler.Login(db)) + app.GET(util.BasePath+"/logout", handler.Logout(), handler.ValidSession) + app.GET(util.BasePath+"/profile", handler.LoadProfile(db), handler.ValidSession) + app.POST(util.BasePath+"/profile", handler.UpdateProfile(db), handler.ValidSession) } var sendmail emailer.Emailer @@ -145,7 +148,6 @@ func main() { } app.GET(util.BasePath+"/_health", handler.Health()) - app.GET(util.BasePath+"/logout", handler.Logout(), handler.ValidSession) app.POST(util.BasePath+"/new-client", handler.NewClient(db), handler.ValidSession, handler.ContentTypeJson) app.POST(util.BasePath+"/update-client", handler.UpdateClient(db), handler.ValidSession, handler.ContentTypeJson) app.POST(util.BasePath+"/email-client", handler.EmailClient(db, sendmail, defaultEmailSubject, defaultEmailContent), handler.ValidSession, handler.ContentTypeJson) diff --git a/router/router.go b/router/router.go index 45ee836..0f9facc 100644 --- a/router/router.go +++ b/router/router.go @@ -63,6 +63,11 @@ func New(tmplBox *rice.Box, extraData map[string]string, secret []byte) *echo.Ec log.Fatal(err) } + tmplProfileString, err := tmplBox.String("profile.html") + if err != nil { + log.Fatal(err) + } + tmplClientsString, err := tmplBox.String("clients.html") if err != nil { log.Fatal(err) @@ -94,6 +99,7 @@ func New(tmplBox *rice.Box, extraData map[string]string, secret []byte) *echo.Ec } templates := make(map[string]*template.Template) templates["login.html"] = template.Must(template.New("login").Funcs(funcs).Parse(tmplLoginString)) + templates["profile.html"] = template.Must(template.New("profile").Funcs(funcs).Parse(tmplBaseString + tmplProfileString)) templates["clients.html"] = template.Must(template.New("clients").Funcs(funcs).Parse(tmplBaseString + tmplClientsString)) templates["server.html"] = template.Must(template.New("server").Funcs(funcs).Parse(tmplBaseString + tmplServerString)) templates["global_settings.html"] = template.Must(template.New("global_settings").Funcs(funcs).Parse(tmplBaseString + tmplGlobalSettingsString)) diff --git a/store/jsondb/jsondb.go b/store/jsondb/jsondb.go index b0d39f5..f39a452 100644 --- a/store/jsondb/jsondb.go +++ b/store/jsondb/jsondb.go @@ -127,13 +127,18 @@ func (o *JsonDB) GetUser() (model.User, error) { return user, o.conn.Read("server", "users", &user) } +// SaveUser func to user info to the database +func (o *JsonDB) SaveUser(user model.User) error { + return o.conn.Write("server", "users", user) +} + // GetGlobalSettings func to query global settings from the database func (o *JsonDB) GetGlobalSettings() (model.GlobalSetting, error) { settings := model.GlobalSetting{} return settings, o.conn.Read("server", "global_settings", &settings) } -// GetServer func to query Server setting from the database +// GetServer func to query Server settings from the database func (o *JsonDB) GetServer() (model.Server, error) { server := model.Server{} // read server interface information @@ -157,7 +162,7 @@ func (o *JsonDB) GetServer() (model.Server, error) { func (o *JsonDB) GetClients(hasQRCode bool) ([]model.ClientData, error) { var clients []model.ClientData - // read all client json file in "clients" directory + // read all client json files in "clients" directory records, err := o.conn.ReadAll("clients") if err != nil { return clients, err @@ -208,7 +213,9 @@ func (o *JsonDB) GetClientByID(clientID string, qrCodeSettings model.QRCodeSetti server, _ := o.GetServer() globalSettings, _ := o.GetGlobalSettings() client := client - client.UseServerDNS = qrCodeSettings.IncludeDNS + if !qrCodeSettings.IncludeDNS{ + globalSettings.DNSServers = []string{} + } if !qrCodeSettings.IncludeMTU { globalSettings.MTU = 0 } diff --git a/store/store.go b/store/store.go index 2dca8a7..86d6224 100644 --- a/store/store.go +++ b/store/store.go @@ -7,6 +7,7 @@ import ( type IStore interface { Init() error GetUser() (model.User, error) + SaveUser(user model.User) error GetGlobalSettings() (model.GlobalSetting, error) GetServer() (model.Server, error) GetClients(hasQRCode bool) ([]model.ClientData, error) diff --git a/templates/base.html b/templates/base.html index 8f7a5ed..0761e5f 100644 --- a/templates/base.html +++ b/templates/base.html @@ -87,7 +87,11 @@
- {{if .baseData.CurrentUser}} {{.baseData.CurrentUser}} {{else}} Administrator {{end}} + {{if .baseData.CurrentUser}} + {{.baseData.CurrentUser}} + {{else}} + Administrator + {{end}}
diff --git a/templates/clients.html b/templates/clients.html index cdce8c0..fa0d513 100644 --- a/templates/clients.html +++ b/templates/clients.html @@ -429,7 +429,7 @@ Wireguard Clients }); } - // submitEmailClient function for sending an email to the client with the configuration + // submitEmailClient function for sending an email with the configuration to the client function submitEmailClient() { const client_id = $("#e_client_id").val(); const email = $("#e_client_email").val(); diff --git a/templates/global_settings.html b/templates/global_settings.html index 3b0724e..e013349 100644 --- a/templates/global_settings.html +++ b/templates/global_settings.html @@ -199,6 +199,7 @@ Global Settings // Load DNS server to the form {{range .globalSettings.DNSServers}} + $("#dns_servers").removeTag('{{.}}'); $("#dns_servers").addTag('{{.}}'); {{end}} diff --git a/templates/profile.html b/templates/profile.html new file mode 100644 index 0000000..c2d3b95 --- /dev/null +++ b/templates/profile.html @@ -0,0 +1,110 @@ +{{ define "title"}} +Profile +{{ end }} + +{{ define "top_css"}} +{{ end }} + +{{ define "username"}} +{{ .username }} +{{ end }} + +{{ define "page_title"}} +Profile +{{ end }} + +{{ define "page_content"}} +
+
+ +
+ +
+
+
+

Update user information

+
+ + +
+
+
+ + +
+
+ + +
+ + +
+
+
+ +
+
+ +
+
+{{ end }} + +{{ define "bottom_js"}} + +{{ end }} diff --git a/templates/server.html b/templates/server.html index b82cf63..366d301 100644 --- a/templates/server.html +++ b/templates/server.html @@ -110,7 +110,7 @@ Wireguard Server Settings