wg5clients.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/bash
  2. # 配置步骤 WireGuard服务端
  3. cd /etc/wireguard
  4. # 然后开始生成 密匙对(公匙+私匙)。
  5. wg genkey | tee sprivatekey | wg pubkey > spublickey
  6. wg genkey | tee cprivatekey1 | wg pubkey > cpublickey1
  7. wg genkey | tee cprivatekey2 | wg pubkey > cpublickey2
  8. wg genkey | tee cprivatekey3 | wg pubkey > cpublickey3
  9. wg genkey | tee cprivatekey4 | wg pubkey > cpublickey4
  10. wg genkey | tee cprivatekey5 | wg pubkey > cpublickey5
  11. # 获得服务器ip
  12. serverip=$(curl -4 icanhazip.com)
  13. # 生成服务端 多用户配置文件
  14. cat <<EOF >wg0.conf
  15. [Interface]
  16. PrivateKey = $(cat sprivatekey)
  17. Address = 10.0.0.1/24
  18. PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  19. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
  20. ListenPort = 9009
  21. DNS = 8.8.8.8
  22. MTU = 1300
  23. [Peer]
  24. PublicKey = $(cat cpublickey1)
  25. AllowedIPs = 10.0.0.2/32
  26. [Peer]
  27. PublicKey = $(cat cpublickey2)
  28. AllowedIPs = 10.0.0.8/32
  29. [Peer]
  30. PublicKey = $(cat cpublickey3)
  31. AllowedIPs = 10.0.0.18/32
  32. [Peer]
  33. PublicKey = $(cat cpublickey4)
  34. AllowedIPs = 10.0.0.88/32
  35. [Peer]
  36. PublicKey = $(cat cpublickey5)
  37. AllowedIPs = 10.0.0.188/32
  38. EOF
  39. # 生成简洁的客户端配置
  40. cat <<EOF >client.conf
  41. [Interface]
  42. PrivateKey = $(cat cprivatekey1)
  43. Address = 10.0.0.2/24
  44. DNS = 8.8.8.8
  45. MTU = 1300
  46. [Peer]
  47. PublicKey = $(cat spublickey)
  48. Endpoint = $serverip:9009
  49. AllowedIPs = 0.0.0.0/0, ::0/0
  50. PersistentKeepalive = 25
  51. EOF
  52. cat <<EOF >client_2.conf
  53. [Interface]
  54. PrivateKey = $(cat cprivatekey2)
  55. Address = 10.0.0.8/24
  56. DNS = 8.8.8.8
  57. MTU = 1300
  58. [Peer]
  59. PublicKey = $(cat spublickey)
  60. Endpoint = $serverip:9009
  61. AllowedIPs = 0.0.0.0/0, ::0/0
  62. PersistentKeepalive = 25
  63. EOF
  64. cat <<EOF >client_3.conf
  65. [Interface]
  66. PrivateKey = $(cat cprivatekey3)
  67. Address = 10.0.0.18/24
  68. DNS = 8.8.8.8
  69. MTU = 1300
  70. [Peer]
  71. PublicKey = $(cat spublickey)
  72. Endpoint = $serverip:9009
  73. AllowedIPs = 0.0.0.0/0, ::0/0
  74. PersistentKeepalive = 25
  75. EOF
  76. cat <<EOF >client_4.conf
  77. [Interface]
  78. PrivateKey = $(cat cprivatekey4)
  79. Address = 10.0.0.88/24
  80. DNS = 8.8.8.8
  81. MTU = 1300
  82. [Peer]
  83. PublicKey = $(cat spublickey)
  84. Endpoint = $serverip:9009
  85. AllowedIPs = 0.0.0.0/0, ::0/0
  86. PersistentKeepalive = 25
  87. EOF
  88. cat <<EOF >client_5.conf
  89. [Interface]
  90. PrivateKey = $(cat cprivatekey5)
  91. Address = 10.0.0.188/24
  92. DNS = 8.8.8.8
  93. MTU = 1300
  94. [Peer]
  95. PublicKey = $(cat spublickey)
  96. Endpoint = $serverip:9009
  97. AllowedIPs = 0.0.0.0/0, ::0/0
  98. PersistentKeepalive = 25
  99. EOF
  100. # 重启wg服务器
  101. wg-quick down wg0
  102. wg-quick up wg0
  103. wg
  104. # 打包客户端 配置
  105. tar cvf wg5clients.tar client*
  106. curl --upload-file ./wg5clients.tar https://transfer.sh/wg5clients.tar
  107. echo '按提示的网址下载客户端包,保留2星期'