You've already forked remote-local-storage
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| abc8c77c8e | |||
| 1b315c53b0 | |||
| ea930dceac | |||
| 769dffb4b5 | |||
| 8d7834b38f | |||
| 2bf8c9926d |
27
README.md
27
README.md
@@ -36,13 +36,22 @@ Below is my [Caddy][6] file config for phanpy.
|
||||
|
||||
```Caddyfile
|
||||
http://phanpy.{$PRIVATE_ONION} {
|
||||
header {
|
||||
Content-Security-Policy "default-src 'self' 'unsafe-inline' social.balki.me wss://social.balki.me data: ;"
|
||||
Referrer-Policy same-origin
|
||||
}
|
||||
reverse_proxy /wrap/* localhost:3242
|
||||
root * /var/www/phanpy/
|
||||
file_server
|
||||
@firstreq {
|
||||
path /
|
||||
header !Referer
|
||||
}
|
||||
|
||||
redir @firstreq /wrap/index.html
|
||||
|
||||
header {
|
||||
Content-Security-Policy "default-src 'self' 'unsafe-inline' social.balki.me wss://social.balki.me data: ;"
|
||||
Referrer-Policy same-origin
|
||||
}
|
||||
|
||||
reverse_proxy /wrap/* unix//run/rls.sock
|
||||
|
||||
root * /usr/share/webapps/phanpy/
|
||||
file_server
|
||||
}
|
||||
```
|
||||
|
||||
@@ -61,6 +70,10 @@ Let me know at `@balki@balki.me` in fediverse, if you find it useful
|
||||
```bash
|
||||
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
|
||||
|
||||
go 1.23.0
|
||||
go 1.25.6
|
||||
|
||||
require go.balki.me/anyhttp v0.3.0
|
||||
require go.balki.me/anyhttp v0.5.2
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,2 +1,2 @@
|
||||
go.balki.me/anyhttp v0.3.0 h1:WtBQ0rnkg567sX/O4ij/+qBbdCIUt5VURSe718sITBY=
|
||||
go.balki.me/anyhttp v0.3.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s=
|
||||
go.balki.me/anyhttp v0.5.2 h1:et4tCDXLeXpWfMNvRKG7ojfrnlr3du7cEaG966MLSpA=
|
||||
go.balki.me/anyhttp v0.5.2/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s=
|
||||
|
||||
54
main.go
54
main.go
@@ -1,4 +1,6 @@
|
||||
// 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
|
||||
|
||||
import (
|
||||
@@ -9,6 +11,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.balki.me/anyhttp"
|
||||
@@ -17,55 +20,84 @@ import (
|
||||
//go:embed wrap.html
|
||||
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")
|
||||
dumpFile := flag.String("dumpfile", "dump.json", "Path where local storage is saved in server")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
// Handle root path - show error message
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Write([]byte("<h2>Error!</h2><p>Check your webserver config, You should not see this!</p>"))
|
||||
if _, err := w.Write([]byte("<h2>Error!</h2><p>Check your webserver config, You should not see this!</p>")); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
// Serve the wrap.html page at /wrap/ path
|
||||
http.HandleFunc("/wrap/", func(w http.ResponseWriter, r *http.Request) {
|
||||
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) {
|
||||
|
||||
// Read the request body containing localStorage data
|
||||
data, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(*dumpFile, data, 0o400); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
// Generate dump filename
|
||||
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.Println("Saved!")
|
||||
|
||||
// Send OK response
|
||||
if _, err := w.Write([]byte("OK")); err != nil {
|
||||
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")
|
||||
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("{}")
|
||||
}
|
||||
|
||||
// Send the data back to client
|
||||
if _, err := w.Write(data); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Println("Sent!")
|
||||
})
|
||||
|
||||
// Start the HTTP server
|
||||
err := anyhttp.ListenAndServe(*addr, nil)
|
||||
if err != nil {
|
||||
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>
|
||||
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
|
||||
<script>
|
||||
|
||||
// Load local storage from server on page load
|
||||
(async () => {
|
||||
const resp = await fetch("loadLS");
|
||||
const lsItems = JSON.parse(await resp.text());
|
||||
@@ -13,6 +13,8 @@
|
||||
console.log("Loaded local storage");
|
||||
})();
|
||||
|
||||
// Function to save local storage to server
|
||||
// This function is called periodically to sync localStorage with server
|
||||
function postLocalStorage() {
|
||||
(async () => {
|
||||
const resp = await fetch("saveLS", {
|
||||
@@ -26,6 +28,7 @@
|
||||
})();
|
||||
}
|
||||
|
||||
// Set up periodic saving every 10 minutes
|
||||
setInterval(postLocalStorage, 10 * 60 * 1000) // 10 mins
|
||||
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user