Skip to content

Commit

Permalink
fix: httpserver module hangs on macos platform
Browse files Browse the repository at this point in the history
  • Loading branch information
13ph03nix committed Sep 9, 2022
1 parent 44394b2 commit b26a773
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions pocsuite3/modules/httpserver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(self, bind_ip='0.0.0.0', bind_port=666, is_ipv6=False, use_https=Fa
gen_cert(filepath=certfile)
else:
self.scheme = 'http'

self.certfile = certfile
self.server_locked = False # Avoid call start method muti-times
self.server_started = False # Aviod start server mutl-times
Expand All @@ -119,6 +120,10 @@ def __init__(self, bind_ip='0.0.0.0', bind_port=666, is_ipv6=False, use_https=Fa
self.host_ip = get_host_ip()
self.httpserver = HTTPServerV4

self.url = f'{self.scheme}://{self.bind_ip}:{self.bind_port}'
if self.is_ipv6:
self.url = f'{self.scheme}://[{self.bind_ip}]:{self.bind_port}'

self.__flag = threading.Event() # The identifier used to pause the thread
self.__flag.set() # set flag True
self.__running = threading.Event() # The identifier used to stop the thread
Expand All @@ -127,11 +132,20 @@ def __init__(self, bind_ip='0.0.0.0', bind_port=666, is_ipv6=False, use_https=Fa
def start(self, daemon=True):
# Http server can only allow start once in pocsuite3, avoid muti-threading start muti-times
if self.server_locked:
logger.info(
'Httpd serve has been started on {}://{}:{}, '.format(self.scheme, self.bind_ip, self.bind_port))
logger.info(f'Httpd serve has been started on {self.url}')
return

if check_port(self.host_ip, self.bind_port):
'''
fix httpserver module hangs on macos platform
https://github.com/knownsec/pocsuite3/issues/325
'''
self.check_ip = self.host_ip
if self.bind_ip == '0.0.0.0':
self.check_ip = '127.0.0.1'
elif self.bind_ip == '::':
self.check_ip = '::1'

if check_port(self.check_ip, self.bind_port):
logger.error('Port {} has been occupied, start Httpd serve failed!'.format(self.bind_port))
return

Expand All @@ -143,7 +157,7 @@ def start(self, daemon=True):
detect_count = 10
while detect_count:
try:
if check_port(self.host_ip, self.bind_port):
if check_port(self.check_ip, self.bind_port):
break
except Exception as ex:
logger.error(str(ex))
Expand All @@ -157,7 +171,7 @@ def run(self):
self.__flag.wait()
if not self.server_started:
self.httpd = self.httpserver((self.bind_ip, self.bind_port), self.requestHandler)
logger.info("Starting httpd on {}://{}:{}".format(self.scheme, self.bind_ip, self.bind_port))
logger.info(f"Starting httpd on {self.url}")
if self.https:
if self.certfile:
self.httpd.socket = ssl.wrap_socket(self.httpd.socket, certfile=self.certfile,
Expand All @@ -172,7 +186,7 @@ def run(self):
self.__flag.clear()
self.httpd.shutdown()
self.httpd.server_close()
logger.info('Stop httpd server on {}://{}:{}'.format(self.scheme, self.bind_ip, self.bind_port))
logger.info(f'Stop httpd server on {self.url}')
except Exception as ex:
self.httpd.shutdown()
self.httpd.server_close()
Expand Down

0 comments on commit b26a773

Please sign in to comment.