pmanager.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import json
  2. import os
  3. import signal
  4. import socket
  5. import subprocess
  6. import sys
  7. import time
  8. import websocket
  9. DEBUG = False
  10. DEBUG = True
  11. def log(s):
  12. if not DEBUG:
  13. return
  14. print(s)
  15. def sendmsg(msg):
  16. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17. ret = None
  18. try:
  19. s.connect(('localhost', 9999))
  20. tosend = msg + "\r\n"
  21. encoded_msg = tosend.encode("ascii")
  22. s.sendall(encoded_msg)
  23. ret = s.recv(4096)
  24. s.close()
  25. except Exception as e:
  26. log(e)
  27. return None
  28. log("[nc output] %s " % ret)
  29. return ret
  30. class PManager:
  31. ws = websocket.WebSocket()
  32. ip = "ws://192.168.1.208:9001"
  33. p_list = {}
  34. ptable = {}
  35. log_list = {}
  36. def __init__(self, ptable):
  37. self.ptable = ptable
  38. self.conn(self.ip)
  39. signal.signal(signal.SIGINT, self.sighandler)
  40. signal.signal(signal.SIGCHLD, signal.SIG_IGN) #To kill zombies
  41. while True:
  42. try:
  43. data = self.ws.recv()
  44. except Exception as e:
  45. log(e)
  46. self.conn(self.ip)
  47. continue
  48. try:
  49. j = json.loads(data)
  50. except Exception as e:
  51. log(e)
  52. log("Invalid json?")
  53. continue
  54. if ("type" not in j or "command" not in j or
  55. "key" not in j or j["type"] != "snowmix"):
  56. log("Dropping: %s" % j)
  57. continue
  58. log(j)
  59. if j["command"] == "launch":
  60. opts = None
  61. if "options" in j:
  62. opts = j["options"]
  63. self.launch(j["key"],opts)
  64. if j["command"] == "kill":
  65. self.stop(j["key"])
  66. if j["command"] == "netcat":
  67. sendmsg(j["key"])
  68. def conn(self, target):
  69. if self.ws is None:
  70. self.ws = websocket.WebSocket()
  71. try:
  72. self.ws.connect(target)
  73. except Exception as e:
  74. log(e)
  75. time.sleep(2)
  76. self.conn(target) #keep trying you basterd
  77. def close(self):
  78. if self.ws is None:
  79. return
  80. self.ws.close()
  81. def sighandler(self, sig, frame):
  82. log("\rExiting!")
  83. self.close()
  84. log("Bye!")
  85. sys.exit(0)
  86. def stop(self, key):
  87. if key not in self.p_list:
  88. log("key %s doesn't exist! not killing!" % (key))
  89. return
  90. try:
  91. os.killpg(os.getpgid(self.p_list[key].pid), signal.SIGTERM)
  92. except Exception as e:
  93. log("This process (%d) can't be killed! Why?" % self.p_list[key].pid)
  94. log(e)
  95. try:
  96. self.log_list[key].close()
  97. except Exception as e:
  98. log("ex file: %s" % e)
  99. if key in self.log_list:
  100. del self.log_list[key]
  101. del self.p_list[key]
  102. def launch(self, key, options=None):
  103. if key in self.p_list:
  104. log("key %s exist! not running!" % (key))
  105. return
  106. if key not in self.ptable:
  107. log("This process (%s) is not on my table!" % key)
  108. return
  109. log("Launching! key: %s, opts: %s" % (key, options))
  110. tolaunch = self.ptable[key]
  111. if options is not None:
  112. tolaunch = tolaunch + options
  113. log(tolaunch)
  114. self.log_list[key] = open("/tmp/%s" % key, "w")
  115. proc = subprocess.Popen(tolaunch, preexec_fn=os.setsid, stdout=self.log_list[key])
  116. #Le doy su propio grupo al subproceso
  117. self.p_list[key] = proc
  118. return proc.pid