add-wg.sh 2.6 KB

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