wg5clients.sh 4.7 KB

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