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 @@
Scan for open TCP ports (may take some time)
+