Spaces:
Sleeping
Sleeping
fix sed pattern for JSON with spaces after colon, use -T for timeout, --post-file for body
Browse files122345767810111213910111213141520212223242526272829161718192021222324252627383940412829303132333446473536373839515240414243564458454647616248495051#!/bin/sh
SERVER="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"UCLIENT="uclient-fetch -q -O - -T 10"
mkdir -p "$TMPDIR"
log() { logger -t wstunnel "$1"}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 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 TERM
log "starting wstunnel client"
while true; do resp=$(fetch_poll) fetch_rc=$?
if [ "$fetch_rc" != "0" ] || [ -z "$resp" ]; then [ "$fetch_rc" != "0" ] && log "poll failed (rc=$fetch_rc)" rc=$? if [ "$rc" != "0" ] || [ -z "$resp" ] || [ "$resp" = "{}" ]; then [ "$rc" != "0" ] && log "poll rc=$rc" sleep "$POLL_INTERVAL" continue fi
rid=$(echo "$resp" | sed -n 's/.*"rid":"\([^"]*\)".*/\1/p') url_enc=$(echo "$resp" | sed -n 's/.*"url_encoded":"\([^"]*\)".*/\1/p') rid=$(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" ]; then local_url="${LOCAL_CGI}?url=${url_enc}" log "request $rid: $local_url"
log "request $rid" body_file="$TMPDIR/body-$rid" $UCLIENT "$local_url" > "$body_file" 2>&1 u_rc=$?
if [ "$u_rc" = "0" ] && [ -s "$body_file" ]; then log "response $rid: success ($(wc -c < "$body_file") bytes)" log "success" respond "$rid" "200" "text/html" "$body_file" & else fsize=$(wc -c < "$body_file" 2>/dev/null || echo 0) log "fetch failed (rc=$u_rc, size=$fsize)" log "local failed (rc=$u_rc)" echo "fetch failed rc=$u_rc" > "$body_file" respond "$rid" "502" "text/plain" "$body_file" & fitunnel_client.sh+14-28