wireguard.sh 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. #!/bin/bash
  2. dir="/etc/VPS-MX"
  3. SCPfrm="${dir}/herramientas" && [[ ! -d ${SCPfrm} ]] && exit
  4. SCPinst="${dir}/protocolos" && [[ ! -d ${SCPinst} ]] && exit
  5. [[ ! -d /usr/local/megat ]] && exit
  6. mportas() {
  7. unset portas
  8. portas_var=$(lsof -V -i tcp -P -n | grep -v "ESTABLISHED" | grep -v "COMMAND" | grep "LISTEN")
  9. while read port; do
  10. var1=$(echo $port | awk '{print $1}') && var2=$(echo $port | awk '{print $9}' | awk -F ":" '{print $2}')
  11. [[ "$(echo -e $portas | grep "$var1 $var2")" ]] || portas+="$var1 $var2\n"
  12. done <<<"$portas_var"
  13. i=1
  14. echo -e "$portas"
  15. }
  16. fun_ip() {
  17. MIP=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
  18. MIP2=$(wget -qO- ifconfig.me)
  19. [[ "$MIP" != "$MIP2" ]] && IP="$MIP2" || IP="$MIP"
  20. }
  21. [[ ! -d /etc/VPS-MX/wireguard ]] && mkdir /etc/VPS-MX/wireguard
  22. # Detect Debian users running the script with "sh" instead of bash
  23. if readlink /proc/$$/exe | grep -q "dash"; then
  24. echo 'Este instalador debe ejecutarse con "bash", no con "sh".'
  25. exit
  26. fi
  27. # Discard stdin. Needed when running from an one-liner which includes a newline
  28. read -N 999999 -t 0.001
  29. # Detect OpenVZ 6
  30. if [[ $(uname -r | cut -d "." -f 1) -eq 2 ]]; then
  31. echo "El sistema está ejecutando un kernel antiguo, que es incompatible con este instalador"
  32. exit
  33. fi
  34. # Detect OS
  35. # $os_version variables aren't always in use, but are kept here for convenience
  36. if grep -qs "ubuntu" /etc/os-release; then
  37. os="ubuntu"
  38. os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')
  39. elif [[ -e /etc/debian_version ]]; then
  40. os="debian"
  41. os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)
  42. elif [[ -e /etc/centos-release ]]; then
  43. os="centos"
  44. os_version=$(grep -oE '[0-9]+' /etc/centos-release | head -1)
  45. elif [[ -e /etc/fedora-release ]]; then
  46. os="fedora"
  47. os_version=$(grep -oE '[0-9]+' /etc/fedora-release | head -1)
  48. else
  49. echo "Este instalador parece estar ejecutándose en una distribución no compatible. Las distribuciones compatibles son Ubuntu, Debian, CentOS y Fedora"
  50. exit
  51. fi
  52. if [[ "$os" == "ubuntu" && "$os_version" -lt 1804 ]]; then
  53. echo "Se requiere Ubuntu 18.04 o superior para usar este instalador. Esta versión de Ubuntu es demasiado antigua y no es compatible"
  54. exit
  55. fi
  56. if [[ "$os" == "debian" && "$os_version" -lt 9 ]]; then
  57. echo "Se requiere Debian 9+ o superior para usar este instalador. Esta versión de Debian es demasiado antigua y no tiene soporte"
  58. exit
  59. fi
  60. if [[ "$os" == "centos" && "$os_version" -lt 7 ]]; then
  61. echo "CentOS 7 or higher is required to use this installer. This version of CentOS is too old and unsupported."
  62. exit
  63. fi
  64. # Detect environments where $PATH does not include the sbin directories
  65. if ! grep -q sbin <<<"$PATH"; then
  66. echo '$PATH no incluye sen. Intenta usar "su -" en lugar de "su".'
  67. exit
  68. fi
  69. systemd-detect-virt -cq
  70. is_container="$?"
  71. if [[ "$os" == "fedora" && "$os_version" -eq 31 && $(uname -r | cut -d "." -f 2) -lt 6 && ! "$is_container" -eq 0 ]]; then
  72. echo 'Fedora 31 is supported, but the kernel is outdated. Upgrade the kernel using "dnf upgrade kernel" and restart.'
  73. exit
  74. fi
  75. if [[ "$EUID" -ne 0 ]]; then
  76. echo "Este instalador debe ejecutarse con privilegios de superusuario"
  77. exit
  78. fi
  79. if [[ "$is_container" -eq 0 ]]; then
  80. if [ "$(uname -m)" != "x86_64" ]; then
  81. echo "En sistemas en contenedores, este instalador solo admite la arquitectura x86_64. El sistema se ejecuta en $(uname -m) y no es compatible"
  82. exit
  83. fi
  84. # TUN device is required to use BoringTun if running inside a container
  85. if [[ ! -e /dev/net/tun ]] || ! (exec 7<>/dev/net/tun) 2>/dev/null; then
  86. echo "El sistema no tiene disponible el dispositivo TUN. TUN debe estar habilitado antes de ejecutar este instalador"
  87. exit
  88. fi
  89. fi
  90. function setup_environment() {
  91. ### define colors ###
  92. lightred=$'\033[1;31m' # light red
  93. red=$'\033[0;31m' # red
  94. lightgreen=$'\033[1;32m' # light green
  95. green=$'\033[0;32m' # green
  96. lightblue=$'\033[1;34m' # light blue
  97. blue=$'\033[0;34m' # blue
  98. lightpurple=$'\033[1;35m' # light purple
  99. purple=$'\033[0;35m' # purple
  100. lightcyan=$'\033[1;36m' # light cyan
  101. cyan=$'\033[0;36m' # cyan
  102. lightgray=$'\033[0;37m' # light gray
  103. white=$'\033[1;37m' # white
  104. brown=$'\033[0;33m' # brown
  105. yellow=$'\033[1;33m' # yellow
  106. darkgray=$'\033[1;30m' # dark gray
  107. black=$'\033[0;30m' # black
  108. nocolor=$'\e[0m' # no color
  109. echo -e -n "${lightred}"
  110. echo -e -n "${red}"
  111. echo -e -n "${lightgreen}"
  112. echo -e -n "${green}"
  113. echo -e -n "${lightblue}"
  114. echo -e -n "${blue}"
  115. echo -e -n "${lightpurple}"
  116. echo -e -n "${purple}"
  117. echo -e -n "${lightcyan}"
  118. echo -e -n "${cyan}"
  119. echo -e -n "${lightgray}"
  120. echo -e -n "${white}"
  121. echo -e -n "${brown}"
  122. echo -e -n "${yellow}"
  123. echo -e -n "${darkgray}"
  124. echo -e -n "${black}"
  125. echo -e -n "${nocolor}"
  126. clear
  127. # Set Vars
  128. LOGFILE='/var/log/wireguardSH.log'
  129. }
  130. new_client_dns() {
  131. echo -e -n "${lightgreen}"
  132. echo "Seleccione un servidor DNS para el cliente"
  133. echo " 1) DNS DEFAULT del sistema actual"
  134. echo " 2) Google"
  135. echo " 3) 1.1.1.1"
  136. echo " 4) OpenDNS"
  137. echo " 5) Quad9"
  138. echo " 6) AdGuard"
  139. echo -e -n "${nocolor}"
  140. read -p "DNS server [1]: " dns
  141. until [[ -z "$dns" || "$dns" =~ ^[1-6]$ ]]; do
  142. echo -e -n "${red}"
  143. echo "$dns: invalid selection."
  144. echo -e -n "${green}"
  145. read -p "DNS server [1]: " dns
  146. done
  147. # DNS
  148. case "$dns" in
  149. 1 | "")
  150. # Locate the proper resolv.conf
  151. # Needed for systems running systemd-resolved
  152. if grep -q '^nameserver 127.0.0.53' "/etc/resolv.conf"; then
  153. resolv_conf="/run/systemd/resolve/resolv.conf"
  154. else
  155. resolv_conf="/etc/resolv.conf"
  156. fi
  157. # Extract nameservers and provide them in the required format
  158. dns=$(grep -v '^#\|^;' "$resolv_conf" | grep '^nameserver' | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | xargs | sed -e 's/ /, /g')
  159. ;;
  160. 2)
  161. dns="8.8.8.8, 8.8.4.4"
  162. ;;
  163. 3)
  164. dns="1.1.1.1, 1.0.0.1"
  165. ;;
  166. 4)
  167. dns="208.67.222.222, 208.67.220.220"
  168. ;;
  169. 5)
  170. dns="9.9.9.9, 149.112.112.112"
  171. ;;
  172. 6)
  173. dns="94.140.14.14, 94.140.15.15"
  174. ;;
  175. esac
  176. }
  177. new_client_setup() {
  178. # Given a list of the assigned internal IPv4 addresses, obtain the lowest still
  179. # available octet. Important to start looking at 2, because 1 is our gateway.
  180. octet=2
  181. while grep AllowedIPs /etc/wireguard/wg0.conf | cut -d "." -f 4 | cut -d "/" -f 1 | grep -q "$octet"; do
  182. ((octet++))
  183. done
  184. # Don't break the WireGuard configuration in case the address space is full
  185. if [[ "$octet" -eq 255 ]]; then
  186. echo "253 clients are already configured. The WireGuard internal subnet is full!"
  187. exit
  188. fi
  189. key=$(wg genkey)
  190. psk=$(wg genpsk)
  191. # Configure client in the server
  192. cat <<EOF >>/etc/wireguard/wg0.conf
  193. # BEGIN_PEER $client
  194. [Peer]
  195. PublicKey = $(wg pubkey <<<$key)
  196. PresharedKey = $psk
  197. AllowedIPs = 10.7.0.$octet/32$(grep -q 'fddd:2c4:2c4:2c4::1' /etc/wireguard/wg0.conf && echo ", fddd:2c4:2c4:2c4::$octet/128")
  198. # END_PEER $client
  199. EOF
  200. # Create client configuration
  201. cat <<EOF >/etc/VPS-MX/wireguard/"$client".conf
  202. [Interface]
  203. Address = 10.7.0.$octet/24$(grep -q 'fddd:2c4:2c4:2c4::1' /etc/wireguard/wg0.conf && echo ", fddd:2c4:2c4:2c4::$octet/64")
  204. DNS = $dns
  205. PrivateKey = $key
  206. [Peer]
  207. PublicKey = $(grep PrivateKey /etc/wireguard/wg0.conf | cut -d " " -f 3 | wg pubkey)
  208. PresharedKey = $psk
  209. AllowedIPs = 0.0.0.0/0, ::/0
  210. Endpoint = $(grep '^# ENDPOINT' /etc/wireguard/wg0.conf | cut -d " " -f 3):$(grep ListenPort /etc/wireguard/wg0.conf | cut -d " " -f 3)
  211. PersistentKeepalive = 25
  212. EOF
  213. }
  214. setup_environment
  215. install() {
  216. echo -e -n "${green}"
  217. # If system has a single IPv4, it is selected automatically. Else, ask the user
  218. if [[ $(ip -4 addr | grep inet | grep -vEc '127(\.[0-9]{1,3}){3}') -eq 1 ]]; then
  219. ip=$(ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}')
  220. else
  221. number_of_ip=$(ip -4 addr | grep inet | grep -vEc '127(\.[0-9]{1,3}){3}')
  222. echo
  223. echo -e -n "${lightgreen}"
  224. echo "¿Qué dirección IPv4 se debe usar?"
  225. ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | nl -s ') '
  226. read -p "IPv4 address [1]: " ip_number
  227. until [[ -z "$ip_number" || "$ip_number" =~ ^[0-9]+$ && "$ip_number" -le "$number_of_ip" ]]; do
  228. echo -e -n "${red}"
  229. echo "$ip_number: invalid selection."
  230. read -p "IPv4 address [1]: " ip_number
  231. echo -e -n "${green}"
  232. done
  233. [[ -z "$ip_number" ]] && ip_number="1"
  234. ip=$(ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | sed -n "$ip_number"p)
  235. fi
  236. # If $ip is a private IP address, the server must be behind NAT
  237. if echo "$ip" | grep -qE '^(10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.|192\.168)'; then
  238. echo
  239. echo -e -n "${lightgreen}"
  240. echo "Este servidor está detrás de NAT. ¿Cuál es la dirección IPv4 pública o el nombre de host?"
  241. # Get public IP and sanitize with grep
  242. get_public_ip=$(grep -m 1 -oE '^[0-9]{1,3}(\.[0-9]{1,3}){3}$' <<<"$(wget -T 10 -t 1 -4qO- "http://ip1.dynupdate.no-ip.com/" || curl -m 10 -4Ls "http://ip1.dynupdate.no-ip.com/")")
  243. read -p "Public IPv4 address / hostname [$get_public_ip]: " public_ip
  244. # If the checkip service is unavailable and user didn't provide input, ask again
  245. until [[ -n "$get_public_ip" || -n "$public_ip" ]]; do
  246. echo -e -n "${red}"
  247. echo "Invalid input."
  248. read -p "Public IPv4 address / hostname: " public_ip
  249. echo -e -n "${green}"
  250. done
  251. [[ -z "$public_ip" ]] && public_ip="$get_public_ip"
  252. fi
  253. # If system has a single IPv6, it is selected automatically
  254. if [[ $(ip -6 addr | grep -c 'inet6 [23]') -eq 1 ]]; then
  255. ip6=$(ip -6 addr | grep 'inet6 [23]' | cut -d '/' -f 1 | grep -oE '([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}')
  256. fi
  257. # If system has multiple IPv6, ask the user to select one
  258. if [[ $(ip -6 addr | grep -c 'inet6 [23]') -gt 1 ]]; then
  259. number_of_ip6=$(ip -6 addr | grep -c 'inet6 [23]')
  260. echo
  261. echo -e -n "${lightgreen}"
  262. echo "Which IPv6 address should be used?"
  263. ip -6 addr | grep 'inet6 [23]' | cut -d '/' -f 1 | grep -oE '([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}' | nl -s ') '
  264. read -p "IPv6 address [1]: " ip6_number
  265. until [[ -z "$ip6_number" || "$ip6_number" =~ ^[0-9]+$ && "$ip6_number" -le "$number_of_ip6" ]]; do
  266. echo -e -n "${red}"
  267. echo "$ip6_number: invalid selection."
  268. read -p "IPv6 address [1]: " ip6_number
  269. echo -e -n "${green}"
  270. done
  271. [[ -z "$ip6_number" ]] && ip6_number="1"
  272. ip6=$(ip -6 addr | grep 'inet6 [23]' | cut -d '/' -f 1 | grep -oE '([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}' | sed -n "$ip6_number"p)
  273. fi
  274. echo
  275. echo -e -n "${lightgreen}"
  276. echo " INGRESE UN PUERTO PARA WireGuard"
  277. #echo -e -n "${nocolor}"
  278. #read -p "Puerto [51820]: " port
  279. #until [[ -z "$port" || "$port" =~ ^[0-9]+$ && "$port" -le 65535 ]]; do
  280. # echo -e -n "${red}"
  281. #echo "$port: invalid port."
  282. #read -p "Puerto [51820]: " port
  283. #echo -e -n "${green}"
  284. #done
  285. while true; do
  286. echo -ne "\033[1;37m"
  287. read -p " Puerto [51820]: " port
  288. echo ""
  289. [[ $(mportas | grep -w "$port") ]] || break
  290. echo -e "\033[1;33m Esta puerta está en uso"
  291. unset port
  292. done
  293. [[ -z "$port" ]] && port="51820"
  294. echo
  295. echo -e -n "${lightgreen}"
  296. echo "Introduzca un nombre para el primer cliente: "
  297. echo -e -n "${nocolor}"
  298. read -p "Nombre [cliente]: " unsanitized_client
  299. # Allow a limited set of characters to avoid conflicts
  300. client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<<"$unsanitized_client")
  301. [[ -z "$client" ]] && client="client"
  302. echo
  303. new_client_dns
  304. # Set up automatic updates for BoringTun if the user is fine with that
  305. if [[ "$is_container" -eq 0 ]]; then
  306. echo
  307. echo -e -n "${lightgreen}"
  308. echo "Se instalará BoringTun para configurar WireGuard en el sistema"
  309. read -p "¿Deberían habilitarse las actualizaciones automáticas para ello? [Y/n]: " boringtun_updates
  310. until [[ "$boringtun_updates" =~ ^[yYnN]*$ ]]; do
  311. echo "$remove: invalid selection."
  312. read -p "Should automatic updates be enabled for it? [Y/n]: " boringtun_updates
  313. done
  314. if [[ "$boringtun_updates" =~ ^[yY]*$ ]]; then
  315. if [[ "$os" == "centos" || "$os" == "fedora" ]]; then
  316. cron="cronie"
  317. elif [[ "$os" == "debian" || "$os" == "ubuntu" ]]; then
  318. cron="cron"
  319. fi
  320. fi
  321. echo -e -n "${nocolor}"
  322. fi
  323. echo
  324. echo -e -n "${lightgreen}"
  325. echo "La instalación de WireGuard está lista para comenzar"
  326. echo -e -n "${nocolor}"
  327. # Install a firewall in the rare case where one is not already available
  328. if ! systemctl is-active --quiet firewalld.service && ! hash iptables 2>/dev/null; then
  329. if [[ "$os" == "centos" || "$os" == "fedora" ]]; then
  330. firewall="firewalld"
  331. # We don't want to silently enable firewalld, so we give a subtle warning
  332. # If the user continues, firewalld will be installed and enabled during setup
  333. echo "También se instalará firewalld, que es necesario para administrar las tablas de enrutamiento"
  334. elif [[ "$os" == "debian" || "$os" == "ubuntu" ]]; then
  335. # iptables is way less invasive than firewalld so no warning is given
  336. firewall="iptables"
  337. fi
  338. fi
  339. echo -e -n "${lightgreen}"
  340. read -n1 -r -p "Presione enter para continuar..."
  341. echo -e -n "${nocolor}"
  342. # Install WireGuard
  343. # If not running inside a container, set up the WireGuard kernel module
  344. if [[ ! "$is_container" -eq 0 ]]; then
  345. if [[ "$os" == "ubuntu" ]]; then
  346. # Ubuntu
  347. apt-get update
  348. apt-get install -y wireguard qrencode $firewall
  349. elif [[ "$os" == "debian" && "$os_version" -eq 10 ]]; then
  350. # Debian 10
  351. if ! grep -qs '^deb .* buster-backports main' /etc/apt/sources.list /etc/apt/sources.list.d/*.list; then
  352. echo "deb http://deb.debian.org/debian buster-backports main" >>/etc/apt/sources.list
  353. fi
  354. apt-get update
  355. # Try to install kernel headers for the running kernel and avoid a reboot. This
  356. # can fail, so it's important to run separately from the other apt-get command.
  357. apt-get install -y linux-headers-"$(uname -r)"
  358. # There are cleaner ways to find out the $architecture, but we require an
  359. # specific format for the package name and this approach provides what we need.
  360. architecture=$(dpkg --get-selections 'linux-image-*-*' | cut -f 1 | grep -oE '[^-]*$' -m 1)
  361. # linux-headers-$architecture points to the latest headers. We install it
  362. # because if the system has an outdated kernel, there is no guarantee that old
  363. # headers were still downloadable and to provide suitable headers for future
  364. # kernel updates.
  365. apt-get install -y linux-headers-"$architecture"
  366. apt-get install -y wireguard qrencode $firewall
  367. elif [[ "$os" == "debian" && "$os_version" -eq 9 ]]; then
  368. # Debian 10
  369. if ! grep -qs '^deb .* stretch-backports main' /etc/apt/sources.list /etc/apt/sources.list.d/*.list; then
  370. echo "deb http://deb.debian.org/debian stretch-backports main" >>/etc/apt/sources.list
  371. fi
  372. apt-get update
  373. # Try to install kernel headers for the running kernel and avoid a reboot. This
  374. # can fail, so it's important to run separately from the other apt-get command.
  375. apt-get install -y linux-headers-"$(uname -r)"
  376. # There are cleaner ways to find out the $architecture, but we require an
  377. # specific format for the package name and this approach provides what we need.
  378. architecture=$(dpkg --get-selections 'linux-image-*-*' | cut -f 1 | grep -oE '[^-]*$' -m 1)
  379. # linux-headers-$architecture points to the latest headers. We install it
  380. # because if the system has an outdated kernel, there is no guarantee that old
  381. # headers were still downloadable and to provide suitable headers for future
  382. # kernel updates.
  383. apt-get install -y linux-headers-"$architecture"
  384. apt-get install -y wireguard qrencode $firewall
  385. elif [[ "$os" == "centos" && "$os_version" -eq 8 ]]; then
  386. # CentOS 8
  387. dnf install -y epel-release elrepo-release
  388. dnf install -y kmod-wireguard wireguard-tools qrencode $firewall
  389. mkdir -p /etc/wireguard/
  390. elif [[ "$os" == "centos" && "$os_version" -eq 7 ]]; then
  391. # CentOS 7
  392. yum install -y epel-release https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
  393. yum install -y yum-plugin-elrepo
  394. yum install -y kmod-wireguard wireguard-tools qrencode $firewall
  395. mkdir -p /etc/wireguard/
  396. elif [[ "$os" == "fedora" ]]; then
  397. # Fedora
  398. dnf install -y wireguard-tools qrencode $firewall
  399. mkdir -p /etc/wireguard/
  400. fi
  401. # Else, we are inside a container and BoringTun needs to be used
  402. else
  403. # Install required packages
  404. if [[ "$os" == "ubuntu" ]]; then
  405. # Ubuntu
  406. apt-get update
  407. apt-get install -y qrencode ca-certificates $cron $firewall
  408. apt-get install -y wireguard-tools --no-install-recommends
  409. elif [[ "$os" == "debian" && "$os_version" -eq 10 ]]; then
  410. # Debian 10
  411. if ! grep -qs '^deb .* buster-backports main' /etc/apt/sources.list /etc/apt/sources.list.d/*.list; then
  412. echo "deb http://deb.debian.org/debian buster-backports main" >>/etc/apt/sources.list
  413. fi
  414. apt-get update
  415. apt-get install -y qrencode ca-certificates $cron $firewall
  416. apt-get install -y wireguard-tools --no-install-recommends
  417. elif [[ "$os" == "debian" && "$os_version" -eq 9 ]]; then
  418. # Debian 10
  419. if ! grep -qs '^deb .* stretch-backports main' /etc/apt/sources.list /etc/apt/sources.list.d/*.list; then
  420. echo "deb http://deb.debian.org/debian stretch-backports main" >>/etc/apt/sources.list
  421. fi
  422. apt-get update
  423. apt-get install -y qrencode ca-certificates $cron $firewall
  424. apt-get install -y wireguard-tools --no-install-recommends
  425. elif [[ "$os" == "centos" && "$os_version" -eq 8 ]]; then
  426. # CentOS 8
  427. dnf install -y epel-release
  428. dnf install -y wireguard-tools qrencode ca-certificates tar $cron $firewall
  429. mkdir -p /etc/wireguard/
  430. elif [[ "$os" == "centos" && "$os_version" -eq 7 ]]; then
  431. # CentOS 7
  432. yum install -y epel-release
  433. yum install -y wireguard-tools qrencode ca-certificates tar $cron $firewall
  434. mkdir -p /etc/wireguard/
  435. elif [[ "$os" == "fedora" ]]; then
  436. # Fedora
  437. dnf install -y wireguard-tools qrencode ca-certificates tar $cron $firewall
  438. mkdir -p /etc/wireguard/
  439. [[ ! -d /etc/VPS-MX/wireguard ]] && mkdir /etc/VPS-MX/wireguard
  440. fi
  441. # Grab the BoringTun binary using wget or curl and extract into the right place.
  442. # Don't use this service elsewhere without permission! Contact me before you do!
  443. { wget -qO- https://wg.nyr.be/1/latest/download 2>/dev/null || curl -sL https://wg.nyr.be/1/latest/download; } | tar xz -C /usr/local/sbin/ --wildcards 'boringtun-*/boringtun' --strip-components 1
  444. # Configure wg-quick to use BoringTun
  445. mkdir /etc/systemd/system/wg-quick@wg0.service.d/ 2>/dev/null
  446. echo "[Service]
  447. Environment=WG_QUICK_USERSPACE_IMPLEMENTATION=boringtun
  448. Environment=WG_SUDO=1" >/etc/systemd/system/wg-quick@wg0.service.d/boringtun.conf
  449. if [[ -n "$cron" ]] && [[ "$os" == "centos" || "$os" == "fedora" ]]; then
  450. systemctl enable --now crond.service
  451. fi
  452. fi
  453. # If firewalld was just installed, enable it
  454. if [[ "$firewall" == "firewalld" ]]; then
  455. systemctl enable --now firewalld.service
  456. fi
  457. # Generate wg0.conf
  458. cat <<EOF >/etc/wireguard/wg0.conf
  459. # Do not alter the commented lines
  460. # They are used by wireguard-install
  461. # ENDPOINT $([[ -n "$public_ip" ]] && echo "$public_ip" || echo "$ip")
  462. [Interface]
  463. Address = 10.7.0.1/24$([[ -n "$ip6" ]] && echo ", fddd:2c4:2c4:2c4::1/64")
  464. PrivateKey = $(wg genkey)
  465. ListenPort = $port
  466. EOF
  467. chmod 600 /etc/wireguard/wg0.conf
  468. # Enable net.ipv4.ip_forward for the system
  469. echo 'net.ipv4.ip_forward=1' >/etc/sysctl.d/30-wireguard-forward.conf
  470. # Enable without waiting for a reboot or service restart
  471. echo 1 >/proc/sys/net/ipv4/ip_forward
  472. if [[ -n "$ip6" ]]; then
  473. # Enable net.ipv6.conf.all.forwarding for the system
  474. echo "net.ipv6.conf.all.forwarding=1" >>/etc/sysctl.d/30-wireguard-forward.conf
  475. # Enable without waiting for a reboot or service restart
  476. echo 1 >/proc/sys/net/ipv6/conf/all/forwarding
  477. fi
  478. if systemctl is-active --quiet firewalld.service; then
  479. # Using both permanent and not permanent rules to avoid a firewalld
  480. # reload.
  481. firewall-cmd --add-port="$port"/udp
  482. firewall-cmd --zone=trusted --add-source=10.7.0.0/24
  483. firewall-cmd --permanent --add-port="$port"/udp
  484. firewall-cmd --permanent --zone=trusted --add-source=10.7.0.0/24
  485. # Set NAT for the VPN subnet
  486. firewall-cmd --direct --add-rule ipv4 nat POSTROUTING 0 -s 10.7.0.0/24 ! -d 10.7.0.0/24 -j SNAT --to "$ip"
  487. firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -s 10.7.0.0/24 ! -d 10.7.0.0/24 -j SNAT --to "$ip"
  488. if [[ -n "$ip6" ]]; then
  489. firewall-cmd --zone=trusted --add-source=fddd:2c4:2c4:2c4::/64
  490. firewall-cmd --permanent --zone=trusted --add-source=fddd:2c4:2c4:2c4::/64
  491. firewall-cmd --direct --add-rule ipv6 nat POSTROUTING 0 -s fddd:2c4:2c4:2c4::/64 ! -d fddd:2c4:2c4:2c4::/64 -j SNAT --to "$ip6"
  492. firewall-cmd --permanent --direct --add-rule ipv6 nat POSTROUTING 0 -s fddd:2c4:2c4:2c4::/64 ! -d fddd:2c4:2c4:2c4::/64 -j SNAT --to "$ip6"
  493. fi
  494. else
  495. # Create a service to set up persistent iptables rules
  496. iptables_path=$(command -v iptables)
  497. ip6tables_path=$(command -v ip6tables)
  498. # nf_tables is not available as standard in OVZ kernels. So use iptables-legacy
  499. # if we are in OVZ, with a nf_tables backend and iptables-legacy is available.
  500. if [[ $(systemd-detect-virt) == "openvz" ]] && readlink -f "$(command -v iptables)" | grep -q "nft" && hash iptables-legacy 2>/dev/null; then
  501. iptables_path=$(command -v iptables-legacy)
  502. ip6tables_path=$(command -v ip6tables-legacy)
  503. fi
  504. echo "[Unit]
  505. Before=network.target
  506. [Service]
  507. Type=oneshot
  508. ExecStart=$iptables_path -t nat -A POSTROUTING -s 10.7.0.0/24 ! -d 10.7.0.0/24 -j SNAT --to $ip
  509. ExecStart=$iptables_path -I INPUT -p udp --dport $port -j ACCEPT
  510. ExecStart=$iptables_path -I FORWARD -s 10.7.0.0/24 -j ACCEPT
  511. ExecStart=$iptables_path -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  512. ExecStop=$iptables_path -t nat -D POSTROUTING -s 10.7.0.0/24 ! -d 10.7.0.0/24 -j SNAT --to $ip
  513. ExecStop=$iptables_path -D INPUT -p udp --dport $port -j ACCEPT
  514. ExecStop=$iptables_path -D FORWARD -s 10.7.0.0/24 -j ACCEPT
  515. ExecStop=$iptables_path -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" >/etc/systemd/system/wg-iptables.service
  516. if [[ -n "$ip6" ]]; then
  517. echo "ExecStart=$ip6tables_path -t nat -A POSTROUTING -s fddd:2c4:2c4:2c4::/64 ! -d fddd:2c4:2c4:2c4::/64 -j SNAT --to $ip6
  518. ExecStart=$ip6tables_path -I FORWARD -s fddd:2c4:2c4:2c4::/64 -j ACCEPT
  519. ExecStart=$ip6tables_path -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  520. ExecStop=$ip6tables_path -t nat -D POSTROUTING -s fddd:2c4:2c4:2c4::/64 ! -d fddd:2c4:2c4:2c4::/64 -j SNAT --to $ip6
  521. ExecStop=$ip6tables_path -D FORWARD -s fddd:2c4:2c4:2c4::/64 -j ACCEPT
  522. ExecStop=$ip6tables_path -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" >>/etc/systemd/system/wg-iptables.service
  523. fi
  524. echo "RemainAfterExit=yes
  525. [Install]
  526. WantedBy=multi-user.target" >>/etc/systemd/system/wg-iptables.service
  527. systemctl enable --now wg-iptables.service
  528. fi
  529. # Generates the custom client.conf
  530. new_client_setup
  531. # Enable and start the wg-quick service
  532. systemctl enable --now wg-quick@wg0.service
  533. # Set up automatic updates for BoringTun if the user wanted to
  534. if [[ "$boringtun_updates" =~ ^[yY]*$ ]]; then
  535. # Deploy upgrade script
  536. cat <<'EOF' >/usr/local/sbin/boringtun-upgrade
  537. #!/bin/bash
  538. latest=$(wget -qO- https://wg.nyr.be/1/latest 2>/dev/null || curl -sL https://wg.nyr.be/1/latest 2>/dev/null)
  539. # If server did not provide an appropriate response, exit
  540. if ! head -1 <<< "$latest" | grep -qiE "^boringtun.+[0-9]+\.[0-9]+.*$"; then
  541. echo "Servidor de actualización no disponible"
  542. exit
  543. fi
  544. current=$(boringtun -V)
  545. if [[ "$current" != "$latest" ]]; then
  546. download="https://wg.nyr.be/1/latest/download"
  547. xdir=$(mktemp -d)
  548. # If download and extraction are successful, upgrade the boringtun binary
  549. if { wget -qO- "$download" 2>/dev/null || curl -sL "$download" ; } | tar xz -C "$xdir" --wildcards "boringtun-*/boringtun" --strip-components 1; then
  550. systemctl stop wg-quick@wg0.service
  551. rm -f /usr/local/sbin/boringtun
  552. mv "$xdir"/boringtun /usr/local/sbin/boringtun
  553. systemctl start wg-quick@wg0.service
  554. echo -e -n "${lightgreen}"
  555. echo "Succesfully updated to $(boringtun -V)"
  556. else
  557. echo -e -n "${red}"
  558. echo "boringtun update failed"
  559. fi
  560. rm -rf "$xdir"
  561. echo -e -n "${nocolor}"
  562. else
  563. echo "$current is up to date"
  564. fi
  565. EOF
  566. chmod +x /usr/local/sbin/boringtun-upgrade
  567. # Add cron job to run the updater daily at a random time between 3:00 and 5:59
  568. {
  569. crontab -l 2>/dev/null
  570. echo "$(($RANDOM % 60)) $(($RANDOM % 3 + 3)) * * * /usr/local/sbin/boringtun-upgrade &>/dev/null"
  571. } | crontab -
  572. fi
  573. code() {
  574. echo
  575. qrencode -t UTF8 </etc/VPS-MX/wireguard/"$client.conf"
  576. echo -e '\xE2\x86\x91 Ese es un código QR que contiene la configuración del cliente.'
  577. echo
  578. }
  579. msg -ama " DESEA VER EL QR [s/n]"
  580. read -p " [ S | N ]: " -e -i n code
  581. [[ "$code" = "s" || "$code" = "S" ]] && $code
  582. # If the kernel module didn't load, system probably had an outdated kernel
  583. # We'll try to help, but will not will not force a kernel upgrade upon the user
  584. if [[ ! "$is_container" -eq 0 ]] && ! modprobe -nq wireguard; then
  585. echo -e -n "${red}"
  586. echo "¡Advertencia!"
  587. echo "La instalación finalizó, pero el módulo kernel de WireGuard no pudo cargarse"
  588. if [[ "$os" == "ubuntu" && "$os_version" -eq 1804 ]]; then
  589. echo 'Upgrade the kernel and headers with "apt-get install linux-generic" and restart.'
  590. #elif [[ "$os" == "debian" && "$os_version" -eq 9 ]]; then
  591. #echo "Actualice el kernel con \"apt-get install linux-image-$architecture\" y reinicie"
  592. elif [[ "$os" == "debian" && "$os_version" -eq 10 ]]; then
  593. echo "Actualice el kernel con \"apt-get install linux-image-$architecture\" y reinicie"
  594. elif [[ "$os" == "centos" && "$os_version" -le 8 ]]; then
  595. echo "Reboot the system to load the most recent kernel."
  596. fi
  597. echo -e -n "${nocolor}"
  598. else
  599. echo -e -n "${green}"
  600. echo "INSTALADO CON EXITO!"
  601. echo -e -n "${nocolor}"
  602. fi
  603. echo
  604. echo -e -n "${lightgreen}"
  605. echo "La configuración del cliente está disponible en la opcion 4" #: /etc/VPS-MX/wireguard/$client.conf"
  606. echo "Se pueden agregar nuevos clientes ejecutando este script nuevamente"
  607. echo -e -n "${nocolor}"
  608. #else
  609. }
  610. selection_fun() {
  611. local selection="null"
  612. local range
  613. for ((i = 0; i <= $1; i++)); do range[$i]="$i "; done
  614. while [[ ! $(echo ${range[*]} | grep -w "$selection") ]]; do
  615. echo -ne " ► Selecione una Opcion: " >&2
  616. read selection
  617. tput cuu1 >&2 && tput dl1 >&2
  618. done
  619. echo $selection
  620. }
  621. menufun() {
  622. clear
  623. #msg -bar
  624. msg -tit
  625. msg -bar
  626. echo -e " \e[1;100mMENÚ WIREGUARD\e[0m"
  627. msg -bar
  628. #echo "Select an option:"
  629. echo -e "\e[1;91m 1) \e[92mAGREGAR NUEVO USUARIO"
  630. echo -e "\e[1;91m 2) \e[97m\e[41mELIMINAR USUARIO\e[0m"
  631. echo -e "\e[1;91m 3) \e[93mDESCARGAR CONFI "
  632. echo -e "\e[1;91m 4) \e[92mINFORMACION DE LA CUENTA"
  633. echo -e "\e[1;91m 5) \e[97m\e[1;41mDESINSTALAR WIREGUARD\e[0m"
  634. echo -e "\e[1;93m 6) \e[91mSALIR"
  635. msg -bar
  636. read -p "$(echo -e "\e[1;97m SELECIONE UNA OPCION:") " option
  637. until [[ "$option" =~ ^[1-6]$ ]]; do
  638. echo "$option: OPCION INVALIDA."
  639. read -p "Selecione Una Opcion: " option
  640. done
  641. echo -e -n "${nocolor}"
  642. case "$option" in
  643. 1)
  644. clear
  645. echo
  646. echo -e -n "${cyan}"
  647. echo " Ingrese El nombre Del Usuario: "
  648. echo -e -n "${nocolor}"
  649. read -p "Nombre: " unsanitized_client
  650. # Allow a limited set of characters to avoid conflicts
  651. client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<<"$unsanitized_client")
  652. while [[ -z "$client" ]] || grep -q "^# BEGIN_PEER $client$" /etc/wireguard/wg0.conf; do
  653. echo "$client: invalid name."
  654. read -p "Nombre: " unsanitized_client
  655. client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<<"$unsanitized_client")
  656. done
  657. echo
  658. new_client_dns
  659. new_client_setup
  660. # Append new client configuration to the WireGuard interface
  661. wg addconf wg0 <(sed -n "/^# BEGIN_PEER $client/,/^# END_PEER $client/p" /etc/wireguard/wg0.conf)
  662. code() {
  663. echo
  664. qrencode -t UTF8 </etc/VPS-MX/wireguard/"$client.conf"
  665. echo -e '\xE2\x86\x91 Ese es un código QR que contiene la configuración de su cliente.'
  666. echo
  667. }
  668. msg -ama " DESEA VER EL QR [s/n]"
  669. read -p " [ S | N ]: " -e -i n code
  670. [[ "$code" = "s" || "$code" = "S" ]] && $code
  671. echo -e -n "${green}"
  672. echo "$client agregado, la configuracion esta en la opcion 4 " #: /etc/VPS-MX/wireguard/$client.conf"
  673. echo -e -n "${nocolor}"
  674. exit
  675. ;;
  676. 2)
  677. # This option could be documented a bit better and maybe even be simplified
  678. # ...but what can I say, I want some sleep too
  679. number_of_clients=$(grep -c '^# BEGIN_PEER' /etc/wireguard/wg0.conf)
  680. if [[ "$number_of_clients" = 0 ]]; then
  681. echo
  682. echo -e -n "${red}"
  683. echo "¡No hay clientes existentes!"
  684. echo -e -n "${nocolor}"
  685. exit
  686. fi
  687. echo
  688. echo -e -n "${green}"
  689. echo "Seleciona la opcion del cliente: "
  690. grep '^# BEGIN_PEER' /etc/wireguard/wg0.conf | cut -d ' ' -f 3 | nl -s ') '
  691. read -p "Cliente: " client_number
  692. until [[ "$client_number" =~ ^[0-9]+$ && "$client_number" -le "$number_of_clients" ]]; do
  693. echo "$client_number: invalid selection."
  694. read -p "Cliente: " client_number
  695. done
  696. client=$(grep '^# BEGIN_PEER' /etc/wireguard/wg0.conf | cut -d ' ' -f 3 | sed -n "$client_number"p)
  697. echo
  698. echo -e -n "${red}"
  699. read -p "Confirmar $client para remover? [y/N]: " remove
  700. until [[ "$remove" =~ ^[yYnN]*$ ]]; do
  701. echo "$remove: invalid selection."
  702. echo -e -n "${red}"
  703. read -p "Confirmar $client para remover? [y/N]: " remove
  704. done
  705. echo -e -n "${nocolor}"
  706. if [[ "$remove" =~ ^[yY]$ ]]; then
  707. # The following is the right way to avoid disrupting other active connections:
  708. # Remove from the live interface
  709. wg set wg0 peer "$(sed -n "/^# BEGIN_PEER $client$/,\$p" /etc/wireguard/wg0.conf | grep -m 1 PublicKey | cut -d " " -f 3)" remove
  710. # Remove from the configuration file
  711. sed -i "/^# BEGIN_PEER $client/,/^# END_PEER $client/d" /etc/wireguard/wg0.conf
  712. echo
  713. echo -e -n "${green}"
  714. echo "$client eliminado!"
  715. rm ~/$client.conf &>/dev/null
  716. rm /etc/VPS-MX/wireguard/$client.conf &>/dev/null
  717. echo -e -n "${nocolor}"
  718. else
  719. echo
  720. echo -e -n "${red}"
  721. echo "$client no eliminado!"
  722. echo -e -n "${nocolor}"
  723. fi
  724. echo -e -n "${nocolor}"
  725. exit
  726. ;;
  727. 3)
  728. clear
  729. #msg -bar
  730. #msg -tit
  731. clear
  732. #msg -bar
  733. msg -tit
  734. n=1
  735. for i in $(ls /etc/VPS-MX/wireguard); do
  736. loc=$(echo $i) #|awk -F "" '{print $1}')
  737. zona=$(printf '%-12s' "$loc")
  738. echo -e " \e[37m [$n] \e[31m> \e[32m$zona"
  739. r[$n]=$zona
  740. selec="$n"
  741. let n++
  742. done
  743. msg -bar
  744. opci=$(selection_fun $selec)
  745. echo ""
  746. cp -r /etc/VPS-MX/wireguard/${r[$opci]} /var/www/html/${r[$opci]}
  747. chmod 777 /var/www/html/${r[$opci]}
  748. fun_ip
  749. msg -bar
  750. msg -ama " LINK DEL CLIENTE: http://$IP:81/${r[$opci]}"
  751. msg -bar
  752. ;;
  753. 4)
  754. clear
  755. msg -bar
  756. msg -tit
  757. n=1
  758. for i in $(ls /etc/VPS-MX/wireguard); do
  759. loc=$(echo $i) #|awk -F "" '{print $1}')
  760. zona=$(printf '%-12s' "$loc")
  761. echo -e " \e[37m [$n] \e[31m> \e[32m$zona"
  762. r[$n]=$zona
  763. selec="$n"
  764. let n++
  765. done
  766. msg -bar
  767. opci=$(selection_fun $selec)
  768. echo ""
  769. echo -e " \e[1;100mCONFIGURACION DEL CLIENTE\e[0m\n\e[97m$(cat /etc/VPS-MX/wireguard/${r[$opci]})"
  770. msg -bar
  771. ;;
  772. 5)
  773. echo
  774. echo -e -n "${red}"
  775. read -p "Confirmar WireGuard para remover? [y/N]: " remove
  776. echo -e -n "${nocolor}"
  777. until [[ "$remove" =~ ^[yYnN]*$ ]]; do
  778. echo -e -n "${red}"
  779. echo "$remove: invalid selection."
  780. read -p "Confirmar WireGuard para remover? [y/N]: " remove
  781. echo -e -n "${nocolor}"
  782. done
  783. if [[ "$remove" =~ ^[yY]$ ]]; then
  784. port=$(grep '^ListenPort' /etc/wireguard/wg0.conf | cut -d " " -f 3)
  785. if systemctl is-active --quiet firewalld.service; then
  786. ip=$(firewall-cmd --direct --get-rules ipv4 nat POSTROUTING | grep '\-s 10.7.0.0/24 '"'"'!'"'"' -d 10.7.0.0/24' | grep -oE '[^ ]+$')
  787. # Using both permanent and not permanent rules to avoid a firewalld reload.
  788. firewall-cmd --remove-port="$port"/udp
  789. firewall-cmd --zone=trusted --remove-source=10.7.0.0/24
  790. firewall-cmd --permanent --remove-port="$port"/udp
  791. firewall-cmd --permanent --zone=trusted --remove-source=10.7.0.0/24
  792. firewall-cmd --direct --remove-rule ipv4 nat POSTROUTING 0 -s 10.7.0.0/24 ! -d 10.7.0.0/24 -j SNAT --to "$ip"
  793. firewall-cmd --permanent --direct --remove-rule ipv4 nat POSTROUTING 0 -s 10.7.0.0/24 ! -d 10.7.0.0/24 -j SNAT --to "$ip"
  794. if grep -qs 'fddd:2c4:2c4:2c4::1/64' /etc/wireguard/wg0.conf; then
  795. ip6=$(firewall-cmd --direct --get-rules ipv6 nat POSTROUTING | grep '\-s fddd:2c4:2c4:2c4::/64 '"'"'!'"'"' -d fddd:2c4:2c4:2c4::/64' | grep -oE '[^ ]+$')
  796. firewall-cmd --zone=trusted --remove-source=fddd:2c4:2c4:2c4::/64
  797. firewall-cmd --permanent --zone=trusted --remove-source=fddd:2c4:2c4:2c4::/64
  798. firewall-cmd --direct --remove-rule ipv6 nat POSTROUTING 0 -s fddd:2c4:2c4:2c4::/64 ! -d fddd:2c4:2c4:2c4::/64 -j SNAT --to "$ip6"
  799. firewall-cmd --permanent --direct --remove-rule ipv6 nat POSTROUTING 0 -s fddd:2c4:2c4:2c4::/64 ! -d fddd:2c4:2c4:2c4::/64 -j SNAT --to "$ip6"
  800. fi
  801. else
  802. systemctl disable --now wg-iptables.service
  803. rm -f /etc/systemd/system/wg-iptables.service
  804. fi
  805. systemctl disable --now wg-quick@wg0.service
  806. rm -f /etc/systemd/system/wg-quick@wg0.service.d/boringtun.conf
  807. rm -f /etc/sysctl.d/30-wireguard-forward.conf
  808. # Different packages were installed if the system was containerized or not
  809. if [[ ! "$is_container" -eq 0 ]]; then
  810. if [[ "$os" == "ubuntu" ]]; then
  811. # Ubuntu
  812. rm -rf /etc/wireguard/
  813. apt-get remove --purge -y wireguard wireguard-tools
  814. elif [[ "$os" == "debian" && "$os_version" -eq 10 ]]; then
  815. # Debian 10
  816. rm -rf /etc/wireguard/
  817. apt-get remove --purge -y wireguard wireguard-dkms wireguard-tools
  818. #elif [[ "$os" == "debian" && "$os_version" -eq 9 ]]; then
  819. # Debian 10
  820. #rm -rf /etc/wireguard/
  821. #apt-get remove --purge -y wireguard wireguard-dkms wireguard-tools
  822. elif [[ "$os" == "centos" && "$os_version" -eq 8 ]]; then
  823. # CentOS 8
  824. rm -rf /etc/wireguard/
  825. dnf remove -y kmod-wireguard wireguard-tools
  826. elif [[ "$os" == "centos" && "$os_version" -eq 7 ]]; then
  827. # CentOS 7
  828. rm -rf /etc/wireguard/
  829. yum remove -y kmod-wireguard wireguard-tools
  830. elif [[ "$os" == "fedora" ]]; then
  831. # Fedora
  832. rm -rf /etc/wireguard/
  833. dnf remove -y wireguard-tools
  834. fi
  835. else
  836. { crontab -l 2>/dev/null | grep -v '/usr/local/sbin/boringtun-upgrade'; } | crontab -
  837. if [[ "$os" == "ubuntu" ]]; then
  838. # Ubuntu
  839. rm -rf /etc/wireguard/
  840. apt-get remove --purge -y wireguard-tools
  841. elif [[ "$os" == "debian" && "$os_version" -eq 10 ]]; then
  842. # Debian 10
  843. rm -rf /etc/wireguard/
  844. apt-get remove --purge -y wireguard-tools
  845. #elif [[ "$os" == "debian" && "$os_version" -eq 9 ]]; then
  846. # Debian 10
  847. #rm -rf /etc/wireguard/
  848. #apt-get remove --purge -y wireguard-tools
  849. elif [[ "$os" == "centos" && "$os_version" -eq 8 ]]; then
  850. # CentOS 8
  851. rm -rf /etc/wireguard/
  852. dnf remove -y wireguard-tools
  853. elif [[ "$os" == "centos" && "$os_version" -eq 7 ]]; then
  854. # CentOS 7
  855. rm -rf /etc/wireguard/
  856. yum remove -y wireguard-tools
  857. elif [[ "$os" == "fedora" ]]; then
  858. # Fedora
  859. rm -rf /etc/wireguard/
  860. dnf remove -y wireguard-tools
  861. fi
  862. rm -f /usr/local/sbin/boringtun /usr/local/sbin/boringtun-upgrade
  863. fi
  864. echo
  865. echo -e -n "${green}"
  866. echo "WireGuard desinstalado!"
  867. rm /etc/VPS-MX/wireguard/*.conf &>/dev/null
  868. echo -e -n "${nocolor}"
  869. else
  870. echo
  871. echo -e -n "${red}"
  872. echo "desinstalacion WireGuard abortado!"
  873. echo -e -n "${nocolor}"
  874. fi
  875. exit
  876. ;;
  877. 6)
  878. exit
  879. ;;
  880. esac
  881. }
  882. #echo -e -n "${nocolor}"
  883. if [[ ! -e /etc/wireguard/wg0.conf ]]; then
  884. clear
  885. #msg -bar
  886. msg -tit
  887. msg -bar
  888. echo -e " \e[1;100mMENÚ WIREGUARD\e[0m"
  889. msg -bar
  890. #echo "Select an option:"
  891. echo -e "\e[1;91m 1) \e[92mINSTALAR WIREGUARD"
  892. echo -e "\e[1;93m 0) \e[91mSALIR"
  893. msg -bar
  894. read -p "$(echo -e "\e[1;97m SELECIONE UNA OPCION:") " option
  895. case $option in
  896. 1) install ;;
  897. 0) exit ;;
  898. esac
  899. else
  900. menufun
  901. fi
  902. #