Spaces:
Sleeping
Sleeping
fix sed pattern for JSON with spaces after colon, use -T for timeout, --post-file for body
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#!/bin/shSERVER="https://mtsky-free-server-docker.hf.space"LOCAL_CGI="${LOCAL_CGI:-http://192.168.1.1/cgi-bin/api}"TMPDIR="/tmp/wstunnel"POLL_INTERVAL="${POLL_INTERVAL:-1}"UCLIENT="uclient-fetch -q -O - -s 10 --no-check-certificate"mkdir -p "$TMPDIR"log() {logger -t wstunnel "$1"}fetch_poll() {$UCLIENT "$SERVER/poll" 2>&1}respond() {local rid="$1" status="$2" content_type="$3" file="$4"uclient-fetch -q -O /dev/null -s 10 --no-check-certificate \-f "$file" -T "application/octet-stream" \"$SERVER/respond?rid=$rid&status=$status&content_type=$content_type" 2>&1}cleanup() {rm -f "$TMPDIR"/*log "stopped"exit 0}trap cleanup INT TERMlog "starting wstunnel client"while true; doresp=$(fetch_poll)fetch_rc=$?if [ "$fetch_rc" != "0" ] || [ -z "$resp" ]; then[ "$fetch_rc" != "0" ] && log "poll failed (rc=$fetch_rc)"sleep "$POLL_INTERVAL"continuefirid=$(echo "$resp" | sed -n 's/.*"rid":"\([^"]*\)".*/\1/p')url_enc=$(echo "$resp" | sed -n 's/.*"url_encoded":"\([^"]*\)".*/\1/p')if [ -n "$rid" ] && [ -n "$url_enc" ]; thenlocal_url="${LOCAL_CGI}?url=${url_enc}"log "request $rid: $local_url"body_file="$TMPDIR/body-$rid"$UCLIENT "$local_url" > "$body_file" 2>&1u_rc=$?if [ "$u_rc" = "0" ] && [ -s "$body_file" ]; thenlog "response $rid: success ($(wc -c < "$body_file") bytes)"respond "$rid" "200" "text/html" "$body_file" &elsefsize=$(wc -c < "$body_file" 2>/dev/null || echo 0)log "fetch failed (rc=$u_rc, size=$fsize)"echo "fetch failed rc=$u_rc" > "$body_file"respond "$rid" "502" "text/plain" "$body_file" &fi123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051#!/bin/shSERVER="https://mtsky-free-server-docker.hf.space"LOCAL_CGI="${LOCAL_CGI:-http://192.168.1.1/cgi-bin/api}"TMPDIR="/tmp/wstunnel"POLL_INTERVAL="${POLL_INTERVAL:-1}"UCLIENT="uclient-fetch -q -O - -T 10"mkdir -p "$TMPDIR"log() { logger -t wstunnel "$1"; }fetch_poll() {$UCLIENT "$SERVER/poll" 2>&1}respond() {local rid="$1" status="$2" ct="$3" file="$4"uclient-fetch -q -O /dev/null -T 10 --post-file="$file" \"$SERVER/respond?rid=$rid&status=$status&content_type=$ct" 2>&1}cleanup() { rm -f "$TMPDIR"/*; log "stopped"; exit 0; }trap cleanup INT TERMlog "starting wstunnel client"while true; doresp=$(fetch_poll)rc=$?if [ "$rc" != "0" ] || [ -z "$resp" ] || [ "$resp" = "{}" ]; then[ "$rc" != "0" ] && log "poll rc=$rc"sleep "$POLL_INTERVAL"continuefirid=$(echo "$resp" | sed -n 's/.*"rid":[ ]*"\([^"]*\)".*/\1/p')url_enc=$(echo "$resp" | sed -n 's/.*"url_encoded":[ ]*"\([^"]*\)".*/\1/p')if [ -n "$rid" ] && [ -n "$url_enc" ]; thenlocal_url="${LOCAL_CGI}?url=${url_enc}"log "request $rid"body_file="$TMPDIR/body-$rid"$UCLIENT "$local_url" > "$body_file" 2>&1u_rc=$?if [ "$u_rc" = "0" ] && [ -s "$body_file" ]; thenlog "success"respond "$rid" "200" "text/html" "$body_file" &elselog "local failed (rc=$u_rc)"echo "fetch failed rc=$u_rc" > "$body_file"respond "$rid" "502" "text/plain" "$body_file" &fi
tunnel_client.sh+14-28