From 2f015e2f3f433656c3b23647e37cafebf172917e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D1=80=D0=BE?= =?UTF-8?q?=D1=85=D0=BE=D1=80=D0=BE=D0=B2?= Date: Mon, 18 Feb 2019 00:37:00 +0100 Subject: [PATCH] Make it possible to configure docker via environment variables --- README.md | 12 +++++++++--- start.sh | 39 +++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 65462b7..ca67d3c 100644 --- a/README.md +++ b/README.md @@ -32,11 +32,17 @@ docker run -d --network=host seriyps/mtproto-proxy docker run -d --network=host seriyps/mtproto-proxy -p 443 -s d0d6e111bada5511fcce9584deadbeef -t dcbe8f1493fa4cd9ab300891c0b5b326 ``` +or via environmet variables + +```bash +docker run -d --network=host -e MTP_PORT=443 -e MTP_SECRET=d0d6e111bada5511fcce9584deadbeef -e MTP_TAG=dcbe8f1493fa4cd9ab300891c0b5b326 seriyps/mtproto-proxy +``` + Where -* `-p 443` proxy port -* `-s d0d6e111bada5511fcce9584deadbeef` proxy secret (don't append `dd`! it should be 32 chars long!) -* `-t dcbe8f1493fa4cd9ab300891c0b5b326` ad-tag that you get from [@MTProxybot](https://t.me/MTProxybot) +* `-p 443` / `MTP_PORT` proxy port +* `-s d0d6e111bada5511fcce9584deadbeef` / `MTP_SECRET` proxy secret (don't append `dd`! it should be 32 chars long!) +* `-t dcbe8f1493fa4cd9ab300891c0b5b326` / `MTP_TAG` ad-tag that you get from [@MTProxybot](https://t.me/MTProxybot) ### To run with custom config-file diff --git a/start.sh b/start.sh index 94f245b..b755948 100755 --- a/start.sh +++ b/start.sh @@ -10,6 +10,13 @@ usage() { echo "${THIS}" echo "To start in single-port mode configured from command-line:" echo "${THIS} -p -s -t " + echo "Parameters:" + echo "-p : port to listen on. 1-65535" + echo "-s : proxy secret. 32 hex characters 0-9 a-f" + echo "-t : promo tag, that you get from @MTProxybot. 32 hex characters" + echo "port secret and tag can also be configured via environment variables:" + echo "MTP_PORT, MTP_SECRET, MTP_TAG" + echo "If both command line and environment are set, command line have higher priority" } error() { @@ -18,36 +25,44 @@ error() { exit 1 } -NUM_OPTS=0 -PORT="" -SECRET="" -TAG="" +# check environment variables +PORT=${MTP_PORT:-""} +SECRET=${MTP_SECRET:-""} +TAG=${MTP_TAG:-""} +# check command line options while getopts "p:s:t:h" o; do case "${o}" in p) PORT=${OPTARG} - test ${PORT} -gt 0 -a ${PORT} -lt 65535 || error "Invalid port value: ${PORT}" ;; s) SECRET=${OPTARG} - [ -n "`echo $SECRET | grep -x '[[:xdigit:]]\{32\}'`" ] || error "Invalid secret. Should be 32 chars of 0-9 a-f" ;; t) TAG=${OPTARG} - [ -n "`echo $TAG | grep -x '[[:xdigit:]]\{32\}'`" ] || error "Invalid tag. Should be 32 chars of 0-9 a-f" ;; h) usage exit 0 esac - NUM_OPTS=$((NUM_OPTS + 1)) done -if [ $NUM_OPTS -eq 0 ]; then - exec $CMD -elif [ $NUM_OPTS -eq 3 ]; then +# if at least one option is set... +if [ -n "${PORT}" -o -n "${SECRET}" -o -n "${TAG}" ]; then + # If at least one of them not set... + [ -z "${PORT}" -o -z "${SECRET}" -o -z "${SECRET}" ] && \ + error "Not enough options: -p '${PORT}' -s '${SECRET}' -t '${TAG}'" + + # validate format + [ ${PORT} -gt 0 -a ${PORT} -lt 65535 ] || \ + error "Invalid port value: ${PORT}" + [ -n "`echo $SECRET | grep -x '[[:xdigit:]]\{32\}'`" ] || \ + error "Invalid secret. Should be 32 chars of 0-9 a-f" + [ -n "`echo $TAG | grep -x '[[:xdigit:]]\{32\}'`" ] || \ + error "Invalid tag. Should be 32 chars of 0-9 a-f" + exec $CMD -mtproto_proxy ports "[#{name => mtproto_proxy, port => $PORT, secret => <<\"$SECRET\">>, tag => <<\"$TAG\">>}]" else - error "Not enough options: -p '${PORT}' -s '${SECRET}' -t '${TAG}'" + exec $CMD fi