wgmtu.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #!/bin/bash
  2. # 定义文字颜色
  3. Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m" && Font="\033[0m"
  4. Yellow='\033[0;33m' && SkyBlue='\033[0;36m'
  5. # 修改mtu数值
  6. setmtu(){
  7. echo -e "${GreenBG}WireGuard 修改服务器端MTU值,最大效率加大网速,默认值 MTU = 1420 "
  8. echo -e "WireGuard 客户端可以MTU参数自动,请修改电脑客户端TunSafe配置把MTU行注释掉。${Font}"
  9. read -p "请输入数字(1200--1500): " num
  10. if [[ ${num} -ge 1200 ]] && [[ ${num} -le 1500 ]]; then
  11. mtu=$num
  12. else
  13. mtu=1420
  14. fi
  15. wg-quick down wg0
  16. sed -i "s/MTU = .*$/MTU = ${mtu}/g" /etc/wireguard/wg0.conf
  17. wg-quick up wg0
  18. echo -e "${RedBG} 服务器端MTU值已经修改! ${Font}"
  19. }
  20. # 修改端口号
  21. setport(){
  22. echo -e "${GreenBG}修改 WireGuard 服务器端端口号,客户端要自行修改${Font}"
  23. read -p "请输入数字(100--60000): " num
  24. if [[ ${num} -ge 100 ]] && [[ ${num} -le 60000 ]]; then
  25. port=$num
  26. wg-quick down wg0
  27. sed -i "s/ListenPort = .*$/ListenPort = ${port}/g" /etc/wireguard/wg0.conf
  28. wg-quick up wg0
  29. echo -e "${RedBG} 端口号已经修改, 客户端请手工修改! ${Font}"
  30. else
  31. echo -e "${RedBG} 没有修改端口号! ${Font}"
  32. fi
  33. }
  34. # 显示手机客户端二维码
  35. wgconf(){
  36. echo -e "${RedBG}:: 显示手机客户端二维码 (如改端口,请先菜单5重置客户端配置) ${Font}"
  37. read -p "请输入数字(2-9),默认2号: " x
  38. if [[ ${x} -ge 2 ]] && [[ ${x} -le 9 ]]; then
  39. i=$x
  40. else
  41. i=2
  42. fi
  43. host=$(hostname -s)
  44. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o - -t UTF8
  45. echo -e "${GreenBG}:: 配置文件: wg_${host}_$i.conf 生成二维码,请用手机客户端扫描使用 ${Font}"
  46. echo -e "${RedBG}SSH工具推荐Git-Bash 2.20; GCP_SSH(浏览器)字体Courier New 二维码正常${Font}"
  47. }
  48. # 重置 WireGuard 客户端配置和数量
  49. wg_clients(){
  50. echo -e "${RedBG}:: 注意原来的客户端配置都会删除,按 Ctrl+ C 可以紧急撤销 ${Font}"
  51. wget -O ~/wg100 https://git.io/fp6r0 >/dev/null 2>&1
  52. bash ~/wg100
  53. rm ~/wg100
  54. }
  55. # 安装Udp2Raw服务TCP伪装,加速功能
  56. ss_kcp_udp2raw_wg_speed(){
  57. # 一键安装 SS+Kcp+Udp2Raw 脚本 快速安装 for debian 9
  58. wget -qO- git.io/fpZIW | bash
  59. wget -O ~/ss_wg_set_raw https://git.io/fpKnF >/dev/null 2>&1
  60. bash ~/ss_wg_set_raw
  61. rm ~/ss_wg_set_raw
  62. }
  63. # 常用工具和配置
  64. get_tools_conf(){
  65. apt-get update
  66. apt-get install -y htop tmux screen iperf3
  67. wget -O .vimrc --no-check-certificate https://raw.githubusercontent.com/hongwenjun/srgb/master/vim/_vimrc
  68. wget -O .bashrc --no-check-certificate https://raw.githubusercontent.com/hongwenjun/srgb/master/vim/_bashrc
  69. wget -O .tmux.conf --no-check-certificate https://raw.githubusercontent.com/hongwenjun/tmux_for_windows/master/.tmux.conf
  70. }
  71. # 主菜单输入数字 88 # 隐藏功能:从源VPS克隆服务端配置,获得常用工具和配置
  72. scp_conf(){
  73. echo -e "${RedBG}:: 警告: 警告: 警告: VPS服务器已经被GFW防火墙关照,按 Ctrl+ C 可以紧急逃离! ${Font}"
  74. echo "隐藏功能:从源VPS克隆服务端配置,共用客户端配置"
  75. read -p "请输入源VPS的IP地址(域名):" vps_ip
  76. cmd="scp root@${vps_ip}:/etc/wireguard/* /etc/wireguard/. "
  77. echo -e "${GreenBG}# ${cmd} ${Font} 现在运行scp命令,按提示输入yes,源vps的root密码"
  78. ${cmd}
  79. wg-quick down wg0 >/dev/null 2>&1
  80. wg-quick up wg0 >/dev/null 2>&1
  81. echo -e "${RedBG} 我真不知道WG服务器端是否已经使用源vps的配置启动! ${Font}"
  82. if [ ! -f '/usr/bin/tmux' ]; then
  83. get_tools_conf
  84. fi
  85. }
  86. # 隐藏功能开放: 一键脚本全家桶
  87. onekey_plus(){
  88. echo -e "${RedBG} 一键安装设置全家桶 by 蘭雅sRGB ${Font}"
  89. cat <<EOF
  90. # 下载 IPTABLES 设置防火墙规则 脚本 By 蘭雅sRGB
  91. wget -qO safe_iptables.sh git.io/fhUSe && bash safe_iptables.sh
  92. # Google Cloud Platform GCP实例开启密码与root用户登陆
  93. wget -qO- git.io/fpQWf | bash
  94. # 一键安装 vnstat 流量检测 by 蘭雅sRGB
  95. wget -qO- git.io/fxxlb | bash
  96. # 一键安装wireguard 脚本 Debian 9 (源:逗比网安装笔记)
  97. wget -qO- git.io/fptwc | bash
  98. # 一键 WireGuard 多用户配置共享脚本 by 蘭雅sRGB
  99. wget -qO- https://git.io/fpnQt | bash
  100. # 一键安装 SS+Kcp+Udp2Raw 脚本 快速安装 for Debian 9
  101. wget -qO- git.io/fpZIW | bash
  102. # 一键安装 SS+Kcp+Udp2Raw 脚本 for Debian 9 Ubuntu (编译安装)
  103. wget -qO- git.io/fx6UQ | bash
  104. # Telegram 代理 MTProxy Go版 一键脚本(源:逗比网)
  105. wget -qO mtproxy_go.sh git.io/fpWo4 && bash mtproxy_go.sh
  106. # linux下golang环境搭建自动脚本 by 蘭雅sRGB
  107. wget -qO- https://git.io/fp4jf | bash
  108. # SuperBench.sh 一键测试服务器的基本参数
  109. wget -qO- git.io/superbench.sh | bash
  110. # 使用BestTrace查看VPS的去程和回程
  111. wget -qO- git.io/fp5lf | bash
  112. # qrencode 生成二维码 -o- 参数显示在屏幕 -t utf8 文本格式
  113. cat wg_vultr_5.conf | qrencode -o- -t utf8
  114. EOF
  115. echo -e "${GreenBG} 开源项目:https://github.com/hongwenjun/vps_setup ${Font}"
  116. }
  117. safe_iptables(){
  118. # IPTABLES 设置防火墙规则 脚本 By 蘭雅sRGB 特别感谢 TaterLi 指导
  119. wget -qO safe_iptables.sh git.io/fhUSe && bash safe_iptables.sh
  120. }
  121. # 更新wgmtu脚本
  122. update(){
  123. # 安装 bash wgmtu 脚本用来设置服务器
  124. wget -O ~/wgmtu https://raw.githubusercontent.com/hongwenjun/vps_setup/master/Wireguard/wgmtu.sh >/dev/null 2>&1
  125. }
  126. # 更新 WireGuard
  127. wireguard_update(){
  128. yum update -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  129. apt update -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  130. echo -e "${RedBG} 更新完成 ${Font}"
  131. }
  132. # 卸载 WireGuard
  133. wireguard_remove(){
  134. wg-quick down wg0
  135. yum remove -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  136. apt remove -y wireguard-dkms wireguard-tools >/dev/null 2>&1
  137. rm -rf /etc/wireguard/
  138. echo -e "${RedBG} 卸载完成 ${Font}"
  139. }
  140. rc-local_remove(){
  141. echo -e "${RedBG}推荐: 卸载Udp2Raw服务使用 vim /etc/rc.local 手工编辑修改 ${Font}"
  142. echo -e "${GreenBG} 按 Ctrl + C 可以取消 卸载操作 ${Font}"
  143. read -p "请任意键确认:" yes
  144. systemctl stop rc-local
  145. mv /etc/rc.local ~/rc.local
  146. echo -e "${RedBG} 卸载完成,rc.local 备份在 /root 目录 ${Font}"
  147. }
  148. update_remove_menu(){
  149. echo -e "${RedBG} 更新 或卸载 WireGuard服务端和Udp2Raw 子菜单 ${Font}"
  150. echo -e "${Green}> 1. 更新 WireGuard 服务端"
  151. echo -e "> 2. 卸载 WireGuard 服务端"
  152. echo -e "> 3. 卸载 Udp2Raw 服务"
  153. echo -e "> 4. 退出${Font}"
  154. echo
  155. read -p "请输入数字(1-4):" num_x
  156. case "$num_x" in
  157. 1)
  158. wireguard_update
  159. ;;
  160. 2)
  161. wireguard_remove
  162. ;;
  163. 3)
  164. rc-local_remove
  165. ;;
  166. 4)
  167. exit 1
  168. ;;
  169. *)
  170. ;;
  171. esac
  172. }
  173. # 删除最末尾的peer
  174. del_last_peer(){
  175. peer_key=$(wg show wg0 allowed-ips | tail -1 | awk '{print $1}')
  176. wg set wg0 peer $peer_key remove
  177. wg-quick save wg0
  178. echo -e "${SkyBlue}删除客户端 peer: ${Yellow} ${peer_key} ${SkyBlue} 完成.${Font}"
  179. }
  180. # 显示激活Peer表
  181. display_peer(){
  182. # peer和ip表写临时文件
  183. wg show wg0 allowed-ips > /tmp/peer_list
  184. peer_cnt=$(cat /tmp/peer_list | wc -l)
  185. # 显示 peer和ip表
  186. echo -e "${RedBG} ID ${GreenBG} Peer: <base64 public key> ${SkyBlue} IP_Addr: ${Font}"
  187. for i in `seq 1 250`
  188. do
  189. peer=$(cat /tmp/peer_list | head -n $i | tail -1 | awk '{print $1}')
  190. ip=$(cat /tmp/peer_list | head -n $i | tail -1 | awk '{print $2}')
  191. line="> ${Red}${i} ${Yellow}${peer}${Font} ${ip}"
  192. echo -e $line
  193. if [ $i -ge $peer_cnt ]; then
  194. break
  195. fi
  196. done
  197. }
  198. del_peer(){
  199. display_peer
  200. echo
  201. echo -e "${RedBG}请选择 IP_Addr 对应 ID 号码,指定客户端配置将删除! ${Font}"
  202. read -p "请输入ID号数字(1-X):" x
  203. if [[ ${x} -ge 1 ]] && [[ ${x} -le ${peer_cnt} ]]; then
  204. i=$x
  205. peer_key=$(cat /tmp/peer_list | head -n $i | tail -1 | awk '{print $1}')
  206. wg set wg0 peer $peer_key remove
  207. wg-quick save wg0
  208. echo -e "${SkyBlue}删除客户端 peer: ${Yellow} ${peer_key} ${SkyBlue} 完成.${Font}"
  209. else
  210. echo -e "命令行使用: ${GreenBG} wg set wg0 peer <base64 public key> remove ${Font}"
  211. fi
  212. rm /tmp/peer_list
  213. }
  214. # 添加新的客户端peer
  215. add_peer(){
  216. # 服务器 IP 端口 ,新客户端 序号和IP
  217. port=$(wg | grep 'listening port:' | awk '{print $3}')
  218. serverip=$(curl -4 ip.sb) && host=$(hostname -s) && cd /etc/wireguard
  219. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  220. ipnum=$(wg show wg0 allowed-ips | tail -1 | awk '{print $2}' | awk -F '[./]' '{print $4}')
  221. i=$((10#${ipnum}+1)) && ip=10.0.0.${i}
  222. # 生成客户端配置文件
  223. cat <<EOF >wg_${host}_$i.conf
  224. [Interface]
  225. PrivateKey = $(cat cprivatekey)
  226. Address = $ip/24
  227. DNS = 8.8.8.8
  228. [Peer]
  229. PublicKey = $(cat spublickey)
  230. Endpoint = $serverip:$port
  231. AllowedIPs = 0.0.0.0/0, ::0/0
  232. PersistentKeepalive = 25
  233. EOF
  234. # 在wg服务器中生效客户端peer
  235. wg set wg0 peer $(cat cpublickey) allowed-ips $ip/32
  236. wg-quick save wg0
  237. # 显示客户端
  238. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o wg_${host}_$i.png
  239. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o - -t UTF8
  240. echo -e "${SkyBlue}新客户端peer添加完成; 文件:${Yellow} /etc/wireguard/wg_${host}_$i.conf ${Font}"
  241. cat /etc/wireguard/wg_${host}_$i.conf
  242. }
  243. wg_clients_menu(){
  244. echo -e "${RedBG} 添加/删除 WireGuard 客户端配置 子菜单 ${Font}"
  245. echo -e "${Green}> 1. 添加一个 WireGuard 客户端配置"
  246. echo -e "> 2. 删除末尾 WireGuard 客户端配置"
  247. echo -e "> 3. 指定删除 WireGuard 客户端配置"
  248. echo "------------------------------------------------------"
  249. echo -e "> 4. 退出"
  250. echo -e "> 5.${RedBG} 重置 WireGuard 客户端配置和数量 ${Font}"
  251. echo
  252. read -p "请输入数字(1-5):" num_x
  253. case "$num_x" in
  254. 1)
  255. add_peer
  256. ;;
  257. 2)
  258. del_last_peer
  259. ;;
  260. 3)
  261. del_peer
  262. ;;
  263. 4)
  264. exit 1
  265. ;;
  266. 5)
  267. wg_clients
  268. ;;
  269. *)
  270. ;;
  271. esac
  272. }
  273. # 设置菜单
  274. start_menu(){
  275. clear
  276. echo -e "${RedBG} 一键安装 WireGuard 脚本 For Debian_9 Ubuntu Centos_7 ${Font}"
  277. echo -e "${GreenBG} 开源项目:https://github.com/hongwenjun/vps_setup ${Font}"
  278. echo -e "${Green}> 1. 显示手机客户端二维码"
  279. echo -e "> 2. 修改 WireGuard 服务器端 MTU 值"
  280. echo -e "> 3. 修改 WireGuard 端口号"
  281. echo -e "> 4. 安装 WireGuard+Speeder+Udp2Raw 和 SS+Kcp+Udp2RAW 一键脚本"
  282. echo "----------------------------------------------------------"
  283. echo -e "${SkyBlue}> 5. 添加/删除 WireGuard 客户端配置"
  284. echo -e "> 6. 更新/卸载 WireGuard服务端和Udp2Raw"
  285. echo -e "> 7. vps_setup 一键脚本全家桶大礼包"
  286. echo -e "> 8. ${RedBG} 小白一键设置防火墙 ${Font}"
  287. echo
  288. read -p "请输入数字(1-8):" num
  289. case "$num" in
  290. 1)
  291. wgconf
  292. ;;
  293. 2)
  294. setmtu
  295. ;;
  296. 3)
  297. setport
  298. ;;
  299. 4)
  300. ss_kcp_udp2raw_wg_speed
  301. ;;
  302. 5)
  303. wg_clients_menu
  304. ;;
  305. 6)
  306. update_remove_menu
  307. update
  308. exit 1
  309. ;;
  310. 7)
  311. onekey_plus
  312. ;;
  313. 8)
  314. safe_iptables
  315. ;;
  316. 88)
  317. scp_conf
  318. ;;
  319. *)
  320. display_peer
  321. ;;
  322. esac
  323. }
  324. start_menu