diff libervia/backend/plugins/plugin_xep_0065.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents b86912d3fd33
children
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0065.py	Tue Jun 18 12:06:45 2024 +0200
+++ b/libervia/backend/plugins/plugin_xep_0065.py	Wed Jun 19 18:44:57 2024 +0200
@@ -96,8 +96,8 @@
 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 @@
 
 
 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 @@
             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 @@
 
 
 class SOCKSv5(protocol.Protocol):
-    CHUNK_SIZE = 2 ** 16
+    CHUNK_SIZE = 2**16
 
     def __init__(self, session_hash=None):
         """
@@ -377,9 +386,11 @@
         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 @@
             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 @@
 
     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 @@
             )
             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 @@
         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 @@
         @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 @@
 
         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 @@
         @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 @@
 
             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 @@
 
         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 @@
         """
         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