Skip to content

Instantly share code, notes, and snippets.

@evansd
Created October 8, 2012 12:13
Show Gist options
  • Save evansd/3852205 to your computer and use it in GitHub Desktop.
Save evansd/3852205 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Simple SSL forwarder designed for working with HTTPS in development
# With no arguments, it will create a self-signed certificate for
# localhost and forward SSL traffic on port 8443 to port 8000
set -e
which openssl > /dev/null || (
echo "openssl not installed, try: sudo apt-get install openssl"
exit 1
)
which stunnel4 > /dev/null || (
echo "stunnel4 not installed, try: sudo apt-get install stunnel4"
exit 1
)
HTTP_PORT=${1:-8000}
HTTPS_PORT=${2:-8443}
THIS_DIR=`dirname "$(which "$0")"`
: ${CERTIFICATE_FILE:="$THIS_DIR/self-signed-cert.pem"}
: ${HTTPS_HOST:=localhost}
: ${DEBUG_LEVEL:=4}
if [ ! -e "$CERTIFICATE_FILE" ]; then
echo "$CERTIFICATE_FILE does not exist, generating self-signed certificate for $HTTPS_HOST"
openssl req -new -newkey rsa:4096 -days 3650 -nodes -x509 \
-subj "/CN=$HTTPS_HOST" \
-keyout "$CERTIFICATE_FILE" >> "$CERTIFICATE_FILE"
fi
echo "Forwarding SSL port $HTTPS_PORT to port $HTTP_PORT"
echo "Usage: $0 [HTTP_PORT] [HTTPS_PORT]"
echo "SSL forwarder running on: https://$HTTPS_HOST:$HTTPS_PORT"
# Pipe stunnel's config in over stdout (hence '-fd 0' argument)
echo -e \
"debug = $DEBUG_LEVEL\n" \
"foreground = yes\n" \
"pid =\n" \
"[https]\n" \
"cert = $CERTIFICATE_FILE\n" \
"accept = $HTTPS_PORT\n" \
"connect = $HTTP_PORT\n" \
| exec stunnel4 -fd 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment