wg5clients.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/bin/bash
  2. # WireGuard VPN多用户服务端 自动配置脚本
  3. # 本脚本(WireGuard 多用户配置)一键安装短网址
  4. # wget -qO- https://git.io/fpnQt | bash
  5. #############################################################
  6. help_info()
  7. {
  8. cat <<EOF
  9. # 一键安装wireguard 脚本 Debian 9 (源:逗比网安装笔记)
  10. wget -qO- git.io/fptwc | bash
  11. # 一键安装wireguard 脚本 Ubuntu (源:逗比网安装笔记)
  12. wget -qO- git.io/fpcnL | bash
  13. # CentOS 7 一键脚本安装WireGuard (1.先升级内核-重启)
  14. wget -qO wg.sh git.io/fhnhS && bash wg.sh kernel
  15. bash wg.sh # 2.重启后安装
  16. EOF
  17. }
  18. #############################################################
  19. #定义文字颜色
  20. Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m" && Font="\033[0m"
  21. #定义提示信息
  22. Info="${Green}[信息]${Font}" && OK="${Green}[OK]${Font}" && Error="${Red}[错误]${Font}"
  23. # 检查是否安装 WireGuard
  24. if [ ! -f '/usr/bin/wg' ]; then
  25. clear
  26. echo -e "${RedBG} 一键安装 WireGuard 脚本 For Debian_9 Ubuntu Centos_7 ${Font}"
  27. echo -e "${GreenBG} 开源项目:https://github.com/hongwenjun/vps_setup ${Font}"
  28. help_info
  29. echo -e "${Red}:: 检测到你的vps没有安装wireguard,请选择复制一键脚本安装 ${Font}"
  30. exit 1
  31. fi
  32. #############################################################
  33. # 定义修改端口号,适合已经安装WireGuard而不想改端口
  34. #生成随机端口
  35. rand(){
  36. min=$1
  37. max=$(($2-$min+1))
  38. num=$(cat /dev/urandom | head -n 10 | cksum | awk -F ' ' '{print $1}')
  39. echo $(($num%$max+$min))
  40. }
  41. port=$(rand 1000 60000)
  42. mtu=1420
  43. host=$(hostname -s)
  44. ip_list=(2 5 8 178 186 118 158 198 168 9)
  45. # 获得服务器ip,自动获取
  46. if [ ! -f '/usr/bin/curl' ]; then
  47. apt update && apt install -y curl
  48. fi
  49. serverip=$(curl -4 ip.sb)
  50. # 安装二维码插件
  51. if [ ! -f '/usr/bin/qrencode' ]; then
  52. apt -y install qrencode
  53. fi
  54. # 安装 bash wgmtu 脚本用来设置服务器
  55. wget -O ~/wgmtu https://raw.githubusercontent.com/hongwenjun/vps_setup/master/Wireguard/wgmtu.sh
  56. #############################################################
  57. # wg配置文件目录 /etc/wireguard
  58. mkdir -p /etc/wireguard
  59. chmod 777 -R /etc/wireguard
  60. cd /etc/wireguard
  61. # 然后开始生成 密匙对(公匙+私匙)。
  62. wg genkey | tee sprivatekey | wg pubkey > spublickey
  63. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  64. # 生成服务端配置文件
  65. cat <<EOF >wg0.conf
  66. [Interface]
  67. PrivateKey = $(cat sprivatekey)
  68. Address = 10.0.0.1/24
  69. PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  70. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
  71. ListenPort = $port
  72. DNS = 8.8.8.8
  73. MTU = $mtu
  74. [Peer]
  75. PublicKey = $(cat cpublickey)
  76. AllowedIPs = 10.0.0.188/32
  77. EOF
  78. # 生成简洁的客户端配置
  79. cat <<EOF >client.conf
  80. [Interface]
  81. PrivateKey = $(cat cprivatekey)
  82. Address = 10.0.0.188/24
  83. DNS = 8.8.8.8
  84. # MTU = $mtu
  85. # PreUp = start .\route\routes-up.bat
  86. # PostDown = start .\route\routes-down.bat
  87. [Peer]
  88. PublicKey = $(cat spublickey)
  89. Endpoint = $serverip:$port
  90. AllowedIPs = 0.0.0.0/0, ::0/0
  91. PersistentKeepalive = 25
  92. EOF
  93. # 添加 2-9 号多用户配置
  94. for i in {2..9}
  95. do
  96. ip=10.0.0.${ip_list[$i]}
  97. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  98. cat <<EOF >>wg0.conf
  99. [Peer]
  100. PublicKey = $(cat cpublickey)
  101. AllowedIPs = $ip/32
  102. EOF
  103. cat <<EOF >wg_${host}_$i.conf
  104. [Interface]
  105. PrivateKey = $(cat cprivatekey)
  106. Address = $ip/24
  107. DNS = 8.8.8.8
  108. [Peer]
  109. PublicKey = $(cat spublickey)
  110. Endpoint = $serverip:$port
  111. AllowedIPs = 0.0.0.0/0, ::0/0
  112. PersistentKeepalive = 25
  113. EOF
  114. cat /etc/wireguard/wg_${host}_$i.conf| qrencode -o wg_${host}_$i.png
  115. done
  116. # vps网卡如果不是eth0,修改成实际网卡
  117. ni=$(ls /sys/class/net | awk {print} | grep -e eth. -e ens. -e venet.)
  118. if [ $ni != "eth0" ]; then
  119. sed -i "s/eth0/${ni}/g" /etc/wireguard/wg0.conf
  120. fi
  121. # 重启wg服务器
  122. wg-quick down wg0
  123. wg-quick up wg0
  124. conf_url=http://${serverip}:8000
  125. cat <<EOF > ~/wg5
  126. # 打包客户端配置,开启临时WEB服务下载
  127. next() {
  128. printf "# %-70s\n" "-" | sed 's/\s/-/g'
  129. }
  130. host=$(hostname -s)
  131. cd /etc/wireguard/
  132. tar cvf wg5clients.tar client* wg*
  133. echo -e "${GreenBG}# Windows 客户端配置,请复制配置文本 ${Font}"
  134. cat /etc/wireguard/client.conf && next
  135. cat /etc/wireguard/wg_${host}_2.conf && next
  136. cat /etc/wireguard/wg_${host}_3.conf && next
  137. cat /etc/wireguard/wg_${host}_4.conf && next
  138. echo -e "${RedBG} 一键安装 WireGuard 脚本 For Debian_9 Ubuntu Centos_7 ${Font}"
  139. echo -e "${GreenBG} 开源项目:https://github.com/hongwenjun/vps_setup ${Font}"
  140. echo
  141. echo -e "# ${Info} 新手使用${GreenBG} bash wg5 ${Font} 命令,使用临时网页下载配置和手机客户端二维码配置"
  142. echo -e "# ${Info} 大佬使用${GreenBG} bash wgmtu ${Font} 命令,服务端高级配置和添加删除客户端数量"
  143. # echo -e "# ${Info} 请网页打开 ${GreenBG}${conf_url}${Font} 下载配置文件 wg5clients.tar ,${RedBG}注意: 完成后请重启VPS.${Font}"
  144. # python -m SimpleHTTPServer 8000 &
  145. echo ""
  146. # echo -e "# ${Info} 访问 ${GreenBG}${conf_url}${Font} 点PNG二维码, ${RedBG}手机扫描二维码后请立即重启VPS。${Font}"
  147. EOF
  148. # 显示服务器配置信息
  149. bash ~/wg5
  150. # 用户选择下载配置和修改mtu
  151. sed -i "s/# python -m/python -m/g" ~/wg5
  152. sed -i "s/# echo -e/echo -e/g" ~/wg5