diff --git a/go.mod b/go.mod index 716b4d4..2fe6eb4 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21.1 require ( github.com/containrrr/shoutrrr v0.8.0 github.com/google/uuid v1.3.1 + github.com/jmoiron/sqlx v1.3.5 github.com/spf13/viper v1.16.0 golang.org/x/crypto v0.14.0 modernc.org/sqlite v1.26.0 diff --git a/go.sum b/go.sum index 42aa1e7..94ae8d4 100644 --- a/go.sum +++ b/go.sum @@ -70,6 +70,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2 github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -142,6 +144,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc= github.com/jarcoal/httpmock v1.3.0/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= +github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= +github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= @@ -155,6 +159,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -162,6 +168,7 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= diff --git a/internal/db/db-edit.go b/internal/db/edit.go similarity index 100% rename from internal/db/db-edit.go rename to internal/db/edit.go diff --git a/internal/db/quote_str.go b/internal/db/quote_str.go index d577812..aa2d36c 100644 --- a/internal/db/quote_str.go +++ b/internal/db/quote_str.go @@ -5,7 +5,3 @@ import "strings" func quoteStr(str string) string { return strings.ReplaceAll(str, "'", "''") } - -func unquoteStr(str string) string { - return strings.ReplaceAll(str, "''", "'") -} diff --git a/internal/db/db-connect.go b/internal/db/sqlite.go similarity index 54% rename from internal/db/db-connect.go rename to internal/db/sqlite.go index 2546d31..e0340b0 100644 --- a/internal/db/db-connect.go +++ b/internal/db/sqlite.go @@ -1,10 +1,11 @@ package db import ( - "database/sql" "log" "sync" + "github.com/jmoiron/sqlx" + // Import sqlite module _ "modernc.org/sqlite" @@ -17,7 +18,7 @@ var mu sync.Mutex func dbExec(path, sqlStatement string) { mu.Lock() - db, err := sql.Open("sqlite", path) + db, err := sqlx.Connect("sqlite", path) check.IfError(err) defer db.Close() @@ -33,28 +34,15 @@ func Select(path string) (dbHosts []models.Host) { sqlStatement := `SELECT * FROM "now" ORDER BY DATE DESC` mu.Lock() - db, _ := sql.Open("sqlite", path) + db, _ := sqlx.Connect("sqlite", path) defer db.Close() - res, err := db.Query(sqlStatement) + err := db.Select(&dbHosts, sqlStatement) mu.Unlock() if err != nil { log.Fatal("ERROR: db_select: ", err) } - dbHosts = []models.Host{} - for res.Next() { - var oneHost models.Host - err = res.Scan(&oneHost.ID, &oneHost.Name, &oneHost.IP, &oneHost.Mac, &oneHost.Hw, &oneHost.Date, &oneHost.Known, &oneHost.Now) - if err != nil { - log.Fatal(err) - } - oneHost.Name = unquoteStr(oneHost.Name) - oneHost.Hw = unquoteStr(oneHost.Hw) - dbHosts = append(dbHosts, oneHost) - } - - //fmt.Println("Select all:", dbHosts) return dbHosts } diff --git a/internal/models/models.go b/internal/models/models.go index 47ccaaf..e690e73 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -23,14 +23,14 @@ type Conf struct { // Host - one host type Host struct { - ID uint16 - Name string - IP string - Mac string - Hw string - Date string - Known uint16 - Now uint16 + ID uint16 `db:"ID"` + Name string `db:"NAME"` + IP string `db:"IP"` + Mac string `db:"MAC"` + Hw string `db:"HW"` + Date string `db:"DATE"` + Known uint16 `db:"KNOWN"` + Now uint16 `db:"NOW"` } // // History for hosts diff --git a/internal/scan/start.go b/internal/scan/start.go index 067f94a..78449a8 100644 --- a/internal/scan/start.go +++ b/internal/scan/start.go @@ -25,7 +25,7 @@ func Start(appConfig models.Conf, quit chan bool) { plusDate := lastDate.Add(time.Duration(appConfig.Timeout) * time.Second) if nowDate.After(plusDate) { - structHosts = arpScan(appConfig.Iface, appConfig.LogLevel) + structHosts = arpScan(appConfig.Iface, appConfig.LogLevel) // arpscan.go dbHosts = db.Select(appConfig.DbPath) toMap() diff --git a/internal/web/host.go b/internal/web/host.go index 65c3474..4f0f2e2 100644 --- a/internal/web/host.go +++ b/internal/web/host.go @@ -21,7 +21,7 @@ func delHandler(w http.ResponseWriter, r *http.Request) { db.Delete(AppConfig.DbPath, uint16(id)) - http.Redirect(w, r, "/home/", 302) + http.Redirect(w, r, "/", 302) } func hostHandler(w http.ResponseWriter, r *http.Request) { diff --git a/internal/web/index.go b/internal/web/index.go index c3f0469..33bec38 100644 --- a/internal/web/index.go +++ b/internal/web/index.go @@ -3,14 +3,13 @@ package web import ( "net/http" - "github.com/aceberg/WatchYourLAN/internal/db" "github.com/aceberg/WatchYourLAN/internal/models" ) func indexHandler(w http.ResponseWriter, r *http.Request) { var guiData models.GuiData - AllHosts = db.Select(AppConfig.DbPath) + updateAllHosts() guiData.Config = AppConfig guiData.Hosts = AllHosts diff --git a/internal/web/line.go b/internal/web/line.go index 2cce657..411cc32 100644 --- a/internal/web/line.go +++ b/internal/web/line.go @@ -9,6 +9,8 @@ import ( func lineHandler(w http.ResponseWriter, r *http.Request) { var guiData models.GuiData + updateAllHosts() + guiData.Config = AppConfig guiData.Hosts = []models.Host{} diff --git a/internal/web/search.go b/internal/web/search.go index 897e9af..42caed5 100644 --- a/internal/web/search.go +++ b/internal/web/search.go @@ -8,17 +8,23 @@ import ( ) func searchHandler(w http.ResponseWriter, r *http.Request) { + var guiData models.GuiData + search := r.FormValue("search") + updateAllHosts() + foundHosts := []models.Host{} for _, oneHost := range AllHosts { if inString(oneHost.Name, search) || inString(oneHost.IP, search) || inString(oneHost.Mac, search) || inString(oneHost.Hw, search) || inString(oneHost.Date, search) { foundHosts = append(foundHosts, oneHost) } } - AllHosts = foundHosts - http.Redirect(w, r, r.Header.Get("Referer"), 302) + guiData.Config = AppConfig + guiData.Hosts = foundHosts + + execTemplate(w, "index", guiData) } func inString(str1 string, str2 string) bool { diff --git a/internal/web/sort.go b/internal/web/sort.go index 98e5ed6..86a5b74 100644 --- a/internal/web/sort.go +++ b/internal/web/sort.go @@ -8,7 +8,6 @@ import ( "sort" "strconv" - "github.com/aceberg/WatchYourLAN/internal/db" "github.com/aceberg/WatchYourLAN/internal/models" ) @@ -44,6 +43,7 @@ func sortByIPs(method string) { } func sortHandler(w http.ResponseWriter, r *http.Request) { + var guiData models.GuiData sortMethod := r.FormValue("sort_method") @@ -73,10 +73,13 @@ func sortHandler(w http.ResponseWriter, r *http.Request) { case "known-down": sortByField("desc", "Known") default: - AllHosts = db.Select(AppConfig.DbPath) + updateAllHosts() } - http.Redirect(w, r, r.Header.Get("Referer"), 302) + guiData.Config = AppConfig + guiData.Hosts = AllHosts + + execTemplate(w, "line", guiData) } func sortByField(method string, field string) { diff --git a/internal/web/webgui.go b/internal/web/webgui.go index e07e1a8..835fc3b 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -23,10 +23,9 @@ func Gui(configPath, nodePath string) { address := AppConfig.GuiIP + ":" + AppConfig.GuiPort QuitScan = make(chan bool) - go scan.Start(AppConfig, QuitScan) - AllHosts = db.Select(AppConfig.DbPath) + updateAllHosts() // webgui.go log.Println("=================================== ") log.Printf("Web GUI at http://%s", address) @@ -35,19 +34,23 @@ func Gui(configPath, nodePath string) { http.HandleFunc("/login/", loginHandler) // login.go http.HandleFunc("/", auth.Auth(indexHandler, &authConf)) - http.HandleFunc("/auth_conf/", auth.Auth(authConfHandler, &authConf)) // auth-conf.go - http.HandleFunc("/auth_save/", auth.Auth(saveAuthHandler, &authConf)) // auth-conf.go - http.HandleFunc("/clear/", auth.Auth(clearHandler, &authConf)) // config.go - http.HandleFunc("/config/", auth.Auth(configHandler, &authConf)) // config.go - http.HandleFunc("/del_host/", auth.Auth(delHandler, &authConf)) - http.HandleFunc("/host/", auth.Auth(hostHandler, &authConf)) - http.HandleFunc("/line/", auth.Auth(lineHandler, &authConf)) - http.HandleFunc("/port_scan/", auth.Auth(portHandler, &authConf)) - http.HandleFunc("/save_config/", auth.Auth(saveConfigHandler, &authConf)) - http.HandleFunc("/search_hosts/", auth.Auth(searchHandler, &authConf)) - http.HandleFunc("/sort_hosts/", auth.Auth(sortHandler, &authConf)) - http.HandleFunc("/test_notify/", auth.Auth(testNotifyHandler, &authConf)) - http.HandleFunc("/update_host/", auth.Auth(updateHandler, &authConf)) + http.HandleFunc("/auth_conf/", auth.Auth(authConfHandler, &authConf)) // auth-conf.go + http.HandleFunc("/auth_save/", auth.Auth(saveAuthHandler, &authConf)) // auth-conf.go + http.HandleFunc("/clear/", auth.Auth(clearHandler, &authConf)) // config.go + http.HandleFunc("/config/", auth.Auth(configHandler, &authConf)) // config.go + http.HandleFunc("/del_host/", auth.Auth(delHandler, &authConf)) // host.go + http.HandleFunc("/host/", auth.Auth(hostHandler, &authConf)) // host.go + http.HandleFunc("/line/", auth.Auth(lineHandler, &authConf)) // line.go + http.HandleFunc("/port_scan/", auth.Auth(portHandler, &authConf)) // port.go + http.HandleFunc("/save_config/", auth.Auth(saveConfigHandler, &authConf)) // config.go + http.HandleFunc("/search_hosts/", auth.Auth(searchHandler, &authConf)) // search.go + http.HandleFunc("/sort_hosts/", auth.Auth(sortHandler, &authConf)) // sort.go + http.HandleFunc("/test_notify/", auth.Auth(testNotifyHandler, &authConf)) // config.go + http.HandleFunc("/update_host/", auth.Auth(updateHandler, &authConf)) // update.go err := http.ListenAndServe(address, nil) check.IfError(err) } + +func updateAllHosts() { + AllHosts = db.Select(AppConfig.DbPath) +}