From 48d385e230937d211eed8cf0eac4a419f013c456 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Sun, 21 Aug 2022 16:18:43 +0700 Subject: [PATCH] docker alpine --- Dockerfile | 13 ++++++++++--- Dockerfile.ubuntu | 13 +++++++++++++ Makefile | 2 +- src/{work.go => compare.go} | 2 +- src/getconfig.go | 2 ++ src/main.go | 32 ++++++++++++++++++++++---------- src/templates/header.html | 2 +- 7 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 Dockerfile.ubuntu rename src/{work.go => compare.go} (90%) diff --git a/Dockerfile b/Dockerfile index c6c5bf7..dc5ee59 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,17 @@ -FROM ubuntu:20.04 +FROM golang:alpine AS builder -RUN apt update && apt install -y arp-scan && apt clean \ +RUN apk add build-base +COPY src /src +RUN cd /src && go build . + + +FROM alpine + +RUN apk add arp-scan \ && mkdir /data COPY src/templates /app/templates -COPY src/watchyourlan /app/ +COPY --from=builder /src/watchyourlan /app/ EXPOSE 8840 diff --git a/Dockerfile.ubuntu b/Dockerfile.ubuntu new file mode 100644 index 0000000..c6c5bf7 --- /dev/null +++ b/Dockerfile.ubuntu @@ -0,0 +1,13 @@ +FROM ubuntu:20.04 + +RUN apt update && apt install -y arp-scan && apt clean \ + && mkdir /data + +COPY src/templates /app/templates +COPY src/watchyourlan /app/ + +EXPOSE 8840 + +WORKDIR /app + +ENTRYPOINT ["./watchyourlan"] \ No newline at end of file diff --git a/Makefile b/Makefile index 70463f1..5ff44bb 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ DUSER=aceberg DNAME=watchyourlan -VERSION=0.4 +VERSION=0.5 mod: cd src && \ diff --git a/src/work.go b/src/compare.go similarity index 90% rename from src/work.go rename to src/compare.go index 3d7ceb0..121f1de 100644 --- a/src/work.go +++ b/src/compare.go @@ -16,7 +16,7 @@ func host_in_db(host Host, dbHosts []Host) bool { return false } -func db_compare(foundHosts []Host, dbHosts []Host) { +func hosts_compare(foundHosts []Host, dbHosts []Host) { // fmt.Println("Found hosts:", foundHosts) // fmt.Println("DB hosts:", dbHosts) diff --git a/src/getconfig.go b/src/getconfig.go index bf1a392..1eb9960 100644 --- a/src/getconfig.go +++ b/src/getconfig.go @@ -10,6 +10,7 @@ func get_config(path string) (config Conf) { viper.SetDefault("DBPATH", "/data/db.sqlite") viper.SetDefault("GUIIP", "localhost") viper.SetDefault("GUIPORT", "8840") + viper.SetDefault("TIMEOUT", "60") viper.SetConfigFile(path) viper.SetConfigType("env") @@ -21,6 +22,7 @@ func get_config(path string) (config Conf) { config.DbPath = viper.Get("DBPATH").(string) config.GuiIP = viper.Get("GUIIP").(string) config.GuiPort = viper.Get("GUIPORT").(string) + config.Timeout = viper.GetInt("TIMEOUT") // fmt.Println(viper.Get("DBPATH")) diff --git a/src/main.go b/src/main.go index 0814c1a..8b2f9fb 100644 --- a/src/main.go +++ b/src/main.go @@ -1,8 +1,9 @@ package main -// import ( -// "fmt" -// ) +import ( + // "fmt" + "time" +) type Host struct { Id uint16 @@ -20,21 +21,32 @@ type Conf struct { DbPath string GuiIP string GuiPort string + Timeout int } var AppConfig Conf var AllHosts []Host +func scan() { + for { + foundHosts := parse_output(scan_iface(AppConfig.Iface)) + dbHosts := db_select() + db_setnow() + hosts_compare(foundHosts, dbHosts) + + AllHosts = db_select() + // fmt.Println("Refresh") + time.Sleep(time.Duration(AppConfig.Timeout) * time.Second) + } +} + func main() { + AllHosts = []Host{} AppConfig = get_config("./data/config") db_create() - - foundHosts := parse_output(scan_iface(AppConfig.Iface)) - dbHosts := db_select() - db_setnow() - db_compare(foundHosts, dbHosts) - - AllHosts = db_select() + // fmt.Println("Timeout: ", AppConfig.Timeout) + go scan() + webgui() } \ No newline at end of file diff --git a/src/templates/header.html b/src/templates/header.html index 666ebd2..c5ee0c3 100644 --- a/src/templates/header.html +++ b/src/templates/header.html @@ -4,7 +4,7 @@