Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0065.py @ 3401:5c97ad3d1653
plugin XEP-0065: some cleaning:
- reordered imports and removed useless try/except
- log warning before sending error
- avoid crash if connection is lost before session has has been received in server mode.
- fixed missing string formatting keywords
- renamed incorrect attribute and show explicitly that it's not used
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 12 Nov 2020 14:53:15 +0100 |
parents | 559a625a236b |
children | be6d91572633 |
comparison
equal
deleted
inserted
replaced
3400:77ad87763f32 | 3401:5c97ad3d1653 |
---|---|
52 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 52 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
53 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 53 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
54 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 54 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
55 # THE SOFTWARE. | 55 # THE SOFTWARE. |
56 | 56 |
57 from sat.core.i18n import _ | 57 import struct |
58 from sat.core.log import getLogger | 58 import hashlib |
59 | 59 import uuid |
60 log = getLogger(__name__) | 60 from collections import namedtuple |
61 from sat.core.constants import Const as C | 61 from zope.interface import implementer |
62 from sat.core import exceptions | |
63 from sat.tools import sat_defer | |
64 from twisted.internet import protocol | 62 from twisted.internet import protocol |
65 from twisted.internet import reactor | 63 from twisted.internet import reactor |
66 from twisted.internet import error as internet_error | 64 from twisted.internet import error as internet_error |
67 from twisted.words.protocols.jabber import error as jabber_error | 65 from twisted.words.protocols.jabber import error as jabber_error |
68 from twisted.words.protocols.jabber import jid | 66 from twisted.words.protocols.jabber import jid |
69 from twisted.words.protocols.jabber import xmlstream | 67 from twisted.words.protocols.jabber import xmlstream |
70 from twisted.internet import defer | 68 from twisted.internet import defer |
71 from collections import namedtuple | |
72 import struct | |
73 import hashlib | |
74 import uuid | |
75 | |
76 from zope.interface import implementer | |
77 | |
78 try: | |
79 from twisted.words.protocols.xmlstream import XMPPHandler | |
80 except ImportError: | |
81 from wokkel.subprotocols import XMPPHandler | |
82 | |
83 from wokkel import disco, iwokkel | 69 from wokkel import disco, iwokkel |
70 from sat.core.i18n import _ | |
71 from sat.core.log import getLogger | |
72 from sat.core.constants import Const as C | |
73 from sat.core import exceptions | |
74 from sat.tools import sat_defer | |
75 | |
76 | |
77 log = getLogger(__name__) | |
84 | 78 |
85 | 79 |
86 PLUGIN_INFO = { | 80 PLUGIN_INFO = { |
87 C.PI_NAME: "XEP 0065 Plugin", | 81 C.PI_NAME: "XEP 0065 Plugin", |
88 C.PI_IMPORT_NAME: "XEP-0065", | 82 C.PI_IMPORT_NAME: "XEP-0065", |
506 self._startNegotiation() | 500 self._startNegotiation() |
507 | 501 |
508 def connectRequested(self, addr, port): | 502 def connectRequested(self, addr, port): |
509 # Check that this session is expected | 503 # Check that this session is expected |
510 if not self.factory.addToSession(addr.decode('utf-8'), self): | 504 if not self.factory.addToSession(addr.decode('utf-8'), self): |
511 self.sendErrorReply(REPLY_CONN_REFUSED) | |
512 log.warning( | 505 log.warning( |
513 "Unexpected connection request received from {host}".format( | 506 "Unexpected connection request received from {host}".format( |
514 host=self.transport.getPeer().host | 507 host=self.transport.getPeer().host |
515 ) | 508 ) |
516 ) | 509 ) |
510 self.sendErrorReply(REPLY_CONN_REFUSED) | |
517 return | 511 return |
518 self._session_hash = addr.decode('utf-8') | 512 self._session_hash = addr.decode('utf-8') |
519 self.connectCompleted(addr, 0) | 513 self.connectCompleted(addr, 0) |
520 | 514 |
521 def startTransfer(self, chunk_size): | 515 def startTransfer(self, chunk_size): |
590 def connectionLost(self, reason): | 584 def connectionLost(self, reason): |
591 log.debug("Socks5 connection lost: {}".format(reason.value)) | 585 log.debug("Socks5 connection lost: {}".format(reason.value)) |
592 if self.state != STATE_READY: | 586 if self.state != STATE_READY: |
593 self.connection.errback(reason) | 587 self.connection.errback(reason) |
594 if self.server_mode: | 588 if self.server_mode: |
595 self.factory.removeFromSession(self._session_hash, self, reason) | 589 try: |
590 session_hash = self._session_hash | |
591 except AttributeError: | |
592 log.debug("no session has been received yet") | |
593 else: | |
594 self.factory.removeFromSession(session_hash, self, reason) | |
596 | 595 |
597 | 596 |
598 class Socks5ServerFactory(protocol.ServerFactory): | 597 class Socks5ServerFactory(protocol.ServerFactory): |
599 protocol = SOCKSv5 | 598 protocol = SOCKSv5 |
600 | 599 |
820 try: | 819 try: |
821 result_elt = yield iq_elt.send() | 820 result_elt = yield iq_elt.send() |
822 except jabber_error.StanzaError as failure: | 821 except jabber_error.StanzaError as failure: |
823 log.warning( | 822 log.warning( |
824 "Error while requesting proxy info on {jid}: {error}".format( | 823 "Error while requesting proxy info on {jid}: {error}".format( |
825 proxy.full(), failure | 824 jid=proxy.full(), error=failure |
826 ) | 825 ) |
827 ) | 826 ) |
828 notFound(server) | 827 notFound(server) |
829 | 828 |
830 try: | 829 try: |
1055 candidate=candidate, error=failure.value | 1054 candidate=candidate, error=failure.value |
1056 ) | 1055 ) |
1057 ) | 1056 ) |
1058 candidates[candidates.index(candidate)] = None | 1057 candidates[candidates.index(candidate)] = None |
1059 | 1058 |
1060 def allTested(self): | 1059 def allTested(__): |
1061 log.debug("All candidates have been tested") | 1060 log.debug("All candidates have been tested") |
1062 good_candidates = [c for c in candidates if c] | 1061 good_candidates = [c for c in candidates if c] |
1063 return good_candidates[0] if good_candidates else None | 1062 return good_candidates[0] if good_candidates else None |
1064 | 1063 |
1065 defer_candidates = self.tryCandidates( | 1064 defer_candidates = self.tryCandidates( |
1377 streamhost_used_elt["jid"] = candidate.jid.full() | 1376 streamhost_used_elt["jid"] = candidate.jid.full() |
1378 client.send(result_elt) | 1377 client.send(result_elt) |
1379 | 1378 |
1380 | 1379 |
1381 @implementer(iwokkel.IDisco) | 1380 @implementer(iwokkel.IDisco) |
1382 class XEP_0065_handler(XMPPHandler): | 1381 class XEP_0065_handler(xmlstream.XMPPHandler): |
1383 | 1382 |
1384 def __init__(self, plugin_parent): | 1383 def __init__(self, plugin_parent): |
1385 self.plugin_parent = plugin_parent | 1384 self.plugin_parent = plugin_parent |
1386 self.host = plugin_parent.host | 1385 self.host = plugin_parent.host |
1387 | 1386 |