wg.sh 3.5 KB

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