Skip to content

Commit

Permalink
feature/25 :: Added basic auth to webUI
Browse files Browse the repository at this point in the history
  • Loading branch information
reneManqueros committed Sep 27, 2022
1 parent 0c49949 commit 5f99309
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 52 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
8 changes: 5 additions & 3 deletions src/getconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ 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")

viper.SetConfigFile(configPath)
viper.SetConfigFile(configPath)
viper.SetConfigType("env")
viper.ReadInConfig()
viper.ReadInConfig()

viper.AutomaticEnv() // Get ENVIRONMENT variables

config.Iface = viper.Get("IFACE").(string)
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)
Expand All @@ -37,4 +39,4 @@ func write_config() {
viper.SetConfigType("env")
viper.Set("THEME", AppConfig.Theme)
viper.WriteConfig()
}
}
69 changes: 35 additions & 34 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
package main

import (
"time"
"time"
)

type Host struct {
Id uint16
Name string
Ip string
Mac string
Hw string
Date string
Known uint16
Now uint16
Id uint16
Name string
Ip string
Mac string
Hw string
Date string
Known uint16
Now uint16
}

type Conf struct {
Iface string
DbPath string
GuiIP string
GuiPort string
Timeout int
ShoutUrl string
Theme string
Iface string
DbPath string
GuiIP string
GuiPort string
GuiAuth string
ShoutUrl string
Theme string
Timeout int
}

var AppConfig Conf
var AllHosts []Host

func scan_and_compare() {
var foundHosts []Host
var dbHosts []Host
for { // Endless
foundHosts = arp_scan() // Scan interfaces
dbHosts = db_select() // Select everything from DB
db_setnow() // Mark hosts in DB as offline
hosts_compare(foundHosts, dbHosts) // Compare hosts online and in DB
// and add them to DB
AllHosts = db_select()
time.Sleep(time.Duration(AppConfig.Timeout) * time.Second) // Timeout
}
var foundHosts []Host
var dbHosts []Host
for { // Endless
foundHosts = arp_scan() // Scan interfaces
dbHosts = db_select() // Select everything from DB
db_setnow() // Mark hosts in DB as offline
hosts_compare(foundHosts, dbHosts) // Compare hosts online and in DB
// and add them to DB
AllHosts = db_select()
time.Sleep(time.Duration(AppConfig.Timeout) * time.Second) // Timeout
}
}

func main() {
AllHosts = []Host{}
AppConfig = get_config() // Get config from Defaults, Config file, Env
AllHosts = []Host{}
AppConfig = get_config() // Get config from Defaults, Config file, Env

db_create() // Check if DB exists. Create if not

go scan_and_compare()
db_create() // Check if DB exists. Create if not

webgui() // Start web GUI
}
go scan_and_compare()

webgui() // Start web GUI
}
51 changes: 36 additions & 15 deletions src/web-index.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package main

import (
"fmt"
"log"
"net/http"
"html/template"
"strconv"
"fmt"
"html/template"
"log"
"net/http"
"strconv"
)

func index(w http.ResponseWriter, r *http.Request) {
type allData struct {
Config Conf
Hosts []Host
Hosts []Host
}
var guiData allData
guiData.Config = AppConfig
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

0 comments on commit 5f99309

Please sign in to comment.