This commit is contained in:
aceberg 2022-08-15 12:02:42 +07:00
parent 5ad7f922a3
commit deaadf7c73
10 changed files with 176 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
# Ignore local data
data/
hosts.db

8
Makefile Normal file
View file

@ -0,0 +1,8 @@
mod:
rm go.mod
go mod init watchyourlan
go mod tidy
run:
go run .
build:
go build .

View file

@ -1,2 +1,4 @@
# WatchYourLAN
Local network IP scanner with web gui
Work in progress..

41
arpscan.go Normal file
View file

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

36
db-create.go Normal file
View file

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

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module watchyourlan
go 1.19
require github.com/mattn/go-sqlite3 v1.14.14

2
go.sum Normal file
View file

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

22
index.go Normal file
View file

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

30
main.go Normal file
View file

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

27
templates/index.html Normal file
View file

@ -0,0 +1,27 @@
{{ define "index"}}
<html>
<head>
<title>Watch Your LAN</title>
<meta charset="utf-8">
<link href="https://cdn.jsdelivr.net/npm/bootswatch@5.2.0/dist/spacelab/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Ip</th>
<th>Mac</th>
<th>Hardware</th>
</tr>
{{ range . }}
<tr>
<td>{{ .Name }}</td>
<td>{{ .Ip }}</td>
<td>{{ .Mac }}</td>
<td>{{ .Hw }}</td>
</tr>
{{ end }}
</table>
</body>
</html>
{{ end }}