wireguard-script.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/bash
  2. source /etc/wireguard/params
  3. function add-user() {
  4. endpoint="${ip}:51820"
  5. clear
  6. echo -e "Add WireGuard User"
  7. echo -e "------------------"
  8. read -p "Username : " user
  9. if grep -qw "^### Client ${user}\$" /etc/wireguard/wg0.conf; then
  10. echo -e "User '$user' already exist."
  11. echo -e ""
  12. exit 0
  13. fi
  14. read -p "Duration (day) : " duration
  15. exp=$(date -d +${duration}days +%Y-%m-%d)
  16. expired=$(date -d "${exp}" +"%d %b %Y")
  17. for dot_ip in {2..254}; do
  18. dot_exists=$(grep -c "10.66.66.${dot_ip}" /etc/wireguard/wg0.conf)
  19. if [[ ${dot_exists} == '0' ]]; then
  20. break
  21. fi
  22. done
  23. if [[ ${dot_exists} == '1' ]]; then
  24. clear
  25. echo -e ""
  26. echo -e "The subnet configured only supports 253 clients."
  27. echo -e ""
  28. exit 1
  29. fi
  30. client_ipv4="10.66.66.${dot_ip}"
  31. client_priv_key=$(wg genkey)
  32. client_pub_key=$(echo "${client_priv_key}" | wg pubkey)
  33. client_pre_shared_key=$(wg genpsk)
  34. echo -e "$user\t$exp" >> /iriszz/wireguard/wireguard-clients.txt
  35. echo -e "[Interface]
  36. PrivateKey = ${client_priv_key}
  37. Address = ${client_ipv4}/32
  38. DNS = 8.8.8.8,8.8.4.4
  39. [Peer]
  40. PublicKey = ${server_pub_key}
  41. PresharedKey = ${client_pre_shared_key}
  42. Endpoint = ${endpoint}
  43. AllowedIPs = 0.0.0.0/0" >> /iriszz/wireguard/${user}.conf
  44. echo -e "\n### Client ${user}
  45. [Peer]
  46. PublicKey = ${client_pub_key}
  47. PresharedKey = ${client_pre_shared_key}
  48. AllowedIPs = ${client_ipv4}/32" >> /etc/wireguard/wg0.conf
  49. systemctl restart "wg-quick@wg0"
  50. clear
  51. echo -e "WireGuard User Information"
  52. echo -e "--------------------------"
  53. echo -e "Username : $user"
  54. echo -e "Expired date : $expired"
  55. echo -e ""
  56. }
  57. function delete-user(){
  58. clear
  59. echo -e "Delete WireGuard User"
  60. echo -e "---------------------"
  61. read -p "Username : " user
  62. echo -e ""
  63. if grep -qw "^### Client ${user}\$" /etc/wireguard/wg0.conf; then
  64. sed -i "/^### Client ${user}\$/,/^$/d" /etc/wireguard/wg0.conf
  65. if grep -q "### Client" /etc/wireguard/wg0.conf; then
  66. line=$(grep -n AllowedIPs /etc/wireguard/wg0.conf | tail -1 | awk -F: '{print $1}')
  67. head -${line} /etc/wireguard/wg0.conf > /tmp/wg0.conf
  68. mv /tmp/wg0.conf /etc/wireguard/wg0.conf
  69. else
  70. head -7 /etc/wireguard/wg0.conf > /tmp/wg0.conf
  71. mv /tmp/wg0.conf /etc/wireguard/wg0.conf
  72. fi
  73. rm -f /iriszz/wireguard/${user}.conf
  74. sed -i "/\b$user\b/d" /iriszz/wireguard/wireguard-clients.txt
  75. service wg-quick@wg0 restart
  76. echo -e "User '$user' deleted successfully."
  77. echo -e ""
  78. else
  79. echo -e "User '$user' does not exist."
  80. echo -e ""
  81. exit 0
  82. fi
  83. }
  84. function extend-user() {
  85. clear
  86. echo -e "Extend WireGuard User"
  87. echo -e "---------------------"
  88. read -p "Username : " user
  89. if ! grep -qw "$user" /iriszz/wireguard/wireguard-clients.txt; then
  90. echo -e ""
  91. echo -e "User '$user' does not exist."
  92. echo -e ""
  93. exit 0
  94. fi
  95. read -p "Duration (day) : " extend
  96. exp_old=$(cat /iriszz/wireguard/wireguard-clients.txt | grep -w $user | awk '{print $2}')
  97. diff=$((($(date -d "${exp_old}" +%s)-$(date +%s))/(86400)))
  98. duration=$(expr $diff + $extend + 1)
  99. exp_new=$(date -d +${duration}days +%Y-%m-%d)
  100. exp=$(date -d "${exp_new}" +"%d %b %Y")
  101. sed -i "/\b$user\b/d" /iriszz/wireguard/wireguard-clients.txt
  102. echo -e "$user\t$exp_new" >> /iriszz/wireguard/wireguard-clients.txt
  103. clear
  104. echo -e ""
  105. echo -e "WireGuard User Information"
  106. echo -e "--------------------------"
  107. echo -e "Username : $user"
  108. echo -e "Expired date : $exp"
  109. echo -e ""
  110. }
  111. function user-list() {
  112. clear
  113. echo -e ""
  114. echo -e "==============================="
  115. echo -e "Username Exp. Date"
  116. echo -e "-------------------------------"
  117. while read expired
  118. do
  119. user=$(echo $expired | awk '{print $1}')
  120. exp=$(echo $expired | awk '{print $2}')
  121. exp_date=$(date -d"${exp}" "+%d %b %Y")
  122. printf "%-17s %2s\n" "$user" "$exp_date"
  123. done < /iriszz/wireguard/wireguard-clients.txt
  124. total=$(wc -l /iriszz/wireguard/wireguard-clients.txt | awk '{print $1}')
  125. echo -e "-------------------------------"
  126. echo -e "Total accounts: $total"
  127. echo -e "==============================="
  128. echo -e ""
  129. }
  130. function show-config() {
  131. clear
  132. echo -e "WireGuard Config"
  133. echo -e "----------------"
  134. read -p "User : " user
  135. if grep -qw "^### Client ${user}\$" /etc/wireguard/wg0.conf; then
  136. exp=$(cat /iriszz/wireguard/wireguard-clients.txt | grep -w "$user" | awk '{print $2}')
  137. exp_date=$(date -d"${exp}" "+%d %b %Y")
  138. echo -e "Expired : $exp_date"
  139. echo -e ""
  140. echo -e "QR Code"
  141. echo -e "-------"
  142. qrencode -t ansiutf8 -l L < /iriszz/wireguard/${user}.conf
  143. echo -e ""
  144. echo -e "Config"
  145. echo -e "------"
  146. cat /iriszz/wireguard/${user}.conf
  147. echo -e ""
  148. else
  149. echo -e ""
  150. echo -e "User '$user' does not exist."
  151. echo -e ""
  152. exit 0
  153. fi
  154. }
  155. clear
  156. echo -e "===========[ WireGuard Menu ]==========="
  157. echo -e ""
  158. echo -e " [1] Add WireGuard user"
  159. echo -e " [2] Delete WireGuard user"
  160. echo -e " [3] Extend WireGuard user"
  161. echo -e " [4] WireGuard user list"
  162. echo -e " [5] Show WireGuard configuration"
  163. echo -e " [6] Exit"
  164. echo -e ""
  165. until [[ ${option} =~ ^[1-6]$ ]]; do
  166. read -rp "Select an option [1-6]: " option
  167. done
  168. case "${option}" in
  169. 1)
  170. add-user
  171. ;;
  172. 2)
  173. delete-user
  174. ;;
  175. 3)
  176. extend-user
  177. ;;
  178. 4)
  179. user-list
  180. ;;
  181. 5)
  182. clear
  183. show-config
  184. ;;
  185. 6)
  186. clear
  187. exit 0
  188. ;;
  189. esac