dacid_dht.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2018 Milis Linux
  3. # Author: milisarge <milisarge@gmail.com>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; If not, see <http://www.gnu.org/licenses/>.
  17. from urllib.parse import urlparse
  18. import base64, json
  19. from opendht import *
  20. import opendht as dht
  21. import threading, time
  22. from random import randint
  23. import sys
  24. import hashlib
  25. class Dht():
  26. def __init__(self,port=None,bs=None):
  27. self.sunucu_isim="d-acid_sunucu"
  28. if bs is None:
  29. self.sunucu="bootstrap.ring.cx:4222"
  30. else:
  31. self.sunucu=bs
  32. self.id = Identity()
  33. self.id.generate()
  34. self.node = DhtRunner()
  35. if port is None:
  36. port=randint(60000,65000)
  37. self.node.run(self.id, port=port)
  38. bss=self.sunucu.split(":")[0]
  39. bsp=self.sunucu.split(":")[1]
  40. self.node.bootstrap(bss,bsp)
  41. def hash_al(self,anahtar):
  42. sha1 = hashlib.sha1()
  43. sha1.update(anahtar.encode())
  44. anahtar_hash=sha1.hexdigest()
  45. return anahtar_hash
  46. def kayit_edildi(self):
  47. time.sleep(10)
  48. print ("--------")
  49. def sunucu_kayit(self,deger,anahtar=None):
  50. #ahash=self.hash_al(anahtar)
  51. if anahtar is None:
  52. anahtar=self.sunucu_isim
  53. time.sleep(1)
  54. #sonuc=self.node.put(dht.InfoHash.get(anahtar),dht.Value(deger.encode()),self.kayit_edildi())
  55. sonuc=self.node.put(dht.InfoHash.get(anahtar),dht.Value(deger.encode()))
  56. #dht dugumunu durdurmak için
  57. self.node=None
  58. return sonuc
  59. def sunucular(self,anahtar=None):
  60. liste=[]
  61. #arama saniyesi
  62. arasn=10
  63. print ("(%s) altında %s saniye arama yapılacak!" % (self.sunucu,arasn))
  64. if anahtar is None:
  65. anahtar=self.sunucu_isim
  66. bitis = 1
  67. while bitis > 0:
  68. bitis += 1
  69. veriler=(self.node.get(InfoHash.get(anahtar)))
  70. for veri in veriler:
  71. sunbilgi=(veri.data).decode('ascii')
  72. if sunbilgi not in liste:
  73. liste.append(sunbilgi)
  74. print (len(liste)," adet kayıt bulundu.",sunbilgi)
  75. time.sleep(1)
  76. if bitis == arasn:
  77. print ("dht sunucu tarama sonlandırıldı.")
  78. break
  79. return liste