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 @@