Bubble proxy service
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

flex_init.sh 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/bash
  2. #
  3. # Utility to initialize a bubble-flexrouter.
  4. #
  5. # Note: Initialization requires the `htpasswd` program to be installed, to compute the bcrypted password
  6. #
  7. # Usage:
  8. #
  9. # flex_init.sh [-f|--force] [-b|--bcrypt] [flex-password-env-var]
  10. #
  11. # -f or --force : Recreate token file, password file and SSH key, even if present
  12. #
  13. # -b or --bcrypt : If set, this means that the contents of the flex-password-env-var
  14. # is a bcrypted password, not a plaintext password
  15. #
  16. # flex-password-env-var : Name of environment variable containing password to bcrypt and write to password file
  17. # Default value is BUBBLE_FR_PASS, or if not set, from prompt
  18. # Ignored if password file already exists and -f / --force was not specified
  19. #
  20. # The init command will create the following files:
  21. # ${HOME}/.bfr_pass # the bcrypted password
  22. # ${HOME}/.bfr_token # the flex router token
  23. # ${HOME}/.ssh/flex.pub # the SSH public key
  24. # ${HOME}/.ssh/flex # the SSH private key
  25. #
  26. function die {
  27. echo 1>&2 "${1}"
  28. exit 1
  29. }
  30. case "$(uname -a | awk '{print $1}')" in
  31. Linux*)
  32. if [[ -z "${BUBBLE_DIST_HOME}" ]] ; then
  33. SHA_CMD="sha256sum"
  34. fi
  35. ;;
  36. Darwin*)
  37. SHA_CMD="shasum -a 256"
  38. ;;
  39. CYGWIN*)
  40. SHA_CMD="sha256sum"
  41. ;;
  42. esac
  43. function rand_string() {
  44. cat /dev/random | strings | head -c 1000 | ${SHA_CMD}
  45. }
  46. function write_ssh_key() {
  47. KEY_FILE="${1}"
  48. KEY_DIR="$(dirname ${KEY_FILE})"
  49. if [[ ! -d "${KEY_DIR}" ]] ; then
  50. mkdir -p "${KEY_DIR}" && chmod 700 ${KEY_DIR} || die "Error creating SSH key directory: ${KEY_DIR}"
  51. fi
  52. ssh-keygen -t rsa -q -N '' -C 'bubble-flexrouter' -f ${KEY_FILE} || die "Error generating SSH key: ${KEY_FILE}"
  53. }
  54. echo "Initializing flex-router"
  55. FORCE=0
  56. DO_BCRYPT=1
  57. while [[ ! -z "${1}" && ${1} == -* ]] ; do
  58. if [[ ${1} == "--force" || ${1} == "-f" ]] ; then
  59. FORCE=1
  60. shift
  61. elif [[ ${1} == "--bcrypt" || ${1} == "-b" ]] ; then
  62. DO_BCRYPT=0
  63. shift
  64. else
  65. die "Only allowed options are: --force / -f and --bcrypt / -b"
  66. fi
  67. done
  68. BFR_PASSWORD_FILE="${HOME}/.bfr_pass"
  69. BFR_TOKEN_FILE="${HOME}/.bfr_token"
  70. BFR_SSH_KEY_FILE="${HOME}/.ssh/flex"
  71. WRITE_PASS=0
  72. if [[ -s ${BFR_PASSWORD_FILE} ]] ; then
  73. if [[ ${FORCE} -eq 1 ]] ; then
  74. echo "Password file exists but -f / --force was set, overwriting: ${BFR_PASSWORD_FILE}"
  75. WRITE_PASS=1
  76. else
  77. echo "Password file exists, not overwriting: ${BFR_PASSWORD_FILE}"
  78. fi
  79. else
  80. WRITE_PASS=1
  81. fi
  82. if [[ ${WRITE_PASS} -eq 1 ]] ; then
  83. if [[ $DO_BCRYPT -eq 1 ]] ; then
  84. if [[ -z "$(which htpasswd)" ]] ; then
  85. die "htpasswd command not found, cannot bcrypt password"
  86. fi
  87. fi
  88. BFR_PASSWORD_VAR="${1}"
  89. if [[ -z "${BFR_PASSWORD_VAR}" ]] ; then
  90. BFR_PASSWORD_VAR="BUBBLE_FR_PASS"
  91. fi
  92. BFR_PASSWORD="${!BFR_PASSWORD_VAR}"
  93. if [[ -z "${BFR_PASSWORD}" ]] ; then
  94. read -sp "Bubble Flex Router Password: " BFR_PASSWORD
  95. fi
  96. if [[ $DO_BCRYPT -eq 1 ]] ; then
  97. echo "$(htpasswd -nbBC 12 USER "${BFR_PASSWORD}" | awk -F ':' '{print $2}')" > ${BFR_PASSWORD_FILE} || die "Error writing password file"
  98. else
  99. echo "${BFR_PASSWORD}" > ${BFR_PASSWORD_FILE} || die "Error writing password file"
  100. fi
  101. chmod 600 ${BFR_PASSWORD_FILE} || die "Error setting permission on password file: ${BFR_PASSWORD_FILE}"
  102. echo "Wrote bcrypted password to ${BFR_PASSWORD_FILE}"
  103. fi
  104. if [[ -s ${BFR_TOKEN_FILE} ]] ; then
  105. if [[ ${FORCE} -eq 0 ]] ; then
  106. echo "Token file exists, not overwriting: ${BFR_TOKEN_FILE}"
  107. else
  108. echo "Token file exists but -f / --force was set, overwriting: ${BFR_TOKEN_FILE}"
  109. echo "$(rand_string)" > "${BFR_TOKEN_FILE}"
  110. fi
  111. else
  112. echo "Token file not found or empty, creating: ${BFR_TOKEN_FILE}"
  113. echo "$(rand_string)" > "${BFR_TOKEN_FILE}"
  114. fi
  115. chmod 600 ${BFR_TOKEN_FILE} || die "Error setting permission on token file: ${BFR_TOKEN_FILE}"
  116. if [[ -s ${BFR_SSH_KEY_FILE} ]] ; then
  117. if [[ ${FORCE} -eq 0 ]] ; then
  118. echo "SSH key file exists, not overwriting: ${BFR_SSH_KEY_FILE}"
  119. else
  120. echo "SSH key file exists but -f / --force was set, overwriting: ${BFR_SSH_KEY_FILE}"
  121. rm -f ${BFR_SSH_KEY_FILE} ${BFR_SSH_KEY_FILE}.pub || die "Error removing existing key file: ${BFR_SSH_KEY_FILE} and ${BFR_SSH_KEY_FILE}.pub"
  122. write_ssh_key ${BFR_SSH_KEY_FILE}
  123. fi
  124. else
  125. echo "SSH key file not found or empty, creating: ${BFR_SSH_KEY_FILE}"
  126. write_ssh_key ${BFR_SSH_KEY_FILE}
  127. fi
  128. echo "Initialization completed successfully"