/lib/sender/polling.py

https://github.com/j3ssie/Osmedeus · Python · 81 lines · 59 code · 16 blank · 6 comment · 16 complexity · b84c13a667b6b1941824e77561b7f915 MD5 · raw file

  1. import os
  2. import sys
  3. import time
  4. sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
  5. from lib.sender import send
  6. from lib.core import utils
  7. def waiting(options, delay=20, times=0):
  8. elapsed_time = 0
  9. if times:
  10. count = 0
  11. module_name = options.get('CURRENT_MODULE', False)
  12. utils.print_info('Waiting for {0} module'.format(module_name))
  13. checking = poll_status(options)
  14. while checking:
  15. time.sleep(delay)
  16. if not times:
  17. # just don't print this too much
  18. if ((elapsed_time / delay) % 10) == 0:
  19. utils.print_info('Waiting for {0} module'.format(module_name))
  20. time.sleep(delay)
  21. if times:
  22. utils.print_info('Waiting for {0} module {1}/{2}'.format(module_name, str(count), str(times)))
  23. if count == int(times):
  24. poll_status(options, forced=True)
  25. utils.print_bad("Something bad with {0} module but force to continue".format(module_name))
  26. break
  27. count += 1
  28. checking = poll_status(options)
  29. # print(checking)
  30. # clear old stuff because it's may be failed
  31. # continious will check output file so don't worry
  32. def clear_activities(options):
  33. ws = utils.get_workspace(options=options)
  34. module = options.get('CURRENT_MODULE', False)
  35. url = options.get('REMOTE_API') + "/api/activities/clear/"
  36. body = {
  37. "workspace": ws,
  38. "module": module,
  39. }
  40. headers = send.osmedeus_headers
  41. headers['Authorization'] = options.get('JWT')
  42. r = send.send_post(url, body, headers=headers, is_json=True)
  43. if r and r.json().get('status') == 200:
  44. utils.print_good("Clean old activities for {0}:{1}".format(ws, module))
  45. return True
  46. return False
  47. # parsing command
  48. def poll_status(options, forced=False):
  49. # /api/activities/get/?workspace=duckduckgo.com&modules=SubdomainScanning
  50. ws = utils.get_workspace(options=options)
  51. module = options.get('CURRENT_MODULE', False)
  52. if module:
  53. url = options.get('REMOTE_API') + "/api/activities/get/?workspace={0}&module={1}".format(ws, module)
  54. else:
  55. url = options.get('REMOTE_API') + "/api/activities/get/?workspace={0}".format(ws)
  56. headers = send.osmedeus_headers
  57. headers['Authorization'] = options.get('JWT')
  58. if forced:
  59. r = send.send_post(url, data=None, headers=headers)
  60. else:
  61. r = send.send_get(url, data=None, headers=headers)
  62. if r and r.json().get('status') == 'Done':
  63. return False
  64. if not r or r.json().get('status') != 'Done':
  65. return True