Scan host for open ports
This commit is contained in:
parent
58ae81e5de
commit
e371e7bc48
8 changed files with 156 additions and 24 deletions
42
internal/port/scan.go
Normal file
42
internal/port/scan.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -33,6 +33,7 @@ func saveConfigHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
AppConfig.Iface = r.FormValue("iface")
|
AppConfig.Iface = r.FormValue("iface")
|
||||||
|
AppConfig.DbPath = r.FormValue("dbpath")
|
||||||
AppConfig.GuiIP = r.FormValue("host")
|
AppConfig.GuiIP = r.FormValue("host")
|
||||||
AppConfig.GuiPort = r.FormValue("port")
|
AppConfig.GuiPort = r.FormValue("port")
|
||||||
AppConfig.ShoutURL = r.FormValue("shout")
|
AppConfig.ShoutURL = r.FormValue("shout")
|
||||||
|
|
|
||||||
29
internal/web/const-var.go
Normal file
29
internal/web/const-var.go
Normal file
|
|
@ -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/"
|
||||||
37
internal/web/port.go
Normal file
37
internal/web/port.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,10 @@
|
||||||
<td>Interfaces</td>
|
<td>Interfaces</td>
|
||||||
<td><input name="iface" type="text" class="form-control" value="{{ .Config.Iface }}"></td>
|
<td><input name="iface" type="text" class="form-control" value="{{ .Config.Iface }}"></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Path to DB</td>
|
||||||
|
<td><input name="dbpath" type="text" class="form-control" value="{{ .Config.DbPath }}"></td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Host</td>
|
<td>Host</td>
|
||||||
<td><input name="host" type="text" class="form-control" value="{{ .Config.GuiIP }}"></td>
|
<td><input name="host" type="text" class="form-control" value="{{ .Config.GuiIP }}"></td>
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,30 @@
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
{{ range .Hosts }}
|
||||||
|
{{ if eq .Now 1 }}
|
||||||
|
<p>Scan for open TCP ports (may take some time)</p>
|
||||||
|
<table class="table">
|
||||||
|
<form action="/port_scan/" method="post">
|
||||||
|
<input name="ip" type="hidden" class="form-control" value="{{ .IP }}">
|
||||||
|
<tr>
|
||||||
|
<td>Start port</td>
|
||||||
|
<td><input name="begin" type="text" class="form-control" placeholder="1"></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>End port</td>
|
||||||
|
<td><input name="end" type="text" class="form-control" placeholder="65535"></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><button type="submit" class="btn btn-success">Scan host</button></td>
|
||||||
|
<td></td>
|
||||||
|
</tr>
|
||||||
|
</form>
|
||||||
|
</table>
|
||||||
|
{{ end }}
|
||||||
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
18
internal/web/templates/port.html
Normal file
18
internal/web/templates/port.html
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
{{ define "port" }}
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="container mt-5">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<p>Open TCP ports:</p>
|
||||||
|
{{ range .Themes }}
|
||||||
|
<a href="http://{{ . }}" target="_blank">{{ . }}</a>
|
||||||
|
<br>
|
||||||
|
{{ end }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
{{ template "footer" }}
|
||||||
|
{{ end }}
|
||||||
|
|
@ -1,39 +1,15 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/aceberg/WatchYourLAN/internal/check"
|
"github.com/aceberg/WatchYourLAN/internal/check"
|
||||||
"github.com/aceberg/WatchYourLAN/internal/conf"
|
"github.com/aceberg/WatchYourLAN/internal/conf"
|
||||||
"github.com/aceberg/WatchYourLAN/internal/db"
|
"github.com/aceberg/WatchYourLAN/internal/db"
|
||||||
"github.com/aceberg/WatchYourLAN/internal/models"
|
|
||||||
"github.com/aceberg/WatchYourLAN/internal/scan"
|
"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
|
// Gui - start web GUI
|
||||||
func Gui(configPath string) {
|
func Gui(configPath string) {
|
||||||
|
|
||||||
|
|
@ -61,6 +37,7 @@ func Gui(configPath string) {
|
||||||
http.HandleFunc("/home/", homeHandler)
|
http.HandleFunc("/home/", homeHandler)
|
||||||
http.HandleFunc("/host/", hostHandler)
|
http.HandleFunc("/host/", hostHandler)
|
||||||
http.HandleFunc("/line/", lineHandler)
|
http.HandleFunc("/line/", lineHandler)
|
||||||
|
http.HandleFunc("/port_scan/", portHandler)
|
||||||
http.HandleFunc("/save_config/", saveConfigHandler)
|
http.HandleFunc("/save_config/", saveConfigHandler)
|
||||||
http.HandleFunc("/search_hosts/", searchHandler)
|
http.HandleFunc("/search_hosts/", searchHandler)
|
||||||
http.HandleFunc("/sort_hosts/", sortHandler)
|
http.HandleFunc("/sort_hosts/", sortHandler)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue