Skip to content

Commit b26a773

Browse files
committed
fix: httpserver module hangs on macos platform
1 parent 44394b2 commit b26a773

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

pocsuite3/modules/httpserver/__init__.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def __init__(self, bind_ip='0.0.0.0', bind_port=666, is_ipv6=False, use_https=Fa
102102
gen_cert(filepath=certfile)
103103
else:
104104
self.scheme = 'http'
105+
105106
self.certfile = certfile
106107
self.server_locked = False # Avoid call start method muti-times
107108
self.server_started = False # Aviod start server mutl-times
@@ -119,6 +120,10 @@ def __init__(self, bind_ip='0.0.0.0', bind_port=666, is_ipv6=False, use_https=Fa
119120
self.host_ip = get_host_ip()
120121
self.httpserver = HTTPServerV4
121122

123+
self.url = f'{self.scheme}://{self.bind_ip}:{self.bind_port}'
124+
if self.is_ipv6:
125+
self.url = f'{self.scheme}://[{self.bind_ip}]:{self.bind_port}'
126+
122127
self.__flag = threading.Event() # The identifier used to pause the thread
123128
self.__flag.set() # set flag True
124129
self.__running = threading.Event() # The identifier used to stop the thread
@@ -127,11 +132,20 @@ def __init__(self, bind_ip='0.0.0.0', bind_port=666, is_ipv6=False, use_https=Fa
127132
def start(self, daemon=True):
128133
# Http server can only allow start once in pocsuite3, avoid muti-threading start muti-times
129134
if self.server_locked:
130-
logger.info(
131-
'Httpd serve has been started on {}://{}:{}, '.format(self.scheme, self.bind_ip, self.bind_port))
135+
logger.info(f'Httpd serve has been started on {self.url}')
132136
return
133137

134-
if check_port(self.host_ip, self.bind_port):
138+
'''
139+
fix httpserver module hangs on macos platform
140+
https://github.com/knownsec/pocsuite3/issues/325
141+
'''
142+
self.check_ip = self.host_ip
143+
if self.bind_ip == '0.0.0.0':
144+
self.check_ip = '127.0.0.1'
145+
elif self.bind_ip == '::':
146+
self.check_ip = '::1'
147+
148+
if check_port(self.check_ip, self.bind_port):
135149
logger.error('Port {} has been occupied, start Httpd serve failed!'.format(self.bind_port))
136150
return
137151

@@ -143,7 +157,7 @@ def start(self, daemon=True):
143157
detect_count = 10
144158
while detect_count:
145159
try:
146-
if check_port(self.host_ip, self.bind_port):
160+
if check_port(self.check_ip, self.bind_port):
147161
break
148162
except Exception as ex:
149163
logger.error(str(ex))
@@ -157,7 +171,7 @@ def run(self):
157171
self.__flag.wait()
158172
if not self.server_started:
159173
self.httpd = self.httpserver((self.bind_ip, self.bind_port), self.requestHandler)
160-
logger.info("Starting httpd on {}://{}:{}".format(self.scheme, self.bind_ip, self.bind_port))
174+
logger.info(f"Starting httpd on {self.url}")
161175
if self.https:
162176
if self.certfile:
163177
self.httpd.socket = ssl.wrap_socket(self.httpd.socket, certfile=self.certfile,
@@ -172,7 +186,7 @@ def run(self):
172186
self.__flag.clear()
173187
self.httpd.shutdown()
174188
self.httpd.server_close()
175-
logger.info('Stop httpd server on {}://{}:{}'.format(self.scheme, self.bind_ip, self.bind_port))
189+
logger.info(f'Stop httpd server on {self.url}')
176190
except Exception as ex:
177191
self.httpd.shutdown()
178192
self.httpd.server_close()

0 commit comments

Comments
 (0)