known button
This commit is contained in:
parent
ee895f8f8a
commit
21fafce4a8
7 changed files with 93 additions and 51 deletions
|
|
@ -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"`
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
44
src/index.go
44
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)
|
||||
}
|
||||
25
src/main.go
25
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()
|
||||
}
|
||||
|
|
@ -3,7 +3,11 @@
|
|||
<head>
|
||||
<title>Watch Your LAN</title>
|
||||
<meta charset="utf-8">
|
||||
<!--Auto refresh (seconds)-->
|
||||
<!-- <meta http-equiv="refresh" content="10"> -->
|
||||
<!--Favicon-->
|
||||
<link href="data:image/x-icon;base64,AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAA/6k7AP+uSQD/z50A7urjAD85MgAAOO8AWlpaAADIawAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADEREQAAAAAzEREREQAAAzEREREREAAzFxEREVERADN3d3VVVREDM3d3dYVlERMzd3d1VVUREzM3ERESERETMzcRESERERMzNxESMREREzM3ESNEQREwMzcSMxEREwAzNyM0REQzAAMzMzMzMzAAADMzMzMzAAAAADMzMwAAD4HwAA4AcAAMADAACAAQAAgAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIABAACAAQAAwAMAAOAHAAD4HwAA" rel="icon" type="image/x-icon" />
|
||||
<!--Bootstrap theme-->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootswatch@5.2.0/dist/solar/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
|
||||
|
|
|
|||
|
|
@ -43,8 +43,8 @@
|
|||
<th> </th>
|
||||
</tr>
|
||||
{{ range . }}
|
||||
{{ if eq .Now 0 }}
|
||||
<form action="/update_db/" method="post">
|
||||
<form action="/update_host/" method="post">
|
||||
<input name="id" type="hidden" class="form-control" value="{{ .Id }}">
|
||||
<tr>
|
||||
<td>
|
||||
<input name="name" type="text" class="form-control text-dark" value="{{ .Name }}">
|
||||
|
|
@ -55,9 +55,9 @@
|
|||
<td>{{ .Date }}</td>
|
||||
<td>
|
||||
{{ if eq .Known 1 }}
|
||||
<input class="form-check-input" type="checkbox" checked>
|
||||
<input name="known" class="form-check-input" type="checkbox" checked>
|
||||
{{ else }}
|
||||
<input class="form-check-input" type="checkbox">
|
||||
<input name="known" class="form-check-input" type="checkbox">
|
||||
{{ end }}
|
||||
</td>
|
||||
<td>
|
||||
|
|
@ -65,7 +65,6 @@
|
|||
</td>
|
||||
</tr>
|
||||
</form>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
</table>
|
||||
</div>
|
||||
|
|
|
|||
25
src/work.go
25
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue