feature/25 :: Added basic auth to webUI
This commit is contained in:
parent
0c49949a5d
commit
5f9930988d
4 changed files with 77 additions and 52 deletions
|
|
@ -51,6 +51,7 @@ Configuration can be done through config file or environment variables
|
||||||
| DBPATH | Path to Database | /data/db.sqlite |
|
| DBPATH | Path to Database | /data/db.sqlite |
|
||||||
| GUIIP | Address for web GUI | localhost (127.0.0.1) |
|
| GUIIP | Address for web GUI | localhost (127.0.0.1) |
|
||||||
| GUIPORT | Port for web GUI | 8840 |
|
| 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) |
|
| 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) | "" |
|
| 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 |
|
| THEME | Any theme name from https://bootswatch.com in lowcase | solar |
|
||||||
|
|
|
||||||
|
|
@ -11,13 +11,14 @@ func get_config() (config Conf) {
|
||||||
viper.SetDefault("DBPATH", "/data/db.sqlite")
|
viper.SetDefault("DBPATH", "/data/db.sqlite")
|
||||||
viper.SetDefault("GUIIP", "localhost")
|
viper.SetDefault("GUIIP", "localhost")
|
||||||
viper.SetDefault("GUIPORT", "8840")
|
viper.SetDefault("GUIPORT", "8840")
|
||||||
|
viper.SetDefault("GUIAUTH", "")
|
||||||
viper.SetDefault("TIMEOUT", "60")
|
viper.SetDefault("TIMEOUT", "60")
|
||||||
viper.SetDefault("SHOUTRRR_URL", "")
|
viper.SetDefault("SHOUTRRR_URL", "")
|
||||||
viper.SetDefault("THEME", "solar")
|
viper.SetDefault("THEME", "solar")
|
||||||
|
|
||||||
viper.SetConfigFile(configPath)
|
viper.SetConfigFile(configPath)
|
||||||
viper.SetConfigType("env")
|
viper.SetConfigType("env")
|
||||||
viper.ReadInConfig()
|
viper.ReadInConfig()
|
||||||
|
|
||||||
viper.AutomaticEnv() // Get ENVIRONMENT variables
|
viper.AutomaticEnv() // Get ENVIRONMENT variables
|
||||||
|
|
||||||
|
|
@ -25,6 +26,7 @@ func get_config() (config Conf) {
|
||||||
config.DbPath = viper.Get("DBPATH").(string)
|
config.DbPath = viper.Get("DBPATH").(string)
|
||||||
config.GuiIP = viper.Get("GUIIP").(string)
|
config.GuiIP = viper.Get("GUIIP").(string)
|
||||||
config.GuiPort = viper.Get("GUIPORT").(string)
|
config.GuiPort = viper.Get("GUIPORT").(string)
|
||||||
|
config.GuiAuth = viper.Get("GUIAUTH").(string)
|
||||||
config.Timeout = viper.GetInt("TIMEOUT")
|
config.Timeout = viper.GetInt("TIMEOUT")
|
||||||
config.ShoutUrl = viper.Get("SHOUTRRR_URL").(string)
|
config.ShoutUrl = viper.Get("SHOUTRRR_URL").(string)
|
||||||
config.Theme = viper.Get("THEME").(string)
|
config.Theme = viper.Get("THEME").(string)
|
||||||
|
|
|
||||||
65
src/main.go
65
src/main.go
|
|
@ -1,54 +1,55 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Host struct {
|
type Host struct {
|
||||||
Id uint16
|
Id uint16
|
||||||
Name string
|
Name string
|
||||||
Ip string
|
Ip string
|
||||||
Mac string
|
Mac string
|
||||||
Hw string
|
Hw string
|
||||||
Date string
|
Date string
|
||||||
Known uint16
|
Known uint16
|
||||||
Now uint16
|
Now uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
type Conf struct {
|
type Conf struct {
|
||||||
Iface string
|
Iface string
|
||||||
DbPath string
|
DbPath string
|
||||||
GuiIP string
|
GuiIP string
|
||||||
GuiPort string
|
GuiPort string
|
||||||
Timeout int
|
GuiAuth string
|
||||||
ShoutUrl string
|
ShoutUrl string
|
||||||
Theme string
|
Theme string
|
||||||
|
Timeout int
|
||||||
}
|
}
|
||||||
|
|
||||||
var AppConfig Conf
|
var AppConfig Conf
|
||||||
var AllHosts []Host
|
var AllHosts []Host
|
||||||
|
|
||||||
func scan_and_compare() {
|
func scan_and_compare() {
|
||||||
var foundHosts []Host
|
var foundHosts []Host
|
||||||
var dbHosts []Host
|
var dbHosts []Host
|
||||||
for { // Endless
|
for { // Endless
|
||||||
foundHosts = arp_scan() // Scan interfaces
|
foundHosts = arp_scan() // Scan interfaces
|
||||||
dbHosts = db_select() // Select everything from DB
|
dbHosts = db_select() // Select everything from DB
|
||||||
db_setnow() // Mark hosts in DB as offline
|
db_setnow() // Mark hosts in DB as offline
|
||||||
hosts_compare(foundHosts, dbHosts) // Compare hosts online and in DB
|
hosts_compare(foundHosts, dbHosts) // Compare hosts online and in DB
|
||||||
// and add them to DB
|
// and add them to DB
|
||||||
AllHosts = db_select()
|
AllHosts = db_select()
|
||||||
time.Sleep(time.Duration(AppConfig.Timeout) * time.Second) // Timeout
|
time.Sleep(time.Duration(AppConfig.Timeout) * time.Second) // Timeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
AllHosts = []Host{}
|
AllHosts = []Host{}
|
||||||
AppConfig = get_config() // Get config from Defaults, Config file, Env
|
AppConfig = get_config() // Get config from Defaults, Config file, Env
|
||||||
|
|
||||||
db_create() // Check if DB exists. Create if not
|
db_create() // Check if DB exists. Create if not
|
||||||
|
|
||||||
go scan_and_compare()
|
go scan_and_compare()
|
||||||
|
|
||||||
webgui() // Start web GUI
|
webgui() // Start web GUI
|
||||||
}
|
}
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"html/template"
|
||||||
"net/http"
|
"log"
|
||||||
"html/template"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func index(w http.ResponseWriter, r *http.Request) {
|
func index(w http.ResponseWriter, r *http.Request) {
|
||||||
type allData struct {
|
type allData struct {
|
||||||
Config Conf
|
Config Conf
|
||||||
Hosts []Host
|
Hosts []Host
|
||||||
}
|
}
|
||||||
var guiData allData
|
var guiData allData
|
||||||
guiData.Config = AppConfig
|
guiData.Config = AppConfig
|
||||||
|
|
@ -48,6 +48,27 @@ func update_host(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, r.Header.Get("Referer"), 302)
|
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() {
|
func webgui() {
|
||||||
// fmt.Println(FoundHosts)
|
// fmt.Println(FoundHosts)
|
||||||
address := AppConfig.GuiIP + ":" + AppConfig.GuiPort
|
address := AppConfig.GuiIP + ":" + AppConfig.GuiPort
|
||||||
|
|
@ -56,13 +77,13 @@ func webgui() {
|
||||||
log.Println(fmt.Sprintf("Web GUI at http://%s", address))
|
log.Println(fmt.Sprintf("Web GUI at http://%s", address))
|
||||||
log.Println("=================================== ")
|
log.Println("=================================== ")
|
||||||
|
|
||||||
http.HandleFunc("/", index)
|
http.HandleFunc("/", basicAuth(index))
|
||||||
http.HandleFunc("/home/", home)
|
http.HandleFunc("/home/", basicAuth(home))
|
||||||
http.HandleFunc("/offline/", offline)
|
http.HandleFunc("/offline/", basicAuth(offline))
|
||||||
http.HandleFunc("/online/", online)
|
http.HandleFunc("/online/", basicAuth(online))
|
||||||
http.HandleFunc("/search_hosts/", search_hosts)
|
http.HandleFunc("/search_hosts/", basicAuth(search_hosts))
|
||||||
http.HandleFunc("/sort_hosts/", sort_hosts)
|
http.HandleFunc("/sort_hosts/", basicAuth(sort_hosts))
|
||||||
http.HandleFunc("/theme/", theme)
|
http.HandleFunc("/theme/", basicAuth(theme))
|
||||||
http.HandleFunc("/update_host/", update_host)
|
http.HandleFunc("/update_host/", basicAuth(update_host))
|
||||||
http.ListenAndServe(address, nil)
|
http.ListenAndServe(address, nil)
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue