wgmtu.sh 14 KB

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