Reformatted code base using black.

This commit is contained in:
Goffi 2024-06-19 18:44:57 +02:00
commit 53fdaa9d09

View file

@ -96,8 +96,8 @@ BS_REQUEST = IQ_SET + '/query[@xmlns="' + NS_BS + '"]'
TIMER_KEY = "timer"
DEFER_KEY = "finished" # key of the deferred used to track session end
SERVER_STARTING_PORT = (
0
) # starting number for server port search (0 to ask automatic attribution)
0 # starting number for server port search (0 to ask automatic attribution)
)
# priorities are candidates local priorities, must be a int between 0 and 65535
PRIORITY_BEST_DIRECT = 10000
@ -169,8 +169,17 @@ ProxyInfos = namedtuple("ProxyInfos", ["host", "jid", "port"])
class Candidate(object):
def __init__(self, host, port, type_, priority, jid_, id_=None, priority_local=False,
factory=None,):
def __init__(
self,
host,
port,
type_,
priority,
jid_,
id_=None,
priority_local=False,
factory=None,
):
"""
@param host(unicode): host IP or domain
@param port(int): port
@ -247,7 +256,7 @@ class Candidate(object):
multiplier = 10
else:
raise exceptions.InternalError("Unknown {} type !".format(self.type))
return 2 ** 16 * multiplier + self._local_priority
return 2**16 * multiplier + self._local_priority
def activate(self, client, sid, peer_jid, local_jid):
"""Activate the proxy candidate
@ -289,7 +298,7 @@ def get_session_hash(requester_jid, target_jid, sid):
class SOCKSv5(protocol.Protocol):
CHUNK_SIZE = 2 ** 16
CHUNK_SIZE = 2**16
def __init__(self, session_hash=None):
"""
@ -377,9 +386,11 @@ class SOCKSv5(protocol.Protocol):
try:
# Parse out data
ver, ulen = struct.unpack("BB", self.buf[:2])
uname, = struct.unpack("%ds" % ulen, self.buf[2 : ulen + 2])
plen, = struct.unpack("B", self.buf[ulen + 2])
password, = struct.unpack("%ds" % plen, self.buf[ulen + 3 : ulen + 3 + plen])
(uname,) = struct.unpack("%ds" % ulen, self.buf[2 : ulen + 2])
(plen,) = struct.unpack("B", self.buf[ulen + 2])
(password,) = struct.unpack(
"%ds" % plen, self.buf[ulen + 3 : ulen + 3 + plen]
)
# Trim off fron of the buffer
self.buf = self.buf[3 + ulen + plen :]
# Fire event to authenticate user
@ -443,7 +454,7 @@ class SOCKSv5(protocol.Protocol):
return None
def _make_request(self):
hash_ = self._session_hash.encode('utf-8')
hash_ = self._session_hash.encode("utf-8")
request = struct.pack(
"!5B%dsH" % len(hash_),
SOCKS5_VER,
@ -501,7 +512,7 @@ class SOCKSv5(protocol.Protocol):
def connect_requested(self, addr, port):
# Check that this session is expected
if not self.factory.add_to_session(addr.decode('utf-8'), self):
if not self.factory.add_to_session(addr.decode("utf-8"), self):
log.warning(
"Unexpected connection request received from {host}".format(
host=self.transport.getPeer().host
@ -509,7 +520,7 @@ class SOCKSv5(protocol.Protocol):
)
self.send_error_reply(REPLY_CONN_REFUSED)
return
self._session_hash = addr.decode('utf-8')
self._session_hash = addr.decode("utf-8")
self.connect_completed(addr, 0)
def start_transfer(self, chunk_size):
@ -724,7 +735,9 @@ class XEP_0065(object):
self.host = host
# session data
self.hash_clients_map = {} # key: hash of the transfer session, value: session data
self.hash_clients_map = (
{}
) # key: hash of the transfer session, value: session data
self._cache_proxies = {} # key: server jid, value: proxy data
# misc data
@ -879,7 +892,9 @@ class XEP_0065(object):
@return (D(list[Candidate])): list of candidates, ordered by priority
"""
server_factory = self.get_socks_5_server_factory()
local_port, ext_port, local_ips, external_ip = await self._get_network_data(client)
local_port, ext_port, local_ips, external_ip = await self._get_network_data(
client
)
try:
proxy = await self.get_proxy(client, local_jid)
except exceptions.NotFound:
@ -1020,7 +1035,9 @@ class XEP_0065(object):
return defers_list
def get_best_candidate(self, client, candidates, session_hash, peer_session_hash=None):
def get_best_candidate(
self, client, candidates, session_hash, peer_session_hash=None
):
"""Get best candidate (according to priority) which can connect
@param candidates(iterable[Candidate]): candidates to test
@ -1137,7 +1154,8 @@ class XEP_0065(object):
@return (D): Deferred fired when session is finished
"""
session_data = self._create_session(
client, stream_object, local_jid, to_jid, sid, True)
client, stream_object, local_jid, to_jid, sid, True
)
session_data[client] = client
@ -1159,9 +1177,13 @@ class XEP_0065(object):
d = iq_elt.send()
args = [client, session_data, local_jid]
d.addCallbacks(self._iq_negotiation_cb, self._iq_negotiation_eb, args, None, args)
d.addCallbacks(
self._iq_negotiation_cb, self._iq_negotiation_eb, args, None, args
)
defer.ensureDeferred(self.get_candidates(client, local_jid)).addCallback(got_candidates)
defer.ensureDeferred(self.get_candidates(client, local_jid)).addCallback(
got_candidates
)
return session_data[DEFER_KEY]
def _iq_negotiation_cb(self, iq_elt, client, session_data, local_jid):
@ -1181,9 +1203,9 @@ class XEP_0065(object):
streamhost_jid = jid.JID(streamhost_used_elt["jid"])
try:
candidate = next((
c for c in session_data["candidates"] if c.jid == streamhost_jid
))
candidate = next(
(c for c in session_data["candidates"] if c.jid == streamhost_jid)
)
except StopIteration:
log.warning(
"Candidate [{jid}] is unknown !".format(jid=streamhost_jid.full())
@ -1220,8 +1242,9 @@ class XEP_0065(object):
"""
return self._create_session(*args, **kwargs)[DEFER_KEY]
def _create_session(self, client, stream_object, local_jid, to_jid, sid,
requester=False):
def _create_session(
self, client, stream_object, local_jid, to_jid, sid, requester=False
):
"""Called when a bytestream is imminent
@param stream_object(iface.IStreamProducer): File object where data will be