You've already forked remote-local-storage
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| abc8c77c8e | |||
| 1b315c53b0 | |||
| ea930dceac | |||
| 769dffb4b5 |
17
README.md
17
README.md
@@ -36,12 +36,21 @@ Below is my [Caddy][6] file config for phanpy.
|
|||||||
|
|
||||||
```Caddyfile
|
```Caddyfile
|
||||||
http://phanpy.{$PRIVATE_ONION} {
|
http://phanpy.{$PRIVATE_ONION} {
|
||||||
|
@firstreq {
|
||||||
|
path /
|
||||||
|
header !Referer
|
||||||
|
}
|
||||||
|
|
||||||
|
redir @firstreq /wrap/index.html
|
||||||
|
|
||||||
header {
|
header {
|
||||||
Content-Security-Policy "default-src 'self' 'unsafe-inline' social.balki.me wss://social.balki.me data: ;"
|
Content-Security-Policy "default-src 'self' 'unsafe-inline' social.balki.me wss://social.balki.me data: ;"
|
||||||
Referrer-Policy same-origin
|
Referrer-Policy same-origin
|
||||||
}
|
}
|
||||||
reverse_proxy /wrap/* localhost:3242
|
|
||||||
root * /var/www/phanpy/
|
reverse_proxy /wrap/* unix//run/rls.sock
|
||||||
|
|
||||||
|
root * /usr/share/webapps/phanpy/
|
||||||
file_server
|
file_server
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -61,6 +70,10 @@ Let me know at `@balki@balki.me` in fediverse, if you find it useful
|
|||||||
```bash
|
```bash
|
||||||
go install go.balki.me/remote-local-storage@latest
|
go install go.balki.me/remote-local-storage@latest
|
||||||
```
|
```
|
||||||
|
### AUR
|
||||||
|
|
||||||
|
- https://aur.archlinux.org/packages/remote-local-storage-git
|
||||||
|
- (PKGBUILD) https://gitea.balki.me/balki-aur/remote-local-storage
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -1,5 +1,5 @@
|
|||||||
module go.balki.me/remote-local-storage
|
module go.balki.me/remote-local-storage
|
||||||
|
|
||||||
go 1.23.4
|
go 1.25.6
|
||||||
|
|
||||||
require go.balki.me/anyhttp v0.4.0
|
require go.balki.me/anyhttp v0.5.2
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -1,2 +1,2 @@
|
|||||||
go.balki.me/anyhttp v0.4.0 h1:K639Mc8qCIO5B6ugLJCLQenkwxh9rihEK5JZ/xNfvV0=
|
go.balki.me/anyhttp v0.5.2 h1:et4tCDXLeXpWfMNvRKG7ojfrnlr3du7cEaG966MLSpA=
|
||||||
go.balki.me/anyhttp v0.4.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s=
|
go.balki.me/anyhttp v0.5.2/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s=
|
||||||
|
|||||||
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)
|
||||||
|
|||||||
119
test.sh
Executable file
119
test.sh
Executable file
@@ -0,0 +1,119 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Test script for remote-local-storage application
|
||||||
|
# This script starts the server and tests both saveLS and loadLS endpoints
|
||||||
|
# with curl's --resolve flag to test multiple hostnames
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Define test variables
|
||||||
|
PORT="3243"
|
||||||
|
|
||||||
|
# Flags
|
||||||
|
KEEP_FILES=false
|
||||||
|
while getopts "k" FLAG; do
|
||||||
|
case "${FLAG}" in
|
||||||
|
k) KEEP_FILES=true ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# Create temporary directory for testing
|
||||||
|
TEST_DIR=$(mktemp -d)
|
||||||
|
echo "Using test directory: $TEST_DIR"
|
||||||
|
echo "To keep files, run with: ./test.sh -k"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Cleanup function
|
||||||
|
cleanup() {
|
||||||
|
echo "Cleaning up..."
|
||||||
|
kill $(jobs -p) 2>/dev/null || true
|
||||||
|
if [ "$KEEP_FILES" = false ]; then
|
||||||
|
rm -rf "$TEST_DIR"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Set trap to always run cleanup on exit
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
# Save binary path and cd into temp directory
|
||||||
|
TEST_BINARY="$PWD/rls"
|
||||||
|
cd "$TEST_DIR"
|
||||||
|
echo "Starting remote-local-storage server on localhost:$PORT..."
|
||||||
|
"$TEST_BINARY" -address="localhost:$PORT" &
|
||||||
|
SERVER_PID=$!
|
||||||
|
|
||||||
|
# Give the server a moment to start
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
# Define dump files
|
||||||
|
DUMP_FILE="localhost:$PORT-dump.json"
|
||||||
|
EXAMPLE_DUMP_FILE="example.com:$PORT-dump.json"
|
||||||
|
BASE_URL="http://localhost:$PORT"
|
||||||
|
|
||||||
|
echo "Testing loadLS endpoint for localhost (should return empty JSON)..."
|
||||||
|
curl -s "$BASE_URL/wrap/loadLS" | jq .
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Testing saveLS endpoint with sample data for localhost..."
|
||||||
|
SAMPLE_DATA='{"testKey":"testValue","anotherKey":"anotherValue"}'
|
||||||
|
curl -s -X POST -H "Content-Type: application/json" \
|
||||||
|
--data "$SAMPLE_DATA" "$BASE_URL/wrap/saveLS"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Testing loadLS endpoint after saving (should return saved data)..."
|
||||||
|
curl -s "$BASE_URL/wrap/loadLS" | jq .
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Checking localhost dump file contents..."
|
||||||
|
if [ -f "$DUMP_FILE" ]; then
|
||||||
|
echo "Dump file exists at: $DUMP_FILE"
|
||||||
|
jq . "$DUMP_FILE"
|
||||||
|
else
|
||||||
|
echo "Dump file not found!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Testing with --resolve flag for different hostname (example.com)..."
|
||||||
|
echo "Testing loadLS endpoint for example.com..."
|
||||||
|
curl -s --resolve "example.com:$PORT:127.0.0.1" \
|
||||||
|
"http://example.com:$PORT/wrap/loadLS" | jq .
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Saving data for example.com..."
|
||||||
|
EXAMPLE_DATA='{"exampleKey":"exampleValue","exampleNum":123}'
|
||||||
|
curl -s -X POST --resolve "example.com:$PORT:127.0.0.1" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
--data "$EXAMPLE_DATA" "http://example.com:$PORT/wrap/saveLS"
|
||||||
|
|
||||||
|
# Wait a moment for server processing
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Loading data for example.com..."
|
||||||
|
curl -s --resolve "example.com:$PORT:127.0.0.1" \
|
||||||
|
"http://example.com:$PORT/wrap/loadLS" | jq .
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Checking example.com dump file contents..."
|
||||||
|
if [ -f "$EXAMPLE_DUMP_FILE" ]; then
|
||||||
|
echo "Dump file exists at: $EXAMPLE_DUMP_FILE"
|
||||||
|
jq . "$EXAMPLE_DUMP_FILE"
|
||||||
|
echo ""
|
||||||
|
echo "Verification:"
|
||||||
|
echo " ✓ localhost dump file: $DUMP_FILE"
|
||||||
|
echo " ✓ example.com dump file: $EXAMPLE_DUMP_FILE"
|
||||||
|
echo " ✓ Two different dump files generated for two different hosts"
|
||||||
|
echo ""
|
||||||
|
if [ "$KEEP_FILES" = false ]; then
|
||||||
|
echo "Files were created in $TEST_DIR"
|
||||||
|
echo "Run: ls -la $TEST_DIR"
|
||||||
|
else
|
||||||
|
echo "Files kept in $TEST_DIR for inspection"
|
||||||
|
fi
|
||||||
|
echo ""
|
||||||
|
echo "Test completed successfully!"
|
||||||
|
else
|
||||||
|
echo "Dump file not found!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
@@ -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