wgmtu.sh 13 KB

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