package main
import (
"context"
_ "embed"
"encoding/base64"
"encoding/json"
"crypto/rand"
"fmt"
"html"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
//go:embed assets/qrcode.min.js
var qrcodeJS []byte
type activeShare struct {
server *http.Server
listener net.Listener
port int
token string
fileBuf []byte
filename string
}
var (
activeMu sync.Mutex
active *activeShare
)
func stopActiveShare() {
activeMu.Lock()
defer activeMu.Unlock()
if active != nil {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_ = active.server.Shutdown(ctx)
active = nil
}
}
func MobileQrScript(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
_, _ = w.Write(qrcodeJS)
}
func MobilePage(w http.ResponseWriter, r *http.Request) {
body := `
Send To Mobile
Pick an HTML file, then start sharing. Scan the QR with a phone on the same WiFi.
The phone gets a choice of View or Download.
Multiple network interfaces found. Pick the one your phone can reach:
First run on Windows may show a firewall prompt — allow access for "Private networks".
`
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte(Page("/mobile", "Send To Mobile", body)))
}
func MobilePick(w http.ResponseWriter, r *http.Request) {
picked := PickFiles(
"Select HTML",
[]FileFilter{{Name: "HTML", Extensions: []string{"html", "htm"}}},
false, "",
)
if len(picked) == 0 {
writeJSON(w, map[string]any{"path": nil})
return
}
writeJSON(w, map[string]any{"path": picked[0], "name": filepath.Base(picked[0])})
}
func MobileStart(w http.ResponseWriter, r *http.Request) {
stopActiveShare()
var req struct {
Path string `json:"path"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, map[string]any{"error": err.Error()})
return
}
if req.Path == "" || !fileExists(req.Path) {
writeJSON(w, map[string]any{"error": "File missing."})
return
}
fileBuf, err := os.ReadFile(req.Path)
if err != nil {
writeJSON(w, map[string]any{"error": "Read failed: " + err.Error()})
return
}
filename := filepath.Base(req.Path)
// 6 random bytes -> base64url, no padding
tokenBytes := make([]byte, 6)
if _, err := rand.Read(tokenBytes); err != nil {
writeJSON(w, map[string]any{"error": "Token gen failed: " + err.Error()})
return
}
token := strings.TrimRight(base64.URLEncoding.EncodeToString(tokenBytes), "=")
cfgPort := LoadConfig().SendToMobile.Port
listener, err := net.Listen("tcp", "0.0.0.0:"+strconv.Itoa(cfgPort))
if err != nil {
writeJSON(w, map[string]any{"error": "Failed to bind: " + err.Error()})
return
}
port := listener.Addr().(*net.TCPAddr).Port
share := &activeShare{
listener: listener,
port: port,
token: token,
fileBuf: fileBuf,
filename: filename,
}
share.server = &http.Server{Handler: shareHandler(share)}
go func() { _ = share.server.Serve(listener) }()
ips := getLanIPs()
if len(ips) == 0 {
_ = share.server.Close()
writeJSON(w, map[string]any{"error": "No non-loopback IPv4 interface found. Are you on a network?"})
return
}
activeMu.Lock()
active = share
activeMu.Unlock()
type urlInfo struct {
IP string `json:"ip"`
Iface string `json:"iface"`
URL string `json:"url"`
}
urls := make([]urlInfo, 0, len(ips))
for _, ip := range ips {
urls = append(urls, urlInfo{
IP: ip.address,
Iface: ip.iface,
URL: fmt.Sprintf("http://%s:%d/s/%s/", ip.address, port, token),
})
}
writeJSON(w, map[string]any{"urls": urls, "filename": filename})
}
func MobileStop(w http.ResponseWriter, r *http.Request) {
stopActiveShare()
writeJSON(w, map[string]any{"ok": true})
}
func shareHandler(s *activeShare) http.Handler {
prefix := "/s/" + s.token
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || !strings.HasPrefix(r.URL.Path, prefix) {
http.NotFound(w, r)
return
}
rest := strings.TrimPrefix(r.URL.Path, prefix)
switch rest {
case "", "/":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
_, _ = w.Write([]byte(chooserPage(s.filename)))
case "/view":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
_, _ = w.Write(s.fileBuf)
case "/file":
safeName := safeFilename(s.filename)
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", `attachment; filename="`+safeName+`"`)
w.Header().Set("Content-Length", strconv.Itoa(len(s.fileBuf)))
w.Header().Set("Cache-Control", "no-store")
_, _ = w.Write(s.fileBuf)
default:
http.NotFound(w, r)
}
})
}
var unsafeFilenameRx = regexp.MustCompile(`[^A-Za-z0-9._-]`)
func safeFilename(s string) string { return unsafeFilenameRx.ReplaceAllString(s, "_") }
func chooserPage(filename string) string {
escName := html.EscapeString(filename)
return `
Send To Mobile
HPL Toolbox
` + escName + `
View in browser
Download .html
`
}
type lanIP struct {
address string
iface string
}
func getLanIPs() []lanIP {
var out []lanIP
ifaces, err := net.Interfaces()
if err != nil {
return out
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
continue
}
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, a := range addrs {
ipNet, ok := a.(*net.IPNet)
if !ok {
continue
}
ip4 := ipNet.IP.To4()
if ip4 == nil || ip4.IsLoopback() {
continue
}
out = append(out, lanIP{address: ip4.String(), iface: iface.Name})
}
}
return out
}