0

I'm looking for a little script using nc which will be used to add CORS headers (at least Access-Control-Allow-Origin: *) in order for my local ionic dev application to access a remote webservice.

Anthony O.
  • 674
  • 1
  • 5
  • 14

1 Answers1

1

Here is a little script inspired by this blog which I called cors-http-proxy.sh:

#!/bin/sh -e

if [ $# != 3 ]
then
    echo "usage: $0 <src-port> <dst-host> <dst-port>"
    exit 0
fi

while true; do
    TMP=`mktemp -d`
    BACK=$TMP/pipe.back
    SENT=$TMP/pipe.sent
    RCVD=$TMP/pipe.rcvd
    trap 'rm -rf "$TMP"' EXIT
    mkfifo -m 0600 "$BACK" "$SENT" "$RCVD"
    sed 's/^/ => /' <"$SENT" &
    sed 's/^/<=  /' <"$RCVD" &
    nc -l -p "$1" <"$BACK" | sed -u "s/^Host: .*$/Host: $2:$3/" | tee "$SENT" | nc "$2" "$3" | sed -u "/^Date: /a Access-Control-Allow-Origin: *" | tee "$RCVD" >"$BACK"
done
Anthony O.
  • 674
  • 1
  • 5
  • 14
  • Very nice, trying this out right now. why did you add the `while true` ? – keisar Aug 11 '17 at 15:01
  • Unfortunately doesn't work for binary data (file upload), need to find a replacement for sed that can work with binary data... – keisar Aug 11 '17 at 16:31
  • It didn't help me eventually (because of binary support), but it is very useful. I made some adjustments for OSX, you can see it here: https://pastebin.com/4wZutf8h – keisar Aug 11 '17 at 17:15