wg5clients.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/bash
  2. # WireGuard VPN多用户服务端 自动配置脚本
  3. # 本脚本(WireGuard 多用户配置)一键安装短网址
  4. # wget -qO- https://git.io/fpnQt | bash
  5. # 本脚本适合已经安装 WireGuard VPN 的vps
  6. # 如果你的vps没有安装 WireGuard ,可以用下行命令先安装
  7. # 一键安装wireguard 脚本 debian 9
  8. # wget -qO- git.io/fptwc | bash
  9. #############################################################
  10. # 定义修改端口号,适合已经安装WireGuard而不想改端口
  11. port=9009
  12. mtu=1200
  13. host=$(hostname -s)
  14. ip_list=(2 8 18 88 188 118 158 198 168 186)
  15. # 获得服务器ip,自动获取
  16. if [ $host == "debian" ]; then
  17. apt update && apt install -y curl
  18. fi
  19. serverip=$(curl -4 icanhazip.com)
  20. # 安装二维码插件
  21. apt -y install qrencode
  22. #############################################################
  23. # 转到wg配置文件目录
  24. cd /etc/wireguard
  25. # 然后开始生成 密匙对(公匙+私匙)。
  26. wg genkey | tee sprivatekey | wg pubkey > spublickey
  27. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  28. # 生成服务端配置文件
  29. cat <<EOF >wg0.conf
  30. [Interface]
  31. PrivateKey = $(cat sprivatekey)
  32. Address = 10.0.0.1/24
  33. PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  34. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
  35. ListenPort = $port
  36. DNS = 8.8.8.8
  37. MTU = $mtu
  38. [Peer]
  39. PublicKey = $(cat cpublickey)
  40. AllowedIPs = 10.0.0.2/32
  41. EOF
  42. # 生成简洁的客户端配置
  43. cat <<EOF >client.conf
  44. [Interface]
  45. PrivateKey = $(cat cprivatekey)
  46. Address = 10.0.0.2/24
  47. DNS = 8.8.8.8
  48. MTU = $mtu
  49. # PreUp = start .\route\routes-up.bat
  50. # PostDown = start .\route\routes-down.bat
  51. [Peer]
  52. PublicKey = $(cat spublickey)
  53. Endpoint = $serverip:$port
  54. AllowedIPs = 0.0.0.0/0, ::0/0
  55. PersistentKeepalive = 25
  56. EOF
  57. # 添加 1-9 多用户配置子程序
  58. for i in {1..9}
  59. do
  60. ip=10.0.0.${ip_list[$i]}
  61. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  62. cat <<EOF >>wg0.conf
  63. [Peer]
  64. PublicKey = $(cat cpublickey)
  65. AllowedIPs = $ip/32
  66. EOF
  67. cat <<EOF >wg_${host}_$i.conf
  68. [Interface]
  69. PrivateKey = $(cat cprivatekey)
  70. Address = $ip/24
  71. DNS = 8.8.8.8
  72. MTU = $mtu
  73. [Peer]
  74. PublicKey = $(cat spublickey)
  75. Endpoint = $serverip:$port
  76. AllowedIPs = 0.0.0.0/0, ::0/0
  77. PersistentKeepalive = 25
  78. EOF
  79. cat /etc/wireguard/wg_${host}_$i.conf| qrencode -o wg_${host}_$i.png
  80. done
  81. # vps网卡如果不是eth0,修改成实际网卡
  82. ni=$(ls /sys/class/net | awk {print} | head -n 1)
  83. if [ $ni != "eth0" ]; then
  84. sed -i "s/eth0/${ni}/g" /etc/wireguard/wg0.conf
  85. fi
  86. # 重启wg服务器
  87. wg-quick down wg0
  88. wg-quick up wg0
  89. wg
  90. #定义文字颜色
  91. Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m" && Font="\033[0m"
  92. #定义提示信息
  93. Info="${Green}[信息]${Font}" && OK="${Green}[OK]${Font}" && Error="${Red}[错误]${Font}"
  94. conf_url=http://${serverip}:8000
  95. cat <<EOF >wg5
  96. # 打包10个客户端配置,手机扫描二维码2号配置,PC使用1号配置
  97. next() {
  98. printf "# %-70s\n" "-" | sed 's/\s/-/g'
  99. }
  100. host=$(hostname -s)
  101. cd /etc/wireguard/
  102. tar cvf wg5clients.tar client* wg*
  103. cat /etc/wireguard/wg_${host}_1.conf | qrencode -o - -t UTF8
  104. echo "# 手机扫描二维码2号配置,PC使用配置复制下面文本"
  105. cat /etc/wireguard/client.conf && next
  106. cat /etc/wireguard/wg_${host}_1.conf && next
  107. cat /etc/wireguard/wg_${host}_2.conf && next
  108. cat /etc/wireguard/wg_${host}_3.conf && next
  109. cat /etc/wireguard/wg_${host}_4.conf && next
  110. echo -e "# ${Info} wg 查看有效的客户端;删除客户端使用 wg set wg0 peer xxxx_填对应IP的公钥_xxxx remove"
  111. echo -e "# ${Info} 再次显示本文本使用${GreenBG} bash wg5 ${Font} 命令,通过下面2种方式获得其他的配置文件"
  112. echo -e "# ${Info} 请网页打开 ${GreenBG}${conf_url}${Font} 下载配置文件 wg5clients.tar ,${RedBG}注意: 完成后请重启VPS.${Font}"
  113. # echo "# scp [email protected]:/etc/wireguard/wg5clients.tar wg5clients.tar"
  114. # 简单的web服务器,使用后,请重启vps
  115. python -m SimpleHTTPServer 8000 &
  116. echo ""
  117. echo -e "# ${Info} 访问 ${GreenBG}${conf_url}${Font} 有惊喜, 手机扫描二维码后请立即重启VPS。"
  118. EOF
  119. cp wg5 ~/wg5
  120. bash wg5