2025-07-16 15:39:28 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"embed"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
2025-07-16 16:21:18 +01:00
|
|
|
"os"
|
2025-07-16 15:39:28 +01:00
|
|
|
"os/exec"
|
2025-07-16 16:21:18 +01:00
|
|
|
"os/signal"
|
2025-07-16 15:39:28 +01:00
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
2025-07-16 16:21:18 +01:00
|
|
|
"syscall"
|
2025-07-16 15:39:28 +01:00
|
|
|
"time"
|
|
|
|
|
|
2025-07-22 07:28:30 +01:00
|
|
|
"gonetkit/landingpage"
|
|
|
|
|
"gonetkit/parser"
|
|
|
|
|
"gonetkit/passwordgenerator"
|
|
|
|
|
"gonetkit/pwpusher"
|
|
|
|
|
"gonetkit/resolver"
|
2025-07-16 15:39:28 +01:00
|
|
|
|
|
|
|
|
"github.com/getlantern/systray"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2025-07-16 16:21:18 +01:00
|
|
|
addr = flag.String("host", "127.0.0.1", "IP to bind")
|
|
|
|
|
port = flag.Int("port", 5555, "Port to run on")
|
|
|
|
|
headless = flag.Bool("headless", false, "Force headless mode (disable system tray)")
|
2025-07-16 15:39:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:embed web/*
|
|
|
|
|
var embeddedFiles embed.FS
|
|
|
|
|
|
|
|
|
|
func onReady(addrPort string, shutdownCh chan struct{}) {
|
|
|
|
|
var iconPath string
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
iconPath = "web/favicon.ico"
|
|
|
|
|
} else {
|
2025-07-17 06:07:33 +01:00
|
|
|
iconPath = "web/favicon.ico"
|
2025-07-16 15:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iconData, err := fs.ReadFile(embeddedFiles, iconPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Failed to load system tray icon (%s): %v", iconPath, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(iconData) == 0 {
|
|
|
|
|
log.Printf("System tray icon (%s) is empty", iconPath)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.Printf("Loaded system tray icon (%s): %d bytes", iconPath, len(iconData))
|
|
|
|
|
|
|
|
|
|
// SetIcon does not return an error, so call it directly
|
|
|
|
|
systray.SetIcon(iconData)
|
|
|
|
|
|
|
|
|
|
systray.SetTitle("HeaderAnalyzer")
|
|
|
|
|
systray.SetTooltip("Email Header Analyzer")
|
|
|
|
|
mOpen := systray.AddMenuItem("Open Web UI", "Open the web interface")
|
|
|
|
|
mQuit := systray.AddMenuItem("Quit", "Quit the app")
|
|
|
|
|
go func() {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-mOpen.ClickedCh:
|
|
|
|
|
url := "http://" + addrPort
|
|
|
|
|
openBrowser(url)
|
|
|
|
|
case <-mQuit.ClickedCh:
|
|
|
|
|
systray.Quit()
|
|
|
|
|
close(shutdownCh)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func openBrowser(url string) {
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
|
case "windows":
|
|
|
|
|
exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
|
|
|
|
|
case "darwin":
|
|
|
|
|
exec.Command("open", url).Start()
|
|
|
|
|
default:
|
|
|
|
|
exec.Command("xdg-open", url).Start()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 16:21:18 +01:00
|
|
|
// isHeadless checks if the system is running in a headless environment
|
|
|
|
|
func isHeadless() bool {
|
|
|
|
|
// Check for common headless indicators
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
// On Windows, assume GUI is available
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if DISPLAY is set (X11)
|
|
|
|
|
if display := os.Getenv("DISPLAY"); display != "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if WAYLAND_DISPLAY is set (Wayland)
|
|
|
|
|
if waylandDisplay := os.Getenv("WAYLAND_DISPLAY"); waylandDisplay != "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if XDG_SESSION_TYPE indicates a graphical session
|
|
|
|
|
if sessionType := os.Getenv("XDG_SESSION_TYPE"); sessionType == "x11" || sessionType == "wayland" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if we're in SSH session
|
|
|
|
|
if os.Getenv("SSH_CONNECTION") != "" || os.Getenv("SSH_CLIENT") != "" || os.Getenv("SSH_TTY") != "" {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if TERM indicates we're in a terminal
|
|
|
|
|
if term := os.Getenv("TERM"); term == "linux" || strings.Contains(term, "tty") {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If none of the above, assume headless on Linux/Unix
|
|
|
|
|
return runtime.GOOS == "linux"
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 15:39:28 +01:00
|
|
|
func main() {
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
// Initialize password generator word list
|
|
|
|
|
passwordgenerator.InitWordList()
|
|
|
|
|
|
2025-07-17 08:33:04 +01:00
|
|
|
// Create handlers with separate template sets
|
2025-07-19 12:58:22 +01:00
|
|
|
landingHandler, err := landingpage.NewHandler(embeddedFiles)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Failed to initialize landing page handler: %v", err)
|
|
|
|
|
}
|
2025-07-17 08:33:04 +01:00
|
|
|
indexHandler := parser.NewHandler(embeddedFiles)
|
|
|
|
|
dnsHandler := resolver.NewHandler(embeddedFiles)
|
|
|
|
|
passwordHandler := passwordgenerator.NewHandler(embeddedFiles)
|
2025-07-16 15:39:28 +01:00
|
|
|
|
2025-07-17 21:52:52 +01:00
|
|
|
// Initialize PWPusher with embedded files
|
|
|
|
|
pwPusher, err := pwpusher.NewHandler(embeddedFiles, "default-encryption-key-change-in-production")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Failed to initialize PWPusher: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 15:39:28 +01:00
|
|
|
// Use embedded static files for web assets
|
|
|
|
|
staticFS, err := fs.Sub(embeddedFiles, "web")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
// Serve static files from embedded FS
|
|
|
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
|
|
|
|
|
|
|
|
|
|
// Serve favicon and tray icon from embedded FS
|
|
|
|
|
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
data, err := fs.ReadFile(staticFS, "favicon.ico")
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "image/x-icon")
|
|
|
|
|
w.Write(data)
|
|
|
|
|
})
|
|
|
|
|
|
2025-07-19 12:58:22 +01:00
|
|
|
// Serve CSS file with correct MIME type
|
|
|
|
|
http.HandleFunc("/style.css", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
data, err := fs.ReadFile(staticFS, "style.css")
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "text/css")
|
|
|
|
|
w.Write(data)
|
|
|
|
|
})
|
|
|
|
|
|
2025-07-22 07:28:30 +01:00
|
|
|
// Serve Tailwind CSS file with correct MIME type
|
|
|
|
|
http.HandleFunc("/style-tailwind.css", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
data, err := fs.ReadFile(staticFS, "style-tailwind.css")
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "text/css")
|
|
|
|
|
w.Write(data)
|
|
|
|
|
})
|
|
|
|
|
|
2025-07-19 12:58:22 +01:00
|
|
|
http.Handle("/", landingHandler)
|
|
|
|
|
|
|
|
|
|
http.Handle("/analyze", indexHandler)
|
2025-07-16 15:39:28 +01:00
|
|
|
|
2025-07-17 08:33:04 +01:00
|
|
|
http.Handle("/dns", dnsHandler)
|
2025-07-16 15:39:28 +01:00
|
|
|
|
2025-07-17 08:33:04 +01:00
|
|
|
http.HandleFunc("/api/dns", resolver.DNSAPIHandler)
|
2025-07-16 15:39:28 +01:00
|
|
|
|
2025-07-19 12:58:22 +01:00
|
|
|
http.Handle("/pwgenerator", passwordHandler)
|
2025-07-16 15:39:28 +01:00
|
|
|
|
2025-07-19 12:58:22 +01:00
|
|
|
http.HandleFunc("/api/pwgenerator", passwordgenerator.PasswordAPIHandler)
|
2025-07-16 15:39:28 +01:00
|
|
|
|
2025-07-19 12:58:22 +01:00
|
|
|
http.HandleFunc("/api/pwgenerator/info", passwordgenerator.PasswordInfoAPIHandler)
|
2025-07-16 15:39:28 +01:00
|
|
|
|
2025-07-17 21:52:52 +01:00
|
|
|
// Register PWPusher routes
|
|
|
|
|
pwPusher.RegisterRoutesWithDefault()
|
|
|
|
|
|
2025-07-16 15:39:28 +01:00
|
|
|
addrPort := fmt.Sprintf("%s:%d", *addr, *port)
|
|
|
|
|
fmt.Printf("Listening on http://%s\n", addrPort)
|
|
|
|
|
|
|
|
|
|
srv := &http.Server{Addr: addrPort}
|
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
|
|
|
|
}()
|
|
|
|
|
|
2025-07-16 16:21:18 +01:00
|
|
|
// Check if we're in a headless environment
|
|
|
|
|
if *headless || isHeadless() {
|
|
|
|
|
if *headless {
|
|
|
|
|
fmt.Println("Headless mode forced via command line flag.")
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("Headless environment detected. System tray disabled.")
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("Access the web interface at: http://%s\n", addrPort)
|
|
|
|
|
fmt.Println("Press Ctrl+C to stop the server.")
|
|
|
|
|
|
|
|
|
|
// Set up signal handling for graceful shutdown
|
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
|
|
// Wait for interrupt signal
|
|
|
|
|
<-sigChan
|
|
|
|
|
fmt.Println("\nShutting down server...")
|
2025-07-16 15:39:28 +01:00
|
|
|
|
2025-07-16 16:21:18 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
srv.Shutdown(ctx)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("GUI environment detected. Starting system tray...")
|
|
|
|
|
systray.Run(func() { onReady(addrPort, shutdownCh) }, func() {})
|
|
|
|
|
|
|
|
|
|
<-shutdownCh
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
srv.Shutdown(ctx)
|
|
|
|
|
}
|
2025-07-16 15:39:28 +01:00
|
|
|
}
|