/code/default/x_tunnel/local/heroku_front/check_ip.py

https://gitlab.com/Mirros/XX-net · Python · 159 lines · 115 code · 39 blank · 5 comment · 26 complexity · efd0a232ac9766380699640031fa6d5c MD5 · raw file

  1. #!/usr/bin/env python2
  2. # coding:utf-8
  3. import sys
  4. import os
  5. import threading
  6. current_path = os.path.dirname(os.path.abspath(__file__))
  7. root_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir, os.pardir))
  8. data_path = os.path.abspath(os.path.join(root_path, os.pardir, os.pardir, 'data'))
  9. module_data_path = os.path.join(data_path, 'x_tunnel')
  10. python_path = os.path.abspath( os.path.join(root_path, 'python27', '1.0'))
  11. sys.path.append(root_path)
  12. noarch_lib = os.path.abspath( os.path.join(python_path, 'lib', 'noarch'))
  13. sys.path.append(noarch_lib)
  14. if sys.platform == "win32":
  15. win32_lib = os.path.abspath( os.path.join(python_path, 'lib', 'win32'))
  16. sys.path.append(win32_lib)
  17. elif sys.platform.startswith("linux"):
  18. linux_lib = os.path.abspath( os.path.join(python_path, 'lib', 'linux'))
  19. sys.path.append(linux_lib)
  20. elif sys.platform == "darwin":
  21. darwin_lib = os.path.abspath( os.path.join(python_path, 'lib', 'darwin'))
  22. sys.path.append(darwin_lib)
  23. extra_lib = "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python"
  24. sys.path.append(extra_lib)
  25. import xlog
  26. logger = xlog.getLogger("check_ip")
  27. logger.set_buffer(500)
  28. from front_base.openssl_wrap import SSLContext
  29. from front_base.host_manager import HostManagerBase
  30. from front_base.connect_creator import ConnectCreator
  31. from front_base.check_ip import CheckIp
  32. from config import Config
  33. class CheckAllIp(object):
  34. def __init__(self):
  35. config_path = os.path.join(module_data_path, "heroku_front.json")
  36. config = Config(config_path)
  37. openssl_context = SSLContext(logger)
  38. host_manager = HostManagerBase()
  39. connect_creator = ConnectCreator(logger, config, openssl_context, host_manager,
  40. debug=True)
  41. self.check_ip = CheckIp(logger, config, connect_creator)
  42. self.lock = threading.Lock()
  43. self.in_fd = open("good_ip.txt", "r")
  44. self.out_fd = open(
  45. os.path.join(module_data_path, "heroku_checked_ip.txt"),
  46. "w"
  47. )
  48. def get_ip(self):
  49. with self.lock:
  50. while True:
  51. line = self.in_fd.readline()
  52. if not line:
  53. raise Exception()
  54. try:
  55. ip = line.split()[0]
  56. return ip
  57. except:
  58. continue
  59. def write_ip(self, ip, host, handshake):
  60. with self.lock:
  61. self.out_fd.write("%s %s gws %d 0 0\n" % (ip, host, handshake))
  62. self.out_fd.flush()
  63. def checker(self):
  64. while True:
  65. try:
  66. ip = self.get_ip()
  67. except Exception as e:
  68. xlog.info("no ip left")
  69. return
  70. try:
  71. res = self.check_ip.check_ip(ip)
  72. except Exception as e:
  73. xlog.warn("check except:%r", e)
  74. continue
  75. if not res or not res.ok:
  76. xlog.debug("ip:%s fail", ip)
  77. continue
  78. self.write_ip(ip, res.domain, res.handshake_time)
  79. def run(self):
  80. for i in range(0, 10):
  81. threading.Thread(target=self.checker).run()
  82. def check_all():
  83. check = CheckAllIp()
  84. check.run()
  85. exit(0)
  86. def check_one(ip, top_domain, wait_time):
  87. config_path = os.path.join(module_data_path, "heroku_front.json")
  88. config = Config(config_path)
  89. openssl_context = SSLContext(logger)
  90. host_manager = HostManagerBase()
  91. connect_creator = ConnectCreator(logger, config, openssl_context, host_manager,
  92. debug=True)
  93. check_ip = CheckIp(logger, config, connect_creator)
  94. res = check_ip.check_ip(ip, host=top_domain, wait_time=wait_time)
  95. if not res:
  96. print("connect fail")
  97. elif res.ok:
  98. print("success, domain:%s handshake:%d" % (res.domain, res.handshake_time))
  99. else:
  100. print("not support")
  101. if __name__ == "__main__":
  102. check_all()
  103. # case 1: only ip
  104. # case 2: ip + domain
  105. # connect use domain
  106. if len(sys.argv) > 1:
  107. ip = sys.argv[1]
  108. else:
  109. ip = "54.225.129.54"
  110. print("Usage: check_ip.py [ip] [top_domain] [wait_time=0]")
  111. print("test ip:%s" % ip)
  112. if len(sys.argv) > 2:
  113. top_domain = sys.argv[2]
  114. else:
  115. top_domain = None
  116. if len(sys.argv) > 3:
  117. wait_time = int(sys.argv[3])
  118. else:
  119. wait_time = 0
  120. check_one(ip, top_domain, wait_time)