wgmtu.sh 14 KB

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