|
| 1 | +#!/bin/sh |
| 2 | +set -eu |
| 3 | + |
| 4 | +# Use a single, fixed cipher for consistent tests across different tools |
| 5 | +TLS_CIPHERS=ECDHE-RSA-AES256-GCM-SHA384 |
| 6 | +TLS_PROTO=TLSv1.2 |
| 7 | + |
| 8 | +# Use non-root ports |
| 9 | +NGINX_PORT=9080 |
| 10 | +NGINX_TLS_PORT=9443 |
| 11 | +STUNNEL_TLS_PORT=9444 |
| 12 | +HITCH_TLS_PORT=9445 |
| 13 | +SOCAT_TLS_PORT=9446 |
| 14 | + |
| 15 | +REPEAT=${REPEAT-30} |
| 16 | + |
| 17 | +PEM=stunnel.pem |
| 18 | +TESTDIR="${PWD}/tmp" |
| 19 | +RAMDIR=/dev/shm/stunnel-test |
| 20 | +mkdir -p "${RAMDIR}" "${TESTDIR}" |
| 21 | + |
| 22 | +cd "${TESTDIR}" |
| 23 | + |
| 24 | +test -f "${PEM}" || openssl req -new -subj "/C=GB/CN=foo.example.com" -days 10 -x509 -noenc -batch -out "${PEM}" -keyout "${PEM}" -noenc -batch |
| 25 | + |
| 26 | +cat >stunnel.conf <<EOF |
| 27 | +fips=no |
| 28 | +socket = r:TCP_NODELAY=1 |
| 29 | +socket = a:TCP_NODELAY=1 |
| 30 | +socket = l:TCP_NODELAY=1 |
| 31 | +socket = r:SO_KEEPALIVE=1 |
| 32 | +socket = a:SO_KEEPALIVE=1 |
| 33 | +
|
| 34 | +[test] |
| 35 | +accept = :::${STUNNEL_TLS_PORT} |
| 36 | +cert = ${PEM} |
| 37 | +connect = ${NGINX_PORT} |
| 38 | +ciphers = ${TLS_CIPHERS} |
| 39 | +options = CIPHER_SERVER_PREFERENCE |
| 40 | +sslVersion = ${TLS_PROTO} |
| 41 | +EOF |
| 42 | + |
| 43 | +truncate --size 1GiB "${RAMDIR}/1GiB" |
| 44 | + |
| 45 | +cat >nginx.conf <<EOF |
| 46 | +error_log stderr error; |
| 47 | +pid ${TESTDIR}/nginx.pid; |
| 48 | +daemon on; |
| 49 | +events { } |
| 50 | +http { |
| 51 | + client_body_temp_path "${TESTDIR}/nginx-client_body"; |
| 52 | + proxy_temp_path "${TESTDIR}/nginx-proxy"; |
| 53 | + fastcgi_temp_path "${TESTDIR}/nginx-fcgi"; |
| 54 | + uwsgi_temp_path ${TESTDIR}/nginx-uwsgi"; |
| 55 | + scgi_temp_path ${TESTDIR}/nginx-scgi"; |
| 56 | + access_log off; |
| 57 | + tcp_nodelay on; |
| 58 | + sendfile on; |
| 59 | + server { |
| 60 | + gzip off; |
| 61 | + listen "${NGINX_PORT}" reuseport; |
| 62 | + listen "${NGINX_TLS_PORT}" reuseport ssl; |
| 63 | + ssl_certificate "${PEM}"; |
| 64 | + ssl_certificate_key "${PEM}"; |
| 65 | + ssl_protocols "${TLS_PROTO}"; |
| 66 | + ssl_ciphers "${TLS_CIPHERS}"; |
| 67 | + root "${RAMDIR}"; |
| 68 | + location = /hello { |
| 69 | + default_type text/plain; |
| 70 | + return 200 "Hello"; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +EOF |
| 75 | + |
| 76 | +cat >hitch.conf <<EOF |
| 77 | +backend="[127.0.0.1]:${NGINX_PORT}" |
| 78 | +ciphers="${TLS_CIPHERS}" |
| 79 | +daemon=on |
| 80 | +frontend={ |
| 81 | + host="*" |
| 82 | + port="${HITCH_TLS_PORT}" |
| 83 | + pem-file="${PEM}" |
| 84 | + prefer-server-ciphers=on |
| 85 | + tls-protos=${TLS_PROTO} |
| 86 | +} |
| 87 | +EOF |
| 88 | + |
| 89 | +killall -v -9 stunnel nginx hitch socat || : |
| 90 | + |
| 91 | +# Start servers |
| 92 | +nginx -c "${TESTDIR}/nginx.conf" |
| 93 | +socat -b65536 "OPENSSL-LISTEN:${SOCAT_TLS_PORT},fork,reuseaddr,cert=${PEM},verify=0,cipher=${TLS_CIPHERS},compress=none,reuseport" "TCP4:127.0.0.1:${NGINX_PORT}" & |
| 94 | +stunnel "${TESTDIR}/stunnel.conf" |
| 95 | +hitch --config "${TESTDIR}/hitch.conf" |
| 96 | + |
| 97 | +DEST=${DEST:-127.0.0.1} |
| 98 | + |
| 99 | +# Start clients (they could also use a pipe, for simplicity use a local port) |
| 100 | +for PORT in ${NGINX_TLS_PORT} ${STUNNEL_TLS_PORT} ${HITCH_TLS_PORT} ${SOCAT_TLS_PORT}; do |
| 101 | + PORT2=$(( "${PORT}" + 10000 )) |
| 102 | + cat >"stunnel-client-${PORT}.conf" <<EOF |
| 103 | + client=yes |
| 104 | + foreground=no |
| 105 | + socket=r:TCP_NODELAY=1 |
| 106 | + socket=r:SO_KEEPALIVE=1 |
| 107 | + socket=a:SO_KEEPALIVE=1 |
| 108 | + fips=no |
| 109 | + connect=${DEST}:${PORT} |
| 110 | + sslVersion=${TLS_PROTO} |
| 111 | + ciphers=${TLS_CIPHERS} |
| 112 | + [client-proxy] |
| 113 | + accept=127.0.0.1:${PORT2} |
| 114 | +EOF |
| 115 | + stunnel "${TESTDIR}/stunnel-client-${PORT}.conf" |
| 116 | + PORT3=$(( ${PORT} + 20000 )) |
| 117 | + socat -b65536 "TCP4-LISTEN:${PORT3},bind=127.0.0.1,reuseport" "OPENSSL:${DEST}:${PORT},cipher=${TLS_CIPHERS},verify=0"& |
| 118 | +done |
| 119 | + |
| 120 | + |
| 121 | +measure() |
| 122 | +{ |
| 123 | + echo |
| 124 | + echo "curl --> ${1} ${2} ${3}" |
| 125 | + for i in $(seq "${REPEAT}"); do |
| 126 | + curl -k -o /dev/null "${2}/1GiB" --write-out "%{url},%{scheme},%{http_code},%{size_download},%{speed_download},%{time_pretransfer},%{time_total}\n" |
| 127 | + done |
| 128 | + echo |
| 129 | +} |
| 130 | + |
| 131 | +measure "nginx" "http://${DEST}:${NGINX_PORT}" "" |
| 132 | + |
| 133 | +NGINX_URL="http://127.0.0.1:${NGINX_PORT}" |
| 134 | + |
| 135 | +measure "nginx-ssl" "https://${DEST}:${NGINX_TLS_PORT}" "" |
| 136 | + |
| 137 | +measure "stunnel" "https://${DEST}:${STUNNEL_TLS_PORT}" "-> nginx ${NGINX_URL}" |
| 138 | +measure "hitch" "https://${DEST}:${HITCH_TLS_PORT}" "-> nginx ${NGINX_URL}" |
| 139 | +measure "socat" "https://${DEST}:${SOCAT_TLS_PORT}" "-> nginx ${NGINX_URL}" |
| 140 | + |
| 141 | +measure "stunnel(client)" "http://127.0.0.1:1${STUNNEL_TLS_PORT}" "-> stunnel https://${DEST}:${STUNNEL_TLS_PORT} -> nginx ${NGINX_URL}" |
| 142 | +measure "stunnel(client)" "http://127.0.0.1:1${HITCH_TLS_PORT}" "-> hitch https://${DEST}:${HITCH_TLS_PORT} -> nginx ${NGINX_URL}" |
| 143 | +measure "stunnel(client)" "http://127.0.0.1:1${SOCAT_TLS_PORT}" "-> socat https://${DEST}:${SOCAT_TLS_PORT} -> nginx ${NGINX_URL}" |
0 commit comments