The main Bubble source repository. Contains the Bubble API server, the web UI, documentation and utilities. https://getbubblenow.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mitmdump_monitor.sh 2.3 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2020 Bubble, Inc. All rights reserved. For personal (non-commercial) use, see license: https://bubblev.com/bubble-license/
  4. #
  5. LOG=/tmp/bubble.mitmdump_monitor.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. BUBBLE_MITM_MARKER=/home/bubble/.mitmdump_monitor
  15. ROOT_KEY_MARKER=/usr/share/bubble/mitmdump_monitor
  16. # Start with MITM proxy turned off
  17. if [[ ! -f ${BUBBLE_MITM_MARKER} ]] ; then
  18. echo -n off > ${BUBBLE_MITM_MARKER} && chown bubble ${BUBBLE_MITM_MARKER}
  19. fi
  20. if [[ ! -f ${ROOT_KEY_MARKER} ]] ; then
  21. sleep 1s
  22. mkdir -p "$(dirname ${ROOT_KEY_MARKER})" && chmod 755 "$(dirname ${ROOT_KEY_MARKER})"
  23. echo -n on > ${ROOT_KEY_MARKER} && touch ${ROOT_KEY_MARKER} && chmod 644 ${ROOT_KEY_MARKER}
  24. fi
  25. function ensureMitmOn {
  26. log "Flushing PREROUTING before enabling MITM services"
  27. iptables -F PREROUTING -t nat || log "Error flushing port forwarding when enabling MITM services"
  28. log "Enabling MITM port forwarding on TCP port 80 -> 8888"
  29. iptables -I PREROUTING 1 -t nat -p tcp --dport 80 -j REDIRECT --to-ports 8888 || log "Error enabling MITM port forwarding 80 -> 8888"
  30. log "Enabling MITM port forwarding on TCP port 443 -> 8888"
  31. iptables -I PREROUTING 1 -t nat -p tcp --dport 443 -j REDIRECT --to-ports 8888 || log "Error enabling MITM port forwarding 443 -> 8888"
  32. echo -n on > ${ROOT_KEY_MARKER}
  33. }
  34. function ensureMitmOff {
  35. log "Flushing PREROUTING to disable MITM services"
  36. iptables -F PREROUTING -t nat || log "Error flushing port forwarding when disabling MITM services"
  37. echo -n off > ${ROOT_KEY_MARKER}
  38. }
  39. log "Watching marker file ${BUBBLE_MITM_MARKER} ..."
  40. sleep 2s && touch ${BUBBLE_MITM_MARKER} # first time through, always check and set on/off state
  41. while : ; do
  42. if [[ $(stat -c %Y ${BUBBLE_MITM_MARKER}) -gt $(stat -c %Y ${ROOT_KEY_MARKER}) ]] ; then
  43. if [[ ! -z "$(cmp -b ${ROOT_KEY_MARKER} ${BUBBLE_MITM_MARKER})" ]] ; then
  44. if [[ "$(cat ${BUBBLE_MITM_MARKER} | tr -d [[:space:]])" == "on" ]] ; then
  45. ensureMitmOn
  46. elif [[ "$(cat ${BUBBLE_MITM_MARKER} | tr -d [[:space:]])" == "off" ]] ; then
  47. ensureMitmOff
  48. else
  49. log "Error: marker file ${BUBBLE_MITM_MARKER} contained invalid value: $(cat ${BUBBLE_MITM_MARKER} | head -c 5)"
  50. fi
  51. fi
  52. fi
  53. sleep 5s
  54. done