@@ -102,6 +102,7 @@ def __init__(self, bind_ip='0.0.0.0', bind_port=666, is_ipv6=False, use_https=Fa
102
102
gen_cert (filepath = certfile )
103
103
else :
104
104
self .scheme = 'http'
105
+
105
106
self .certfile = certfile
106
107
self .server_locked = False # Avoid call start method muti-times
107
108
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
119
120
self .host_ip = get_host_ip ()
120
121
self .httpserver = HTTPServerV4
121
122
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
+
122
127
self .__flag = threading .Event () # The identifier used to pause the thread
123
128
self .__flag .set () # set flag True
124
129
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
127
132
def start (self , daemon = True ):
128
133
# Http server can only allow start once in pocsuite3, avoid muti-threading start muti-times
129
134
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 } ' )
132
136
return
133
137
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 ):
135
149
logger .error ('Port {} has been occupied, start Httpd serve failed!' .format (self .bind_port ))
136
150
return
137
151
@@ -143,7 +157,7 @@ def start(self, daemon=True):
143
157
detect_count = 10
144
158
while detect_count :
145
159
try :
146
- if check_port (self .host_ip , self .bind_port ):
160
+ if check_port (self .check_ip , self .bind_port ):
147
161
break
148
162
except Exception as ex :
149
163
logger .error (str (ex ))
@@ -157,7 +171,7 @@ def run(self):
157
171
self .__flag .wait ()
158
172
if not self .server_started :
159
173
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 } " )
161
175
if self .https :
162
176
if self .certfile :
163
177
self .httpd .socket = ssl .wrap_socket (self .httpd .socket , certfile = self .certfile ,
@@ -172,7 +186,7 @@ def run(self):
172
186
self .__flag .clear ()
173
187
self .httpd .shutdown ()
174
188
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 } ' )
176
190
except Exception as ex :
177
191
self .httpd .shutdown ()
178
192
self .httpd .server_close ()
0 commit comments