Scan host for open ports

This commit is contained in:
aceberg 2023-01-04 21:26:32 +07:00
parent 58ae81e5de
commit e371e7bc48
8 changed files with 156 additions and 24 deletions

42
internal/port/scan.go Normal file
View 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
}

View file

@ -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")

29
internal/web/const-var.go Normal file
View 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
View 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)
}

View file

@ -10,6 +10,10 @@
<td>Interfaces</td>
<td><input name="iface" type="text" class="form-control" value="{{ .Config.Iface }}"></td>
</tr>
<tr>
<td>Path to DB</td>
<td><input name="dbpath" type="text" class="form-control" value="{{ .Config.DbPath }}"></td>
</tr>
<tr>
<td>Host</td>
<td><input name="host" type="text" class="form-control" value="{{ .Config.GuiIP }}"></td>

View file

@ -72,6 +72,30 @@
</td>
</tr>
</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>

View 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 }}

View file

@ -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)