feature/25 :: Added basic auth to webUI

This commit is contained in:
Rene 2022-09-27 17:15:22 -06:00
parent 0c49949a5d
commit 5f9930988d
4 changed files with 77 additions and 52 deletions

View file

@ -51,6 +51,7 @@ Configuration can be done through config file or environment variables
| DBPATH | Path to Database | /data/db.sqlite |
| GUIIP | Address for web GUI | localhost (127.0.0.1) |
| GUIPORT | Port for web GUI | 8840 |
| GUIAUTH | Basic auth credentials for web GUI, e.g.: GUIAUTH=user:pass | (empty - no auth) |
| TIMEOUT | Time between scans (seconds) | 60 (1 minute) |
| SHOUTRRR_URL | Url to any notification service supported by [Shoutrrr](https://github.com/containrrr/shoutrrr/tree/main/docs/services) (gotify, email, telegram and others) | "" |
| THEME | Any theme name from https://bootswatch.com in lowcase | solar |

View file

@ -11,6 +11,7 @@ func get_config() (config Conf) {
viper.SetDefault("DBPATH", "/data/db.sqlite")
viper.SetDefault("GUIIP", "localhost")
viper.SetDefault("GUIPORT", "8840")
viper.SetDefault("GUIAUTH", "")
viper.SetDefault("TIMEOUT", "60")
viper.SetDefault("SHOUTRRR_URL", "")
viper.SetDefault("THEME", "solar")
@ -25,6 +26,7 @@ func get_config() (config Conf) {
config.DbPath = viper.Get("DBPATH").(string)
config.GuiIP = viper.Get("GUIIP").(string)
config.GuiPort = viper.Get("GUIPORT").(string)
config.GuiAuth = viper.Get("GUIAUTH").(string)
config.Timeout = viper.GetInt("TIMEOUT")
config.ShoutUrl = viper.Get("SHOUTRRR_URL").(string)
config.Theme = viper.Get("THEME").(string)

View file

@ -20,9 +20,10 @@ type Conf struct {
DbPath string
GuiIP string
GuiPort string
Timeout int
GuiAuth string
ShoutUrl string
Theme string
Timeout int
}
var AppConfig Conf

View file

@ -2,9 +2,9 @@ package main
import (
"fmt"
"html/template"
"log"
"net/http"
"html/template"
"strconv"
)
@ -48,6 +48,27 @@ func update_host(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.Header.Get("Referer"), 302)
}
func basicAuth(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if AppConfig.GuiAuth == "" {
next.ServeHTTP(w, r)
return
}
username, password, ok := r.BasicAuth()
if ok {
userCredentials := fmt.Sprintf(`%s:%s`, username, password)
if userCredentials == AppConfig.GuiAuth {
next.ServeHTTP(w, r)
return
}
}
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
}
}
func webgui() {
// fmt.Println(FoundHosts)
address := AppConfig.GuiIP + ":" + AppConfig.GuiPort
@ -56,13 +77,13 @@ func webgui() {
log.Println(fmt.Sprintf("Web GUI at http://%s", address))
log.Println("=================================== ")
http.HandleFunc("/", index)
http.HandleFunc("/home/", home)
http.HandleFunc("/offline/", offline)
http.HandleFunc("/online/", online)
http.HandleFunc("/search_hosts/", search_hosts)
http.HandleFunc("/sort_hosts/", sort_hosts)
http.HandleFunc("/theme/", theme)
http.HandleFunc("/update_host/", update_host)
http.HandleFunc("/", basicAuth(index))
http.HandleFunc("/home/", basicAuth(home))
http.HandleFunc("/offline/", basicAuth(offline))
http.HandleFunc("/online/", basicAuth(online))
http.HandleFunc("/search_hosts/", basicAuth(search_hosts))
http.HandleFunc("/sort_hosts/", basicAuth(sort_hosts))
http.HandleFunc("/theme/", basicAuth(theme))
http.HandleFunc("/update_host/", basicAuth(update_host))
http.ListenAndServe(address, nil)
}