From e371e7bc48fdd8bf2fd252d64b325e860de4aa02 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Wed, 4 Jan 2023 21:26:32 +0700 Subject: [PATCH] Scan host for open ports --- internal/port/scan.go | 42 ++++++++++++++++++++++++++++++ internal/web/config.go | 1 + internal/web/const-var.go | 29 +++++++++++++++++++++ internal/web/port.go | 37 ++++++++++++++++++++++++++ internal/web/templates/config.html | 4 +++ internal/web/templates/host.html | 24 +++++++++++++++++ internal/web/templates/port.html | 18 +++++++++++++ internal/web/webgui.go | 25 +----------------- 8 files changed, 156 insertions(+), 24 deletions(-) create mode 100644 internal/port/scan.go create mode 100644 internal/web/const-var.go create mode 100644 internal/web/port.go create mode 100644 internal/web/templates/port.html diff --git a/internal/port/scan.go b/internal/port/scan.go new file mode 100644 index 0000000..ea305d7 --- /dev/null +++ b/internal/port/scan.go @@ -0,0 +1,42 @@ +package port + +import ( + "fmt" + "net" + "time" +) + +func isOpen(host string, port int) bool { + + timeout := 3 * time.Second + target := fmt.Sprintf("%s:%d", host, port) + + conn, err := net.DialTimeout("tcp", target, timeout) + if err != nil { + return false + } + + if conn != nil { + conn.Close() + return true + } + + return false +} + +// Scan - scan all TCP ports of a host +func Scan(host string, begin, end int) []string { + var onePort string + var ports []string + + ports = []string{} + + for i := begin; i < end; i++ { + if isOpen(host, i) { + onePort = fmt.Sprintf("%s:%d", host, i) + ports = append(ports, onePort) + } + } + + return ports +} diff --git a/internal/web/config.go b/internal/web/config.go index 77f2972..ca76e69 100644 --- a/internal/web/config.go +++ b/internal/web/config.go @@ -33,6 +33,7 @@ func saveConfigHandler(w http.ResponseWriter, r *http.Request) { var err error AppConfig.Iface = r.FormValue("iface") + AppConfig.DbPath = r.FormValue("dbpath") AppConfig.GuiIP = r.FormValue("host") AppConfig.GuiPort = r.FormValue("port") AppConfig.ShoutURL = r.FormValue("shout") diff --git a/internal/web/const-var.go b/internal/web/const-var.go new file mode 100644 index 0000000..183141d --- /dev/null +++ b/internal/web/const-var.go @@ -0,0 +1,29 @@ +package web + +import ( + "embed" + + "github.com/aceberg/WatchYourLAN/internal/models" +) + +var ( + // AppConfig - app config + AppConfig models.Conf + + // AllHosts - all hosts from DB + AllHosts []models.Host + + // ConfigPath - path to config file + ConfigPath string + + // QuitScan - send stop signal to scan + QuitScan chan bool + + // TemplHTML - embed templates + // + //go:embed templates/* + TemplHTML embed.FS +) + +// TemplPath - path to html templates +const TemplPath = "templates/" diff --git a/internal/web/port.go b/internal/web/port.go new file mode 100644 index 0000000..e8fff80 --- /dev/null +++ b/internal/web/port.go @@ -0,0 +1,37 @@ +package web + +import ( + "html/template" + "net/http" + "strconv" + + "github.com/aceberg/WatchYourLAN/internal/check" + "github.com/aceberg/WatchYourLAN/internal/models" + "github.com/aceberg/WatchYourLAN/internal/port" +) + +func portHandler(w http.ResponseWriter, r *http.Request) { + var guiData models.GuiData + + guiData.Config = AppConfig + guiData.Icon = Icon + + ip := r.FormValue("ip") + beginStr := r.FormValue("begin") + endStr := r.FormValue("end") + + begin, err := strconv.Atoi(beginStr) + check.IfError(err) + + end, err := strconv.Atoi(endStr) + check.IfError(err) + + guiData.Themes = port.Scan(ip, begin, end) + + tmpl, err := template.ParseFS(TemplHTML, TemplPath+"port.html", TemplPath+"header.html", TemplPath+"footer.html") + check.IfError(err) + err = tmpl.ExecuteTemplate(w, "header", guiData) + check.IfError(err) + err = tmpl.ExecuteTemplate(w, "port", guiData) + check.IfError(err) +} diff --git a/internal/web/templates/config.html b/internal/web/templates/config.html index 8637ccc..f4c10e6 100644 --- a/internal/web/templates/config.html +++ b/internal/web/templates/config.html @@ -10,6 +10,10 @@ Interfaces + + Path to DB + + Host diff --git a/internal/web/templates/host.html b/internal/web/templates/host.html index f642389..d539b61 100644 --- a/internal/web/templates/host.html +++ b/internal/web/templates/host.html @@ -72,6 +72,30 @@ +
+
+ {{ range .Hosts }} + {{ if eq .Now 1 }} +

Scan for open TCP ports (may take some time)

+ + + + + + + + + + + + + + + + +
Start port
End port
+ {{ end }} + {{ end }} diff --git a/internal/web/templates/port.html b/internal/web/templates/port.html new file mode 100644 index 0000000..d48a992 --- /dev/null +++ b/internal/web/templates/port.html @@ -0,0 +1,18 @@ +{{ define "port" }} + + +
+
+
+

Open TCP ports:

+ {{ range .Themes }} + {{ . }} +
+ {{ end }} +
+
+
+ + +{{ template "footer" }} +{{ end }} \ No newline at end of file diff --git a/internal/web/webgui.go b/internal/web/webgui.go index 7a9f0d0..283efc1 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -1,39 +1,15 @@ package web import ( - "embed" "log" "net/http" "github.com/aceberg/WatchYourLAN/internal/check" "github.com/aceberg/WatchYourLAN/internal/conf" "github.com/aceberg/WatchYourLAN/internal/db" - "github.com/aceberg/WatchYourLAN/internal/models" "github.com/aceberg/WatchYourLAN/internal/scan" ) -var ( - // AppConfig - app config - AppConfig models.Conf - - // AllHosts - all hosts from DB - AllHosts []models.Host - - // ConfigPath - path to config file - ConfigPath string - - // QuitScan - send stop signal to scan - QuitScan chan bool - - // TemplHTML - embed templates - // - //go:embed templates/* - TemplHTML embed.FS -) - -// TemplPath - path to html templates -const TemplPath = "templates/" - // Gui - start web GUI func Gui(configPath string) { @@ -61,6 +37,7 @@ func Gui(configPath string) { http.HandleFunc("/home/", homeHandler) http.HandleFunc("/host/", hostHandler) http.HandleFunc("/line/", lineHandler) + http.HandleFunc("/port_scan/", portHandler) http.HandleFunc("/save_config/", saveConfigHandler) http.HandleFunc("/search_hosts/", searchHandler) http.HandleFunc("/sort_hosts/", sortHandler)