frps_linux_install.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. # fonts color
  5. Green="\033[32m"
  6. Red="\033[31m"
  7. Yellow="\033[33m"
  8. GreenBG="\033[42;37m"
  9. RedBG="\033[41;37m"
  10. Font="\033[0m"
  11. # fonts color
  12. # variable
  13. FRP_VERSION=0.51.3
  14. REPO=stilleshan/frps
  15. WORK_PATH=$(dirname $(readlink -f $0))
  16. FRP_NAME=frps
  17. FRP_PATH=/usr/local/frp
  18. PROXY_URL="https://ghproxy.com/"
  19. # check frps
  20. if [ -f "/usr/local/frp/${FRP_NAME}" ] || [ -f "/usr/local/frp/${FRP_NAME}.ini" ] || [ -f "/lib/systemd/system/${FRP_NAME}.service" ];then
  21. echo -e "${Green}=========================================================================${Font}"
  22. echo -e "${RedBG}当前已退出脚本.${Font}"
  23. echo -e "${Green}检查到服务器已安装${Font} ${Red}${FRP_NAME}${Font}"
  24. echo -e "${Green}请手动确认和删除${Font} ${Red}/usr/local/frp/${Font} ${Green}目录下的${Font} ${Red}${FRP_NAME}${Font} ${Green}和${Font} ${Red}/${FRP_NAME}.ini${Font} ${Green}文件以及${Font} ${Red}/lib/systemd/system/${FRP_NAME}.service${Font} ${Green}文件,再次执行本脚本.${Font}"
  25. echo -e "${Green}参考命令如下:${Font}"
  26. echo -e "${Red}rm -rf /usr/local/frp/${FRP_NAME}${Font}"
  27. echo -e "${Red}rm -rf /usr/local/frp/${FRP_NAME}.ini${Font}"
  28. echo -e "${Red}rm -rf /lib/systemd/system/${FRP_NAME}.service${Font}"
  29. echo -e "${Green}=========================================================================${Font}"
  30. exit 2
  31. fi
  32. while ! test -z "$(ps -A | grep -w ${FRP_NAME})"; do
  33. FRPSPID=$(ps -A | grep -w ${FRP_NAME} | awk 'NR==1 {print $1}')
  34. kill -9 $FRPSPID
  35. done
  36. # check pkg
  37. if type apt-get >/dev/null 2>&1 ; then
  38. if ! type wget >/dev/null 2>&1 ; then
  39. apt-get install wget -y
  40. fi
  41. if ! type curl >/dev/null 2>&1 ; then
  42. apt-get install curl -y
  43. fi
  44. fi
  45. if type yum >/dev/null 2>&1 ; then
  46. if ! type wget >/dev/null 2>&1 ; then
  47. yum install wget -y
  48. fi
  49. if ! type curl >/dev/null 2>&1 ; then
  50. yum install curl -y
  51. fi
  52. fi
  53. # check network
  54. GOOGLE_HTTP_CODE=$(curl -o /dev/null --connect-timeout 5 --max-time 8 -s --head -w "%{http_code}" "https://www.google.com")
  55. PROXY_HTTP_CODE=$(curl -o /dev/null --connect-timeout 5 --max-time 8 -s --head -w "%{http_code}" "${PROXY_URL}")
  56. # check arch
  57. if [ $(uname -m) = "x86_64" ]; then
  58. PLATFORM=amd64
  59. fi
  60. if [ $(uname -m) = "aarch64" ]; then
  61. PLATFORM=arm64
  62. fi
  63. FILE_NAME=frp_${FRP_VERSION}_linux_${PLATFORM}
  64. # download
  65. if [ $GOOGLE_HTTP_CODE == "200" ]; then
  66. wget -P ${WORK_PATH} https://github.com/fatedier/frp/releases/download/v${FRP_VERSION}/${FILE_NAME}.tar.gz -O ${FILE_NAME}.tar.gz
  67. wget -P ${WORK_PATH} https://raw.githubusercontent.com/${REPO}/master/${FRP_NAME}.ini -O ${FRP_NAME}.ini
  68. else
  69. if [ $PROXY_HTTP_CODE == "200" ]; then
  70. wget -P ${WORK_PATH} ${PROXY_URL}https://github.com/fatedier/frp/releases/download/v${FRP_VERSION}/${FILE_NAME}.tar.gz -O ${FILE_NAME}.tar.gz
  71. wget -P ${WORK_PATH} ${PROXY_URL}https://raw.githubusercontent.com/${REPO}/master/${FRP_NAME}.ini -O ${FRP_NAME}.ini
  72. else
  73. echo -e "${Red}检测 GitHub Proxy 代理失效 开始使用官方地址下载${Font}"
  74. wget -P ${WORK_PATH} https://github.com/fatedier/frp/releases/download/v${FRP_VERSION}/${FILE_NAME}.tar.gz -O ${FILE_NAME}.tar.gz
  75. wget -P ${WORK_PATH} https://raw.githubusercontent.com/${REPO}/master/${FRP_NAME}.ini -O ${FRP_NAME}.ini
  76. fi
  77. fi
  78. tar -zxvf ${FILE_NAME}.tar.gz
  79. mkdir -p ${FRP_PATH}
  80. mv ${FILE_NAME}/${FRP_NAME} ${FRP_PATH}
  81. mv ${FRP_NAME}.ini ${FRP_PATH}
  82. # 配置 frps.service
  83. cat >/lib/systemd/system/frps.service <<'EOF'
  84. [Unit]
  85. Description=Frp Server Service
  86. After=network.target syslog.target
  87. Wants=network.target
  88. [Service]
  89. Type=simple
  90. Restart=on-failure
  91. RestartSec=5s
  92. ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.ini
  93. [Install]
  94. WantedBy=multi-user.target
  95. EOF
  96. systemctl daemon-reload
  97. sudo systemctl start ${FRP_NAME}
  98. sudo systemctl enable ${FRP_NAME}
  99. # clean
  100. rm -rf ${WORK_PATH}/${FILE_NAME}.tar.gz ${WORK_PATH}/${FILE_NAME} ${FRP_NAME}_linux_install.sh
  101. echo -e "${Green}====================================================================${Font}"
  102. echo -e "${Green}安装成功,请先修改 ${FRP_NAME}.ini 文件,确保格式及配置正确无误!${Font}"
  103. echo -e "${Red}vi /usr/local/frp/${FRP_NAME}.ini${Font}"
  104. echo -e "${Green}修改完毕后执行以下命令重启服务:${Font}"
  105. echo -e "${Red}sudo systemctl restart ${FRP_NAME}${Font}"
  106. echo -e "${Green}====================================================================${Font}"