The main Bubble source repository. Contains the Bubble API server, the web UI, documentation and utilities. https://getbubblenow.com
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

67 rader
2.5 KiB

  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2020 Bubble, Inc. All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
  4. #
  5. LOG=/tmp/bubble.wg_monitor_connections.log
  6. function die {
  7. echo 1>&2 "${1}"
  8. log "${1}"
  9. exit 1
  10. }
  11. function log {
  12. echo "$(date): ${1}" >> ${LOG}
  13. }
  14. ALGO_CONFIGS=/root/ansible/roles/algo/algo/configs
  15. BUBBLE_DEVICE_DIR=/home/bubble/wg_devices
  16. if [[ ! -d ${BUBBLE_DEVICE_DIR} ]] ; then
  17. mkdir -p ${BUBBLE_DEVICE_DIR} && chown -R bubble ${BUBBLE_DEVICE_DIR} && chmod 700 ${BUBBLE_DEVICE_DIR} || die "Error creating ${BUBBLE_DEVICE_DIR}"
  18. fi
  19. while : ; do
  20. peer=""
  21. IFS=$'\n'
  22. for line in $(wg show all) ; do
  23. if [[ ! -z "${peer}" ]] ; then
  24. if [[ $(echo "${line}" | tr -d ' ') == allowed* ]] ; then
  25. for ip in $(echo "${line}" | cut -d: -f2- | tr ',' '\n' | tr -d ' ' | cut -d/ -f1) ; do
  26. device_uuids="$(find $(find $(find ${ALGO_CONFIGS} -type d -name wireguard) -type d -name public) -type f | xargs grep -l ${peer} | xargs -n 1 basename)"
  27. if [[ $(echo "${device_uuids}" | wc -l | tr -d ' ') -gt 1 ]] ; then
  28. log "Multiple device UUIDs found for IP ${ip} (not recording anything): ${device_uuids}"
  29. continue
  30. fi
  31. device="$(echo "${device_uuids}" | head -1 | tr -d ' ')"
  32. ip_file="${BUBBLE_DEVICE_DIR}/ip_$(echo ${ip})"
  33. if [[ ! -f ${ip_file} ]] ; then
  34. touch ${ip_file} && chown bubble ${ip_file} && chmod 400 ${ip_file} || log "Error creating ${ip_file}"
  35. fi
  36. device_exists=$(grep -c "${ip}" ${ip_file})
  37. if [[ ${device_exists} -eq 0 ]] ; then
  38. log "recorded device ${device} for IP ${ip}"
  39. echo "${device}" > ${ip_file} || log "Error writing ${device} to ${ip_file}"
  40. fi
  41. device_file="${BUBBLE_DEVICE_DIR}/device_$(echo ${device})"
  42. if [[ ! -f ${device_file} ]] ; then
  43. touch ${device_file} && chown bubble ${device_file} && chmod 400 ${device_file} || log "Error creating ${ip_file}"
  44. fi
  45. ip_exists=$(grep -c "${ip}" ${device_file})
  46. if [[ ${ip_exists} -eq 0 ]] ; then
  47. log "recorded IP ${ip} for device ${device}"
  48. echo "${ip}" >> ${device_file} || log "Error writing ${ip} to ${device_file}"
  49. fi
  50. done
  51. peer=""
  52. fi
  53. elif [[ ${line} == peer* ]] ; then
  54. peer="$(echo "${line}" | awk '{print $NF}')"
  55. fi
  56. done
  57. sleep 30s
  58. done