#!/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 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 peer agrees to SHARE with you: accepts an invite created in the app # sh tools/peer.sh send [lat] [lon] push one fake ping to a viewer (default: Montreal) # sh tools/peer.sh get 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 (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 " >&2; exit 1; } . "$STATE" } auth_post() { # auth_post 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 " >&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 )" ;; accept) [ -n "$2" ] || { echo "usage: peer.sh accept " >&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 [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 " >&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