Bubble proxy service
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

115 satır
3.3 KiB

  1. #!/bin/bash
  2. #
  3. # Register a flex router with a Bubble.
  4. #
  5. # Note: Registration requires the `curl` and `jq` commands to be installed.
  6. #
  7. # Usage:
  8. #
  9. # flex_register.sh bubble-hostname [flex-password-env-var]
  10. #
  11. # bubble-hostname : hostname of the bubble to register with
  12. #
  13. # flex-password-env-var : Name of environment variable containing bubble-flexrouter password
  14. # Default value is BUBBLE_FR_PASS
  15. #
  16. # Environment variables:
  17. #
  18. # BUBBLE_USER : username to login as. If empty, you'll be prompted for it.
  19. # BUBBLE_PASS : password to login with. If empty, you'll be prompted for it.
  20. # BFR_ADMIN_PORT : port on localhost where bubble-flexrouter admin API is listening. Default is 9833
  21. # BUBBLE_VPN_SUBNET : subnet prefix for VPN addresses. Default is "10.19."
  22. #
  23. LOGIN_JSON=$(mktemp /tmp/bubble_login.XXXXXXX.json)
  24. chmod 600 ${LOGIN_JSON}
  25. SESSION_FILE=$(mktemp /tmp/bubble_session.XXXXXXX.txt)
  26. chmod 600 ${SESSION_FILE}
  27. function die {
  28. echo 1>&2 "${1}"
  29. rm -f ${LOGIN_JSON} ${SESSION_FILE}
  30. exit 1
  31. }
  32. function vpn_ip() {
  33. SUBNET_MATCH=${1?no subnet match provided}
  34. case "$(uname -a | awk '{print $1}')" in
  35. Linux*)
  36. ip addr | grep inet | grep 10.19 | awk '{print $2}' | awk -F '/' '{print $1}'
  37. ;;
  38. Darwin*)
  39. ifconfig | grep inet | grep 10.19 | awk '{print $2}'
  40. ;;
  41. CYGWIN*)
  42. ipconfig | grep 10.19 | awk '{print $NF}'
  43. ;;
  44. esac
  45. }
  46. if [[ -z "$(which curl)" ]] ; then
  47. die "No curl command found on PATH"
  48. fi
  49. if [[ -z "$(which jq)" ]] ; then
  50. die "No jq command found on PATH"
  51. fi
  52. BUBBLE_HOSTNAME=${1:?no bubble-hostname provided}
  53. BUBBLE_FR_PASS_ENV_VAR=${2:-BUBBLE_FR_PASS}
  54. if [[ -z "${BFR_ADMIN_PORT}" ]] ; then
  55. BFR_ADMIN_PORT="9833"
  56. fi
  57. if [[ -z "${BUBBLE_VPN_SUBNET}" ]] ; then
  58. BUBBLE_VPN_SUBNET="10.19."
  59. fi
  60. FR_PASSWORD=${!BUBBLE_FR_PASS_ENV_VAR}
  61. if [[ -z "${FR_PASSWORD}" ]] ; then
  62. die "bubble-flexrouter password environment variable was not defined or was empty: ${BUBBLE_FR_PASS_ENV_VAR}"
  63. fi
  64. BUBBLE_VPN_IP=$(vpn_ip ${BUBBLE_VPN_SUBNET})
  65. if [[ -z "${BUBBLE_VPN_IP}" ]] ; then
  66. die "No VPN IP address found (expected something starting with ${BUBBLE_VPN_SUBNET}). Connect to your Bubble and try again."
  67. fi
  68. if [[ -z "${BUBBLE_USER}" ]] ; then
  69. read -p "Bubble Username: " BUBBLE_USER
  70. fi
  71. if [[ -z "${BUBBLE_USER}" ]] ; then
  72. die "No username provided"
  73. fi
  74. if [[ -z "${BUBBLE_PASS}" ]] ; then
  75. read -sp "Bubble Password: " BUBBLE_PASS
  76. fi
  77. if [[ -z "${BUBBLE_PASS}" ]] ; then
  78. die "No password provided"
  79. fi
  80. set -o pipefail
  81. echo "{\"name\": \"${BUBBLE_USER}\", \"password\": \"${BUBBLE_PASS}\"}" > ${LOGIN_JSON} \
  82. && curl -s -H 'Content-Type: application/json' https://${BUBBLE_HOSTNAME}:1443/api/auth/login -d @${LOGIN_JSON} | jq -r .token > ${SESSION_FILE} \
  83. || die "Login error"
  84. rm -f ${LOGIN_JSON}
  85. if [[ ! -s ${SESSION_FILE} ]] ; then
  86. die "Login error: session not found in JSON response"
  87. fi
  88. SESSION_TOKEN="$(cat ${SESSION_FILE} | tr -d [[:space:]])"
  89. echo "{
  90. \"password\": \"${FR_PASSWORD}\",
  91. \"session\": \"${SESSION_TOKEN}\",
  92. \"bubble\": \"${BUBBLE_HOSTNAME}\",
  93. \"ip\": \"${BUBBLE_VPN_IP}\"
  94. }" > ${SESSION_FILE} && \
  95. curl -s -H 'Content-Type: application/json' http://127.0.0.1:${BFR_ADMIN_PORT}/register -d @${SESSION_FILE} \
  96. || die "Registration error"
  97. rm -f ${SESSION_FILE}
  98. echo "Registration successful"