From deaadf7c73869fd2d1e8819db30ec2459d94e723 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Mon, 15 Aug 2022 12:02:42 +0700 Subject: [PATCH] Makefile --- .gitignore | 3 +++ Makefile | 8 ++++++++ README.md | 2 ++ arpscan.go | 41 +++++++++++++++++++++++++++++++++++++++++ db-create.go | 36 ++++++++++++++++++++++++++++++++++++ go.mod | 5 +++++ go.sum | 2 ++ index.go | 22 ++++++++++++++++++++++ main.go | 30 ++++++++++++++++++++++++++++++ templates/index.html | 27 +++++++++++++++++++++++++++ 10 files changed, 176 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 arpscan.go create mode 100644 db-create.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 index.go create mode 100644 main.go create mode 100644 templates/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b2f7365 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Ignore local data +data/ +hosts.db \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..84ca203 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +mod: + rm go.mod + go mod init watchyourlan + go mod tidy +run: + go run . +build: + go build . \ No newline at end of file diff --git a/README.md b/README.md index 22a0154..cc5a406 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # WatchYourLAN Local network IP scanner with web gui + +Work in progress.. \ No newline at end of file diff --git a/arpscan.go b/arpscan.go new file mode 100644 index 0000000..b0fbeea --- /dev/null +++ b/arpscan.go @@ -0,0 +1,41 @@ +package main + +import ( + // "fmt" + "log" + "os/exec" + "strings" + "time" +) + +func scan_iface(iface string) (string) { + cmd, err := exec.Command("sudo", "arp-scan", "-l", "-N", "-x", "-I", iface).Output() + if err != nil { + log.Fatal(err) + } + + return string(cmd) + // fmt.Println(text) +} + +func parse_output(text string) ([]Host) { + var foundHosts = []Host{} + + perString := strings.Split(text, "\n") + currentTime := time.Now() + + for _, v := range perString { + if v != "" { + var oneHost Host + p := strings.Split(v, " ") + oneHost.Ip = p[0] + oneHost.Mac = p[1] + oneHost.Hw = p[2] + oneHost.Date = currentTime.Format("2006-01-02 15:04:05") + foundHosts = append(foundHosts, oneHost) + } + } + + return foundHosts + // fmt.Println(foundHosts) +} \ No newline at end of file diff --git a/db-create.go b/db-create.go new file mode 100644 index 0000000..1975fde --- /dev/null +++ b/db-create.go @@ -0,0 +1,36 @@ +package main + +import ( + "os" + "fmt" + "log" + "database/sql" + _ "github.com/mattn/go-sqlite3" +) + +func db_create(dbPath string) { + if _, err := os.Stat(dbPath); err == nil { + fmt.Println("DB exists") + } else { + sqlStatement := `CREATE TABLE "now" ( + "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + "NAME" TEXT, + "IP" TEXT, + "MAC" TEXT, + "HW" TEXT, + "DATE" TEXT, + "KNOWN" INTEGER DEFAULT 0 + );` + db_exec(dbPath, sqlStatement) + } +} + +func db_exec (dbPath string, sqlStatement string) { + db, _ := sql.Open("sqlite3", dbPath) + defer db.Close() + + _, err := db.Exec(sqlStatement) + if err != nil { + log.Fatal(err) + } +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8062895 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module watchyourlan + +go 1.19 + +require github.com/mattn/go-sqlite3 v1.14.14 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d5da9bc --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.14 h1:qZgc/Rwetq+MtyE18WhzjokPD93dNqLGNT3QJuLvBGw= +github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= diff --git a/index.go b/index.go new file mode 100644 index 0000000..79d9227 --- /dev/null +++ b/index.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "net/http" + "html/template" +) + +var FoundHosts []Host + +func index (w http.ResponseWriter, r *http.Request) { + fmt.Println(FoundHosts) + tmpl, _ := template.ParseFiles("templates/index.html") + tmpl.ExecuteTemplate(w, "index", FoundHosts) +} + +func webgui (hosts []Host) { + // fmt.Println(FoundHosts) + FoundHosts = hosts + http.HandleFunc("/", index) + http.ListenAndServe(":8840", nil) +} \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..bba0fa1 --- /dev/null +++ b/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" +) + +type Host struct { + Id uint16 + Name string + Ip string + Mac string + Hw string + Date string + Known uint16 +} + +func main() { + iface := "virbr-bw" + db_path := "data/hosts.db" + + text := scan_iface(iface) + foundHosts := parse_output(text) + + fmt.Println(foundHosts) + + db_create(db_path) + + fmt.Println("http://localhost:8840") + webgui(foundHosts) +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..7408418 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,27 @@ +{{ define "index"}} + +
+| Name | +Ip | +Mac | +Hardware | +
|---|---|---|---|
| {{ .Name }} | +{{ .Ip }} | +{{ .Mac }} | +{{ .Hw }} | +