wg.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/bash
  2. # Debian 9 & 10 64bit
  3. # Ubuntu 18.04 & 20.04 bit
  4. # Centos 7 & 8 64bit
  5. # ==================================================
  6. # Check OS version
  7. if [[ -e /etc/debian_version ]]; then
  8. source /etc/os-release
  9. OS=$ID # debian or ubuntu
  10. elif [[ -e /etc/centos-release ]]; then
  11. source /etc/os-release
  12. OS=centos
  13. fi
  14. Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
  15. Info="${Green_font_prefix}[information]${Font_color_suffix}"
  16. if [[ -e /etc/wireguard/params ]]; then
  17. echo -e "${Info} WireGuard sudah diinstal, silahkan ketik addwg untuk menambah client."
  18. exit 1
  19. fi
  20. echo -e "${Info} Wireguard VPS AutoScript "
  21. # Detect public IPv4 address and pre-fill for the user
  22. # Detect public interface and pre-fill for the user
  23. SERVER_PUB_NIC=$(ip -o $ANU -4 route show to default | awk '{print $5}');
  24. # Install WireGuard tools and module
  25. if [[ $OS == 'ubuntu' ]]; then
  26. apt install -y wireguard
  27. elif [[ $OS == 'debian' ]]; then
  28. echo "deb http://deb.debian.org/debian/ unstable main" >/etc/apt/sources.list.d/unstable.list
  29. printf 'Package: *\nPin: release a=unstable\nPin-Priority: 90\n' >/etc/apt/preferences.d/limit-unstable
  30. apt update
  31. apt install -y wireguard-tools iptables iptables-persistent
  32. apt install -y linux-headers-$(uname -r)
  33. elif [[ ${OS} == 'centos' ]]; then
  34. curl -Lo /etc/yum.repos.d/wireguard.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo
  35. yum -y update
  36. yum -y install wireguard-dkms wireguard-tools
  37. fi
  38. apt install iptables iptables-persistent -y
  39. # Make sure the directory exists (this does not seem the be the case on fedora)
  40. mkdir /etc/wireguard >/dev/null 2>&1
  41. chmod 600 -R /etc/wireguard/
  42. SERVER_PRIV_KEY=$(wg genkey)
  43. SERVER_PUB_KEY=$(echo "$SERVER_PRIV_KEY" | wg pubkey)
  44. # Save WireGuard settings
  45. echo "SERVER_PUB_NIC=$SERVER_PUB_NIC
  46. SERVER_WG_NIC=wg0
  47. SERVER_WG_IPV4=10.66.66.1
  48. SERVER_PORT=7070
  49. SERVER_PRIV_KEY=$SERVER_PRIV_KEY
  50. SERVER_PUB_KEY=$SERVER_PUB_KEY" >/etc/wireguard/params
  51. source /etc/wireguard/params
  52. # Add server interface
  53. echo "[Interface]
  54. Address = $SERVER_WG_IPV4/24
  55. ListenPort = $SERVER_PORT
  56. PrivateKey = $SERVER_PRIV_KEY
  57. PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o $SERVER_PUB_NIC -j MASQUERADE;
  58. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o $SERVER_PUB_NIC -j MASQUERADE;" >>"/etc/wireguard/wg0.conf"
  59. iptables -t nat -I POSTROUTING -s 10.66.66.1/24 -o $SERVER_PUB_NIC -j MASQUERADE
  60. iptables -I INPUT 1 -i wg0 -j ACCEPT
  61. iptables -I FORWARD 1 -i $SERVER_PUB_NIC -o wg0 -j ACCEPT
  62. iptables -I FORWARD 1 -i wg0 -o $SERVER_PUB_NIC -j ACCEPT
  63. iptables -I INPUT 1 -i $SERVER_PUB_NIC -p udp --dport 7070 -j ACCEPT
  64. iptables-save > /etc/iptables.up.rules
  65. iptables-restore -t < /etc/iptables.up.rules
  66. netfilter-persistent save
  67. netfilter-persistent reload
  68. systemctl start "wg-quick@wg0"
  69. systemctl enable "wg-quick@wg0"
  70. # Check if WireGuard is running
  71. systemctl is-active --quiet "wg-quick@wg0"
  72. WG_RUNNING=$?
  73. # Tambahan
  74. cd /usr/bin
  75. wget -O add-wg "https://raw.githubusercontent.com/Dimas1441/yamate/main/1/add-wg.sh"
  76. wget -O del-wg "https://raw.githubusercontent.com/Dimas1441/yamate/main/1/del-wg.sh"
  77. wget -O cek-wg "https://raw.githubusercontent.com/Dimas1441/yamate/main/1/cek-wg.sh"
  78. wget -O renew-wg "https://raw.githubusercontent.com/Dimas1441/yamate/main/1/renew-wg.sh"
  79. chmod +x add-wg
  80. chmod +x del-wg
  81. chmod +x cek-wg
  82. chmod +x renew-wg
  83. cd
  84. rm -f /root/wg.sh