Loading

MTProxy v0

  1. #!/bin/bash
  2.  
  3. : ${CURL="/usr/bin/curl"}
  4. : ${CURL_ARGS="--retry 5 --retry-delay 1"}
  5.  
  6. #: ${MTPROXY="/usr/bin/mtproto-proxy"}
  7. : ${MTPROXY="MTProxy/objs/bin/mtproto-proxy"}
  8.  
  9. : ${PROXY_CONFIG="proxy-multi.conf"}
  10. : ${PROXY_SECRET="proxy-secret"}
  11.  
  12. : ${PROXY_CONFIG_URL="https://core.telegram.org/getProxyConfig"}
  13. : ${PROXY_SECRET_URL="https://core.telegram.org/getProxySecret"}
  14.  
  15. #: ${DATESTR="1 days 03:00:00"}
  16. : ${DATESTR="5 sec"}
  17.  
  18. _echo() {
  19.     local dt="$(date +'%F %T')"
  20.  
  21.     echo "[${dt}] $1"
  22. }
  23.  
  24. _curl() {
  25.     local url=$1
  26.     local out=$2
  27.     local status
  28.  
  29.     ${CURL} -s ${CURL_ARGS} ${url} -o ${out}
  30.  
  31.     (( status = $? ))
  32.     (( status > 0 )) \
  33.         && _echo "curl ${url} failed. Return status: ${status}."
  34.  
  35.     return ${status}
  36. }
  37.  
  38. curl_proxy() {
  39.     local url=$1
  40.     local out=$2
  41.  
  42.     if [ ! -f ${out} ]
  43.     then
  44.         _curl ${url} ${out}
  45.  
  46.         if (( $? > 0 ))
  47.         then
  48.             _echo "${out} not found. Exiting."
  49.             exit
  50.         fi
  51.  
  52.         return 1
  53.     fi
  54.  
  55.     _curl ${url} ${out}_
  56.  
  57.     if (( $? > 0 ))
  58.     then
  59.         _echo "Using old ${out}"
  60.         return 0
  61.     fi
  62.  
  63.     cmp -s ${out} ${out}_
  64.  
  65.     if (( $? > 0 ))
  66.     then
  67.         mv ${out}_ ${out}
  68.         _echo "${out} updated"
  69.         return 1
  70.     fi
  71.  
  72.     rm ${out}_
  73.     _echo "${out} has not changed"
  74. }
  75.  
  76. curl_all() {
  77.     local status
  78.  
  79.     _echo "curl getProxyConfig"
  80.     curl_proxy \
  81.         ${PROXY_CONFIG_URL} \
  82.         ${PROXY_CONFIG}
  83.  
  84.     (( status = $? ))
  85.  
  86.     _echo "curl getProxySecret"
  87.     curl_proxy \
  88.         ${PROXY_SECRET_URL} \
  89.         ${PROXY_SECRET}
  90.  
  91.     (( status = $? ))
  92.  
  93.     return ${status}
  94. }
  95.  
  96. mtproxy_start() {
  97.     local status
  98.  
  99.     ${MTPROXY} \
  100.         -u nobody -p 8888 -H 5443 \
  101.         -S 11111111111111111111111111111111 \
  102.         --aes-pwd proxy-secret proxy-multi.conf -M 1 2>/dev/null >> /dev/null
  103.  
  104.     (( status = $? ))
  105.     (( status > 0 )) \
  106.         && printf ${status} > mtperr
  107.  
  108.     return ${status}
  109. }
  110.  
  111. mtproxy_stop() {
  112.     kill -TERM -${M_PID}
  113. }
  114.  
  115. mtproxy_sleep() {
  116.     local s1 s2 ts
  117.  
  118.     (( s1 = $(date +%s) ))
  119.     (( s2 = $(date +%s -d "${DATESTR}") ))
  120.     (( ts = ${s2} - ${s1} ))
  121.  
  122.     local dt="$(date +%c -d @${s2})"
  123.  
  124.     _echo "MTProxy started. Next restart at ${dt}."
  125.  
  126. #   timeout --preserve-status ${ts}s wait ${MTPID}
  127.  
  128.     sleep ${ts}
  129.  
  130.     return $?
  131. }
  132.  
  133. mtproxy_wait() {
  134.     local status
  135.  
  136.     mtproxy_sleep &
  137.     S_PID=$!
  138.  
  139.     wait -n ${M_PID} ${S_PID}
  140.     status=$?
  141.  
  142.     kill -TERM -${S_PID} 2>/dev/null
  143.  
  144.     return ${status}
  145. }
  146.  
  147. # Clear current MTProxy PID
  148. unset M_PID
  149.  
  150. # Clear current sleepping PID
  151. unset S_PID
  152.  
  153. # Validate ${DATESTR}
  154. date + -d "${DATESTR}" > /dev/null
  155.  
  156. if (( $? > 0 ))
  157. then
  158.     _echo "Wrong date format: ${DATESTR}. Exiting."
  159.     exit
  160. fi
  161.  
  162. curl_all
  163.  
  164. mtproxy_start &
  165. M_PID=$!
  166.  
  167. while mtproxy_wait
  168. do
  169.     curl_all && continue
  170.  
  171.     mtproxy_stop
  172.  
  173.     mtproxy_start &
  174.     M_PID=$!
  175. done