From 21fafce4a8a15873f1d266d1a5078c5e9ce3b7d4 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Sun, 21 Aug 2022 14:29:51 +0700 Subject: [PATCH] known button --- src/db-connect.go | 8 +++---- src/db-edit.go | 29 ++++++++++++++++++++------ src/index.go | 44 ++++++++++++++++++++++++++++++++------- src/main.go | 25 ++++++++-------------- src/templates/header.html | 4 ++++ src/templates/index.html | 9 ++++---- src/work.go | 25 +++++++++++----------- 7 files changed, 93 insertions(+), 51 deletions(-) diff --git a/src/db-connect.go b/src/db-connect.go index 9f8f158..86ae5b3 100644 --- a/src/db-connect.go +++ b/src/db-connect.go @@ -7,8 +7,8 @@ import ( _ "github.com/mattn/go-sqlite3" ) -func db_exec (dbPath string, sqlStatement string) { - db, _ := sql.Open("sqlite3", dbPath) +func db_exec(sqlStatement string) { + db, _ := sql.Open("sqlite3", AppConfig.DbPath) defer db.Close() _, err := db.Exec(sqlStatement) @@ -17,8 +17,8 @@ func db_exec (dbPath string, sqlStatement string) { } } -func db_select(dbPath string) (dbHosts []Host) { - db, _ := sql.Open("sqlite3", dbPath) +func db_select() (dbHosts []Host) { + db, _ := sql.Open("sqlite3", AppConfig.DbPath) defer db.Close() sqlStatement := `SELECT * FROM "now"` diff --git a/src/db-edit.go b/src/db-edit.go index ffdd3dc..15f66f3 100644 --- a/src/db-edit.go +++ b/src/db-edit.go @@ -5,8 +5,8 @@ import ( "fmt" ) -func db_create(dbPath string) { - if _, err := os.Stat(dbPath); err == nil { +func db_create() { + if _, err := os.Stat(AppConfig.DbPath); err == nil { fmt.Println("DB exists") } else { sqlStatement := `CREATE TABLE "now" ( @@ -19,14 +19,31 @@ func db_create(dbPath string) { "KNOWN" INTEGER DEFAULT 0, "NOW" INTEGER DEFAULT 0 );` - db_exec(dbPath, sqlStatement) + db_exec(sqlStatement) fmt.Println("Table created!") } } -func db_insert(dbPath string, oneHost Host) { - sqlStatement := `INSERT INTO "now" (NAME, IP, MAC, HW, DATE, KNOWN, NOW) VALUES ('%s','%s','%s','%s','%s','%d','%d');` +func db_insert(oneHost Host) { + sqlStatement := `INSERT INTO "now" (NAME, IP, MAC, HW, DATE, KNOWN, NOW) + VALUES ('%s','%s','%s','%s','%s','%d','%d');` sqlStatement = fmt.Sprintf(sqlStatement, oneHost.Name, oneHost.Ip, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now) //fmt.Println("Insert statement:", sqlStatement) - db_exec(dbPath, sqlStatement) + db_exec(sqlStatement) +} + +func db_update(oneHost Host) { + sqlStatement := `UPDATE "now" set + NAME = '%s', IP = '%s', MAC = '%s', HW = '%s', DATE = '%s', + KNOWN = '%d', NOW = '%d' + WHERE ID = '%d';` + sqlStatement = fmt.Sprintf(sqlStatement, oneHost.Name, oneHost.Ip, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now, oneHost.Id) + // fmt.Println("Update statement:", sqlStatement) + db_exec(sqlStatement) +} + +func db_setnow() { + sqlStatement := `UPDATE "now" set NOW = '0';` + // fmt.Println("Set now 0 statement:", sqlStatement) + db_exec(sqlStatement) } \ No newline at end of file diff --git a/src/index.go b/src/index.go index b923733..ab57417 100644 --- a/src/index.go +++ b/src/index.go @@ -4,21 +4,49 @@ import ( "fmt" "net/http" "html/template" + "strconv" ) -var FoundHosts []Host - -func index (w http.ResponseWriter, r *http.Request) { - fmt.Println(FoundHosts) +func index(w http.ResponseWriter, r *http.Request) { + //fmt.Println(AllHosts) tmpl, _ := template.ParseFiles("templates/index.html", "templates/header.html") - tmpl.ExecuteTemplate(w, "index", FoundHosts) + tmpl.ExecuteTemplate(w, "index", AllHosts) } -func webgui (config Conf, hosts []Host) { +func update_host(w http.ResponseWriter, r *http.Request) { + idStr := r.FormValue("id") + name := r.FormValue("name") + knownStr := r.FormValue("known") + + if idStr == "" { + fmt.Fprintf(w, "No data!") + } else { + var known uint16 + id, _ := strconv.Atoi(idStr) + known = 0 + if knownStr == "on" { + known = 1 + } + + for i, oneHost := range AllHosts { + if oneHost.Id == uint16(id) { + AllHosts[i].Name = name + AllHosts[i].Known = known + db_update(AllHosts[i]) + } + } + } + + http.Redirect(w, r, "/", http.StatusSeeOther) +} + +func webgui() { // fmt.Println(FoundHosts) - FoundHosts = hosts - address := config.GuiIP + ":" + config.GuiPort + address := AppConfig.GuiIP + ":" + AppConfig.GuiPort + + fmt.Println(fmt.Sprintf("http://%s", address)) http.HandleFunc("/", index) + http.HandleFunc("/update_host/", update_host) http.ListenAndServe(address, nil) } \ No newline at end of file diff --git a/src/main.go b/src/main.go index c750f28..0814c1a 100644 --- a/src/main.go +++ b/src/main.go @@ -1,8 +1,8 @@ package main -import ( - "fmt" -) +// import ( +// "fmt" +// ) type Host struct { Id uint16 @@ -23,25 +23,18 @@ type Conf struct { } var AppConfig Conf +var AllHosts []Host func main() { AppConfig = get_config("./data/config") + db_create() foundHosts := parse_output(scan_iface(AppConfig.Iface)) - - //fmt.Println("Found hosts:", foundHosts) - - db_create(AppConfig.DbPath) - dbHosts := db_select(AppConfig.DbPath) - - //fmt.Println("DB hosts:", dbHosts) - + dbHosts := db_select() + db_setnow() db_compare(foundHosts, dbHosts) - dbHosts = db_select(AppConfig.DbPath) - allHosts := append(foundHosts,dbHosts...) - - fmt.Println(fmt.Sprintf("http://%s:%s", AppConfig.GuiIP, AppConfig.GuiPort)) + AllHosts = db_select() - webgui(AppConfig, allHosts) + webgui() } \ No newline at end of file diff --git a/src/templates/header.html b/src/templates/header.html index 6be2333..666ebd2 100644 --- a/src/templates/header.html +++ b/src/templates/header.html @@ -3,7 +3,11 @@ Watch Your LAN + + + + diff --git a/src/templates/index.html b/src/templates/index.html index 8d09787..ecdce53 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -43,8 +43,8 @@ {{ range . }} - {{ if eq .Now 0 }} -
+ + @@ -55,9 +55,9 @@ {{ .Date }} {{ if eq .Known 1 }} - + {{ else }} - + {{ end }} @@ -65,7 +65,6 @@
- {{ end }} {{ end }} diff --git a/src/work.go b/src/work.go index 049f55f..3d7ceb0 100644 --- a/src/work.go +++ b/src/work.go @@ -1,12 +1,15 @@ package main -import ( - "fmt" -) +// import ( +// "fmt" +// ) func host_in_db(host Host, dbHosts []Host) bool { for _, oneHost := range dbHosts { - if host.Ip == oneHost.Ip { + if host.Ip == oneHost.Ip && host.Mac == oneHost.Mac && host.Hw == oneHost.Hw { + oneHost.Date = host.Date + oneHost.Now = 1 + db_update(oneHost) return true } } @@ -14,16 +17,14 @@ func host_in_db(host Host, dbHosts []Host) bool { } func db_compare(foundHosts []Host, dbHosts []Host) { - fmt.Println("Found hosts:", foundHosts) - fmt.Println("DB hosts:", dbHosts) + // fmt.Println("Found hosts:", foundHosts) + // fmt.Println("DB hosts:", dbHosts) for _, oneHost := range foundHosts { - if host_in_db(oneHost, dbHosts) { - fmt.Println("Host in db") - } else { - fmt.Println("No such host!") - oneHost.Now = 0 - db_insert(AppConfig.DbPath, oneHost) + if !(host_in_db(oneHost, dbHosts)) { + //fmt.Println("No such host!") + oneHost.Now = 1 + db_insert(oneHost) } } } \ No newline at end of file