sunucu.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/python3
  2. from pyftpdlib.handlers import FTPHandler
  3. from pyftpdlib.servers import FTPServer
  4. from pyftpdlib.authorizers import DummyAuthorizer
  5. from pyftpdlib.log import logger
  6. import threading
  7. import os
  8. import time
  9. from random import randint
  10. from dacid_dht import Dht
  11. import hashlib
  12. import subprocess
  13. from subprocess import check_output
  14. servisdir="./servisler"
  15. #HOST = "127.0.0.1"
  16. PORT = 2121
  17. dhtsunucu="10.45.2.129:1923"
  18. #dhtsunucu=None # 10.45.2.127:1923
  19. servis="/port-tarama/"
  20. def get_interface():
  21. for index, item in enumerate(os.listdir("/sys/class/net")):
  22. if "wg" in item:
  23. return item
  24. f = os.popen('ifconfig '+get_interface()+' | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1')
  25. HOST=f.read().strip('\n')
  26. # nmap ile port-tarama servisinin gerçeklenmesi
  27. def port_tarama(param,client_dir):
  28. """thread worker function"""
  29. updir=servisdir+servis+client_dir+"/"
  30. # portu tara- rapor oluştur
  31. subprocess.call(['nmap',"-sV",param,"-oN",updir+"raporT"])
  32. #open(updir+"raporT", 'w').write(rapor)
  33. subprocess.call(['mv',updir+"raporT",updir+"rapor"])
  34. return
  35. # sunucunun kendini diğer düğümler tarafından bulunmasını sağlamak için DHT sunucusuna kayıt ettirmesi.
  36. def dht_kayit_islemi():
  37. while 1:
  38. bilgi=str(HOST)+":"+str(PORT)
  39. bilgi2="192.168.100.3:2121"
  40. #sunucu dht ye kayıt edilecek.
  41. print ("sunucu dht ye kayıt edilecek.")
  42. dht=Dht(bs=dhtsunucu)
  43. if dht.sunucu_kayit(bilgi):
  44. logger.info("Sunucu milisia-dht (%s) kaydı yapıldı.",(dht.sunucu))
  45. time.sleep(3)
  46. dht2=Dht(bs=dhtsunucu)
  47. if dht2.sunucu_kayit(bilgi2):
  48. logger.info("Sunucu milisia-dht (%s) kaydı yapıldı.",(dht.sunucu))
  49. # 5dk da bir kendini dht suncusuna kayıt ettirecek.
  50. time.sleep(300)
  51. class MyHandler(FTPHandler):
  52. def istemci_hash(self,ip,port):
  53. ipport_str=str(ip)+"@"+str(port)
  54. hasho = hashlib.sha1()
  55. hasho.update(ipport_str.encode('utf-8'))
  56. ihash_id=hasho.hexdigest()
  57. return ihash_id
  58. def request_handle(self,file,client_id):
  59. print ("dosya:",file)
  60. istek=open(file,"r").read()
  61. t = threading.Thread(target=port_tarama, args=(istek,client_id,))
  62. t.start()
  63. def on_connect(self):
  64. print ("%s:%s connected" % (self.remote_ip, self.remote_port))
  65. #self.banner(str(randint(100, 1999)))
  66. def on_disconnect(self):
  67. # do something when client disconnects
  68. id=self.istemci_hash(self.remote_ip, self.remote_port)
  69. subprocess.call(['rm','-rf', "./servisler"+servis+id])
  70. pass
  71. def on_login(self, username):
  72. print ("%s:%s logined" % (self.remote_ip, self.remote_port))
  73. id=self.istemci_hash(self.remote_ip, self.remote_port)
  74. print (">>>>",id,"bağlantı dizini oluşturuldu")
  75. self.push(id+"\n")
  76. subprocess.call(['mkdir',"-p","./servisler"+servis+id])
  77. pass
  78. def on_logout(self, username):
  79. # do something when user logs out
  80. pass
  81. def on_file_sent(self, file):
  82. # do something when a file has been sent
  83. pass
  84. def on_file_received(self, file):
  85. # do something when a file has been received
  86. print ("%s:%s dosya_gonderdi" % (self.remote_ip, self.remote_port))
  87. client_id=self.istemci_hash(self.remote_ip, self.remote_port)
  88. self.request_handle(file,client_id)
  89. pass
  90. def on_incomplete_file_sent(self, file):
  91. # do something when a file is partially sent
  92. pass
  93. def on_incomplete_file_received(self, file):
  94. # remove partially uploaded files
  95. import os
  96. os.remove(file)
  97. def main():
  98. logger.info("sistem açıldı")
  99. # dht sunucu kayıt ettirme
  100. dhtkayit = threading.Thread(name='dht_kayit_islemi', target=dht_kayit_islemi)
  101. dhtkayit.setDaemon(True)
  102. dhtkayit.start()
  103. # ftp sunucu servis verme
  104. authorizer = DummyAuthorizer()
  105. #authorizer.add_user('user', '12345', homedir='.', perm='elradfmwMT')
  106. authorizer.add_anonymous(homedir='./servisler',perm='elradfmwMT')
  107. handler = MyHandler
  108. handler.authorizer = authorizer
  109. server = FTPServer((HOST,PORT), handler)
  110. server.serve_forever()
  111. if __name__ == "__main__":
  112. main()