mt_setup.sh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #!/bin/bash
  2. # MTProto一键安装脚本
  3. # Author: palm<https://tizi.blog>
  4. RED="\033[31m" # Error message
  5. GREEN="\033[32m" # Success message
  6. YELLOW="\033[33m" # Warning message
  7. BLUE="\033[36m" # Info message
  8. PLAIN='\033[0m'
  9. export MTG_CONFIG="${MTG_CONFIG:-$HOME/.config/mtg}"
  10. export MTG_ENV="$MTG_CONFIG/env"
  11. export MTG_SECRET="$MTG_CONFIG/secret"
  12. export MTG_CONTAINER="${MTG_CONTAINER:-mtg}"
  13. export MTG_IMAGENAME="${MTG_IMAGENAME:-nineseconds/mtg:1}"
  14. DOCKER_CMD="$(command -v docker)"
  15. OSNAME=`hostnamectl | grep -i system | cut -d: -f2`
  16. IP=`curl -sL -4 ip.sb`
  17. colorEcho() {
  18. echo -e "${1}${@:2}${PLAIN}"
  19. }
  20. checkSystem() {
  21. result=$(id | awk '{print $1}')
  22. if [[ $result != "uid=0(root)" ]]; then
  23. colorEcho $RED " 请以root身份执行该脚本"
  24. exit 1
  25. fi
  26. res=`which yum`
  27. if [[ "$?" != "0" ]]; then
  28. res=`which apt`
  29. if [ "$?" != "0" ]; then
  30. colorEcho $RED " 不受支持的Linux系统"
  31. exit 1
  32. fi
  33. res=`hostnamectl | grep -i ubuntu`
  34. if [[ "${res}" != "" ]]; then
  35. OS="ubuntu"
  36. else
  37. OS="debian"
  38. fi
  39. PMT="apt"
  40. CMD_INSTALL="apt install -y "
  41. CMD_REMOVE="apt remove -y "
  42. else
  43. OS="centos"
  44. PMT="yum"
  45. CMD_INSTALL="yum install -y "
  46. CMD_REMOVE="yum remove -y "
  47. fi
  48. res=`which systemctl`
  49. if [[ "$?" != "0" ]]; then
  50. colorEcho $RED " 系统版本过低,请升级到最新版本"
  51. exit 1
  52. fi
  53. }
  54. status() {
  55. if [[ "$DOCKER_CMD" = "" ]]; then
  56. echo 0
  57. return
  58. elif [[ ! -f $MTG_ENV ]]; then
  59. echo 1
  60. return
  61. fi
  62. port=`grep MTG_PORT $MTG_ENV|cut -d= -f2`
  63. if [[ -z "$port" ]]; then
  64. echo 2
  65. return
  66. fi
  67. res=`ss -ntlp| grep ${port} | grep docker`
  68. if [[ -z "$res" ]]; then
  69. echo 3
  70. else
  71. echo 4
  72. fi
  73. }
  74. statusText() {
  75. res=`status`
  76. case $res in
  77. 3)
  78. echo -e ${GREEN}已安装${PLAIN} ${RED}未运行${PLAIN}
  79. ;;
  80. 4)
  81. echo -e ${GREEN}已安装${PLAIN} ${GREEN}正在运行${PLAIN}
  82. ;;
  83. *)
  84. echo -e ${RED}未安装${PLAIN}
  85. ;;
  86. esac
  87. }
  88. getData() {
  89. read -p " 请输入MTProto端口[100-65535的一个数字]:" PORT
  90. [[ -z "${PORT}" ]] && {
  91. echo -e " ${RED}请输入MTProto端口!${PLAIN}"
  92. exit 1
  93. }
  94. if [[ "${PORT:0:1}" = "0" ]]; then
  95. echo -e " ${RED}端口不能以0开头${PLAIN}"
  96. exit 1
  97. fi
  98. MTG_PORT=$PORT
  99. mkdir -p $MTG_CONFIG
  100. echo "MTG_IMAGENAME=$MTG_IMAGENAME" > "$MTG_ENV"
  101. echo "MTG_PORT=$MTG_PORT" >> "$MTG_ENV"
  102. echo "MTG_CONTAINER=$MTG_CONTAINER" >> "$MTG_ENV"
  103. }
  104. installDocker() {
  105. if [[ "$DOCKER_CMD" != "" ]]; then
  106. systemctl enable docker
  107. systemctl start docker
  108. selinux
  109. return
  110. fi
  111. #$CMD_REMOVE docker docker-engine docker.io containerd runc
  112. $PMT clean all
  113. $CMD_INSTALL wget curl
  114. if [[ $PMT = "apt" ]]; then
  115. apt clean all
  116. apt-get -y install \
  117. apt-transport-https \
  118. ca-certificates \
  119. curl \
  120. gnupg-agent \
  121. software-properties-common
  122. curl -fsSL https://download.docker.com/linux/$OS/gpg | apt-key add -
  123. add-apt-repository \
  124. "deb [arch=amd64] https://download.docker.com/linux/$OS \
  125. $(lsb_release -cs) \
  126. stable"
  127. apt update
  128. else
  129. wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
  130. yum clean all
  131. fi
  132. $CMD_INSTALL docker-ce docker-ce-cli containerd.io
  133. DOCKER_CMD="$(command -v docker)"
  134. if [[ "$DOCKER_CMD" = "" ]]; then
  135. echo -e " ${RED}$OSNAME docker安装失败,请到https://tizi.blog反馈${PLAIN}"
  136. exit 1
  137. fi
  138. systemctl enable docker
  139. systemctl start docker
  140. selinux
  141. }
  142. pullImage() {
  143. if [[ "$DOCKER_CMD" = "" ]]; then
  144. echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}"
  145. exit 1
  146. fi
  147. set -a
  148. source "$MTG_ENV"
  149. set +a
  150. $DOCKER_CMD pull "$MTG_IMAGENAME" > /dev/null
  151. }
  152. selinux() {
  153. if [[ -s /etc/selinux/config ]] && grep 'SELINUX=enforcing' /etc/selinux/config; then
  154. sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config
  155. setenforce 0
  156. fi
  157. }
  158. firewall() {
  159. port=$1
  160. systemctl status firewalld > /dev/null 2>&1
  161. if [[ $? -eq 0 ]];then
  162. firewall-cmd --permanent --add-port=$port/tcp
  163. firewall-cmd --reload
  164. else
  165. nl=`iptables -nL | nl | grep FORWARD | awk '{print $1}'`
  166. if [ "$nl" != "3" ]; then
  167. iptables -I INPUT -p tcp --dport=$port -j ACCEPT
  168. else
  169. res=`ufw status | grep -i inactive`
  170. if [ "$res" = "" ]; then
  171. ufw allow $port/tcp
  172. fi
  173. fi
  174. fi
  175. }
  176. start() {
  177. res=`status`
  178. if [[ $res -lt 3 ]]; then
  179. echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}"
  180. return
  181. fi
  182. set -a
  183. source "$MTG_ENV"
  184. set +a
  185. if [[ ! -f "$MTG_SECRET" ]]; then
  186. $DOCKER_CMD run \
  187. --rm \
  188. "$MTG_IMAGENAME" \
  189. generate-secret tls -c "$(openssl rand -hex 16).com" \
  190. > "$MTG_SECRET"
  191. fi
  192. $DOCKER_CMD ps --filter "Name=$MTG_CONTAINER" -aq | xargs -r $DOCKER_CMD rm -fv > /dev/null
  193. $DOCKER_CMD run \
  194. -d \
  195. --restart=unless-stopped \
  196. --name "$MTG_CONTAINER" \
  197. --ulimit nofile=51200:51200 \
  198. -p "$MTG_PORT:3128" \
  199. "$MTG_IMAGENAME" run "$(cat "$MTG_SECRET")" > /dev/null
  200. sleep 3
  201. res=`ss -ntlp| grep ${MTG_PORT} | grep docker`
  202. if [[ "$res" = "" ]]; then
  203. docker logs $MTG_CONTAINER | tail
  204. echo -e " ${RED}$OSNAME 启动docker镜像失败,请到 https://tizi.blog 反馈${PLAIN}"
  205. exit 1
  206. else
  207. colorEcho $BLUE " MTProto启动成功!"
  208. fi
  209. }
  210. stop() {
  211. res=`status`
  212. if [[ $res -lt 3 ]]; then
  213. echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}"
  214. return
  215. fi
  216. set -a
  217. source "$MTG_ENV"
  218. set +a
  219. $DOCKER_CMD stop $MTG_CONTAINER >> /dev/null
  220. colorEcho $BLUE " MTProto停止成功!"
  221. }
  222. showInfo() {
  223. res=`status`
  224. if [[ $res -lt 3 ]]; then
  225. echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}"
  226. return
  227. fi
  228. SECRET=$(cat "$MTG_SECRET")
  229. set -a
  230. source "$MTG_ENV"
  231. set +a
  232. echo
  233. echo -e " ${RED}MTProto代理信息:${PLAIN}"
  234. echo
  235. echo -n -e " ${BLUE}当前状态:${PLAIN}"
  236. statusText
  237. echo -e " ${BLUE}IP:${PLAIN}${RED}$IP${PLAIN}"
  238. echo -e " ${BLUE}端口:${PLAIN}${RED}$MTG_PORT${PLAIN}"
  239. echo -e " ${BLUE}密钥:${PLAIN}${RED}$SECRET${PLAIN}"
  240. echo ""
  241. echo -e " 如需获取tg://proxy形式的链接,请打开telegrame关注${GREEN}@MTProxybot${PLAIN}生成"
  242. echo ""
  243. }
  244. install() {
  245. getData
  246. installDocker
  247. pullImage
  248. start
  249. firewall $MTG_PORT
  250. showInfo
  251. }
  252. update() {
  253. res=`status`
  254. if [[ $res -lt 2 ]]; then
  255. echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}"
  256. return
  257. fi
  258. pullImage
  259. stop
  260. start
  261. showInfo
  262. }
  263. uninstall() {
  264. echo ""
  265. read -p " 确定卸载MTProto?[y/n]:" answer
  266. if [[ "$answer" = "y" ]] || [[ "$answer" = "Y" ]]; then
  267. stop
  268. rm -rf $MTG_CONFIG
  269. docker system prune -af
  270. systemctl stop docker
  271. systemctl disable docker
  272. $CMD_REMOVE docker-ce docker-ce-cli containerd.io
  273. colorEcho $GREEN " 卸载成功"
  274. fi
  275. }
  276. restart() {
  277. res=`status`
  278. if [[ $res -lt 3 ]]; then
  279. echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}"
  280. return
  281. fi
  282. stop
  283. start
  284. }
  285. reconfig()
  286. {
  287. res=`status`
  288. if [[ $res -lt 2 ]]; then
  289. echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}"
  290. return
  291. fi
  292. getData
  293. stop
  294. start
  295. firewall $MTG_PORT
  296. showInfo
  297. }
  298. showLog() {
  299. res=`status`
  300. if [[ $res -lt 3 ]]; then
  301. echo -e " ${RED}MTProto未安装,请先安装!${PLAIN}"
  302. return
  303. fi
  304. set -a
  305. source "$MTG_ENV"
  306. set +a
  307. $DOCKER_CMD logs $MTG_CONTAINER | tail
  308. }
  309. menu() {
  310. clear
  311. echo "#############################################################"
  312. echo -e "# ${RED}MTProto一键安装脚本${PLAIN} #"
  313. echo "#############################################################"
  314. echo ""
  315. echo -e " ${GREEN}1.${PLAIN} 安装MTProto代理"
  316. echo -e " ${GREEN}2.${PLAIN} 更新MTProto代理"
  317. echo -e " ${GREEN}3.${PLAIN} 卸载MTProto代理"
  318. echo " -------------"
  319. echo -e " ${GREEN}4.${PLAIN} 启动MTProto代理"
  320. echo -e " ${GREEN}5.${PLAIN} 重启MTProto代理"
  321. echo -e " ${GREEN}6.${PLAIN} 停止MTProto代理"
  322. echo " -------------"
  323. echo -e " ${GREEN}7.${PLAIN} 查看MTProto信息"
  324. echo -e " ${GREEN}8.${PLAIN} 修改MTProto配置"
  325. echo -e " ${GREEN}9.${PLAIN} 查看MTProto日志"
  326. echo " -------------"
  327. echo -e " ${GREEN}0.${PLAIN} 退出"
  328. echo
  329. echo -n " 当前状态:"
  330. statusText
  331. echo
  332. read -p " 请选择操作[0-9]:" answer
  333. case $answer in
  334. 0)
  335. exit 0
  336. ;;
  337. 1)
  338. install
  339. ;;
  340. 2)
  341. update
  342. ;;
  343. 3)
  344. uninstall
  345. ;;
  346. 4)
  347. start
  348. ;;
  349. 5)
  350. restart
  351. ;;
  352. 6)
  353. stop
  354. ;;
  355. 7)
  356. showInfo
  357. ;;
  358. 8)
  359. reconfig
  360. ;;
  361. 9)
  362. showLog
  363. ;;
  364. *)
  365. echo -e " ${RED}请选择正确的操作!${PLAIN}"
  366. exit 1
  367. ;;
  368. esac
  369. }
  370. checkSystem
  371. menu