1
0

wgmtu.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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}:: 显示客户端配置和手机二维码 (默认2号),请输入数字${Font}\c"
  33. read -p "(2-9): " x
  34. if [[ ${x} -ge 2 ]] && [[ ${x} -le 9 ]]; then
  35. i=$x
  36. else
  37. i=2
  38. fi
  39. host=$(hostname -s)
  40. echo -e "${SkyBlue}:: 客户端配置文件: wg_${host}_$i.conf ${Font}"
  41. cat /etc/wireguard/wg_${host}_$i.conf
  42. echo -e "${SkyBlue}:: 请使用组合键 Ctrl+Ins 复制文本给Windows客户端使用${Font}"
  43. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o - -t UTF8
  44. echo -e "${Green}:: 配置文件: wg_${host}_$i.conf 生成二维码,请用手机客户端扫描使用${Font}"
  45. echo -e "${SkyBlue}:: 安卓手机WireGuard官方APP目前支持纯IPV6连接,是否显示IPV6二维码?${Font}\c"
  46. read -p "(Y/N): " key
  47. case $key in
  48. Y)
  49. ipv6_QRcode $i
  50. ;;
  51. y)
  52. ipv6_QRcode $i
  53. ;;
  54. esac
  55. echo -e "${SkyBlue}:: SSH工具推荐Git-Bash 2.20; GCP_SSH(浏览器)字体Courier New 二维码正常${Font}"
  56. }
  57. # 显示IPV6手机客户端二维码
  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}:: 获取IPV6地址不正确,你的服务器可能没有IPV6网络支持!${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地址: [${serveripv6}] 请确认服务器和本地网络支持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. # 重置 WireGuard 客户端配置和数量
  79. wg_clients(){
  80. echo -e "${Red}:: 注意原来的客户端配置都会删除,按 Ctrl+ C 可以紧急撤销 ${Font}"
  81. # 转到wg配置文件目录
  82. cd /etc/wireguard
  83. cp wg0.conf conf.wg0.bak
  84. echo -e "${SkyBlue}:: 输入客户端Peer总数${Font}\c"
  85. read -p "(2--200): " num_x
  86. if [[ ${num_x} -ge 2 ]] && [[ ${num_x} -le 200 ]]; then
  87. wg_num=OK
  88. else
  89. num_x=3
  90. fi
  91. # 服务器 IP 和 端口
  92. port=$(wg show wg0 listen-port) && host=$(hostname -s)
  93. get_serverip
  94. # 删除原配置,让IP和ID号对应; 保留原来服务器的端口等配置
  95. rm /etc/wireguard/wg_${host}_* >/dev/null 2>&1
  96. line_num=$(cat -n wg0.conf | grep 'AllowedIPs' | head -n 1 | awk '{print $1}')
  97. head -n ${line_num} conf.wg0.bak > wg0.conf
  98. # 重启wg服务器
  99. wg-quick down wg0 >/dev/null 2>&1
  100. wg-quick up wg0 >/dev/null 2>&1
  101. # 重新生成用户配置数量
  102. for i in `seq 2 200`
  103. do
  104. ip=10.0.0.${i}
  105. ip6=${ipv6_range}${i}
  106. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  107. wg set wg0 peer $(cat cpublickey) allowed-ips "${ip}/32, ${ip6}"
  108. cat <<EOF >wg_${host}_$i.conf
  109. [Interface]
  110. PrivateKey = $(cat cprivatekey)
  111. Address = $ip/24, $ip6/64
  112. DNS = 8.8.8.8, 2001:4860:4860::8888
  113. [Peer]
  114. PublicKey = $(wg show wg0 public-key)
  115. Endpoint = $serverip:$port
  116. AllowedIPs = 0.0.0.0/0, ::0/0
  117. PersistentKeepalive = 25
  118. EOF
  119. cat wg_${host}_$i.conf | qrencode -o wg_${host}_$i.png
  120. if [ $i -ge $num_x ]; then break; fi
  121. done
  122. wg-quick save wg0
  123. clear && display_peer
  124. cat /etc/wireguard/wg_${host}_2.conf
  125. echo -e "${SkyBlue}:: 使用${GreenBG} bash wg5 ${SkyBlue}命令,可以临时网页下载配置和二维码${Font}"
  126. }
  127. # 安装 WireGuard+Speeder+Udp2Raw 和 SS+Kcp+Udp2RAW 配置
  128. ss_kcp_udp2raw_wg_speed(){
  129. # 下载/编译 shadowsocks-libev
  130. wget -qO- git.io/fhExJ | bash
  131. wget -O ~/ss_wg_set_raw git.io/fpKnF >/dev/null 2>&1
  132. bash ~/ss_wg_set_raw
  133. rm ~/ss_wg_set_raw
  134. }
  135. # 定义文字颜色
  136. Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m"
  137. Font="\033[0m" && Yellow="\033[0;33m" && SkyBlue="\033[0;36m"
  138. echo_SkyBlue(){
  139. echo -e "${SkyBlue}$1${Font}"
  140. }
  141. echo_Yellow(){
  142. echo -e "${Yellow}$1${Font}"
  143. }
  144. echo_GreenBG(){
  145. echo -e "${GreenBG}$1${Font}"
  146. }
  147. echo_RedBG(){
  148. echo -e "${RedBG}$1${Font}"
  149. }
  150. safe_iptables(){
  151. # IPTABLES 设置防火墙规则 脚本 By 蘭雅sRGB 特别感谢 TaterLi 指导
  152. wget -qO safe_iptables.sh git.io/fhUSe && bash safe_iptables.sh
  153. }
  154. # 更新wgmtu脚本
  155. update_self(){
  156. # Get WireGuard Management Command : bash wgmtu
  157. wget -O ~/wgmtu https://raw.githubusercontent.com/hongwenjun/vps_setup/english/wgmtu.sh
  158. }
  159. # 更新 WireGuard
  160. wireguard_update(){
  161. yum update -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  162. apt update -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  163. echo -e "${RedBG} 更新完成 ${Font}"
  164. }
  165. # 卸载 WireGuard
  166. wireguard_remove(){
  167. wg-quick down wg0
  168. yum remove -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  169. apt remove -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  170. rm -rf /etc/wireguard/
  171. echo -e "${RedBG} 卸载完成 ${Font}"
  172. }
  173. # 更新/安装 UDP2RAW KCPTUN UDPspeeder 工具
  174. udp2raw_update()
  175. {
  176. systemctl stop rc-local
  177. # 下载 UDP2RAW
  178. udp2raw_ver=$(wget --no-check-certificate -qO- https://api.github.com/repos/wangyu-/udp2raw-tunnel/releases/latest | grep 'tag_name' | cut -d\" -f4)
  179. wget https://github.com/wangyu-/udp2raw-tunnel/releases/download/${udp2raw_ver}/udp2raw_binaries.tar.gz
  180. tar xf udp2raw_binaries.tar.gz
  181. mv udp2raw_amd64 /usr/bin/udp2raw
  182. rm udp2raw* -rf
  183. rm version.txt
  184. # 下载 KCPTUN
  185. kcp_ver=$(wget --no-check-certificate -qO- https://api.github.com/repos/xtaci/kcptun/releases/latest | grep 'tag_name' | cut -d\" -f4)
  186. kcp_gz_ver=${kcp_ver:1:8}
  187. kcptun_tar_gz=kcptun-linux-amd64-${kcp_gz_ver}.tar.gz
  188. wget https://github.com/xtaci/kcptun/releases/download/${kcp_ver}/$kcptun_tar_gz
  189. tar xf $kcptun_tar_gz
  190. mv server_linux_amd64 /usr/bin/kcp-server
  191. rm $kcptun_tar_gz
  192. rm client_linux_amd64
  193. # 下载 UDPspeeder
  194. udpspeeder_ver=$(wget --no-check-certificate -qO- https://api.github.com/repos/wangyu-/UDPspeeder/releases/latest | grep 'tag_name' | cut -d\" -f4)
  195. wget https://github.com/wangyu-/UDPspeeder/releases/download/${udpspeeder_ver}/speederv2_binaries.tar.gz
  196. tar xf speederv2_binaries.tar.gz
  197. mv speederv2_amd64 /usr/bin/speederv2
  198. rm speederv2* -rf
  199. rm version.txt
  200. systemctl restart rc-local
  201. ps aux | grep -e kcp -e udp -e speed -e ss-server
  202. ss-server -h | head -2 && kcp-server -v && udp2raw -h | head -2 && speederv2 -h | head -2
  203. }
  204. rc-local_remove(){
  205. echo -e "${RedBG} 卸载Udp2Raw套接服务配置 /etc/rc.local ${Font}"
  206. systemctl stop rc-local
  207. rm /usr/bin/udp2raw /usr/bin/kcp-server /usr/bin/speederv2
  208. ps aux | grep -e kcp -e udp -e speed
  209. mv /etc/rc.local ~/rc.local
  210. echo -e "${RedBG} 卸载完成,备份在 /root/rc.local ${Font}"
  211. }
  212. update_remove_menu(){
  213. echo -e "${RedBG} 更新/卸载 WireGuard服务端和Udp2Raw ${Font}"
  214. echo -e "${Green}> 1. 更新 WireGuard 服务端"
  215. echo -e "> 2. 卸载 WireGuard 服务端"
  216. echo -e "> 3. 更新 Udp2Raw KCPTUN UDPspeeder 软件"
  217. echo -e "> 4. 卸载 Udp2Raw KCPTUN UDPspeeder 服务套件"
  218. echo -e "> 5. 退出${Font}"
  219. echo
  220. read -p "请输入数字(1-4):" num_x
  221. case "$num_x" in
  222. 1)
  223. wireguard_update
  224. ;;
  225. 2)
  226. wireguard_remove
  227. ;;
  228. 3)
  229. udp2raw_update
  230. ;;
  231. 4)
  232. rc-local_remove
  233. ;;
  234. 5)
  235. exit 1
  236. ;;
  237. *)
  238. ;;
  239. esac
  240. }
  241. # 删除末尾的Peer
  242. del_last_peer(){
  243. peer_key=$(wg show wg0 allowed-ips | tail -1 | awk '{print $1}')
  244. wg set wg0 peer $peer_key remove
  245. wg-quick save wg0
  246. echo -e "${SkyBlue}:: 删除客户端 peer: ${Yellow} ${peer_key} ${SkyBlue} 完成.${Font}"
  247. }
  248. # 显示激活Peer表
  249. display_peer(){
  250. # peer和ip表写临时文件
  251. wg show wg0 allowed-ips > /tmp/peer_list
  252. # 显示 peer和ip表
  253. echo -e "${RedBG} ID ${GreenBG} Peer: <base64 public key> ${SkyBlue} IP_Addr: ${Font}"
  254. i=1
  255. while read -r line || [[ -n $line ]]; do
  256. peer=$(echo $line | awk '{print $1}')
  257. ip=$(echo $line | awk '{print $2}')
  258. line="> ${Red}${i} ${Yellow}${peer}${Font} ${ip}"
  259. echo -e $line && let i++
  260. done < /tmp/peer_list
  261. }
  262. # 选择删除Peer客户端
  263. del_peer(){
  264. display_peer
  265. echo
  266. echo -e "${RedBG}请选择 IP_Addr 对应 ID 号码,指定客户端配置将删除! ${Font}"
  267. read -p "请输入ID号数字(1-X):" x
  268. peer_cnt=$(cat /tmp/peer_list | wc -l)
  269. if [[ ${x} -ge 1 ]] && [[ ${x} -le ${peer_cnt} ]]; then
  270. i=$x
  271. peer_key=$(cat /tmp/peer_list | head -n $i | tail -1 | awk '{print $1}')
  272. wg set wg0 peer $peer_key remove
  273. wg-quick save wg0
  274. echo -e "${SkyBlue}:: 删除客户端 peer: ${Yellow} ${peer_key} ${SkyBlue} 完成.${Font}"
  275. else
  276. echo -e "${SkyBlue}:: 命令使用: ${GreenBG} wg set wg0 peer <base64 public key> remove ${Font}"
  277. fi
  278. rm /tmp/peer_list
  279. }
  280. # 添加新的客户端peer
  281. add_peer(){
  282. # 服务器 IP 端口 ,新客户端 序号和IP
  283. port=$(wg show wg0 listen-port)
  284. get_serverip && host=$(hostname -s) && cd /etc/wireguard
  285. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  286. ipnum=$(wg show wg0 allowed-ips | tail -1 | awk '{print $2}' | awk -F '[./]' '{print $4}')
  287. i=$((10#${ipnum}+1)) && ip=10.0.0.${i} ip6=${ipv6_range}${i}
  288. # 生成客户端配置文件
  289. cat <<EOF >wg_${host}_$i.conf
  290. [Interface]
  291. PrivateKey = $(cat cprivatekey)
  292. Address = $ip/24, $ip6/64
  293. DNS = 8.8.8.8, 2001:4860:4860::8888
  294. [Peer]
  295. PublicKey = $(wg show wg0 public-key)
  296. Endpoint = $serverip:$port
  297. AllowedIPs = 0.0.0.0/0, ::0/0
  298. PersistentKeepalive = 25
  299. EOF
  300. # 在wg服务器中生效客户端peer
  301. wg set wg0 peer $(cat cpublickey) allowed-ips "${ip}/32, ${ip6}"
  302. wg-quick save wg0
  303. # 显示客户端
  304. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o wg_${host}_$i.png
  305. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o - -t UTF8
  306. echo -e "${SkyBlue}:: 新客户端peer添加完成; 文件:${Yellow} /etc/wireguard/wg_${host}_$i.conf ${Font}"
  307. cat /etc/wireguard/wg_${host}_$i.conf
  308. }
  309. wg_clients_menu(){
  310. echo -e "${RedBG} Add/Delete WireGuard Client Peer Management ${Font}"
  311. echo -e "${Green}> 1. Add One WireGuard Client Peer "
  312. echo -e "> 2. Delete Last WireGuard Client Peer "
  313. echo -e "> 3. Delete Choose WireGuard Client Peer "
  314. echo "------------------------------------------------------"
  315. echo -e "${SkyBlue}> 4. Exit"
  316. echo -e "> 5.${RedBG} Reset WireGuard All Client Peer${Font}"
  317. echo
  318. read -p "Please Enter the Number(1-5):" num_x
  319. case "$num_x" in
  320. 1)
  321. add_peer
  322. ;;
  323. 2)
  324. del_last_peer
  325. ;;
  326. 3)
  327. del_peer
  328. ;;
  329. 4)
  330. display_peer
  331. exit 1
  332. ;;
  333. 5)
  334. wg_clients
  335. ;;
  336. *)
  337. ;;
  338. esac
  339. }
  340. # 设置菜单
  341. start_menu(){
  342. clear
  343. echo_RedBG " One-Step Automated Install WireGuard Script For Debian_9 Ubuntu Centos_7 "
  344. echo_GreenBG " Open Source Project: https://github.com/hongwenjun/vps_setup "
  345. echo -e "${Green}> 1. Display Client Configuration and QR code for Mobile Phone "
  346. echo -e "> 2. Modify WireGuard Server MTU Number"
  347. echo -e "> 3. Modify WireGuard Server Port"
  348. echo -e "> 4. WireGuard+Speeder+Udp2Raw and SS+Kcp+Udp2RAW Automated Configuration"
  349. echo "----------------------------------------------------------"
  350. echo -e "${SkyBlue}> 5. Add/Delete WireGuard Client Peer Management"
  351. echo -e "> 6. Update/Remove WireGuard and Udp2Raw Service"
  352. echo -e "> 7. Replace English to Simplified Chinese(中文)"
  353. echo -e "> 8. ${RedBG} IPTABLES Firewall Setup Script ${Font}"
  354. echo
  355. echo_SkyBlue "Usage: ${GreenBG} bash wgmtu ${SkyBlue} [ setup | remove | vps | bench | -U ] "
  356. echo_SkyBlue " [ v2ray | vnstat | log | trace | -h ] "
  357. echo
  358. read -p "Please Enter the Number(1-8):" num
  359. case "$num" in
  360. 1)
  361. conf_QRcode
  362. ;;
  363. 2)
  364. setmtu
  365. ;;
  366. 3)
  367. setport
  368. ;;
  369. 4)
  370. ss_kcp_udp2raw_wg_speed
  371. ;;
  372. 5)
  373. wg_clients_menu
  374. ;;
  375. 6)
  376. update_remove_menu
  377. update_self
  378. exit 1
  379. ;;
  380. 7)
  381. wget -O wgmtu https://git.io/wgmtu && bash wgmtu
  382. exit 1
  383. ;;
  384. 8)
  385. safe_iptables
  386. ;;
  387. # Manage menu input command line parameters
  388. setup)
  389. ss_kcp_udp2raw_wg_speed
  390. ;;
  391. remove)
  392. wireguard_remove
  393. rc-local_remove
  394. ;;
  395. 9999)
  396. bash <(curl -L -s https://git.io/wireguard.sh) 9999
  397. ;;
  398. -U)
  399. update_self
  400. ;;
  401. -h)
  402. wgmtu_help
  403. ;;
  404. vps)
  405. bash <(curl -L -s https://git.io/vps.setup)
  406. ;;
  407. vnstat)
  408. wget -qO- git.io/fxxlb | bash
  409. ;;
  410. bench)
  411. wget -qO- git.io/superbench.sh | bash
  412. ;;
  413. trace)
  414. wget -qO- git.io/fp5lf | bash
  415. ;;
  416. v2ray)
  417. bash <(curl -L -s https://git.io/v2ray_ss.sh)
  418. ;;
  419. log)
  420. cat vps_setup.log
  421. ;;
  422. *)
  423. display_peer
  424. ;;
  425. esac
  426. }
  427. wgmtu_help(){
  428. echo_SkyBlue "Usage: ${GreenBG} bash wgmtu ${SkyBlue} [ setup | remove | vps | bench | -U ] "
  429. echo_SkyBlue " [ v2ray | vnstat | log | trace | -h ] "
  430. echo
  431. echo_Yellow "[setup 惊喜 | remove 卸载 | vps 脚本 | bench 基准测试 | -U 更新]"
  432. echo_Yellow "[v2ray 你懂 | vnstat 流量 | log 信息 | trace 网络回程 | -h 帮助]"
  433. }
  434. # WireGuard 管理命令 bash wgmtu 命令行参数
  435. if [[ $# > 0 ]]; then
  436. key="$1"
  437. case $key in
  438. setup)
  439. ss_kcp_udp2raw_wg_speed
  440. ;;
  441. remove)
  442. wireguard_remove
  443. rc-local_remove
  444. ;;
  445. 88)
  446. scp_conf
  447. ;;
  448. 9999)
  449. bash <(curl -L -s https://git.io/fpnQt) 9999
  450. ;;
  451. -U)
  452. update_self
  453. ;;
  454. -h)
  455. wgmtu_help
  456. ;;
  457. vps)
  458. bash <(curl -L -s https://git.io/vps.sh)
  459. ;;
  460. vnstat)
  461. wget -qO- git.io/fxxlb | bash
  462. ;;
  463. bench)
  464. wget -qO- git.io/superbench.sh | bash
  465. ;;
  466. trace)
  467. wget -qO- git.io/fp5lf | bash
  468. ;;
  469. v2ray)
  470. bash <(curl -L -s https://git.io/v2ray.ss)
  471. ;;
  472. log)
  473. cat vps_setup.log
  474. ;;
  475. esac
  476. else
  477. start_menu
  478. fi