Skip to content

Instantly share code, notes, and snippets.

@kopiro
Last active November 13, 2024 09:53
Show Gist options
  • Select an option

  • Save kopiro/35d3f647166c588dfd7b3c274d7307b5 to your computer and use it in GitHub Desktop.

Select an option

Save kopiro/35d3f647166c588dfd7b3c274d7307b5 to your computer and use it in GitHub Desktop.

Revisions

  1. kopiro revised this gist Nov 13, 2024. 1 changed file with 18 additions and 7 deletions.
    25 changes: 18 additions & 7 deletions cors-proxy.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    function cors-proxy() {
    backend="$1"
    if [ -z "$backend" ]; then
    echo "Please provide a backend URL"
    @@ -32,7 +31,7 @@ function cors-proxy() {
    return;
    fi

    cat <<EOF > /tmp/cors_proxy.conf
    cat <<EOF > /tmp/cors_proxy.conf
    events {}
    http {
    @@ -42,16 +41,28 @@ http {
    ssl_certificate_key ${ssl_key_file};
    access_log /dev/stdout;
    error_log /dev/stderr;
    location / {
    # Handle CORS preflight requests
    if (\$request_method = OPTIONS) {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' '*';
    add_header 'Access-Control-Max-Age' 86400;
    add_header 'Content-Length' 0;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    return 204;
    }
    # Proxy all other requests
    proxy_pass $backend;
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers *;
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' '*';
    }
    }
    }
    EOF

    echo "Starting CORS proxy for '${backend}' at 'https://127.0.0.1:${port}'"
    nginx -c /tmp/cors_proxy.conf -g 'daemon off;'
    }
    nginx -c /tmp/cors_proxy.conf -g 'daemon off;'
  2. kopiro revised this gist Nov 13, 2024. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions cors-proxy.sh
    Original file line number Diff line number Diff line change
    @@ -5,16 +5,19 @@ function cors-proxy() {
    return
    fi

    port="$2" || 5555
    port="$2"
    if [ -z "$port" ]; then
    port=5555
    fi

    SSL_DIR="$HOME/.config/https-server"
    ssl_cert_file="${SSL_DIR}/localhost.crt"
    ssl_key_file="${SSL_DIR}/localhost.key"
    ssl_dir="$HOME/.config/https-server"
    ssl_cert_file="${ssl_dir}/localhost.crt"
    ssl_key_file="${ssl_dir}/localhost.key"

    if [ ! -f "${CRT}" ] || [ ! -f "${ssl_key_file}" ]; then
    if [ ! -f "${ssl_cert_file}" ] || [ ! -f "${ssl_key_file}" ]; then
    echo "Generating certificates and saving it in ${ssl_cert_file}..."

    mkdir -p "${SSL_DIR}"
    mkdir -p "${ssl_dir}"
    openssl req -x509 -out "${ssl_cert_file}" -keyout "${ssl_key_file}" \
    -newkey rsa:2048 -nodes -sha256 \
    -subj '/CN=localhost' -extensions EXT -config <( \
  3. kopiro created this gist Nov 13, 2024.
    54 changes: 54 additions & 0 deletions cors-proxy.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    function cors-proxy() {
    backend="$1"
    if [ -z "$backend" ]; then
    echo "Please provide a backend URL"
    return
    fi

    port="$2" || 5555

    SSL_DIR="$HOME/.config/https-server"
    ssl_cert_file="${SSL_DIR}/localhost.crt"
    ssl_key_file="${SSL_DIR}/localhost.key"

    if [ ! -f "${CRT}" ] || [ ! -f "${ssl_key_file}" ]; then
    echo "Generating certificates and saving it in ${ssl_cert_file}..."

    mkdir -p "${SSL_DIR}"
    openssl req -x509 -out "${ssl_cert_file}" -keyout "${ssl_key_file}" \
    -newkey rsa:2048 -nodes -sha256 \
    -subj '/CN=localhost' -extensions EXT -config <( \
    printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

    echo "Adding certificate to trusted certs, please type your password:"
    sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain "${ssl_cert_file}"
    fi

    if [ ! -f "${ssl_cert_file}" ] || [ ! -f "${ssl_key_file}" ]; then
    echo "Can't find certificate at ${ssl_cert_file} or key at ${ssl_key_file}"
    return;
    fi

    cat <<EOF > /tmp/cors_proxy.conf
    events {}
    http {
    server {
    listen ${port} ssl;
    ssl_certificate ${ssl_cert_file};
    ssl_certificate_key ${ssl_key_file};
    access_log /dev/stdout;
    error_log /dev/stderr;
    location / {
    proxy_pass $backend;
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers *;
    }
    }
    }
    EOF

    echo "Starting CORS proxy for '${backend}' at 'https://127.0.0.1:${port}'"
    nginx -c /tmp/cors_proxy.conf -g 'daemon off;'
    }