wg5clients.sh 4.9 KB

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