Files
honeydany/app/web.go
T

146 lines
5.9 KiB
Go
Raw Normal View History

2025-09-28 06:48:03 +01:00
package app
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
"time"
)
var tpl = template.Must(template.New("base").Parse(`
<!doctype html>
<html>
2025-09-28 07:18:53 +01:00
<head>
<meta charset="utf-8">
<title>Honeypot Dashboard</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
nav { background: #f0f0f0; padding: 10px; margin-bottom: 20px; }
nav a { margin-right: 15px; text-decoration: none; color: #333; }
nav a:hover { color: #007bff; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
.threat-high { background-color: #ffebee; }
.threat-medium { background-color: #fff3e0; }
.threat-low { background-color: #f3e5f5; }
.blacklisted { background-color: #ffcdd2; font-weight: bold; }
pre { white-space: pre-wrap; max-width: 300px; overflow: auto; }
.stats { display: flex; gap: 20px; margin-bottom: 20px; }
.stat-box { background: #f8f9fa; padding: 15px; border-radius: 5px; min-width: 150px; }
.stat-number { font-size: 24px; font-weight: bold; color: #007bff; }
</style>
</head>
2025-09-28 06:48:03 +01:00
<body>
2025-09-28 07:18:53 +01:00
<h1>🍯 Honeypot Dashboard</h1>
<nav>
<a href="/">Home</a>
<a href="/logs">Recent Logs</a>
<a href="/threats">Top Threats</a>
<a href="/blacklist">Blacklist</a>
<a href="/stats">Statistics</a>
</nav>
2025-09-28 06:48:03 +01:00
{{ .Body }}
</body>
</html>
`))
func (a *App) startWeb() {
2025-09-28 07:18:53 +01:00
bind := a.cfg.Web.Bind
port := a.cfg.Web.Port
addr := fmt.Sprintf("%s:%d", bind, port)
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var sb strings.Builder
// Get basic stats
if a.threatIntel != nil {
stats := a.threatIntel.GetStats()
sb.WriteString("<div class=\"stats\">")
sb.WriteString(fmt.Sprintf("<div class=\"stat-box\"><div class=\"stat-number\">%v</div><div>Total IPs</div></div>", stats["total_ips"]))
sb.WriteString(fmt.Sprintf("<div class=\"stat-box\"><div class=\"stat-number\">%v</div><div>Blacklisted</div></div>", stats["blacklisted_ips"]))
sb.WriteString(fmt.Sprintf("<div class=\"stat-box\"><div class=\"stat-number\">%v</div><div>Connections</div></div>", stats["total_connections"]))
sb.WriteString(fmt.Sprintf("<div class=\"stat-box\"><div class=\"stat-number\">%v</div><div>Auth Attempts</div></div>", stats["total_auth_attempts"]))
sb.WriteString("</div>")
}
sb.WriteString("<h2>Quick Actions</h2>")
sb.WriteString("<p><a href=\"/logs\">📋 View Recent Logs</a></p>")
sb.WriteString("<p><a href=\"/threats\">⚠️ View Top Threats</a></p>")
sb.WriteString("<p><a href=\"/blacklist\">🚫 Manage Blacklist</a></p>")
sb.WriteString("<p><a href=\"/stats\">📊 Detailed Statistics</a></p>")
tpl.Execute(w, map[string]interface{}{"Body": template.HTML(sb.String())})
})
mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) {
// display last 200 logs
var rows []Record
if a.logger != nil && a.logger.mode == "sqlite" && a.logger.db != nil {
// query sqlite
q := `SELECT timestamp, remote_addr, remote_port, service, details, raw_payload FROM logs ORDER BY id DESC LIMIT 200`
rs, err := a.logger.db.Query(q)
if err != nil {
http.Error(w, "db query failed", 500)
return
}
defer rs.Close()
for rs.Next() {
var ts, ra, rp, svc, detailsS, raw string
if err := rs.Scan(&ts, &ra, &rp, &svc, &detailsS, &raw); err != nil {
continue
}
var det map[string]string
if err := json.Unmarshal([]byte(detailsS), &det); err != nil {
continue
}
rows = append(rows, Record{Timestamp: parseTime(ts), RemoteAddr: ra, RemotePort: rp, Service: svc, Details: det, RawPayload: raw})
}
} else {
// try to read file based JSON-lines
path := a.cfg.LogPath
b, err := os.ReadFile(path)
if err == nil {
lines := strings.Split(string(b), "\n")
for i := len(lines) - 1; i >= 0 && len(rows) < 200; i-- {
line := strings.TrimSpace(lines[i])
if line == "" {
continue
}
var rec Record
if err := json.Unmarshal([]byte(line), &rec); err != nil {
continue
}
rows = append(rows, rec)
}
}
}
2025-09-28 06:48:03 +01:00
2025-09-28 07:18:53 +01:00
// render simple table
var sb strings.Builder
sb.WriteString("<table border=1 cellpadding=4><tr><th>Time</th><th>Remote</th><th>Service</th><th>Details</th><th>Payload</th></tr>")
for _, r := range rows {
detB, _ := json.Marshal(r.Details)
sb.WriteString(fmt.Sprintf("<tr><td>%s</td><td>%s:%s</td><td>%s</td><td>%s</td><td><pre>%s</pre></td></tr>", r.Timestamp.Format("2006-01-02 15:04:05"), r.RemoteAddr, r.RemotePort, r.Service, template.HTMLEscapeString(string(detB)), template.HTMLEscapeString(r.RawPayload)))
}
sb.WriteString("</table>")
tpl.Execute(w, map[string]interface{}{"Body": template.HTML(sb.String())})
})
2025-09-28 06:48:03 +01:00
srv := &http.Server{Addr: addr, Handler: mux}
2025-09-28 07:18:53 +01:00
a.addHTTPServer(srv)
2025-09-28 06:48:03 +01:00
log.Printf("Dashboard listening on http://%s", addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Printf("dashboard error: %v", err)
}
}
func parseTime(s string) (t time.Time) {
2025-09-28 07:18:53 +01:00
t, _ = time.Parse(time.RFC3339Nano, s)
if t.IsZero() {
// fallback current time
t = time.Now()
}
return
2025-09-28 06:48:03 +01:00
}