1
0

wg5clients.sh 6.6 KB

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