Files

106 lines
2.7 KiB
Go
Raw Permalink Normal View History

2024-08-30 18:16:40 -04:00
// Save and serve HTML local storage in server
2026-01-18 11:40:09 -05:00
// This application provides a wrapper to save and restore localStorage of webapps in a server.
// It's designed for browsers that clear localStorage when exiting (like Tor Browser) or to share localStorage across multiple browsers.
2024-08-30 18:16:40 -04:00
package main
import (
"bytes"
_ "embed"
"flag"
"io"
"log"
"net/http"
"os"
2026-01-18 11:40:09 -05:00
"strings"
2024-08-30 18:16:40 -04:00
"time"
"go.balki.me/anyhttp"
)
//go:embed wrap.html
var html []byte
2026-01-18 11:40:09 -05:00
// getDumpFileName generates a hostname-specific dump filename
func getDumpFileName(host string) string {
// Extract hostname from Host header
hostname := host
if hostname == "" {
hostname = "localhost"
}
// Sanitize hostname to prevent path traversal
hostname = strings.ReplaceAll(hostname, "/", "_")
hostname = strings.ReplaceAll(hostname, "..", "_")
2024-08-30 18:16:40 -04:00
2026-01-18 11:40:09 -05:00
// Create dump file name with hostname
return hostname + "-dump.json"
}
func main() {
2024-09-01 23:33:47 -04:00
addr := flag.String("address", "localhost:3242", "Listen address. See go.balki.me/anyhttp for usage")
flag.Parse()
2024-08-30 18:16:40 -04:00
2026-01-18 11:40:09 -05:00
// Handle root path - show error message
2024-08-30 18:16:40 -04:00
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
2024-12-19 15:56:42 -05:00
if _, err := w.Write([]byte("<h2>Error!</h2><p>Check your webserver config, You should not see this!</p>")); err != nil {
log.Panic(err)
}
2024-08-30 18:16:40 -04:00
})
2026-01-18 11:40:09 -05:00
// Serve the wrap.html page at /wrap/ path
2024-08-30 18:16:40 -04:00
http.HandleFunc("/wrap/", func(w http.ResponseWriter, r *http.Request) {
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(html))
})
2026-01-18 11:40:09 -05:00
// Handle saving local storage data to server
2024-08-30 18:16:40 -04:00
http.HandleFunc("/wrap/saveLS", func(w http.ResponseWriter, r *http.Request) {
2026-01-18 11:40:09 -05:00
// Read the request body containing localStorage data
2024-08-30 18:16:40 -04:00
data, err := io.ReadAll(r.Body)
if err != nil {
log.Panic(err)
}
2026-01-18 11:40:09 -05:00
// Generate dump filename
dumpFile := getDumpFileName(r.Host)
2024-08-30 18:16:40 -04:00
2026-01-18 11:40:09 -05:00
// Write the data to the dump file
if err := os.WriteFile(dumpFile, data, 0o600); err != nil {
2024-08-30 18:16:40 -04:00
log.Panic(err)
}
log.Println("Saved!")
2024-09-01 23:33:47 -04:00
2026-01-18 11:40:09 -05:00
// Send OK response
2024-09-01 23:33:47 -04:00
if _, err := w.Write([]byte("OK")); err != nil {
log.Panic(err)
}
2024-08-30 18:16:40 -04:00
})
2026-01-18 11:40:09 -05:00
// Handle loading local storage data from server
http.HandleFunc("/wrap/loadLS", func(w http.ResponseWriter, r *http.Request) {
// Set proper content type for JSON response
2024-08-30 18:16:40 -04:00
w.Header().Set("Content-Type", "application/json")
2026-01-18 11:40:09 -05:00
// Generate dump filename
dumpFile := getDumpFileName(r.Host)
// Read the dump file
data, err := os.ReadFile(dumpFile)
if err != nil { // First time - return empty JSON
2024-08-30 18:16:40 -04:00
data = []byte("{}")
}
2026-01-18 11:40:09 -05:00
// Send the data back to client
2024-08-30 18:16:40 -04:00
if _, err := w.Write(data); err != nil {
log.Panic(err)
}
log.Println("Sent!")
})
2024-09-01 23:33:47 -04:00
2026-01-18 11:40:09 -05:00
// Start the HTTP server
2024-08-30 18:16:40 -04:00
err := anyhttp.ListenAndServe(*addr, nil)
if err != nil {
log.Panic(err)
}
}