mpsweb.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/python3
  2. #
  3. # Copyright (c) 2017 milisarge Mps Web Arayüzü için websocket aracı sunucusu
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to
  7. # deal in the Software without restriction, including without limitation the
  8. # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. # sell copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. # IN THE SOFTWARE.
  22. from websocket_server import WebsocketServer
  23. import os
  24. import json
  25. from mps import MpsIslev
  26. import subprocess
  27. kamusal_soket="https://localhost:16060/pub/mps?"
  28. sertifika='/etc/nginx/ssl/mpsweb.crt'
  29. bilgi_gonder='curl --cacert %s --request POST -H "Accept: text/json" ' % sertifika
  30. genel_kanal="genel_komut"
  31. indirme_kanal="genel_komut_indirme"
  32. iletim_betik="/opt/mpsweb/iletim.sh"
  33. mps=MpsIslev()
  34. def tr_cevir(mesaj):
  35. return mesaj.encode('iso-8859-1').decode('utf-8')
  36. def genel_iletim(kanal,komut):
  37. os.system(iletim_betik+" "+kanal+" "+komut+" &")
  38. def dosya_iletim(kanal,dosya):
  39. os.system(bilgi_gonder+kamusal_soket+'id='+kanal+' -d @'+dosya)
  40. # Her elsıkışmadan(handshake) sonra çağrılacak yordam
  41. def yeni_istemci(istemci, sunucu):
  42. print("Yeni istemci bağlandı ve verilen id: %d" % istemci['id'])
  43. sunucu.send_message_to_all("yeni istemci katıldı")
  44. # İstemci bağlantıyı kestikten sonra çağrılacak yordam
  45. def istemci_cikis(istemci, sunucu):
  46. print("İstemci(%d) ayrıldı" % istemci['id'])
  47. # ws_araci / istemci
  48. # İstemci bir mesaj gönderdiğinde çağrılacak yordam
  49. def gelen_mesaj(istemci, sunucu, mesaj):
  50. if len(mesaj) > 200:
  51. mesaj = mesaj[:200]+'..'
  52. print("İstemci(%d) gönderdi: %s" % (istemci['id'], mesaj))
  53. mps_param=["@-kl","@kur","@odkp","@-dly"]
  54. if mesaj == "ping_dur":
  55. komut="killall -9 ping"
  56. genel_iletim(genel_kanal,komut)
  57. if mesaj == "ping":
  58. komut="ping 127.0.0.1"
  59. genel_iletim(genel_kanal,komut)
  60. if mesaj == "islem":
  61. os.system("./borulama "+indirme_kanal+" ./paketvt.sh")
  62. #os.system("./borulama ./paketvt.sh &")
  63. if "kamusal_mesaj=" in mesaj :
  64. icerik=mesaj.split("=")[1]
  65. sunucu.send_message_to_all(icerik)
  66. if any(x in mesaj for x in mps_param):
  67. genel_iletim(genel_kanal,mesaj)
  68. if "pkbilgi=" in mesaj :
  69. paket=mesaj.split("=")[1]
  70. mps.bilgi(paket)
  71. mps.kos()
  72. dosya_iletim("paket_bilgi","/tmp/mps_paket_bilgi_"+paket+".html")
  73. if "sistem_yukselt" in mesaj :
  74. genel_iletim(genel_kanal,mps.yukselt())
  75. if "pktalimat=" in mesaj :
  76. paket=mesaj.split("=")[1]
  77. genel_iletim(genel_kanal,mps.talimat(paket))
  78. if "pkkur=" in mesaj :
  79. paket=mesaj.split("=")[1]
  80. komut="mps kur "+paket+" --normal"
  81. genel_iletim(indirme_kanal,komut)
  82. if "pksil=" in mesaj :
  83. paket=mesaj.split("=")[1]
  84. komut="mps sil "+paket+" evet --normal"
  85. genel_iletim(genel_kanal,komut)
  86. if "pkkdl=" in mesaj :
  87. paket=mesaj.split("=")[1]
  88. komut="mps -kdl "+paket+" --normal"
  89. genel_iletim(genel_kanal,komut)
  90. if mesaj == "pkliste":
  91. #os.system("mps paketler --json")
  92. subprocess.call(['mps','paketler', '--json'])
  93. dosya_iletim("paket_liste","/tmp/mps_paketler_listesi")
  94. if mesaj == "pkgrupliste":
  95. mps.gruplar()
  96. mps.kos()
  97. dosya_iletim("paket_grup_liste","/tmp/mps_paket_grup_listesi")
  98. if "grup_paketler=" in mesaj :
  99. grup=mesaj.split("=")[1]
  100. grup=tr_cevir(grup)
  101. mps.grup_paketler(grup)
  102. mps.kos()
  103. dosya_iletim("grup_paketler_liste","/tmp/mps_grup_paketler_listesi")
  104. if mesaj == "sistem":
  105. komut="./sistem_bilgi.sh"
  106. #genel_iletim(genel_kanal,komut)
  107. #os.system('curl --request POST -H "Accept: text/json" http://127.0.0.1:36060 --data `./sistem_bilgi.sh`')
  108. sunucu.send_message(istemci,"sistem bilgisi gönderildi")
  109. if mesaj == "pingo":
  110. os.system("killall borulama")
  111. PORT=36060
  112. sunucu = WebsocketServer(PORT)
  113. sunucu.set_fn_new_client(yeni_istemci)
  114. sunucu.set_fn_client_left(istemci_cikis)
  115. sunucu.set_fn_message_received(gelen_mesaj)
  116. print ("%s portunda mps websocket aracı sunucusu çalışıyor" % PORT)
  117. sunucu.run_forever()