wsread.py 2.4 KB

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