1
0

wg5clients.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. # CentOS7一键脚本安装WireGuard (https://atrandys.com/2018/886.html)
  14. yum install -y wget && \
  15. wget https://raw.githubusercontent.com/atrandys/wireguard/master/wireguard_install.sh \
  16. && chmod +x wireguard_install.sh && ./wireguard_install.sh
  17. EOF
  18. }
  19. #############################################################
  20. #定义文字颜色
  21. Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m" && Font="\033[0m"
  22. #定义提示信息
  23. Info="${Green}[信息]${Font}" && OK="${Green}[OK]${Font}" && Error="${Red}[错误]${Font}"
  24. # 检查是否安装 WireGuard
  25. if [ ! -f '/usr/bin/wg' ]; then
  26. clear
  27. echo -e "${RedBG} 一键安装 WireGuard 脚本 For Debian_9 Ubuntu Centos_7 ${Font}"
  28. echo -e "${GreenBG} 开源项目:https://github.com/hongwenjun/vps_setup ${Font}"
  29. help_info
  30. echo -e "${RedBG} 检测到你的vps没有正确选择脚本,请使用对应系统的脚本安装 ${Font}"
  31. exit 1
  32. fi
  33. #############################################################
  34. # 定义修改端口号,适合已经安装WireGuard而不想改端口
  35. #生成随机端口
  36. rand(){
  37. min=$1
  38. max=$(($2-$min+1))
  39. num=$(cat /dev/urandom | head -n 10 | cksum | awk -F ' ' '{print $1}')
  40. echo $(($num%$max+$min))
  41. }
  42. port=$(rand 1000 60000)
  43. mtu=1420
  44. host=$(hostname -s)
  45. ip_list=(2 5 8 18 88 188 118 158 198 168 186 )
  46. # 获得服务器ip,自动获取
  47. if [ ! -f '/usr/bin/curl' ]; then
  48. apt update && apt install -y curl
  49. fi
  50. serverip=$(curl -4 ip.sb)
  51. # 安装二维码插件
  52. if [ ! -f '/usr/bin/qrencode' ]; then
  53. apt -y install qrencode
  54. fi
  55. # 安装 bash wgmtu 脚本用来设置服务器
  56. wget -O ~/wgmtu https://raw.githubusercontent.com/hongwenjun/vps_setup/master/Wireguard/wgmtu.sh
  57. #############################################################
  58. # 转到wg配置文件目录
  59. cd /etc/wireguard
  60. # 然后开始生成 密匙对(公匙+私匙)。
  61. wg genkey | tee sprivatekey | wg pubkey > spublickey
  62. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  63. # 生成服务端配置文件
  64. cat <<EOF >wg0.conf
  65. [Interface]
  66. PrivateKey = $(cat sprivatekey)
  67. Address = 10.0.0.1/24
  68. PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  69. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
  70. ListenPort = $port
  71. DNS = 8.8.8.8
  72. MTU = $mtu
  73. [Peer]
  74. PublicKey = $(cat cpublickey)
  75. AllowedIPs = 10.0.0.218/32
  76. EOF
  77. # 生成简洁的客户端配置
  78. cat <<EOF >client.conf
  79. [Interface]
  80. PrivateKey = $(cat cprivatekey)
  81. Address = 10.0.0.218/24
  82. DNS = 8.8.8.8
  83. # MTU = $mtu
  84. # PreUp = start .\route\routes-up.bat
  85. # PostDown = start .\route\routes-down.bat
  86. [Peer]
  87. PublicKey = $(cat spublickey)
  88. Endpoint = $serverip:$port
  89. AllowedIPs = 0.0.0.0/0, ::0/0
  90. PersistentKeepalive = 25
  91. EOF
  92. # 添加 2-9 号多用户配置
  93. for i in {2..9}
  94. do
  95. ip=10.0.0.${ip_list[$i]}
  96. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  97. cat <<EOF >>wg0.conf
  98. [Peer]
  99. PublicKey = $(cat cpublickey)
  100. AllowedIPs = $ip/32
  101. EOF
  102. cat <<EOF >wg_${host}_$i.conf
  103. [Interface]
  104. PrivateKey = $(cat cprivatekey)
  105. Address = $ip/24
  106. DNS = 8.8.8.8
  107. [Peer]
  108. PublicKey = $(cat spublickey)
  109. Endpoint = $serverip:$port
  110. AllowedIPs = 0.0.0.0/0, ::0/0
  111. PersistentKeepalive = 25
  112. EOF
  113. cat /etc/wireguard/wg_${host}_$i.conf| qrencode -o wg_${host}_$i.png
  114. done
  115. # vps网卡如果不是eth0,修改成实际网卡
  116. ni=$(ls /sys/class/net | awk {print} | grep -e eth. -e ens. -e venet.)
  117. if [ $ni != "eth0" ]; then
  118. sed -i "s/eth0/${ni}/g" /etc/wireguard/wg0.conf
  119. fi
  120. # 重启wg服务器
  121. wg-quick down wg0
  122. wg-quick up wg0
  123. wg
  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} 命令,服务端高级配置; (至少能vim wgmtu会看脚本爱折腾玩家)"
  143. # echo -e "# ${Info} 请网页打开 ${GreenBG}${conf_url}${Font} 下载配置文件 wg5clients.tar ,${RedBG}注意: 完成后请重启VPS.${Font}"
  144. # echo -e "# scp [email protected]:/etc/wireguard/wg5clients.tar wg5clients.tar"
  145. # python -m SimpleHTTPServer 8000 &
  146. echo ""
  147. # echo -e "# ${Info} 访问 ${GreenBG}${conf_url}${Font} 点PNG二维码, ${RedBG}手机扫描二维码后请立即重启VPS。${Font}"
  148. EOF
  149. # 显示服务器配置信息
  150. bash ~/wg5
  151. # 用户选择下载配置和修改mtu
  152. sed -i "s/# python -m/python -m/g" ~/wg5
  153. sed -i "s/# echo -e/echo -e/g" ~/wg5