add-wg.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. red='\e[1;31m'
  3. green='\e[0;32m'
  4. NC='\e[0m'
  5. MYIP=$(wget -qO- ifconfig.me/ip);
  6. echo "Checking VPS"
  7. clear
  8. # Load params
  9. source /etc/wireguard/params
  10. source /var/lib/premium-script/ipvps.conf
  11. if [[ "$IP" = "" ]]; then
  12. SERVER_PUB_IP=$(wget -qO- ifconfig.me/ip);
  13. else
  14. SERVER_PUB_IP=$IP
  15. fi
  16. echo ""
  17. echo "Tell me a name for the client."
  18. echo "Use one word only, no special characters."
  19. until [[ ${CLIENT_NAME} =~ ^[a-zA-Z0-9_]+$ && ${CLIENT_EXISTS} == '0' ]]; do
  20. read -rp "Client name: " -e CLIENT_NAME
  21. CLIENT_EXISTS=$(grep -w $CLIENT_NAME /etc/wireguard/wg0.conf | wc -l)
  22. if [[ ${CLIENT_EXISTS} == '1' ]]; then
  23. echo ""
  24. echo "A client with the specified name was already created, please choose another name."
  25. exit 1
  26. fi
  27. done
  28. echo "IPv4 Detected"
  29. ENDPOINT="$SERVER_PUB_IP:$SERVER_PORT"
  30. WG_CONFIG="/etc/wireguard/wg0.conf"
  31. LASTIP=$( grep "/32" $WG_CONFIG | tail -n1 | awk '{print $3}' | cut -d "/" -f 1 | cut -d "." -f 4 )
  32. if [[ "$LASTIP" = "" ]]; then
  33. CLIENT_ADDRESS="10.66.66.2"
  34. else
  35. CLIENT_ADDRESS="10.66.66.$((LASTIP+1))"
  36. fi
  37. # Adguard DNS by default
  38. CLIENT_DNS_1="176.103.130.130"
  39. CLIENT_DNS_2="176.103.130.131"
  40. MYIP=$(wget -qO- ifconfig.co);
  41. read -p "Expired (days): " masaaktif
  42. exp=`date -d "$masaaktif days" +"%Y-%m-%d"`
  43. # Generate key pair for the client
  44. CLIENT_PRIV_KEY=$(wg genkey)
  45. CLIENT_PUB_KEY=$(echo "$CLIENT_PRIV_KEY" | wg pubkey)
  46. CLIENT_PRE_SHARED_KEY=$(wg genpsk)
  47. # Create client file and add the server as a peer
  48. echo "[Interface]
  49. PrivateKey = $CLIENT_PRIV_KEY
  50. Address = $CLIENT_ADDRESS/24
  51. DNS = $CLIENT_DNS_1,$CLIENT_DNS_2
  52. [Peer]
  53. PublicKey = $SERVER_PUB_KEY
  54. PresharedKey = $CLIENT_PRE_SHARED_KEY
  55. Endpoint = $ENDPOINT
  56. AllowedIPs = 0.0.0.0/0,::/0" >>"$HOME/$SERVER_WG_NIC-client-$CLIENT_NAME.conf"
  57. # Add the client as a peer to the server
  58. echo -e "### Client $CLIENT_NAME $exp
  59. [Peer]
  60. PublicKey = $CLIENT_PUB_KEY
  61. PresharedKey = $CLIENT_PRE_SHARED_KEY
  62. AllowedIPs = $CLIENT_ADDRESS/32" >>"/etc/wireguard/$SERVER_WG_NIC.conf"
  63. systemctl restart "wg-quick@$SERVER_WG_NIC"
  64. cp $HOME/$SERVER_WG_NIC-client-$CLIENT_NAME.conf /home/vps/public_html/$CLIENT_NAME.conf
  65. clear
  66. sleep 0.5
  67. echo Generate PrivateKey
  68. sleep 0.5
  69. echo Generate PublicKey
  70. sleep 0.5
  71. echo Generate PresharedKey
  72. clear
  73. echo -e ""
  74. echo -e "==========-Wireguard-=========="
  75. echo -e "Wireguard : http://$MYIP:81/$CLIENT_NAME.conf"
  76. echo -e "==============================="
  77. echo -e "Expired On : $exp"
  78. rm -f /root/wg0-client-$CLIENT_NAME.conf