mtproxy_go.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. #!/usr/bin/env bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. #=================================================
  5. # System Required: CentOS/Debian/Ubuntu
  6. # Description: MTProxy Golang
  7. # Version: 1.0.1
  8. # Author: Toyo
  9. # Blog: https://doub.io/shell-jc9/
  10. #=================================================
  11. sh_ver="1.0.1"
  12. filepath=$(cd "$(dirname "$0")"; pwd)
  13. file_1=$(echo -e "${filepath}"|awk -F "$0" '{print $1}')
  14. file="/usr/local/mtproxy-go"
  15. mtproxy_file="/usr/local/mtproxy-go/mtg"
  16. mtproxy_conf="/usr/local/mtproxy-go/mtproxy.conf"
  17. mtproxy_log="/usr/local/mtproxy-go/mtproxy.log"
  18. Now_ver_File="/usr/local/mtproxy-go/ver.txt"
  19. Crontab_file="/usr/bin/crontab"
  20. Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
  21. Info="${Green_font_prefix}[信息]${Font_color_suffix}"
  22. Error="${Red_font_prefix}[错误]${Font_color_suffix}"
  23. Tip="${Green_font_prefix}[注意]${Font_color_suffix}"
  24. check_root(){
  25. [[ $EUID != 0 ]] && echo -e "${Error} 当前非ROOT账号(或没有ROOT权限),无法继续操作,请更换ROOT账号或使用 ${Green_background_prefix}sudo su${Font_color_suffix} 命令获取临时ROOT权限(执行后可能会提示输入当前账号的密码)。" && exit 1
  26. }
  27. #检查系统
  28. check_sys(){
  29. if [[ -f /etc/redhat-release ]]; then
  30. release="centos"
  31. elif cat /etc/issue | grep -q -E -i "debian"; then
  32. release="debian"
  33. elif cat /etc/issue | grep -q -E -i "ubuntu"; then
  34. release="ubuntu"
  35. elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
  36. release="centos"
  37. elif cat /proc/version | grep -q -E -i "debian"; then
  38. release="debian"
  39. elif cat /proc/version | grep -q -E -i "ubuntu"; then
  40. release="ubuntu"
  41. elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
  42. release="centos"
  43. fi
  44. bit=`uname -m`
  45. }
  46. check_installed_status(){
  47. [[ ! -e ${mtproxy_file} ]] && echo -e "${Error} MTProxy 没有安装,请检查 !" && exit 1
  48. }
  49. check_crontab_installed_status(){
  50. if [[ ! -e ${Crontab_file} ]]; then
  51. echo -e "${Error} Crontab 没有安装,开始安装..."
  52. if [[ ${release} == "centos" ]]; then
  53. yum install crond -y
  54. else
  55. apt-get install cron -y
  56. fi
  57. if [[ ! -e ${Crontab_file} ]]; then
  58. echo -e "${Error} Crontab 安装失败,请检查!" && exit 1
  59. else
  60. echo -e "${Info} Crontab 安装成功!"
  61. fi
  62. fi
  63. }
  64. check_pid(){
  65. PID=$(ps -ef| grep "./mtg "| grep -v "grep" | grep -v "init.d" |grep -v "service" |awk '{print $2}')
  66. }
  67. check_new_ver(){
  68. new_ver=$(wget -qO- https://api.github.com/repos/9seconds/mtg/releases| grep "tag_name"| head -n 1| awk -F ":" '{print $2}'| sed 's/\"//g;s/,//g;s/ //g')
  69. [[ -z ${new_ver} ]] && echo -e "${Error} MTProxy 最新版本获取失败!" && exit 1
  70. echo -e "${Info} 检测到 MTProxy 最新版本为 [ ${new_ver} ]"
  71. }
  72. check_ver_comparison(){
  73. now_ver=$(cat ${Now_ver_File})
  74. if [[ "${now_ver}" != "${new_ver}" ]]; then
  75. echo -e "${Info} 发现 MTProxy 已有新版本 [ ${new_ver} ],旧版本 [ ${now_ver} ]"
  76. read -e -p "是否更新 ? [Y/n] :" yn
  77. [[ -z "${yn}" ]] && yn="y"
  78. if [[ $yn == [Yy] ]]; then
  79. check_pid
  80. [[ ! -z $PID ]] && kill -9 ${PID}
  81. \cp "${mtproxy_conf}" "/tmp/mtproxy.conf"
  82. rm -rf ${file}
  83. Download
  84. mv "/tmp/mtproxy.conf" "${mtproxy_conf}"
  85. Start
  86. fi
  87. else
  88. echo -e "${Info} 当前 MTProxy 已是最新版本 [ ${new_ver} ]" && exit 1
  89. fi
  90. }
  91. Download(){
  92. if [[ ! -e "${file}" ]]; then
  93. mkdir "${file}"
  94. else
  95. [[ -e "${mtproxy_file}" ]] && rm -rf "${mtproxy_file}"
  96. fi
  97. cd "${file}"
  98. if [[ ${bit} == "x86_64" ]]; then
  99. bit="amd64"
  100. elif [[ ${bit} == "i386" || ${bit} == "i686" ]]; then
  101. bit="386"
  102. else
  103. bit="arm"
  104. fi
  105. wget --no-check-certificate -N "https://github.com/9seconds/mtg/releases/download/${new_ver}/mtg-linux-${bit}"
  106. [[ ! -e "mtg-linux-${bit}" ]] && echo -e "${Error} MTProxy 下载失败 !" && rm -rf "${file}" && exit 1
  107. mv "mtg-linux-${bit}" "mtg"
  108. [[ ! -e "mtg" ]] && echo -e "${Error} MTProxy 重命名失败 !" && rm -rf "${file}" && exit 1
  109. chmod +x mtg
  110. echo "${new_ver}" > ${Now_ver_File}
  111. }
  112. Service(){
  113. if [[ ${release} = "centos" ]]; then
  114. if ! wget --no-check-certificate "https://raw.githubusercontent.com/P3TERX/doubi_backup/master/service/mtproxy_go_centos" -O /etc/init.d/mtproxy-go; then
  115. echo -e "${Error} MTProxy服务 管理脚本下载失败 !"
  116. rm -rf "${file}"
  117. exit 1
  118. fi
  119. chmod +x "/etc/init.d/mtproxy-go"
  120. chkconfig --add mtproxy-go
  121. chkconfig mtproxy-go on
  122. else
  123. if ! wget --no-check-certificate "https://raw.githubusercontent.com/P3TERX/doubi_backup/master/service/mtproxy_go_debian" -O /etc/init.d/mtproxy-go; then
  124. echo -e "${Error} MTProxy服务 管理脚本下载失败 !"
  125. rm -rf "${file}"
  126. exit 1
  127. fi
  128. chmod +x "/etc/init.d/mtproxy-go"
  129. update-rc.d -f mtproxy-go defaults
  130. fi
  131. echo -e "${Info} MTProxy服务 管理脚本下载完成 !"
  132. }
  133. Installation_dependency(){
  134. \cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  135. }
  136. Write_config(){
  137. cat > ${mtproxy_conf}<<-EOF
  138. PORT = ${mtp_port}
  139. PASSWORD = ${mtp_passwd}
  140. TAG = ${mtp_tag}
  141. NAT-IPv4 = ${mtp_nat_ipv4}
  142. NAT-IPv6 = ${mtp_nat_ipv6}
  143. SECURE = ${mtp_secure}
  144. EOF
  145. }
  146. Read_config(){
  147. [[ ! -e ${mtproxy_conf} ]] && echo -e "${Error} MTProxy 配置文件不存在 !" && exit 1
  148. port=$(cat ${mtproxy_conf}|grep 'PORT = '|awk -F 'PORT = ' '{print $NF}')
  149. passwd=$(cat ${mtproxy_conf}|grep 'PASSWORD = '|awk -F 'PASSWORD = ' '{print $NF}')
  150. tag=$(cat ${mtproxy_conf}|grep 'TAG = '|awk -F 'TAG = ' '{print $NF}')
  151. nat_ipv4=$(cat ${mtproxy_conf}|grep 'NAT-IPv4 = '|awk -F 'NAT-IPv4 = ' '{print $NF}')
  152. nat_ipv6=$(cat ${mtproxy_conf}|grep 'NAT-IPv6 = '|awk -F 'NAT-IPv6 = ' '{print $NF}')
  153. secure=$(cat ${mtproxy_conf}|grep 'SECURE = '|awk -F 'SECURE = ' '{print $NF}')
  154. }
  155. Set_port(){
  156. while true
  157. do
  158. echo -e "请输入 MTProxy 端口 [1-65535]"
  159. read -e -p "(默认: 443):" mtp_port
  160. [[ -z "${mtp_port}" ]] && mtp_port="443"
  161. echo $((${mtp_port}+0)) &>/dev/null
  162. if [[ $? -eq 0 ]]; then
  163. if [[ ${mtp_port} -ge 1 ]] && [[ ${mtp_port} -le 65535 ]]; then
  164. echo && echo "========================"
  165. echo -e " 端口 : ${Red_background_prefix} ${mtp_port} ${Font_color_suffix}"
  166. echo "========================" && echo
  167. break
  168. else
  169. echo "输入错误, 请输入正确的端口。"
  170. fi
  171. else
  172. echo "输入错误, 请输入正确的端口。"
  173. fi
  174. done
  175. }
  176. Set_passwd(){
  177. while true
  178. do
  179. echo "请输入 MTProxy 密匙(手动输入必须为32位,[0-9][a-z][A-Z],建议随机生成)"
  180. read -e -p "(避免出错,强烈推荐随机生成,直接回车):" mtp_passwd
  181. if [[ -z "${mtp_passwd}" ]]; then
  182. mtp_passwd=$(date +%s%N | md5sum | head -c 32)
  183. else
  184. [[ ${#mtp_passwd} != 32 ]] && echo -e "${Error} 请输入正确的密匙(32位字符)。" && continue
  185. fi
  186. echo && echo "========================"
  187. echo -e " 密码 : ${Red_background_prefix} dd${mtp_passwd} ${Font_color_suffix}"
  188. echo "========================" && echo
  189. break
  190. done
  191. }
  192. Set_tag(){
  193. echo "请输入 MTProxy 的 TAG标签(TAG标签必须是32位,TAG标签只有在通过官方机器人 @MTProxybot 分享代理账号后才会获得,不清楚请留空回车)"
  194. read -e -p "(默认:回车跳过):" mtp_tag
  195. if [[ ! -z "${mtp_tag}" ]]; then
  196. echo && echo "========================"
  197. echo -e " TAG : ${Red_background_prefix} ${mtp_tag} ${Font_color_suffix}"
  198. echo "========================" && echo
  199. else
  200. echo
  201. fi
  202. }
  203. Set_nat(){
  204. echo -e "如果本机是NAT服务器(谷歌云、微软云、阿里云等,网卡绑定的IP为 10.xx.xx.xx 开头的),则需要指定公网 IPv4。"
  205. read -e -p "(默认:自动检测 IPv4 地址):" mtp_nat_ipv4
  206. if [[ -z "${mtp_nat_ipv4}" ]]; then
  207. getipv4
  208. if [[ "${ipv4}" == "IPv4_Error" ]]; then
  209. mtp_nat_ipv4=""
  210. else
  211. mtp_nat_ipv4="${ipv4}"
  212. fi
  213. echo && echo "========================"
  214. echo -e " NAT-IPv4 : ${Red_background_prefix} ${mtp_nat_ipv4} ${Font_color_suffix}"
  215. echo "========================" && echo
  216. fi
  217. echo -e "如果本机是NAT服务器(谷歌云、微软云、阿里云等),则需要指定公网 IPv6。"
  218. read -e -p "(默认:自动检测 IPv6 地址):" mtp_nat_ipv6
  219. if [[ -z "${mtp_nat_ipv6}" ]]; then
  220. getipv6
  221. if [[ "${ipv6}" == "IPv6_Error" ]]; then
  222. mtp_nat_ipv6=""
  223. else
  224. mtp_nat_ipv6="${ipv6}"
  225. fi
  226. echo && echo "========================"
  227. echo -e " NAT-IPv6 : ${Red_background_prefix} ${mtp_nat_ipv6} ${Font_color_suffix}"
  228. echo "========================" && echo
  229. fi
  230. }
  231. Set_secure(){
  232. echo -e "是否启用强制安全模式?[Y/n]
  233. 只有启用[安全混淆模式]的客户端才能链接(即密匙头部有 dd 字符),降低服务器被墙几率,建议开启。"
  234. read -e -p "(默认:Y 启用):" mtp_secure
  235. [[ -z "${mtp_secure}" ]] && mtp_secure="Y"
  236. if [[ "${mtp_secure}" == [Yy] ]]; then
  237. mtp_secure="YES"
  238. else
  239. mtp_secure="NO"
  240. fi
  241. echo && echo "========================"
  242. echo -e " 强制安全模式 : ${Red_background_prefix} ${mtp_secure} ${Font_color_suffix}"
  243. echo "========================" && echo
  244. }
  245. Set(){
  246. check_installed_status
  247. echo && echo -e "你要做什么?
  248. ${Green_font_prefix}1.${Font_color_suffix} 修改 端口配置
  249. ${Green_font_prefix}2.${Font_color_suffix} 修改 密码配置
  250. ${Green_font_prefix}3.${Font_color_suffix} 修改 TAG 配置
  251. ${Green_font_prefix}4.${Font_color_suffix} 修改 NAT 配置
  252. ${Green_font_prefix}5.${Font_color_suffix} 修改 强制安全模式 配置
  253. ${Green_font_prefix}6.${Font_color_suffix} 修改 全部配置
  254. ————————————————
  255. ${Green_font_prefix}7.${Font_color_suffix} 监控 运行状态
  256. ${Green_font_prefix}8.${Font_color_suffix} 监控 外网IP变更" && echo
  257. read -e -p "(默认: 取消):" mtp_modify
  258. [[ -z "${mtp_modify}" ]] && echo "已取消..." && exit 1
  259. if [[ "${mtp_modify}" == "1" ]]; then
  260. Read_config
  261. Set_port
  262. mtp_passwd=${passwd}
  263. mtp_tag=${tag}
  264. mtp_nat_ipv4=${nat_ipv4}
  265. mtp_nat_ipv6=${nat_ipv6}
  266. mtp_secure=${secure}
  267. Write_config
  268. Del_iptables
  269. Add_iptables
  270. Restart
  271. elif [[ "${mtp_modify}" == "2" ]]; then
  272. Read_config
  273. Set_passwd
  274. mtp_port=${port}
  275. mtp_tag=${tag}
  276. mtp_nat_ipv4=${nat_ipv4}
  277. mtp_nat_ipv6=${nat_ipv6}
  278. mtp_secure=${secure}
  279. Write_config
  280. Restart
  281. elif [[ "${mtp_modify}" == "3" ]]; then
  282. Read_config
  283. Set_tag
  284. mtp_port=${port}
  285. mtp_passwd=${passwd}
  286. mtp_nat_ipv4=${nat_ipv4}
  287. mtp_nat_ipv6=${nat_ipv6}
  288. mtp_secure=${secure}
  289. Write_config
  290. Restart
  291. elif [[ "${mtp_modify}" == "4" ]]; then
  292. Read_config
  293. Set_nat
  294. mtp_port=${port}
  295. mtp_passwd=${passwd}
  296. mtp_tag=${tag}
  297. mtp_secure=${secure}
  298. Write_config
  299. Restart
  300. elif [[ "${mtp_modify}" == "5" ]]; then
  301. Read_config
  302. Set_secure
  303. mtp_port=${port}
  304. mtp_passwd=${passwd}
  305. mtp_tag=${tag}
  306. mtp_nat_ipv4=${nat_ipv4}
  307. mtp_nat_ipv6=${nat_ipv6}
  308. Write_config
  309. Restart
  310. elif [[ "${mtp_modify}" == "6" ]]; then
  311. Read_config
  312. Set_port
  313. Set_passwd
  314. Set_tag
  315. Set_nat
  316. Set_secure
  317. Write_config
  318. Restart
  319. elif [[ "${mtp_modify}" == "7" ]]; then
  320. Set_crontab_monitor
  321. elif [[ "${mtp_modify}" == "8" ]]; then
  322. Set_crontab_monitorip
  323. else
  324. echo -e "${Error} 请输入正确的数字(1-8)" && exit 1
  325. fi
  326. }
  327. Install(){
  328. check_root
  329. [[ -e ${mtproxy_file} ]] && echo -e "${Error} 检测到 MTProxy 已安装 !" && exit 1
  330. echo -e "${Info} 开始设置 用户配置..."
  331. Set_port
  332. Set_passwd
  333. Set_tag
  334. Set_nat
  335. Set_secure
  336. echo -e "${Info} 开始安装/配置 依赖..."
  337. Installation_dependency
  338. echo -e "${Info} 开始下载/安装..."
  339. check_new_ver
  340. Download
  341. echo -e "${Info} 开始下载/安装 服务脚本(init)..."
  342. Service
  343. echo -e "${Info} 开始写入 配置文件..."
  344. Write_config
  345. echo -e "${Info} 开始设置 iptables防火墙..."
  346. Set_iptables
  347. echo -e "${Info} 开始添加 iptables防火墙规则..."
  348. Add_iptables
  349. echo -e "${Info} 开始保存 iptables防火墙规则..."
  350. Save_iptables
  351. echo -e "${Info} 所有步骤 安装完毕,开始启动..."
  352. Start
  353. }
  354. Start(){
  355. check_installed_status
  356. check_pid
  357. [[ ! -z ${PID} ]] && echo -e "${Error} MTProxy 正在运行,请检查 !" && exit 1
  358. /etc/init.d/mtproxy-go start
  359. sleep 1s
  360. check_pid
  361. [[ ! -z ${PID} ]] && View
  362. }
  363. Stop(){
  364. check_installed_status
  365. check_pid
  366. [[ -z ${PID} ]] && echo -e "${Error} MTProxy 没有运行,请检查 !" && exit 1
  367. /etc/init.d/mtproxy-go stop
  368. }
  369. Restart(){
  370. check_installed_status
  371. check_pid
  372. [[ ! -z ${PID} ]] && /etc/init.d/mtproxy-go stop
  373. /etc/init.d/mtproxy-go start
  374. sleep 1s
  375. check_pid
  376. [[ ! -z ${PID} ]] && View
  377. }
  378. Update(){
  379. check_installed_status
  380. check_new_ver
  381. check_ver_comparison
  382. }
  383. Uninstall(){
  384. check_installed_status
  385. echo "确定要卸载 MTProxy ? (y/N)"
  386. echo
  387. read -e -p "(默认: n):" unyn
  388. [[ -z ${unyn} ]] && unyn="n"
  389. if [[ ${unyn} == [Yy] ]]; then
  390. check_pid
  391. [[ ! -z $PID ]] && kill -9 ${PID}
  392. if [[ -e ${mtproxy_conf} ]]; then
  393. port=$(cat ${mtproxy_conf}|grep 'PORT = '|awk -F 'PORT = ' '{print $NF}')
  394. Del_iptables
  395. Save_iptables
  396. fi
  397. if [[ ! -z $(crontab -l | grep "mtproxy_go.sh monitor") ]]; then
  398. crontab_monitor_cron_stop
  399. fi
  400. rm -rf "${file}"
  401. if [[ ${release} = "centos" ]]; then
  402. chkconfig --del mtproxy-go
  403. else
  404. update-rc.d -f mtproxy-go remove
  405. fi
  406. rm -rf "/etc/init.d/mtproxy-go"
  407. echo && echo "MTProxy 卸载完成 !" && echo
  408. else
  409. echo && echo "卸载已取消..." && echo
  410. fi
  411. }
  412. getipv4(){
  413. ipv4=$(wget -qO- -4 -t1 -T2 ipinfo.io/ip)
  414. if [[ -z "${ipv4}" ]]; then
  415. ipv4=$(wget -qO- -4 -t1 -T2 api.ip.sb/ip)
  416. if [[ -z "${ipv4}" ]]; then
  417. ipv4=$(wget -qO- -4 -t1 -T2 members.3322.org/dyndns/getip)
  418. if [[ -z "${ipv4}" ]]; then
  419. ipv4="IPv4_Error"
  420. fi
  421. fi
  422. fi
  423. }
  424. getipv6(){
  425. ipv6=$(wget -qO- -6 -t1 -T3 ifconfig.co)
  426. if [[ -z "${ipv6}" ]]; then
  427. ipv6="IPv6_Error"
  428. fi
  429. }
  430. View(){
  431. check_installed_status
  432. Read_config
  433. #getipv4
  434. #getipv6
  435. clear && echo
  436. echo -e "Mtproto Proxy 用户配置:"
  437. echo -e "————————————————"
  438. echo -e " 地址\t: ${Green_font_prefix}${nat_ipv4}${Font_color_suffix}"
  439. [[ ! -z "${nat_ipv6}" ]] && echo -e " 地址\t: ${Green_font_prefix}${nat_ipv6}${Font_color_suffix}"
  440. echo -e " 端口\t: ${Green_font_prefix}${port}${Font_color_suffix}"
  441. echo -e " 密匙\t: ${Green_font_prefix}dd${passwd}${Font_color_suffix}"
  442. [[ ! -z "${tag}" ]] && echo -e " TAG \t: ${Green_font_prefix}${tag}${Font_color_suffix}"
  443. echo -e " 链接\t: ${Red_font_prefix}tg://proxy?server=${nat_ipv4}&port=${port}&secret=dd${passwd}${Font_color_suffix}"
  444. echo -e " 链接\t: ${Red_font_prefix}https://t.me/proxy?server=${nat_ipv4}&port=${port}&secret=dd${passwd}${Font_color_suffix}"
  445. [[ ! -z "${nat_ipv6}" ]] && echo -e " 链接\t: ${Red_font_prefix}tg://proxy?server=${nat_ipv6}&port=${port}&secret=dd${passwd}${Font_color_suffix}"
  446. [[ ! -z "${nat_ipv6}" ]] && echo -e " 链接\t: ${Red_font_prefix}https://t.me/proxy?server=${nat_ipv6}&port=${port}&secret=dd${passwd}${Font_color_suffix}"
  447. echo
  448. echo -e " 强制安全模式\t: ${Green_font_prefix}${secure}${Font_color_suffix}"
  449. echo
  450. echo -e " ${Red_font_prefix}注意\t:${Font_color_suffix} 密匙头部的 ${Green_font_prefix}dd${Font_color_suffix} 字符是代表客户端启用${Green_font_prefix}安全混淆模式${Font_color_suffix},可以降低服务器被墙几率。\n \t 另外,在官方机器人处分享账号获取TAG标签时记得删除,获取TAG标签后分享时可以再加上。"
  451. }
  452. View_Log(){
  453. check_installed_status
  454. [[ ! -e ${mtproxy_log} ]] && echo -e "${Error} MTProxy 日志文件不存在 !" && exit 1
  455. echo && echo -e "${Tip} 按 ${Red_font_prefix}Ctrl+C${Font_color_suffix} 终止查看日志" && echo -e "如果需要查看完整日志内容,请用 ${Red_font_prefix}cat ${mtproxy_log}${Font_color_suffix} 命令。" && echo
  456. tail -f ${mtproxy_log}
  457. }
  458. # 显示 连接信息
  459. debian_View_user_connection_info(){
  460. format_1=$1
  461. Read_config
  462. user_IP=$(ss state connected sport = :${port} -tn|sed '1d'|awk '{print $NF}'|awk -F ':' '{print $(NF-1)}'|sort -u)
  463. if [[ -z ${user_IP} ]]; then
  464. user_IP_total="0"
  465. echo -e "端口: ${Green_font_prefix}"${port}"${Font_color_suffix}\t 链接IP总数: ${Green_font_prefix}"${user_IP_total}"${Font_color_suffix}\t 当前链接IP: "
  466. else
  467. user_IP_total=$(echo -e "${user_IP}"|wc -l)
  468. if [[ ${format_1} == "IP_address" ]]; then
  469. echo -e "端口: ${Green_font_prefix}"${port}"${Font_color_suffix}\t 链接IP总数: ${Green_font_prefix}"${user_IP_total}"${Font_color_suffix}\t 当前链接IP: "
  470. get_IP_address
  471. echo
  472. else
  473. user_IP=$(echo -e "\n${user_IP}")
  474. echo -e "端口: ${Green_font_prefix}"${user_port}"${Font_color_suffix}\t 链接IP总数: ${Green_font_prefix}"${user_IP_total}"${Font_color_suffix}\t 当前链接IP: ${Green_font_prefix}${user_IP}${Font_color_suffix}\n"
  475. fi
  476. fi
  477. user_IP=""
  478. }
  479. View_user_connection_info(){
  480. check_installed_status
  481. echo && echo -e "请选择要显示的格式:
  482. ${Green_font_prefix}1.${Font_color_suffix} 显示 IP 格式
  483. ${Green_font_prefix}2.${Font_color_suffix} 显示 IP+IP归属地 格式" && echo
  484. read -e -p "(默认: 1):" mtproxy_connection_info
  485. [[ -z "${mtproxy_connection_info}" ]] && mtproxy_connection_info="1"
  486. if [[ "${mtproxy_connection_info}" == "1" ]]; then
  487. View_user_connection_info_1 ""
  488. elif [[ "${mtproxy_connection_info}" == "2" ]]; then
  489. echo -e "${Tip} 检测IP归属地(ipip.net),如果IP较多,可能时间会比较长..."
  490. View_user_connection_info_1 "IP_address"
  491. else
  492. echo -e "${Error} 请输入正确的数字(1-2)" && exit 1
  493. fi
  494. }
  495. View_user_connection_info_1(){
  496. format=$1
  497. debian_View_user_connection_info "$format"
  498. }
  499. get_IP_address(){
  500. if [[ ! -z ${user_IP} ]]; then
  501. for((integer_1 = ${user_IP_total}; integer_1 >= 1; integer_1--))
  502. do
  503. IP=$(echo "${user_IP}" |sed -n "$integer_1"p)
  504. IP_address=$(wget -qO- -t1 -T2 http://freeapi.ipip.net/${IP}|sed 's/\"//g;s/,//g;s/\[//g;s/\]//g')
  505. echo -e "${Green_font_prefix}${IP}${Font_color_suffix} (${IP_address})"
  506. sleep 1s
  507. done
  508. fi
  509. }
  510. Set_crontab_monitor(){
  511. check_crontab_installed_status
  512. crontab_monitor_status=$(crontab -l|grep "mtproxy_go.sh monitor")
  513. if [[ -z "${crontab_monitor_status}" ]]; then
  514. echo && echo -e "当前监控运行状态模式: ${Red_font_prefix}未开启${Font_color_suffix}" && echo
  515. echo -e "确定要开启 ${Green_font_prefix}MTProxy 服务端运行状态监控${Font_color_suffix} 功能吗?(当进程关闭则自动启动 MTProxy 服务端)[Y/n]"
  516. read -e -p "(默认: y):" crontab_monitor_status_ny
  517. [[ -z "${crontab_monitor_status_ny}" ]] && crontab_monitor_status_ny="y"
  518. if [[ ${crontab_monitor_status_ny} == [Yy] ]]; then
  519. crontab_monitor_cron_start
  520. else
  521. echo && echo " 已取消..." && echo
  522. fi
  523. else
  524. echo && echo -e "当前监控运行状态模式: ${Green_font_prefix}已开启${Font_color_suffix}" && echo
  525. echo -e "确定要关闭 ${Red_font_prefix}MTProxy 服务端运行状态监控${Font_color_suffix} 功能吗?(当进程关闭则自动启动 MTProxy 服务端)[y/N]"
  526. read -e -p "(默认: n):" crontab_monitor_status_ny
  527. [[ -z "${crontab_monitor_status_ny}" ]] && crontab_monitor_status_ny="n"
  528. if [[ ${crontab_monitor_status_ny} == [Yy] ]]; then
  529. crontab_monitor_cron_stop
  530. else
  531. echo && echo " 已取消..." && echo
  532. fi
  533. fi
  534. }
  535. crontab_monitor_cron_start(){
  536. crontab -l > "$file_1/crontab.bak"
  537. sed -i "/mtproxy_go.sh monitor/d" "$file_1/crontab.bak"
  538. echo -e "\n* * * * * /bin/bash $file_1/mtproxy_go.sh monitor" >> "$file_1/crontab.bak"
  539. crontab "$file_1/crontab.bak"
  540. rm -r "$file_1/crontab.bak"
  541. cron_config=$(crontab -l | grep "mtproxy_go.sh monitor")
  542. if [[ -z ${cron_config} ]]; then
  543. echo -e "${Error} MTProxy 服务端运行状态监控功能 启动失败 !" && exit 1
  544. else
  545. echo -e "${Info} MTProxy 服务端运行状态监控功能 启动成功 !"
  546. fi
  547. }
  548. crontab_monitor_cron_stop(){
  549. crontab -l > "$file_1/crontab.bak"
  550. sed -i "/mtproxy_go.sh monitor/d" "$file_1/crontab.bak"
  551. crontab "$file_1/crontab.bak"
  552. rm -r "$file_1/crontab.bak"
  553. cron_config=$(crontab -l | grep "mtproxy_go.sh monitor")
  554. if [[ ! -z ${cron_config} ]]; then
  555. echo -e "${Error} MTProxy 服务端运行状态监控功能 停止失败 !" && exit 1
  556. else
  557. echo -e "${Info} MTProxy 服务端运行状态监控功能 停止成功 !"
  558. fi
  559. }
  560. crontab_monitor(){
  561. check_installed_status
  562. check_pid
  563. #echo "${PID}"
  564. if [[ -z ${PID} ]]; then
  565. echo -e "${Error} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] 检测到 MTProxy服务端 未运行 , 开始启动..." | tee -a ${mtproxy_log}
  566. /etc/init.d/mtproxy-go start
  567. sleep 1s
  568. check_pid
  569. if [[ -z ${PID} ]]; then
  570. echo -e "${Error} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] MTProxy服务端 启动失败..." | tee -a ${mtproxy_log}
  571. else
  572. echo -e "${Info} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] MTProxy服务端 启动成功..." | tee -a ${mtproxy_log}
  573. fi
  574. else
  575. echo -e "${Info} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] MTProxy服务端 进程运行正常..." | tee -a ${mtproxy_log}
  576. fi
  577. }
  578. Set_crontab_monitorip(){
  579. check_crontab_installed_status
  580. crontab_monitor_status=$(crontab -l|grep "mtproxy_go.sh monitorip")
  581. if [[ -z "${crontab_monitor_status}" ]]; then
  582. echo && echo -e "当前监控外网IP模式: ${Red_font_prefix}未开启${Font_color_suffix}" && echo
  583. echo -e "确定要开启 ${Green_font_prefix}服务器外网IP变更监控${Font_color_suffix} 功能吗?(当服务器外网IP变化后,自动重新配置并重启服务端)[Y/n]"
  584. read -e -p "(默认: y):" crontab_monitor_status_ny
  585. [[ -z "${crontab_monitor_status_ny}" ]] && crontab_monitor_status_ny="y"
  586. if [[ ${crontab_monitor_status_ny} == [Yy] ]]; then
  587. crontab_monitor_cron_start2
  588. else
  589. echo && echo " 已取消..." && echo
  590. fi
  591. else
  592. echo && echo -e "当前监控外网IP模式: ${Green_font_prefix}已开启${Font_color_suffix}" && echo
  593. echo -e "确定要关闭 ${Red_font_prefix}服务器外网IP变更监控${Font_color_suffix} 功能吗?(当服务器外网IP变化后,自动重新配置并重启服务端)[Y/n]"
  594. read -e -p "(默认: n):" crontab_monitor_status_ny
  595. [[ -z "${crontab_monitor_status_ny}" ]] && crontab_monitor_status_ny="n"
  596. if [[ ${crontab_monitor_status_ny} == [Yy] ]]; then
  597. crontab_monitor_cron_stop2
  598. else
  599. echo && echo " 已取消..." && echo
  600. fi
  601. fi
  602. }
  603. crontab_monitor_cron_start2(){
  604. crontab -l > "$file_1/crontab.bak"
  605. sed -i "/mtproxy_go.sh monitorip/d" "$file_1/crontab.bak"
  606. echo -e "\n* * * * * /bin/bash $file_1/mtproxy_go.sh monitorip" >> "$file_1/crontab.bak"
  607. crontab "$file_1/crontab.bak"
  608. rm -r "$file_1/crontab.bak"
  609. cron_config=$(crontab -l | grep "mtproxy_go.sh monitorip")
  610. if [[ -z ${cron_config} ]]; then
  611. echo -e "${Error} 服务器外网IP变更监控功能 启动失败 !" && exit 1
  612. else
  613. echo -e "${Info} 服务器外网IP变更监控功能 启动成功 !"
  614. fi
  615. }
  616. crontab_monitor_cron_stop2(){
  617. crontab -l > "$file_1/crontab.bak"
  618. sed -i "/mtproxy_go.sh monitorip/d" "$file_1/crontab.bak"
  619. crontab "$file_1/crontab.bak"
  620. rm -r "$file_1/crontab.bak"
  621. cron_config=$(crontab -l | grep "mtproxy_go.sh monitorip")
  622. if [[ ! -z ${cron_config} ]]; then
  623. echo -e "${Error} 服务器外网IP变更监控功能 停止失败 !" && exit 1
  624. else
  625. echo -e "${Info} 服务器外网IP变更监控功能 停止成功 !"
  626. fi
  627. }
  628. crontab_monitorip(){
  629. check_installed_status
  630. Read_config
  631. getipv4
  632. getipv6
  633. monitorip_yn="NO"
  634. if [[ "${ipv4}" != "IPv4_Error" ]]; then
  635. if [[ "${ipv4}" != "${nat_ipv4}" ]]; then
  636. echo -e "${Info} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] 检测到 服务器外网IPv4变更[旧: ${nat_ipv4},新: ${ipv4}], 开始重新配置并准备重启服务端..." | tee -a ${mtproxy_log}
  637. monitorip_yn="YES"
  638. mtp_nat_ipv4=${ipv4}
  639. fi
  640. else
  641. echo -e "${Error} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] 服务器外网IPv4获取失败..." | tee -a ${mtproxy_log}
  642. mtp_nat_ipv4=${nat_ipv4}
  643. fi
  644. if [[ "${ipv6}" != "IPv6_Error" ]]; then
  645. if [[ "${ipv6}" != "${nat_ipv6}" ]]; then
  646. echo -e "${Info} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] 检测到 服务器外网IPv6变更[旧: ${nat_ipv6},新: ${ipv6}], 开始重新配置并准备重启服务端..." | tee -a ${mtproxy_log}
  647. monitorip_yn="YES"
  648. mtp_nat_ipv6=${ipv6}
  649. fi
  650. else
  651. echo -e "${Error} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] 服务器外网IPv6获取失败..." | tee -a ${mtproxy_log}
  652. mtp_nat_ipv6=${nat_ipv6}
  653. fi
  654. if [[ ${monitorip_yn} == "YES" ]]; then
  655. mtp_port=${port}
  656. mtp_passwd=${passwd}
  657. mtp_tag=${tag}
  658. mtp_secure=${secure}
  659. Write_config
  660. Restart
  661. fi
  662. }
  663. Add_iptables(){
  664. iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${mtp_port} -j ACCEPT
  665. ip6tables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${mtp_port} -j ACCEPT
  666. }
  667. Del_iptables(){
  668. iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport ${port} -j ACCEPT
  669. ip6tables -D INPUT -m state --state NEW -m tcp -p tcp --dport ${port} -j ACCEPT
  670. }
  671. Save_iptables(){
  672. if [[ ${release} == "centos" ]]; then
  673. service iptables save
  674. service ip6tables save
  675. else
  676. iptables-save > /etc/iptables.up.rules
  677. ip6tables-save > /etc/ip6tables.up.rules
  678. fi
  679. }
  680. Set_iptables(){
  681. if [[ ${release} == "centos" ]]; then
  682. service iptables save
  683. service ip6tables save
  684. chkconfig --level 2345 iptables on
  685. chkconfig --level 2345 ip6tables on
  686. else
  687. iptables-save > /etc/iptables.up.rules
  688. ip6tables-save > /etc/ip6tables.up.rules
  689. echo -e '#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules\n/sbin/ip6tables-restore < /etc/ip6tables.up.rules' > /etc/network/if-pre-up.d/iptables
  690. chmod +x /etc/network/if-pre-up.d/iptables
  691. fi
  692. }
  693. Update_Shell(){
  694. sh_new_ver=$(wget --no-check-certificate -qO- -t1 -T3 "https://raw.githubusercontent.com/P3TERX/doubi_backup/master/mtproxy_go.sh"|grep 'sh_ver="'|awk -F "=" '{print $NF}'|sed 's/\"//g'|head -1) && sh_new_type="github"
  695. [[ -z ${sh_new_ver} ]] && echo -e "${Error} 无法链接到 Github !" && exit 0
  696. if [[ -e "/etc/init.d/mtproxy-go" ]]; then
  697. rm -rf /etc/init.d/mtproxy-go
  698. Service
  699. fi
  700. wget -N --no-check-certificate "https://raw.githubusercontent.com/P3TERX/doubi_backup/master/mtproxy_go.sh" && chmod +x mtproxy_go.sh
  701. echo -e "脚本已更新为最新版本[ ${sh_new_ver} ] !(注意:因为更新方式为直接覆盖当前运行的脚本,所以可能下面会提示一些报错,无视即可)" && exit 0
  702. }
  703. check_sys
  704. action=$1
  705. if [[ "${action}" == "monitor" ]]; then
  706. crontab_monitor
  707. elif [[ "${action}" == "monitorip" ]]; then
  708. crontab_monitorip
  709. else
  710. echo && echo -e " MTProxy-Go 一键管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
  711. ---- Toyo | doub.io/shell-jc9 ----
  712. ${Green_font_prefix} 0.${Font_color_suffix} 升级脚本
  713. ————————————
  714. ${Green_font_prefix} 1.${Font_color_suffix} 安装 MTProxy
  715. ${Green_font_prefix} 2.${Font_color_suffix} 更新 MTProxy
  716. ${Green_font_prefix} 3.${Font_color_suffix} 卸载 MTProxy
  717. ————————————
  718. ${Green_font_prefix} 4.${Font_color_suffix} 启动 MTProxy
  719. ${Green_font_prefix} 5.${Font_color_suffix} 停止 MTProxy
  720. ${Green_font_prefix} 6.${Font_color_suffix} 重启 MTProxy
  721. ————————————
  722. ${Green_font_prefix} 7.${Font_color_suffix} 设置 账号配置
  723. ${Green_font_prefix} 8.${Font_color_suffix} 查看 账号信息
  724. ${Green_font_prefix} 9.${Font_color_suffix} 查看 日志信息
  725. ${Green_font_prefix}10.${Font_color_suffix} 查看 链接信息
  726. ————————————" && echo
  727. if [[ -e ${mtproxy_file} ]]; then
  728. check_pid
  729. if [[ ! -z "${PID}" ]]; then
  730. echo -e " 当前状态: ${Green_font_prefix}已安装${Font_color_suffix} 并 ${Green_font_prefix}已启动${Font_color_suffix}"
  731. else
  732. echo -e " 当前状态: ${Green_font_prefix}已安装${Font_color_suffix} 但 ${Red_font_prefix}未启动${Font_color_suffix}"
  733. fi
  734. else
  735. echo -e " 当前状态: ${Red_font_prefix}未安装${Font_color_suffix}"
  736. fi
  737. echo
  738. read -e -p " 请输入数字 [0-10]:" num
  739. case "$num" in
  740. 0)
  741. Update_Shell
  742. ;;
  743. 1)
  744. Install
  745. ;;
  746. 2)
  747. Update
  748. ;;
  749. 3)
  750. Uninstall
  751. ;;
  752. 4)
  753. Start
  754. ;;
  755. 5)
  756. Stop
  757. ;;
  758. 6)
  759. Restart
  760. ;;
  761. 7)
  762. Set
  763. ;;
  764. 8)
  765. View
  766. ;;
  767. 9)
  768. View_Log
  769. ;;
  770. 10)
  771. View_user_connection_info
  772. ;;
  773. *)
  774. echo "请输入正确数字 [0-10]"
  775. ;;
  776. esac
  777. fi