diff --git a/run.sh b/run.sh index b8c2bce..df544de 100755 --- a/run.sh +++ b/run.sh @@ -41,7 +41,7 @@ usage() { Jrules='{"rules":[]}' -TEMP=$(getopt -o "u:k:r:c:j:di" --long "lgp:,lgr:,lgt:,lsp:,lst:,ltr:,ltrx:,ltt:,lttx:,lwp:,lwt:,mtt:,mwp:,mwt:,ttt:,twp:,twt:,user:,hook:,request-domain:,cert-home:,ip-block:,domain-block:,cn-block,ng-server:,ng-proxy:,st-server:,st-map:,json:,stdin,debug" -n "$0" -- $@) +TEMP=$(getopt -o u:k:r:c:j:di --long lgp:,lgr:,lgt:,lsp:,lst:,ltr:,ltrx:,ltt:,lttx:,lwp:,lwt:,mtt:,mwp:,mwt:,ttt:,twp:,twt:,user:,hook:,request-domain:,cert-home:,ip-block:,domain-block:,cn-block,ng-server:,ng-proxy:,st-server:,st-map:,json:,stdin,debug -n "$0" -- "$@") if [ $? != 0 ] ; then usage; exit 1 ; fi eval set -- "$TEMP" @@ -148,7 +148,7 @@ if [ -n "${HOOKURL}" ]; then fi # Acquiring Letsencrypt certs for each request-domain -if [ -n "${CERTDOMAIN}" ]; then +if [ "${#CERTDOMAIN[@]}" -gt 0 ]; then for DOMAIN in "${CERTDOMAIN[@]}" do TRY=0 @@ -171,7 +171,7 @@ fi xopt="certhome=$CERTHOME" for uopt in "${UOPT[@]}"; do xopt="$xopt,$uopt"; done -if [ -z "${SVCMD}" ]; then +if [ "${#SVCMD[@]}" -eq 0 ]; then echo -e "No Xray service creation found. Quit.\n" usage; exit 1 fi @@ -231,7 +231,7 @@ done if [ -n "${DEBUG}" ]; then loglevel="debug"; else loglevel="warning"; fi Jroot=$(echo $Jroot| jq --arg loglevel "${loglevel}" '.log.loglevel |= $loglevel') -if [ -n "${INJECT[@]}" ]; then +if [ "${#INJECT[@]}" -gt 0 ]; then for JSON_IN in "${INJECT[@]}" do Jmerge=$(jq -nc "${JSON_IN}") diff --git a/server-lgp.sh b/server-lgp.sh index 68ff38a..364266d 100755 --- a/server-lgp.sh +++ b/server-lgp.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in d|domain) domain="${kv[1]}" @@ -18,6 +18,9 @@ do f|fallback) fallback+=("${kv[1]}") ;; + flow) + flow="${kv[1]}" + ;; p|port) port="${kv[1]}" ;; @@ -32,6 +35,7 @@ do ;; esac done +unset IFS if [ -z "${domain}" ]; then >&2 echo -e "Error: Domain undefined.\n" diff --git a/server-lgr.sh b/server-lgr.sh index d7f9ec3..dbaa122 100755 --- a/server-lgr.sh +++ b/server-lgr.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in d|dest) dest="${kv[1]}" @@ -50,6 +50,7 @@ do ;; esac done +unset IFS if [ -z "${dest}" ]; then >&2 echo -e "Error: Fake Destination undefined.\n" diff --git a/server-lgt.sh b/server-lgt.sh index 3adb020..58c22cf 100755 --- a/server-lgt.sh +++ b/server-lgt.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in c|certhome) certhome="${kv[1]}" @@ -41,6 +41,7 @@ do ;; esac done +unset IFS if [ -z "${certhome}" ]; then >&2 echo -e "Error: Certhome undefined.\n" diff --git a/server-lsp.sh b/server-lsp.sh index 9d706a7..ab0d48b 100755 --- a/server-lsp.sh +++ b/server-lsp.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in d|domain) domain="${kv[1]}" @@ -18,6 +18,9 @@ do f|fallback) fallback+=("${kv[1]}") ;; + flow) + flow="${kv[1]}" + ;; p|port) port="${kv[1]}" ;; @@ -32,6 +35,7 @@ do ;; esac done +unset IFS if [ -z "${domain}" ]; then >&2 echo -e "Error: Domain undefined.\n" diff --git a/server-lst.sh b/server-lst.sh index aa5c3d0..e9382ba 100755 --- a/server-lst.sh +++ b/server-lst.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in c|certhome) certhome="${kv[1]}" @@ -41,6 +41,7 @@ do ;; esac done +unset IFS if [ -z "${certhome}" ]; then >&2 echo -e "Error: Certhome undefined.\n" diff --git a/server-ltr.sh b/server-ltr.sh index 11713a1..9f4e9be 100755 --- a/server-ltr.sh +++ b/server-ltr.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in d|dest) dest="${kv[1]}" @@ -47,6 +47,7 @@ do ;; esac done +unset IFS if [ -z "${dest}" ]; then >&2 echo -e "Error: Fake Destination undefined.\n" diff --git a/server-ltt.sh b/server-ltt.sh index deafbb4..9e13c1c 100755 --- a/server-ltt.sh +++ b/server-ltt.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in c|certhome) certhome="${kv[1]}" @@ -38,6 +38,7 @@ do ;; esac done +unset IFS if [ -z "${certhome}" ]; then >&2 echo -e "Error: Certhome undefined.\n" diff --git a/server-lwp.sh b/server-lwp.sh index 89dc4ef..cfb181e 100755 --- a/server-lwp.sh +++ b/server-lwp.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in d|domain) domain="${kv[1]}" @@ -18,6 +18,9 @@ do f|fallback) fallback+=("${kv[1]}") ;; + flow) + flow="${kv[1]}" + ;; p|port) port="${kv[1]}" ;; @@ -32,6 +35,7 @@ do ;; esac done +unset IFS if [ -z "${domain}" ]; then >&2 echo -e "Error: Domain undefined.\n" diff --git a/server-lwt.sh b/server-lwt.sh index dd45059..3245a99 100755 --- a/server-lwt.sh +++ b/server-lwt.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in c|certhome) certhome="${kv[1]}" @@ -41,6 +41,7 @@ do ;; esac done +unset IFS if [ -z "${certhome}" ]; then >&2 echo -e "Error: Certhome undefined.\n" diff --git a/server-mtt.sh b/server-mtt.sh index a990450..98b9778 100755 --- a/server-mtt.sh +++ b/server-mtt.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in c|certhome) certhome="${kv[1]}" @@ -38,6 +38,7 @@ do ;; esac done +unset IFS if [ -z "${certhome}" ]; then >&2 echo -e "Error: Certhome undefined.\n" diff --git a/server-mwp.sh b/server-mwp.sh index 2bf3c36..376fe33 100755 --- a/server-mwp.sh +++ b/server-mwp.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in d|domain) domain="${kv[1]}" @@ -18,6 +18,9 @@ do f|fallback) fallback+=("${kv[1]}") ;; + flow) + flow="${kv[1]}" + ;; p|port) port="${kv[1]}" ;; @@ -32,6 +35,7 @@ do ;; esac done +unset IFS if [ -z "${domain}" ]; then >&2 echo -e "Error: Domain undefined.\n" diff --git a/server-mwt.sh b/server-mwt.sh index d5beabd..2f5d54a 100755 --- a/server-mwt.sh +++ b/server-mwt.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in c|certhome) certhome="${kv[1]}" @@ -41,6 +41,7 @@ do ;; esac done +unset IFS if [ -z "${certhome}" ]; then >&2 echo -e "Error: Certhome undefined.\n" diff --git a/server-nginx.sh b/server-nginx.sh index b865607..51e6faa 100755 --- a/server-nginx.sh +++ b/server-nginx.sh @@ -12,7 +12,7 @@ usage() { >&2 echo " --st-server [p=443],[proxy_pass]" } -TEMP=$(getopt -o "m:n:p:s:x:" --long "ng-server:,ng-proxy:,st-server:,st-map:" -n "$0" -- $@) +TEMP=$(getopt -o m:n:p:s:x: --long ng-server:,ng-proxy:,st-server:,st-map: -n "$0" -- "$@") if [ $? != 0 ] ; then usage; exit 1 ; fi eval set -- "$TEMP" @@ -49,13 +49,13 @@ while true ; do esac done -if [ -z "${NGSVR}" ] && [ -z "${STPORT}" ]; then +if [ "${#NGSVR[@]}" -eq 0 ] && [ -z "${STSVR}" ]; then >&2 echo -e "No Stream/Server defined. Quit.\n"; usage; exit 1; fi # Running as root to enable transparent stream. -# sed -i 's/^user \+nginx;$/user root;/g' /etc/nginx/nginx.conf +# sed -i 's/^user \+nginx;$/user root;/g' $NGCONF cd /etc/nginx/conf.d/ if [ -f /etc/nginx/conf.d/default.conf ]; then @@ -63,9 +63,9 @@ if [ -f /etc/nginx/conf.d/default.conf ]; then fi # Remove all lines generated previously after #STREAM_TAG tag. -sed -i '/\#STREAM_TAG/q' /etc/nginx/nginx.conf +sed -i '/\#STREAM_TAG/q' $NGCONF # Remove #STREAM_TAG tag -sed -i '/\#STREAM_TAG/d' /etc/nginx/nginx.conf +sed -i '/\#STREAM_TAG/d' $NGCONF # Generate Nginx Stream server configuration. if [ -n "${STSVR}" ]; then @@ -87,7 +87,7 @@ options=($(echo $STSVR |tr ',' ' ')) if ! [ "${STPORT}" -eq "${STPORT}" ] 2>/dev/null; then >&2 echo "Stream port number must be numeric"; exit 1; fi # Attaching the stream configuration template to the tail of nginx.conf - cat ${STREAM_TPL} >> /etc/nginx/nginx.conf + cat ${STREAM_TPL} >> $NGCONF for stmap in "${STMAP[@]}" do options=($(echo $stmap |tr ',' ' ')) @@ -112,18 +112,18 @@ options=($(echo $STSVR |tr ',' ' ')) done # Adding map.conf down to #XMAP_TAG tag - sed -i '/#XMAP_TAG/r /tmp/stmap.conf' /etc/nginx/nginx.conf + sed -i '/#XMAP_TAG/r /tmp/stmap.conf' $NGCONF # Adding ups.conf down to #XUPSTREAM_TAG tag - sed -i '/#XUPSTREAM_TAG/r /tmp/stups.conf' /etc/nginx/nginx.conf - sed -i "s/STPORT/${STPORT}/g" /etc/nginx/nginx.conf + sed -i '/#XUPSTREAM_TAG/r /tmp/stups.conf' $NGCONF + sed -i "s/STPORT/${STPORT}/g" $NGCONF # Adding "proxy_protocol=on" down to #STPROXY_PASS_TAG tag if [ -n "${STPROXY_PASS}" ]; then echo " proxy_protocol on;" >/tmp/stproxy.conf - sed -i '/#STPROXY_PASS_TAG/r /tmp/stproxy.conf' /etc/nginx/nginx.conf + sed -i '/#STPROXY_PASS_TAG/r /tmp/stproxy.conf' $NGCONF fi rm -rf /tmp/stmap.conf; rm -rf /tmp/stups.conf; rm -rf /tmp/stproxy.conf - echo "Generated /etc/nginx/nginx.conf ====>" - cat /etc/nginx/nginx.conf + echo "Generated $NGCONF ====>" + cat $NGCONF fi # Generating Nginx site server configurations. @@ -155,7 +155,7 @@ do done if [ -z "${certhome}" ]; then echo -e "Error: Nginx certhome undefined.\n"; usage; exit 1; fi - if [ -z "${SITEDOMAINS}" ]; then echo -e "Error: Nginx site domain undefined.\n"; usage; exit 1; fi + if [ "${#SITEDOMAINS[@]}" -eq 0 ]; then echo -e "Error: Nginx site domain undefined.\n"; usage; exit 1; fi if [ -z "${port}" ]; then port=443; fi if ! [ "${port}" -eq "${port}" ] 2>/dev/null; then >&2 echo -e "Port number must be numeric. \n"; exit 1; fi @@ -223,7 +223,7 @@ do done if [ -z "${xhost}" ]; then xhost="127.0.0.1"; fi - if [ -z "${XDOMAINS}" ]; then XDOMAINS=("${ALLDOMAINS[@]}"); fi + if [ "${#XDOMAINS[@]}" -eq 0 ]; then XDOMAINS=("${ALLDOMAINS[@]}"); fi if [ -z "${xnetwork}" ]; then echo "Missing network: $ngproxy"; usage; exit 1; fi if [ -z "${xlocation}" ]; then echo "Missing location: $ngproxy"; usage; exit 1; fi if [ -z "${xport}" ]; then echo "Missing port: $ngproxy"; usage; exit 1; fi diff --git a/server-ttt.sh b/server-ttt.sh index 75c7703..897c264 100755 --- a/server-ttt.sh +++ b/server-ttt.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user|u=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in c|certhome) certhome="${kv[1]}" @@ -38,6 +38,7 @@ do ;; esac done +unset IFS if [ -z "${certhome}" ]; then >&2 echo -e "Error: Certhome undefined.\n" diff --git a/server-twp.sh b/server-twp.sh index 4eb24c9..ac523fb 100755 --- a/server-twp.sh +++ b/server-twp.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in d|domain) domain="${kv[1]}" @@ -18,6 +18,9 @@ do f|fallback) fallback+=("${kv[1]}") ;; + flow) + flow="${kv[1]}" + ;; p|port) port="${kv[1]}" ;; @@ -32,6 +35,7 @@ do ;; esac done +unset IFS if [ -z "${domain}" ]; then >&2 echo -e "Error: Domain undefined.\n" diff --git a/server-twt.sh b/server-twt.sh index 0206143..1565d6a 100755 --- a/server-twt.sh +++ b/server-twt.sh @@ -7,10 +7,10 @@ usage() { >&2 echo -e "User format: user=[:level:email]" } -options=($(echo $1 |tr ',' ' ')) +IFS=',' read -a options <<< "$1" for option in "${options[@]}" do - kv=($(echo $option |tr '=' ' ')) + IFS='=' read -a kv <<< "$option" case "${kv[0]}" in c|certhome) certhome="${kv[1]}" @@ -41,6 +41,7 @@ do ;; esac done +unset IFS if [ -z "${certhome}" ]; then >&2 echo -e "Error: Certhome undefined.\n"