add-wg.sh 2.4 KB

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