Make it possible to configure docker via environment variables

parent 8dd45117
...@@ -32,11 +32,17 @@ docker run -d --network=host seriyps/mtproto-proxy ...@@ -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 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 Where
* `-p 443` proxy port * `-p 443` / `MTP_PORT` proxy port
* `-s d0d6e111bada5511fcce9584deadbeef` proxy secret (don't append `dd`! it should be 32 chars long!) * `-s d0d6e111bada5511fcce9584deadbeef` / `MTP_SECRET` 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) * `-t dcbe8f1493fa4cd9ab300891c0b5b326` / `MTP_TAG` ad-tag that you get from [@MTProxybot](https://t.me/MTProxybot)
### To run with custom config-file ### To run with custom config-file
......
...@@ -10,6 +10,13 @@ usage() { ...@@ -10,6 +10,13 @@ usage() {
echo "${THIS}" echo "${THIS}"
echo "To start in single-port mode configured from command-line:" echo "To start in single-port mode configured from command-line:"
echo "${THIS} -p <port> -s <secret> -t <ad tag>" echo "${THIS} -p <port> -s <secret> -t <ad tag>"
echo "Parameters:"
echo "-p <port>: port to listen on. 1-65535"
echo "-s <secret>: proxy secret. 32 hex characters 0-9 a-f"
echo "-t <ad tag>: 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() { error() {
...@@ -18,36 +25,44 @@ error() { ...@@ -18,36 +25,44 @@ error() {
exit 1 exit 1
} }
NUM_OPTS=0 # check environment variables
PORT="" PORT=${MTP_PORT:-""}
SECRET="" SECRET=${MTP_SECRET:-""}
TAG="" TAG=${MTP_TAG:-""}
# check command line options
while getopts "p:s:t:h" o; do while getopts "p:s:t:h" o; do
case "${o}" in case "${o}" in
p) p)
PORT=${OPTARG} PORT=${OPTARG}
test ${PORT} -gt 0 -a ${PORT} -lt 65535 || error "Invalid port value: ${PORT}"
;; ;;
s) s)
SECRET=${OPTARG} SECRET=${OPTARG}
[ -n "`echo $SECRET | grep -x '[[:xdigit:]]\{32\}'`" ] || error "Invalid secret. Should be 32 chars of 0-9 a-f"
;; ;;
t) t)
TAG=${OPTARG} TAG=${OPTARG}
[ -n "`echo $TAG | grep -x '[[:xdigit:]]\{32\}'`" ] || error "Invalid tag. Should be 32 chars of 0-9 a-f"
;; ;;
h) h)
usage usage
exit 0 exit 0
esac esac
NUM_OPTS=$((NUM_OPTS + 1))
done done
if [ $NUM_OPTS -eq 0 ]; then # if at least one option is set...
exec $CMD if [ -n "${PORT}" -o -n "${SECRET}" -o -n "${TAG}" ]; then
elif [ $NUM_OPTS -eq 3 ]; 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\">>}]" exec $CMD -mtproto_proxy ports "[#{name => mtproto_proxy, port => $PORT, secret => <<\"$SECRET\">>, tag => <<\"$TAG\">>}]"
else else
error "Not enough options: -p '${PORT}' -s '${SECRET}' -t '${TAG}'" exec $CMD
fi fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment