Skip to content

Commit 6a8ccf9

Browse files
committed
trying to narrow down #15 some more
1 parent a094445 commit 6a8ccf9

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

tcpproxy.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,38 @@ def start_proxy_thread(local_socket, args, in_modules, out_modules):
238238
log(args.logfile, 'Connected to %s:%d' % remote_socket.getpeername())
239239
except socket.error as serr:
240240
if serr.errno == errno.ECONNREFUSED:
241-
print('%s:%d - Connection refused' % (args.target_ip, args.target_port))
242-
log(args.logfile, '%s:%d - Connection refused' % (args.target_ip, args.target_port))
241+
for s in [remote_socket, local_socket]:
242+
s.close()
243+
print(f'{time.strftime("%Y%m%d-%H%M%S")}, {args.target_ip}:{args.target_port}- Connection refused')
244+
log(args.logfile, f'{time.strftime("%Y%m%d-%H%M%S")}, {args.target_ip}:{args.target_port}- Connection refused')
245+
return None
246+
elif serr.errno == errno.ETIMEDOUT:
247+
for s in [remote_socket, local_socket]:
248+
s.close()
249+
print(f'{time.strftime("%Y%m%d-%H%M%S")}, {args.target_ip}:{args.target_port}- Connection timed out')
250+
log(args.logfile, f'{time.strftime("%Y%m%d-%H%M%S")}, {args.target_ip}:{args.target_port}- Connection timed out')
243251
return None
244252
else:
253+
for s in [remote_socket, local_socket]:
254+
s.close()
245255
raise serr
246256

247-
update_module_hosts(out_modules, local_socket.getpeername(), remote_socket.getpeername())
248-
update_module_hosts(in_modules, remote_socket.getpeername(), local_socket.getpeername())
257+
try:
258+
update_module_hosts(out_modules, local_socket.getpeername(), remote_socket.getpeername())
259+
update_module_hosts(in_modules, remote_socket.getpeername(), local_socket.getpeername())
260+
except socket.error as serr:
261+
if serr.errno == errno.ENOTCONN:
262+
# kind of a blind shot at fixing issue #15
263+
# I don't yet understand how this error can happen, but if it happens I'll just shut down the thread
264+
# the connection is not in a useful state anymore
265+
for s in [remote_socket, local_socket]:
266+
s.close()
267+
return None
268+
else:
269+
for s in [remote_socket, local_socket]:
270+
s.close()
271+
print(f"{time.strftime('%Y%m%d-%H%M%S')}: Socket exception in start_proxy_thread")
272+
raise serr
249273

250274
# This loop ends when no more data is received on either the local or the
251275
# remote socket

0 commit comments

Comments
 (0)