Something went wrong. Try again.
Self-hosted, federated location sharing app and server that prioritizes user privacy and security
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102#!/bin/sh# A fake second user driven by curl, for testing the app locally without a# second device. State (user id + token) is kept in /tmp/privacypin_peer.env.## Usage:# sh tools/peer.sh create <signup_key> create the peer's account# sh tools/peer.sh id print the peer's user id# sh tools/peer.sh key generate a signup key (peer must be admin)# sh tools/peer.sh invite peer wants to SEE you: prints an invite token to paste in the app# sh tools/peer.sh accept <invite_token> peer agrees to SHARE with you: accepts an invite created in the app# sh tools/peer.sh send <receiver_id> [lat] [lon] push one fake ping to a viewer (default: Montreal)# sh tools/peer.sh get <sender_id> pull pings someone is sending to the peer## Server defaults to http://127.0.0.1:3000 -- override with PP_SERVER.set -e
BASE="${PP_SERVER:-http://127.0.0.1:3000}/v0"STATE="/tmp/privacypin_peer.env"
json_field() { # json_field <field_name> (flat string fields only) sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p"}
load_state() { [ -f "$STATE" ] || { echo "No peer account yet. Run: sh tools/peer.sh create <signup_key>" >&2; exit 1; } . "$STATE"}
auth_post() { # auth_post <path> <body> curl -sS -X POST "$BASE$1" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $PEER_TOKEN" \ -H "X-User-Id: $PEER_ID" \ -d "$2"}
cmd="$1"case "$cmd" in create) [ -n "$2" ] || { echo "usage: peer.sh create <signup_key>" >&2; exit 1; } res=$(curl -sS -X POST "$BASE/create-account" \ -H "Content-Type: application/json" \ -d "{\"signup_key\":\"$2\"}") id=$(echo "$res" | json_field user_id) token=$(echo "$res" | json_field token) [ -n "$id" ] || { echo "create-account failed: $res" >&2; exit 1; } printf 'PEER_ID=%s\nPEER_TOKEN=%s\n' "$id" "$token" > "$STATE" echo "Peer account created. user_id: $id" ;; id) load_state echo "$PEER_ID" ;; key) load_state res=$(auth_post /generate-signup-key '{}') key=$(echo "$res" | json_field signup_key) [ -n "$key" ] || { echo "generate-signup-key failed: $res" >&2; exit 1; } echo "$key" ;; invite) load_state res=$(auth_post /create-invite '{}') token=$(echo "$res" | json_field invite_token) [ -n "$token" ] || { echo "create-invite failed: $res" >&2; exit 1; } echo "Paste this token in the app under \"Sharing with\" -> Accept invite:" echo "$token" echo "(then the app user's location will flow to the peer; check with: peer.sh get <app_user_id>)" ;; accept) [ -n "$2" ] || { echo "usage: peer.sh accept <invite_token>" >&2; exit 1; } load_state res=$(auth_post /accept-invite "{\"invite_token\":\"$2\"}") viewer=$(echo "$res" | json_field viewer_id) [ -n "$viewer" ] || { echo "accept-invite failed: $res" >&2; exit 1; } echo "Peer now shares its location with $viewer." echo "In the app, add the peer under \"Tracking\" with this user id: $PEER_ID" echo "Then push fake positions with: peer.sh send $viewer [lat] [lon]" ;; send) [ -n "$2" ] || { echo "usage: peer.sh send <receiver_id> [lat] [lon]" >&2; exit 1; } load_state lat="${3:-45.5017}" lon="${4:--73.5673}" ts=$(($(date +%s) * 1000)) ping="{\"lat\":$lat,\"lon\":$lon,\"acc\":5.0,\"ts\":$ts}" escaped=$(echo "$ping" | sed 's/"/\\"/g') res=$(auth_post /send-pings "[{\"receiver_id\":\"$2\",\"encrypted_ping\":\"$escaped\"}]") [ -z "$res" ] && echo "Ping sent ($lat, $lon)." || echo "send-pings response: $res" ;; get) [ -n "$2" ] || { echo "usage: peer.sh get <sender_id>" >&2; exit 1; } load_state auth_post /get-pings "{\"sender_id\":\"$2\"}" echo "" ;; *) sed -n '2,14p' "$0" | sed 's/^# \{0,1\}//' exit 1 ;;esac