1
0

wgmtu.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #!/bin/bash
  2. # Get WireGuard Management Command : bash wgmtu
  3. # wget -O ~/wgmtu https://raw.githubusercontent.com/hongwenjun/vps_setup/english/wgmtu.sh
  4. # Modify WireGuard Server MTU Number
  5. setmtu(){
  6. echo -e "${GreenBG}Modify WireGuard Server MTU Number, Default=1420${Font}"
  7. read -p "Please Enter the Number(1200--1500): " num
  8. if [[ ${num} -ge 1200 ]] && [[ ${num} -le 1500 ]]; then
  9. mtu=$num
  10. else
  11. mtu=1420
  12. fi
  13. ip link set mtu $num up dev wg0
  14. wg-quick save wg0
  15. echo -e "${SkyBlue}:: WireGuard Server MTU Number Changed!${Font}"
  16. }
  17. # Modify WireGuard Server Port
  18. setport(){
  19. echo -e "${GreenBG}Modify WireGuard Server Port${Font}"
  20. read -p "Please Enter the Number(100--60000): " num
  21. if [[ ${num} -ge 100 ]] && [[ ${num} -le 60000 ]]; then
  22. port=$num
  23. wg set wg0 listen-port $port
  24. wg-quick save wg0
  25. echo -e "${SkyBlue}:: WireGuard Server Port Number Changed!${Font}"
  26. else
  27. echo -e "${Red}:: Port Number Not Change!${Font}"
  28. fi
  29. }
  30. # Display Client Configuration and Mobile Phone QR code
  31. conf_QRcode(){
  32. echo -e "${Yellow}:: Display Client Configuration and Mobile Phone QR_code."
  33. echo -e "Please Enter the Number${Font}\c"
  34. read -p "(2-9): " x
  35. if [[ ${x} -ge 2 ]] && [[ ${x} -le 9 ]]; then
  36. i=$x
  37. else
  38. i=2
  39. fi
  40. host=$(hostname -s)
  41. echo -e "${SkyBlue}:: Client Configuration: wg_${host}_$i.conf ${Font}"
  42. cat /etc/wireguard/wg_${host}_$i.conf
  43. echo -e "${SkyBlue}:: Please use the key combination Ctrl+Ins to copy the text to the Windows client.${Font}"
  44. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o - -t UTF8
  45. echo -e "${Green}:: Client Config: wg_${host}_$i.conf Generate QR code, Please use the mobile client to scan.${Font}"
  46. echo -e "${SkyBlue}:: Mobile Phone WireGuard APP Support pure IPV6 connection, Whether to display IPV6 QR code?${Font}\c"
  47. read -p "(Y/N): " key
  48. case $key in
  49. Y)
  50. ipv6_QRcode $i
  51. ;;
  52. y)
  53. ipv6_QRcode $i
  54. ;;
  55. esac
  56. }
  57. # Display IPV6 QR code
  58. ipv6_QRcode(){
  59. if [[ $# > 0 ]]; then
  60. i="$1"
  61. fi
  62. get_serverip
  63. serveripv6=$(curl -6 ip.sb)
  64. if [[ -z $serveripv6 ]]; then
  65. echo -e "${Red}:: Get IPV6 address is incorrect, Your Server may not have IPV6 network support!${Font}"
  66. else
  67. cat /etc/wireguard/wg_${host}_$i.conf | sed "s/${serverip}/[${serveripv6}]/g" | qrencode -o - -t UTF8
  68. echo -e "${Green}:: IPV6 Addr: [${serveripv6}] Please confirm server and local network support IPV6!${Font}"
  69. fi
  70. }
  71. get_serverip(){
  72. if [ ! -e '/var/ip_addr' ]; then
  73. echo -n $(curl -4 ip.sb) > /var/ip_addr
  74. fi
  75. serverip=$(cat /var/ip_addr)
  76. ipv6_range="fd08:620c:4df0:65eb::"
  77. }
  78. # Reset WireGuard All Client Peer
  79. wg_clients(){
  80. echo -e "${Red}:: Warning: The original client configuration will be deleted, press Ctrl+C to revoke urgently. ${Font}"
  81. cd /etc/wireguard
  82. cp wg0.conf conf.wg0.bak
  83. echo -e "${SkyBlue}:: Enter the total number of client Peers${Font}\c"
  84. read -p "(2--200): " num_x
  85. if [[ ${num_x} -ge 2 ]] && [[ ${num_x} -le 200 ]]; then
  86. wg_num=OK
  87. else
  88. num_x=3
  89. fi
  90. # Server IP and Port
  91. port=$(wg show wg0 listen-port) && host=$(hostname -s)
  92. get_serverip
  93. # Delete the original configuration, let the IP and ID numbers correspond; retain the configuration of the original server port, etc.
  94. rm /etc/wireguard/wg_${host}_* >/dev/null 2>&1
  95. line_num=$(cat -n wg0.conf | grep 'AllowedIPs' | head -n 1 | awk '{print $1}')
  96. head -n ${line_num} conf.wg0.bak > wg0.conf
  97. # restart WG server
  98. wg-quick down wg0 >/dev/null 2>&1
  99. wg-quick up wg0 >/dev/null 2>&1
  100. # Reset WireGuard All Client Peer
  101. for i in `seq 2 200`
  102. do
  103. ip=10.0.0.${i}
  104. ip6=${ipv6_range}${i}
  105. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  106. wg set wg0 peer $(cat cpublickey) allowed-ips "${ip}/32, ${ip6}"
  107. cat <<EOF >wg_${host}_$i.conf
  108. [Interface]
  109. PrivateKey = $(cat cprivatekey)
  110. Address = $ip/24, $ip6/64
  111. DNS = 8.8.8.8, 2001:4860:4860::8888
  112. [Peer]
  113. PublicKey = $(wg show wg0 public-key)
  114. Endpoint = $serverip:$port
  115. AllowedIPs = 0.0.0.0/0, ::0/0
  116. PersistentKeepalive = 25
  117. EOF
  118. cat wg_${host}_$i.conf | qrencode -o wg_${host}_$i.png
  119. if [ $i -ge $num_x ]; then break; fi
  120. done
  121. wg-quick save wg0
  122. clear && display_peer
  123. cat /etc/wireguard/wg_${host}_2.conf
  124. }
  125. # WireGuard+Speeder+Udp2Raw and SS+Kcp+Udp2RAW Automated Configuration
  126. ss_kcp_udp2raw_wg_speed(){
  127. # install shadowsocks-libev
  128. wget -qO- git.io/fhExJ | bash
  129. wget -O ~/ss_wg_set_raw git.io/fpKnF >/dev/null 2>&1
  130. bash ~/ss_wg_set_raw english
  131. rm ~/ss_wg_set_raw
  132. }
  133. # Definition Display Text Color
  134. Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m"
  135. Font="\033[0m" && Yellow="\033[0;33m" && SkyBlue="\033[0;36m"
  136. echo_SkyBlue(){
  137. echo -e "${SkyBlue}$1${Font}"
  138. }
  139. echo_Yellow(){
  140. echo -e "${Yellow}$1${Font}"
  141. }
  142. echo_GreenBG(){
  143. echo -e "${GreenBG}$1${Font}"
  144. }
  145. echo_RedBG(){
  146. echo -e "${RedBG}$1${Font}"
  147. }
  148. safe_iptables(){
  149. # IPTABLES 设置防火墙规则 脚本 By 蘭雅sRGB 特别感谢 TaterLi 指导
  150. wget -qO safe_iptables.sh git.io/fhUSe && bash safe_iptables.sh
  151. }
  152. # Get WireGuard Management Command : bash wgmtu
  153. update_self(){
  154. wget -O ~/wgmtu https://raw.githubusercontent.com/hongwenjun/vps_setup/english/wgmtu.sh
  155. }
  156. # update WireGuard
  157. wireguard_update(){
  158. yum update -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  159. apt update -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  160. echo -e "${RedBG} Updated! ${Font}"
  161. }
  162. # remove WireGuard
  163. wireguard_remove(){
  164. wg-quick down wg0
  165. yum remove -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  166. apt remove -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  167. rm -rf /etc/wireguard/
  168. echo -e "${RedBG} Removed! ${Font}"
  169. }
  170. # update/install UDP2RAW KCPTUN UDPspeeder
  171. udp2raw_update()
  172. {
  173. systemctl stop rc-local
  174. # download UDP2RAW
  175. udp2raw_ver=$(wget --no-check-certificate -qO- https://api.github.com/repos/wangyu-/udp2raw-tunnel/releases/latest | grep 'tag_name' | cut -d\" -f4)
  176. wget https://github.com/wangyu-/udp2raw-tunnel/releases/download/${udp2raw_ver}/udp2raw_binaries.tar.gz
  177. tar xf udp2raw_binaries.tar.gz
  178. mv udp2raw_amd64 /usr/bin/udp2raw
  179. rm udp2raw* -rf
  180. rm version.txt
  181. # download KCPTUN
  182. kcp_ver=$(wget --no-check-certificate -qO- https://api.github.com/repos/xtaci/kcptun/releases/latest | grep 'tag_name' | cut -d\" -f4)
  183. kcp_gz_ver=${kcp_ver:1:8}
  184. kcptun_tar_gz=kcptun-linux-amd64-${kcp_gz_ver}.tar.gz
  185. wget https://github.com/xtaci/kcptun/releases/download/${kcp_ver}/$kcptun_tar_gz
  186. tar xf $kcptun_tar_gz
  187. mv server_linux_amd64 /usr/bin/kcp-server
  188. rm $kcptun_tar_gz
  189. rm client_linux_amd64
  190. # download UDPspeeder
  191. udpspeeder_ver=$(wget --no-check-certificate -qO- https://api.github.com/repos/wangyu-/UDPspeeder/releases/latest | grep 'tag_name' | cut -d\" -f4)
  192. wget https://github.com/wangyu-/UDPspeeder/releases/download/${udpspeeder_ver}/speederv2_binaries.tar.gz
  193. tar xf speederv2_binaries.tar.gz
  194. mv speederv2_amd64 /usr/bin/speederv2
  195. rm speederv2* -rf
  196. rm version.txt
  197. systemctl restart rc-local
  198. ps aux | grep -e kcp -e udp -e speed -e ss-server
  199. ss-server -h | head -2 && kcp-server -v && udp2raw -h | head -2 && speederv2 -h | head -2
  200. }
  201. rc-local_remove(){
  202. echo -e "${RedBG} Remove Udp2Raw Bridge Service Configuration /etc/rc.local ${Font}"
  203. systemctl stop rc-local
  204. rm /usr/bin/udp2raw /usr/bin/kcp-server /usr/bin/speederv2
  205. ps aux | grep -e kcp -e udp -e speed
  206. mv /etc/rc.local ~/rc.local
  207. echo -e "${RedBG} Removed! Backup on /root/rc.local ${Font}"
  208. }
  209. update_remove_menu(){
  210. echo -e "${RedBG} Update/Remove WireGuard and Udp2Raw Service ${Font}"
  211. echo -e "${Green}> 1. Update WireGuard Service"
  212. echo -e "> 2. Remove WireGuard Service"
  213. echo -e "> 3. Update Udp2Raw KCPTUN UDPspeeder Soft"
  214. echo -e "> 4. Remove Udp2Raw KCPTUN UDPspeeder Service"
  215. echo -e "> 5. Exit${Font}"
  216. echo
  217. read -p "Please Enter the Number(1-5):" num_x
  218. case "$num_x" in
  219. 1)
  220. wireguard_update
  221. ;;
  222. 2)
  223. wireguard_remove
  224. ;;
  225. 3)
  226. udp2raw_update
  227. ;;
  228. 4)
  229. rc-local_remove
  230. ;;
  231. 5)
  232. exit 1
  233. ;;
  234. *)
  235. ;;
  236. esac
  237. }
  238. # Delete the last Peer
  239. del_last_peer(){
  240. peer_key=$(wg show wg0 allowed-ips | tail -1 | awk '{print $1}')
  241. wg set wg0 peer $peer_key remove
  242. wg-quick save wg0
  243. echo -e "${SkyBlue}:: Delete Clint Peer: ${Yellow} ${peer_key} ${SkyBlue} .${Font}"
  244. }
  245. # Display active Peer table
  246. display_peer(){
  247. # Peer and ip table to write temporary files
  248. wg show wg0 allowed-ips > /tmp/peer_list
  249. echo -e "${RedBG} ID ${GreenBG} Peer: <base64 public key> ${SkyBlue} IP_Addr: ${Font}"
  250. i=1
  251. while read -r line || [[ -n $line ]]; do
  252. peer=$(echo $line | awk '{print $1}')
  253. ip=$(echo $line | awk '{print $2}')
  254. line="> ${Red}${i} ${Yellow}${peer}${Font} ${ip}"
  255. echo -e $line && let i++
  256. done < /tmp/peer_list
  257. }
  258. # Select to delete the Peer client
  259. del_peer(){
  260. display_peer
  261. echo
  262. echo -e "${RedBG}Please select IP_Addr corresponding ID number, specify the client configuration will be deleted! ${Font}"
  263. read -p "Please enter the ID number(1-X):" x
  264. peer_cnt=$(cat /tmp/peer_list | wc -l)
  265. if [[ ${x} -ge 1 ]] && [[ ${x} -le ${peer_cnt} ]]; then
  266. i=$x
  267. peer_key=$(cat /tmp/peer_list | head -n $i | tail -1 | awk '{print $1}')
  268. wg set wg0 peer $peer_key remove
  269. wg-quick save wg0
  270. echo -e "${SkyBlue}:: Client peer: ${Yellow} ${peer_key} ${SkyBlue} Removed! ${Font}"
  271. else
  272. echo -e "${SkyBlue}:: Usage: ${GreenBG} wg set wg0 peer <base64 public key> remove ${Font}"
  273. fi
  274. rm /tmp/peer_list
  275. }
  276. # Add new WireGuard Client Peer
  277. add_peer(){
  278. # Server IP port, new client serial number and IP
  279. port=$(wg show wg0 listen-port)
  280. get_serverip && host=$(hostname -s) && cd /etc/wireguard
  281. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  282. ipnum=$(wg show wg0 allowed-ips | tail -1 | awk '{print $2}' | awk -F '[./]' '{print $4}')
  283. i=$((10#${ipnum}+1)) && ip=10.0.0.${i} ip6=${ipv6_range}${i}
  284. # Generate a client profile
  285. cat <<EOF >wg_${host}_$i.conf
  286. [Interface]
  287. PrivateKey = $(cat cprivatekey)
  288. Address = $ip/24, $ip6/64
  289. DNS = 8.8.8.8, 2001:4860:4860::8888
  290. [Peer]
  291. PublicKey = $(wg show wg0 public-key)
  292. Endpoint = $serverip:$port
  293. AllowedIPs = 0.0.0.0/0, ::0/0
  294. PersistentKeepalive = 25
  295. EOF
  296. # Effective client peer in wg server
  297. wg set wg0 peer $(cat cpublickey) allowed-ips "${ip}/32, ${ip6}"
  298. wg-quick save wg0
  299. # display Client peer
  300. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o wg_${host}_$i.png
  301. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o - -t UTF8
  302. echo -e "${SkyBlue}:: New client Peer added; File:${Yellow} /etc/wireguard/wg_${host}_$i.conf ${Font}"
  303. cat /etc/wireguard/wg_${host}_$i.conf
  304. }
  305. wg_clients_menu(){
  306. echo -e "${RedBG} Add/Delete WireGuard Client Peer Management ${Font}"
  307. echo -e "${Green}> 1. Add One WireGuard Client Peer "
  308. echo -e "> 2. Delete Last WireGuard Client Peer "
  309. echo -e "> 3. Delete Choose WireGuard Client Peer "
  310. echo "------------------------------------------------------"
  311. echo -e "${SkyBlue}> 4. Exit"
  312. echo -e "> 5.${RedBG} Reset WireGuard All Client Peer${Font}"
  313. echo
  314. read -p "Please Enter the Number(1-5):" num_x
  315. case "$num_x" in
  316. 1)
  317. add_peer
  318. ;;
  319. 2)
  320. del_last_peer
  321. ;;
  322. 3)
  323. del_peer
  324. ;;
  325. 4)
  326. display_peer
  327. exit 1
  328. ;;
  329. 5)
  330. wg_clients
  331. ;;
  332. *)
  333. ;;
  334. esac
  335. }
  336. # Setting Menu
  337. start_menu(){
  338. clear
  339. echo_RedBG " One-Step Automated Install WireGuard Script For Debian_9 Ubuntu Centos_7 "
  340. echo_GreenBG " Open Source Project: https://github.com/hongwenjun/vps_setup "
  341. echo -e "${Green}> 1. Display Client Configuration and QR code for Mobile Phone "
  342. echo -e "> 2. Modify WireGuard Server MTU Number"
  343. echo -e "> 3. Modify WireGuard Server Port"
  344. echo -e "> 4. WireGuard+Speeder+Udp2Raw and SS+Kcp+Udp2RAW Automated Configuration"
  345. echo "----------------------------------------------------------"
  346. echo -e "${SkyBlue}> 5. Add/Delete WireGuard Client Peer Management"
  347. echo -e "> 6. Update/Remove WireGuard and Udp2Raw Service"
  348. echo -e "> 7. Replace the Script itself with English to Simplified Chinese(中文)"
  349. echo -e "> 8. ${RedBG} IPTABLES Firewall Setup Script ${Font}"
  350. echo
  351. echo_SkyBlue "Usage: ${GreenBG} bash wgmtu ${SkyBlue} [ setup | remove | vps | bench | -U ] "
  352. echo_SkyBlue " [ v2ray | vnstat | log | trace | -h ] "
  353. echo
  354. read -p "Please Enter the Number(1-8):" num
  355. case "$num" in
  356. 1)
  357. conf_QRcode
  358. ;;
  359. 2)
  360. setmtu
  361. ;;
  362. 3)
  363. setport
  364. ;;
  365. 4)
  366. ss_kcp_udp2raw_wg_speed
  367. ;;
  368. 5)
  369. wg_clients_menu
  370. ;;
  371. 6)
  372. update_remove_menu
  373. update_self
  374. exit 1
  375. ;;
  376. 7)
  377. wget -O wgmtu https://git.io/wgmtu && bash wgmtu
  378. exit 1
  379. ;;
  380. 8)
  381. safe_iptables
  382. ;;
  383. # Manage menu input command line parameters
  384. setup)
  385. ss_kcp_udp2raw_wg_speed
  386. ;;
  387. remove)
  388. wireguard_remove
  389. rc-local_remove
  390. ;;
  391. 9999)
  392. bash <(curl -L -s https://git.io/wireguard.sh) 9999
  393. ;;
  394. -U)
  395. update_self
  396. ;;
  397. -h)
  398. wgmtu_help
  399. ;;
  400. vps)
  401. bash <(curl -L -s https://git.io/vps.setup)
  402. ;;
  403. vnstat)
  404. bash <(curl -L -s https://git.io/fxxlb) setup
  405. ;;
  406. bench)
  407. wget -qO- git.io/superbench.sh | bash
  408. ;;
  409. trace)
  410. wget -qO- git.io/fp5lf | bash
  411. ;;
  412. v2ray)
  413. bash <(curl -L -s https://git.io/v2ray_ss.sh)
  414. ;;
  415. log)
  416. cat vps_setup.log
  417. ;;
  418. *)
  419. display_peer
  420. ;;
  421. esac
  422. }
  423. wgmtu_help(){
  424. echo_SkyBlue "Usage: ${GreenBG} bash wgmtu ${SkyBlue} [ setup | remove | vps | bench | -U ] "
  425. echo_SkyBlue " [ v2ray | vnstat | log | trace | -h ] "
  426. echo
  427. echo_Yellow "[setup 惊喜 | remove 卸载 | vps 脚本 | bench 基准测试 | -U 更新]"
  428. echo_Yellow "[v2ray 你懂 | vnstat 流量 | log 信息 | trace 网络回程 | -h 帮助]"
  429. }
  430. # Manage menu input command line parameters
  431. if [[ $# > 0 ]]; then
  432. key="$1"
  433. case $key in
  434. setup)
  435. ss_kcp_udp2raw_wg_speed
  436. ;;
  437. remove)
  438. wireguard_remove
  439. rc-local_remove
  440. ;;
  441. 9999)
  442. bash <(curl -L -s https://git.io/wireguard.sh) 9999
  443. ;;
  444. -U)
  445. update_self
  446. ;;
  447. -h)
  448. wgmtu_help
  449. ;;
  450. vps)
  451. bash <(curl -L -s https://git.io/vps.setup)
  452. ;;
  453. vnstat)
  454. bash <(curl -L -s https://git.io/fxxlb) setup
  455. ;;
  456. bench)
  457. wget -qO- git.io/superbench.sh | bash
  458. ;;
  459. trace)
  460. wget -qO- git.io/fp5lf | bash
  461. ;;
  462. v2ray)
  463. bash <(curl -L -s https://git.io/v2ray_ss.sh)
  464. ;;
  465. log)
  466. cat vps_setup.log
  467. ;;
  468. esac
  469. else
  470. start_menu
  471. fi