Minor optimization of _setSocketPreConnect

This commit is contained in:
Miroslav Stampar
2026-01-28 19:41:37 +01:00
parent ebfc481755
commit cb36ff7f22
3 changed files with 47 additions and 16 deletions

View File

@@ -181,14 +181,14 @@ a033f92d136c707a25927c2383125ddb004d4283db62c004dcd67c3fc242bb1c lib/core/dump.
1966ca704961fb987ab757f0a4afddbf841d1a880631b701487c75cef63d60c3 lib/core/__init__.py 1966ca704961fb987ab757f0a4afddbf841d1a880631b701487c75cef63d60c3 lib/core/__init__.py
914a13ee21fd610a6153a37cbe50830fcbd1324c7ebc1e7fc206d5e598b0f7ad lib/core/log.py 914a13ee21fd610a6153a37cbe50830fcbd1324c7ebc1e7fc206d5e598b0f7ad lib/core/log.py
02a2264324caa249154e024a01bcd7cc40dbca4d647d5d10a50654b4415a6d77 lib/core/optiondict.py 02a2264324caa249154e024a01bcd7cc40dbca4d647d5d10a50654b4415a6d77 lib/core/optiondict.py
86070b96d28df6caa55e56f8fb16ba162524cab92a2d70eb110ae30f25099866 lib/core/option.py c1cb56f2a43e9f2f6b25d5f3d504e856ea21df6fc14af5e37b1000feef2bdb5a lib/core/option.py
8171f6ee33e7742f06bb3014a28324496374beddee7b378ace10a26414a97762 lib/core/patch.py 8171f6ee33e7742f06bb3014a28324496374beddee7b378ace10a26414a97762 lib/core/patch.py
49c0fa7e3814dfda610d665ee02b12df299b28bc0b6773815b4395514ddf8dec lib/core/profiling.py 49c0fa7e3814dfda610d665ee02b12df299b28bc0b6773815b4395514ddf8dec lib/core/profiling.py
03db48f02c3d07a047ddb8fe33a757b6238867352d8ddda2a83e4fec09a98d04 lib/core/readlineng.py 03db48f02c3d07a047ddb8fe33a757b6238867352d8ddda2a83e4fec09a98d04 lib/core/readlineng.py
48797d6c34dd9bb8a53f7f3794c85f4288d82a9a1d6be7fcf317d388cb20d4b3 lib/core/replication.py 48797d6c34dd9bb8a53f7f3794c85f4288d82a9a1d6be7fcf317d388cb20d4b3 lib/core/replication.py
0b8c38a01bb01f843d94a6c5f2075ee47520d0c4aa799cecea9c3e2c5a4a23a6 lib/core/revision.py 0b8c38a01bb01f843d94a6c5f2075ee47520d0c4aa799cecea9c3e2c5a4a23a6 lib/core/revision.py
888daba83fd4a34e9503fe21f01fef4cc730e5cde871b1d40e15d4cbc847d56c lib/core/session.py 888daba83fd4a34e9503fe21f01fef4cc730e5cde871b1d40e15d4cbc847d56c lib/core/session.py
2fcd2dddb4705e168e8fb3a8092f75a01f32118bd07316fc010a83c4eaf91ed1 lib/core/settings.py e2e711274bf226e785203353f9dd205e8fd3b9b11f9470513df38178edf288b6 lib/core/settings.py
cd5a66deee8963ba8e7e9af3dd36eb5e8127d4d68698811c29e789655f507f82 lib/core/shell.py cd5a66deee8963ba8e7e9af3dd36eb5e8127d4d68698811c29e789655f507f82 lib/core/shell.py
bcb5d8090d5e3e0ef2a586ba09ba80eef0c6d51feb0f611ed25299fbb254f725 lib/core/subprocessng.py bcb5d8090d5e3e0ef2a586ba09ba80eef0c6d51feb0f611ed25299fbb254f725 lib/core/subprocessng.py
d35650179816193164a5f177102f18379dfbe6bb6d40fbb67b78d907b41c8038 lib/core/target.py d35650179816193164a5f177102f18379dfbe6bb6d40fbb67b78d907b41c8038 lib/core/target.py

View File

@@ -8,6 +8,7 @@ See the file 'LICENSE' for copying permission
from __future__ import division from __future__ import division
import codecs import codecs
import collections
import functools import functools
import glob import glob
import inspect import inspect
@@ -1057,11 +1058,31 @@ def _setSocketPreConnect():
def _thread(): def _thread():
while kb.get("threadContinue") and not conf.get("disablePrecon"): while kb.get("threadContinue") and not conf.get("disablePrecon"):
try: try:
for key in socket._ready: with kb.locks.socket:
if len(socket._ready[key]) < SOCKET_PRE_CONNECT_QUEUE_SIZE: keys = list(socket._ready.keys())
s = socket.create_connection(*key[0], **dict(key[1]))
with kb.locks.socket: for key in keys:
socket._ready[key].append((s, time.time())) with kb.locks.socket:
q = socket._ready.get(key)
if q is None or len(q) >= SOCKET_PRE_CONNECT_QUEUE_SIZE:
continue
args = key[0]
kwargs = dict(key[1])
s = socket._create_connection(*args, **kwargs)
with kb.locks.socket:
q = socket._ready.get(key)
if q is not None and len(q) < SOCKET_PRE_CONNECT_QUEUE_SIZE:
q.append((s, time.time()))
s = None
if s is not None:
try:
s.close()
except:
pass
except KeyboardInterrupt: except KeyboardInterrupt:
break break
except: except:
@@ -1071,26 +1092,36 @@ def _setSocketPreConnect():
def create_connection(*args, **kwargs): def create_connection(*args, **kwargs):
retVal = None retVal = None
stale = []
key = (tuple(args), frozenset(kwargs.items())) key = (tuple(args), frozenset(kwargs.items()))
with kb.locks.socket: with kb.locks.socket:
if key not in socket._ready: if key not in socket._ready:
socket._ready[key] = [] socket._ready[key] = collections.deque()
while len(socket._ready[key]) > 0: q = socket._ready[key]
candidate, created = socket._ready[key].pop(0) while len(q) > 0:
candidate, created = q.popleft()
if (time.time() - created) < PRECONNECT_CANDIDATE_TIMEOUT: if (time.time() - created) < PRECONNECT_CANDIDATE_TIMEOUT:
retVal = candidate retVal = candidate
break break
else: else:
try: stale.append(candidate)
candidate.shutdown(socket.SHUT_RDWR)
candidate.close() for candidate in stale:
except socket.error: try:
pass candidate.shutdown(socket.SHUT_RDWR)
candidate.close()
except:
pass
if not retVal: if not retVal:
retVal = socket._create_connection(*args, **kwargs) retVal = socket._create_connection(*args, **kwargs)
else:
try:
retVal.settimeout(kwargs.get("timeout", socket.getdefaulttimeout()))
except:
pass
return retVal return retVal

View File

@@ -19,7 +19,7 @@ from lib.core.enums import OS
from thirdparty import six from thirdparty import six
# sqlmap version (<major>.<minor>.<month>.<monthly commit>) # sqlmap version (<major>.<minor>.<month>.<monthly commit>)
VERSION = "1.10.1.72" VERSION = "1.10.1.73"
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable" TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34} TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE) VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)