wg5clients.sh 5.2 KB

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