wireguard-install.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/bin/bash
  2. # wireguard-install
  3. # WireGuard installer for Ubuntu 18.04 LTS, Debian 9 and CentOS 7.
  4. # This script will let you setup your own VPN server in no more than a minute, even if you haven't used WireGuard before.
  5. # It has been designed to be as unobtrusive and universal as possible.
  6. # 一键安装wireguard 脚本
  7. wget -qO- git.io/fptwc | bash
  8. # WireGuard VPN多用户服务端 自动配置脚本 支持IPV6
  9. #############################################################
  10. let port=$RANDOM/2+9999
  11. mtu=1420
  12. ip_list=(2 5 8 178 186 118 158 198 168 9)
  13. ipv6_range="fd08:620c:4df0:65eb::"
  14. # 安装 bash wgmtu 脚本用来设置服务器
  15. wget -O ~/wgmtu https://raw.githubusercontent.com/hongwenjun/vps_setup/english/wgmtu.sh
  16. # 定义文字颜色
  17. Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m"
  18. Font="\033[0m" && Yellow="\033[0;33m" && SkyBlue="\033[0;36m"
  19. echo_SkyBlue(){
  20. echo -e "${SkyBlue}$1${Font}"
  21. }
  22. echo_Yellow(){
  23. echo -e "${Yellow}$1${Font}"
  24. }
  25. echo_GreenBG(){
  26. echo -e "${GreenBG}$1${Font}"
  27. }
  28. echo_RedBG(){
  29. echo -e "${RedBG}$1${Font}"
  30. }
  31. #############################################################
  32. if [[ $# > 0 ]]; then
  33. num="$1"
  34. if [[ ${num} -ge 100 ]] && [[ ${num} -le 60000 ]]; then
  35. port=$num
  36. fi
  37. fi
  38. host=$(hostname -s)
  39. # 获得服务器ip,自动获取
  40. if [ ! -f '/usr/bin/curl' ]; then
  41. apt update && apt install -y curl
  42. fi
  43. if [ ! -e '/var/ip_addr' ]; then
  44. echo -n $(curl -4 ip.sb) > /var/ip_addr
  45. fi
  46. serverip=$(cat /var/ip_addr)
  47. # 安装二维码插件
  48. if [ ! -f '/usr/bin/qrencode' ]; then
  49. apt -y install qrencode
  50. fi
  51. #############################################################
  52. # 打开ip4/ipv6防火墙转发功能
  53. sysctl_config() {
  54. sed -i '/net.ipv4.ip_forward/d' /etc/sysctl.conf
  55. sed -i '/net.ipv6.conf.all.forwarding/d' /etc/sysctl.conf
  56. sed -i '/net.ipv6.conf.default.accept_ra/d' /etc/sysctl.conf
  57. echo 1 > /proc/sys/net/ipv4/ip_forward
  58. echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
  59. echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf
  60. echo "net.ipv6.conf.default.accept_ra=2" >> /etc/sysctl.conf
  61. sysctl -p >/dev/null 2>&1
  62. }
  63. sysctl_config
  64. # wg配置文件目录 /etc/wireguard
  65. mkdir -p /etc/wireguard
  66. chmod 777 -R /etc/wireguard
  67. cd /etc/wireguard
  68. # 然后开始生成 密匙对(公匙+私匙)。
  69. wg genkey | tee sprivatekey | wg pubkey > spublickey
  70. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  71. # 生成服务端配置文件
  72. cat <<EOF >wg0.conf
  73. [Interface]
  74. PrivateKey = $(cat sprivatekey)
  75. Address = 10.0.0.1/24, ${ipv6_range}1/64
  76. PostUp = iptables -I FORWARD -i wg0 -j ACCEPT; iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT; ip6tables -I FORWARD -i wg0 -j ACCEPT; ip6tables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  77. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  78. ListenPort = $port
  79. DNS = 8.8.8.8, 2001:4860:4860::8888
  80. MTU = $mtu
  81. [Peer]
  82. PublicKey = $(cat cpublickey)
  83. AllowedIPs = 10.0.0.188/32, ${ipv6_range}188
  84. EOF
  85. # 生成简洁的客户端配置
  86. cat <<EOF >client.conf
  87. [Interface]
  88. PrivateKey = $(cat cprivatekey)
  89. Address = 10.0.0.188/24, ${ipv6_range}188/64
  90. DNS = 8.8.8.8, 2001:4860:4860::8888
  91. # MTU = $mtu
  92. # PreUp = start .\route\routes-up.bat
  93. # PostDown = start .\route\routes-down.bat
  94. [Peer]
  95. PublicKey = $(cat spublickey)
  96. Endpoint = $serverip:$port
  97. AllowedIPs = 0.0.0.0/0, ::0/0
  98. PersistentKeepalive = 25
  99. EOF
  100. # 添加 2-9 号多用户配置
  101. for i in {2..9}
  102. do
  103. ip=10.0.0.${ip_list[$i]}
  104. ip6=${ipv6_range}${ip_list[$i]}
  105. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  106. cat <<EOF >>wg0.conf
  107. [Peer]
  108. PublicKey = $(cat cpublickey)
  109. AllowedIPs = $ip/32, $ip6
  110. EOF
  111. cat <<EOF >wg_${host}_$i.conf
  112. [Interface]
  113. PrivateKey = $(cat cprivatekey)
  114. Address = $ip/24, $ip6/64
  115. DNS = 8.8.8.8, 2001:4860:4860::8888
  116. [Peer]
  117. PublicKey = $(cat spublickey)
  118. Endpoint = $serverip:$port
  119. AllowedIPs = 0.0.0.0/0, ::0/0
  120. PersistentKeepalive = 25
  121. EOF
  122. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o wg_${host}_$i.png
  123. done
  124. # 重启wg服务器
  125. wg-quick down wg0
  126. wg-quick up wg0
  127. next() {
  128. printf "# %-70s\n" "-" | sed 's/\s/-/g'
  129. }
  130. echo -e "# Windows 客户端配置,请复制配置文本"
  131. cat /etc/wireguard/client.conf && next
  132. cat /etc/wireguard/wg_${host}_2.conf && next
  133. cat /etc/wireguard/wg_${host}_3.conf && next
  134. echo_GreenBG "# WireGuard Management Command."
  135. echo_SkyBlue "Usage: ${GreenBG} bash wgmtu ${SkyBlue} [ setup | remove | vps | bench | -U ] "
  136. echo_SkyBlue " [ v2ray | vnstat | log | trace | -h ] "