You've already forked remote-local-storage
support multiple hosts - by qwencoder
This commit is contained in:
50
main.go
50
main.go
@@ -1,4 +1,6 @@
|
|||||||
// Save and serve HTML local storage in server
|
// Save and serve HTML local storage in server
|
||||||
|
// 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.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -9,6 +11,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.balki.me/anyhttp"
|
"go.balki.me/anyhttp"
|
||||||
@@ -17,57 +20,84 @@ import (
|
|||||||
//go:embed wrap.html
|
//go:embed wrap.html
|
||||||
var html []byte
|
var html []byte
|
||||||
|
|
||||||
func main() {
|
// 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, "..", "_")
|
||||||
|
|
||||||
|
// Create dump file name with hostname
|
||||||
|
return hostname + "-dump.json"
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
addr := flag.String("address", "localhost:3242", "Listen address. See go.balki.me/anyhttp for usage")
|
addr := flag.String("address", "localhost:3242", "Listen address. See go.balki.me/anyhttp for usage")
|
||||||
dumpFile := flag.String("dumpfile", "dump.json", "Path where local storage is saved in server")
|
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// Handle root path - show error message
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
if _, err := w.Write([]byte("<h2>Error!</h2><p>Check your webserver config, You should not see this!</p>")); err != nil {
|
if _, err := w.Write([]byte("<h2>Error!</h2><p>Check your webserver config, You should not see this!</p>")); err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Serve the wrap.html page at /wrap/ path
|
||||||
http.HandleFunc("/wrap/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/wrap/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(html))
|
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(html))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Handle saving local storage data to server
|
||||||
http.HandleFunc("/wrap/saveLS", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/wrap/saveLS", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Read the request body containing localStorage data
|
||||||
data, err := io.ReadAll(r.Body)
|
data, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.WriteFile(*dumpFile, data, 0o600); err != nil {
|
// Generate dump filename
|
||||||
log.Panic(err)
|
dumpFile := getDumpFileName(r.Host)
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
// Write the data to the dump file
|
||||||
|
if err := os.WriteFile(dumpFile, data, 0o600); err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Saved!")
|
log.Println("Saved!")
|
||||||
|
|
||||||
|
// Send OK response
|
||||||
if _, err := w.Write([]byte("OK")); err != nil {
|
if _, err := w.Write([]byte("OK")); err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/wrap/loadLS", func(w http.ResponseWriter, _ *http.Request) {
|
// 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
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
data, err := os.ReadFile(*dumpFile)
|
|
||||||
if err != nil { // First time
|
// Generate dump filename
|
||||||
|
dumpFile := getDumpFileName(r.Host)
|
||||||
|
|
||||||
|
// Read the dump file
|
||||||
|
data, err := os.ReadFile(dumpFile)
|
||||||
|
if err != nil { // First time - return empty JSON
|
||||||
data = []byte("{}")
|
data = []byte("{}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send the data back to client
|
||||||
if _, err := w.Write(data); err != nil {
|
if _, err := w.Write(data); err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
log.Println("Sent!")
|
log.Println("Sent!")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Start the HTTP server
|
||||||
err := anyhttp.ListenAndServe(*addr, nil)
|
err := anyhttp.ListenAndServe(*addr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
|
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
|
||||||
<script>
|
<script>
|
||||||
|
// Load local storage from server on page load
|
||||||
(async () => {
|
(async () => {
|
||||||
const resp = await fetch("loadLS");
|
const resp = await fetch("loadLS");
|
||||||
const lsItems = JSON.parse(await resp.text());
|
const lsItems = JSON.parse(await resp.text());
|
||||||
@@ -13,6 +13,8 @@
|
|||||||
console.log("Loaded local storage");
|
console.log("Loaded local storage");
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
// Function to save local storage to server
|
||||||
|
// This function is called periodically to sync localStorage with server
|
||||||
function postLocalStorage() {
|
function postLocalStorage() {
|
||||||
(async () => {
|
(async () => {
|
||||||
const resp = await fetch("saveLS", {
|
const resp = await fetch("saveLS", {
|
||||||
@@ -26,6 +28,7 @@
|
|||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set up periodic saving every 10 minutes
|
||||||
setInterval(postLocalStorage, 10 * 60 * 1000) // 10 mins
|
setInterval(postLocalStorage, 10 * 60 * 1000) // 10 mins
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user