diff --git a/internal/models/models.go b/internal/models/models.go index 4d3697a..dfbbf4a 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -33,7 +33,7 @@ type Host struct { // GuiData - all data sent to html page type GuiData struct { Config Conf - Hosts []Host + Host Host Themes []string Version string } diff --git a/internal/portscan/scan.go b/internal/portscan/scan.go new file mode 100644 index 0000000..9b6889c --- /dev/null +++ b/internal/portscan/scan.go @@ -0,0 +1,26 @@ +package portscan + +import ( + "fmt" + "net" + "time" +) + +// IsOpen - check one tcp port +func IsOpen(host, port string) bool { + + timeout := 3 * time.Second + target := fmt.Sprintf("%s:%s", host, port) + + conn, err := net.DialTimeout("tcp", target, timeout) + if err != nil { + return false + } + + if conn != nil { + conn.Close() + return true + } + + return false +} diff --git a/internal/web/api.go b/internal/web/api.go index a4ef539..58b79ea 100644 --- a/internal/web/api.go +++ b/internal/web/api.go @@ -3,11 +3,11 @@ package web import ( // "log" "net/http" - "strconv" "github.com/gin-gonic/gin" "github.com/aceberg/WatchYourLAN/internal/db" + "github.com/aceberg/WatchYourLAN/internal/portscan" ) func apiAll(c *gin.Context) { @@ -25,21 +25,28 @@ func apiHistory(c *gin.Context) { func apiHost(c *gin.Context) { idStr := c.Param("id") - id, _ := strconv.Atoi(idStr) - host := getHostByID(id, allHosts) // functions.go + + host := getHostByID(idStr, allHosts) // functions.go c.IndentedJSON(http.StatusOK, host) } +func apiPort(c *gin.Context) { + + addr := c.Param("addr") + port := c.Param("port") + state := portscan.IsOpen(addr, port) + + c.IndentedJSON(http.StatusOK, state) +} + func apiEdit(c *gin.Context) { idStr := c.Param("id") name := c.Param("name") toggleKnown := c.Param("known") - id, _ := strconv.Atoi(idStr) - - host := getHostByID(id, allHosts) // functions.go + host := getHostByID(idStr, allHosts) // functions.go if host.Date != "" { host.Name = name diff --git a/internal/web/functions.go b/internal/web/functions.go index 20f3dac..0e81c29 100644 --- a/internal/web/functions.go +++ b/internal/web/functions.go @@ -1,10 +1,14 @@ package web import ( + "strconv" + "github.com/aceberg/WatchYourLAN/internal/models" ) -func getHostByID(id int, hosts []models.Host) (oneHost models.Host) { +func getHostByID(idStr string, hosts []models.Host) (oneHost models.Host) { + + id, _ := strconv.Atoi(idStr) for _, host := range hosts { if host.ID == id { diff --git a/internal/web/host.go b/internal/web/host.go index 95b8b43..2f5d213 100644 --- a/internal/web/host.go +++ b/internal/web/host.go @@ -1,7 +1,6 @@ package web import ( - "log" "net/http" "github.com/gin-gonic/gin" @@ -14,9 +13,8 @@ func hostHandler(c *gin.Context) { guiData.Config = appConfig idStr := c.Param("id") - log.Println("ID =", idStr) - guiData.Version = idStr + guiData.Host = getHostByID(idStr, allHosts) c.HTML(http.StatusOK, "header.html", guiData) c.HTML(http.StatusOK, "host.html", guiData) diff --git a/internal/web/public/js/scan.js b/internal/web/public/js/scan.js new file mode 100644 index 0000000..0cd2da1 --- /dev/null +++ b/internal/web/public/js/scan.js @@ -0,0 +1,46 @@ +let stop = false; + +function stopScan() { + stop = true; +} + +async function scanAddr() { + const addr = document.getElementById("hostIP").value; + let begin = document.getElementById("begin").value; + let end = document.getElementById("end").value; + + if (begin == "") { + begin = 1 + } + if (end == "") { + end = 65535 + } + let portOpen = false; + stop = false; + + document.getElementById('stopBtn').style.visibility = "visible"; + + for (let i = begin ; i <= end; i++) { + + if (stop) { + break; + } + + let url = '/api/port/'+addr+'/'+i; + portOpen = await (await fetch(url)).json(); + + document.getElementById("curPort").innerHTML = "Scanning port "+i; + + if (portOpen) { + let html = genHTML(addr, i); + document.getElementById('foundPorts').insertAdjacentHTML('beforeend', html); + } + } + + document.getElementById('stopBtn').style.visibility = "hidden"; +} + +function genHTML(addr, port) { + html = `${port} `; + return html; +} \ No newline at end of file diff --git a/internal/web/templates/host.html b/internal/web/templates/host.html index f0859fc..37d85f0 100644 --- a/internal/web/templates/host.html +++ b/internal/web/templates/host.html @@ -1,5 +1,6 @@ {{ define "host.html" }} +
ID | +{{ .Host.ID }} | +
Name | +{{ .Host.Name }} | +
Iface | +{{ .Host.Iface }} | +
IP | +{{ .Host.IP }} | +
MAC | +{{ .Host.Mac }} | +
Hardware | +{{ .Host.Hw }} | +
Date | +{{ .Host.Date }} | +
Known | +{{ .Host.Known }} | +
Online | +{{ if eq .Host.Now 0 }} + + {{ else }} + + {{ end }} + | +