wg5clients.sh 6.1 KB

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