Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0065.py @ 3136:9d0df638c8b4
dates update
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 29 Jan 2020 09:50:54 +0100 |
parents | b64dd7c1496d |
children | 559a625a236b |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
2 # -*- coding: utf-8 -*- |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
3 |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
4 # SAT plugin for managing xep-0065 |
0 | 5 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
6 # Copyright (C) |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
7 # 2002, 2003, 2004 Dave Smith (dizzyd@jabber.org) |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
8 # 2007, 2008 Fabio Forno (xmpp:ff@jabber.bluendo.com) |
3136 | 9 # 2009-2020 Jérôme Poisson (goffi@goffi.org) |
0 | 10 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
11 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
12 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
13 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
14 # (at your option) any later version. |
0 | 15 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
16 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
19 # GNU Affero General Public License for more details. |
0 | 20 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
21 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
22 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 | 23 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
24 # -- |
0 | 25 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
26 # This module is based on proxy65 (http://code.google.com/p/proxy65), |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
27 # originaly written by David Smith and modified by Fabio Forno. |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
28 # It is sublicensed under AGPL v3 (or any later version) as allowed by the original |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
29 # license. |
0 | 30 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
31 # -- |
0 | 32 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
33 # Here is a copy of the original license: |
0 | 34 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
35 # Copyright (C) |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
36 # 2002-2004 Dave Smith (dizzyd@jabber.org) |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
37 # 2007-2008 Fabio Forno (xmpp:ff@jabber.bluendo.com) |
0 | 38 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
39 # Permission is hereby granted, free of charge, to any person obtaining a copy |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
40 # of this software and associated documentation files (the "Software"), to deal |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
41 # in the Software without restriction, including without limitation the rights |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
42 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
43 # copies of the Software, and to permit persons to whom the Software is |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
44 # furnished to do so, subject to the following conditions: |
0 | 45 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
46 # The above copyright notice and this permission notice shall be included in |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
47 # all copies or substantial portions of the Software. |
0 | 48 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
49 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
50 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
51 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
52 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
53 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
54 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
55 # THE SOFTWARE. |
0 | 56 |
771 | 57 from sat.core.i18n import _ |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
956
diff
changeset
|
58 from sat.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
956
diff
changeset
|
60 log = getLogger(__name__) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
61 from sat.core.constants import Const as C |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
62 from sat.core import exceptions |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
63 from sat.tools import sat_defer |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
64 from twisted.internet import protocol |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
65 from twisted.internet import reactor |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
66 from twisted.internet import error as internet_error |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
67 from twisted.words.protocols.jabber import error as jabber_error |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
68 from twisted.words.protocols.jabber import jid |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
69 from twisted.words.protocols.jabber import xmlstream |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
70 from twisted.internet import defer |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
71 from collections import namedtuple |
0 | 72 import struct |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
536
diff
changeset
|
73 import hashlib |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
74 import uuid |
0 | 75 |
3028 | 76 from zope.interface import implementer |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
77 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
78 try: |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
79 from twisted.words.protocols.xmlstream import XMPPHandler |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
80 except ImportError: |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
81 from wokkel.subprotocols import XMPPHandler |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
82 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
83 from wokkel import disco, iwokkel |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
84 |
0 | 85 |
86 PLUGIN_INFO = { | |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
87 C.PI_NAME: "XEP 0065 Plugin", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
88 C.PI_IMPORT_NAME: "XEP-0065", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
89 C.PI_TYPE: "XEP", |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
90 C.PI_MODES: C.PLUG_MODE_BOTH, |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
91 C.PI_PROTOCOLS: ["XEP-0065"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
92 C.PI_DEPENDENCIES: ["IP"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
93 C.PI_RECOMMENDATIONS: ["NAT-PORT"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
94 C.PI_MAIN: "XEP_0065", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
95 C.PI_HANDLER: "yes", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 C.PI_DESCRIPTION: _("""Implementation of SOCKS5 Bytestreams"""), |
0 | 97 } |
98 | |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
99 IQ_SET = '/iq[@type="set"]' |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 NS_BS = "http://jabber.org/protocol/bytestreams" |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
101 BS_REQUEST = IQ_SET + '/query[@xmlns="' + NS_BS + '"]' |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 TIMER_KEY = "timer" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 DEFER_KEY = "finished" # key of the deferred used to track session end |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 SERVER_STARTING_PORT = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
105 0 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
106 ) # starting number for server port search (0 to ask automatic attribution) |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
107 |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
108 # priorities are candidates local priorities, must be a int between 0 and 65535 |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
109 PRIORITY_BEST_DIRECT = 10000 |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
110 PRIORITY_DIRECT = 5000 |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
111 PRIORITY_ASSISTED = 1000 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
112 PRIORITY_PROXY = 0.2 # proxy is the last option for s5b |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
113 CANDIDATE_DELAY = 0.2 # see XEP-0260 §4 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
114 CANDIDATE_DELAY_PROXY = 0.2 # additional time for proxy types (see XEP-0260 §4 note 3) |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
115 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
116 TIMEOUT = 300 # maxium time between session creation and stream start |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
117 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
118 # XXX: by default eveything is automatic |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
119 # TODO: use these params to force use of specific proxy/port/IP |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
120 # PARAMS = """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
121 # <params> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
122 # <general> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
123 # <category name="File Transfer"> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
124 # <param name="Force IP" type="string" /> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
125 # <param name="Force Port" type="int" constraint="1;65535" /> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
126 # </category> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
127 # </general> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
128 # <individual> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
129 # <category name="File Transfer"> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
130 # <param name="Force Proxy" value="" type="string" /> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
131 # <param name="Force Proxy host" value="" type="string" /> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
132 # <param name="Force Proxy port" value="" type="int" constraint="1;65535" /> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
133 # </category> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
134 # </individual> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
135 # </params> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
136 # """ |
0 | 137 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
138 ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
139 STATE_INITIAL, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
140 STATE_AUTH, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 STATE_REQUEST, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
142 STATE_READY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
143 STATE_AUTH_USERPASS, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
144 STATE_CLIENT_INITIAL, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
145 STATE_CLIENT_AUTH, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 STATE_CLIENT_REQUEST, |
3028 | 147 ) = range(8) |
0 | 148 |
149 SOCKS5_VER = 0x05 | |
150 | |
151 ADDR_IPV4 = 0x01 | |
152 ADDR_DOMAINNAME = 0x03 | |
153 ADDR_IPV6 = 0x04 | |
154 | |
155 CMD_CONNECT = 0x01 | |
156 CMD_BIND = 0x02 | |
157 CMD_UDPASSOC = 0x03 | |
158 | |
159 AUTHMECH_ANON = 0x00 | |
160 AUTHMECH_USERPASS = 0x02 | |
161 AUTHMECH_INVALID = 0xFF | |
162 | |
163 REPLY_SUCCESS = 0x00 | |
164 REPLY_GENERAL_FAILUR = 0x01 | |
165 REPLY_CONN_NOT_ALLOWED = 0x02 | |
166 REPLY_NETWORK_UNREACHABLE = 0x03 | |
167 REPLY_HOST_UNREACHABLE = 0x04 | |
168 REPLY_CONN_REFUSED = 0x05 | |
169 REPLY_TTL_EXPIRED = 0x06 | |
170 REPLY_CMD_NOT_SUPPORTED = 0x07 | |
171 REPLY_ADDR_NOT_SUPPORTED = 0x08 | |
172 | |
173 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
174 ProxyInfos = namedtuple("ProxyInfos", ["host", "jid", "port"]) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
175 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
176 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
177 class Candidate(object): |
3040 | 178 def __init__(self, host, port, type_, priority, jid_, id_=None, priority_local=False, |
179 factory=None,): | |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
180 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
181 @param host(unicode): host IP or domain |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
182 @param port(int): port |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
183 @param type_(unicode): stream type (one of XEP_0065.TYPE_*) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
184 @param priority(int): priority |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
185 @param jid_(jid.JID): jid |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
186 @param id_(None, id_): Candidate ID, or None to generate |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
187 @param priority_local(bool): if True, priority is used as local priority, |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
188 else priority is used as global one (and local priority is set to 0) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
189 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
190 assert isinstance(jid_, jid.JID) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
191 self.host, self.port, self.type, self.jid = (host, int(port), type_, jid_) |
3028 | 192 self.id = id_ if id_ is not None else str(uuid.uuid4()) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
193 if priority_local: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
194 self._local_priority = int(priority) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
195 self._priority = self.calculatePriority() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
196 else: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
197 self._local_priority = 0 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
198 self._priority = int(priority) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
199 self.factory = factory |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
200 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
201 def discard(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
202 """Disconnect a candidate if it is connected |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
203 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
204 Used to disconnect tryed client when they are discarded |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
205 """ |
3028 | 206 log.debug("Discarding {}".format(self)) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
207 try: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
208 self.factory.discard() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
209 except AttributeError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
210 pass # no discard for Socks5ServerFactory |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
211 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
212 @property |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
213 def local_priority(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
214 return self._local_priority |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
215 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
216 @property |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
217 def priority(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
218 return self._priority |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
219 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
220 def __str__(self): |
3028 | 221 return "Candidate ({0.priority}): host={0.host} port={0.port} jid={0.jid} type={0.type}{id}".format( |
222 self, id=" id={}".format(self.id if self.id is not None else "") | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
223 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
224 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
225 def __eq__(self, other): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
226 # self.id is is not used in __eq__ as the same candidate can have |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
227 # different ids if proposed by initiator or responder |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
228 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
229 return ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
230 self.host == other.host |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
231 and self.port == other.port |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
232 and self.jid == other.jid |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
233 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
234 except (AttributeError, TypeError): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
235 return False |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
236 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
237 def __ne__(self, other): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
238 return not self.__eq__(other) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
239 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
240 def calculatePriority(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
241 """Calculate candidate priority according to XEP-0260 §2.2 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
242 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
243 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
244 @return (int): priority |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
245 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
246 if self.type == XEP_0065.TYPE_DIRECT: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
247 multiplier = 126 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
248 elif self.type == XEP_0065.TYPE_ASSISTED: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
249 multiplier = 120 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
250 elif self.type == XEP_0065.TYPE_TUNEL: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
251 multiplier = 110 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
252 elif self.type == XEP_0065.TYPE_PROXY: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
253 multiplier = 10 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
254 else: |
3028 | 255 raise exceptions.InternalError("Unknown {} type !".format(self.type)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
256 return 2 ** 16 * multiplier + self._local_priority |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
257 |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
258 def activate(self, client, sid, peer_jid, local_jid): |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
259 """Activate the proxy candidate |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
260 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
261 Send activation request as explained in XEP-0065 § 6.3.5 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
262 Must only be used with proxy candidates |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
263 @param sid(unicode): session id (same as for getSessionHash) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
264 @param peer_jid(jid.JID): jid of the other peer |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
265 @return (D(domish.Element)): IQ result (or error) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
266 """ |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
267 assert self.type == XEP_0065.TYPE_PROXY |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
268 iq_elt = client.IQ() |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
269 iq_elt["from"] = local_jid.full() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
270 iq_elt["to"] = self.jid.full() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
271 query_elt = iq_elt.addElement((NS_BS, "query")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
272 query_elt["sid"] = sid |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
273 query_elt.addElement("activate", content=peer_jid.full()) |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
274 return iq_elt.send() |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
275 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
276 def startTransfer(self, session_hash=None): |
1581
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
277 if self.type == XEP_0065.TYPE_PROXY: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
278 chunk_size = 4096 # Prosody's proxy reject bigger chunks by default |
1581
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
279 else: |
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
280 chunk_size = None |
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
281 self.factory.startTransfer(session_hash, chunk_size=chunk_size) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
282 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
283 |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
284 def getSessionHash(requester_jid, target_jid, sid): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
285 """Calculate SHA1 Hash according to XEP-0065 §5.3.2 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
286 |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
287 @param requester_jid(jid.JID): jid of the requester (the one which activate the proxy) |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
288 @param target_jid(jid.JID): jid of the target |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
289 @param sid(unicode): session id |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
290 @return (str): hash |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
291 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
292 return hashlib.sha1( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
293 (sid + requester_jid.full() + target_jid.full()).encode("utf-8") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
294 ).hexdigest() |
0 | 295 |
296 | |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
297 class SOCKSv5(protocol.Protocol): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
298 CHUNK_SIZE = 2 ** 16 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
299 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
300 def __init__(self, session_hash=None): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
301 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
302 @param session_hash(str): hash of the session |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
303 must only be used in client mode |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
304 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
305 self.connection = defer.Deferred() # called when connection/auth is done |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
306 if session_hash is not None: |
3028 | 307 assert isinstance(session_hash, str) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
308 self.server_mode = False |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
309 self._session_hash = session_hash |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
310 self.state = STATE_CLIENT_INITIAL |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
311 else: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
312 self.server_mode = True |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
313 self.state = STATE_INITIAL |
3028 | 314 self.buf = b"" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
315 self.supportedAuthMechs = [AUTHMECH_ANON] |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
316 self.supportedAddrs = [ADDR_DOMAINNAME] |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
317 self.enabledCommands = [CMD_CONNECT] |
0 | 318 self.peersock = None |
319 self.addressType = 0 | |
320 self.requestType = 0 | |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
321 self._stream_object = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
322 self.active = False # set to True when protocol is actually used for transfer |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
323 # used by factories to know when the finished Deferred can be triggered |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
324 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
325 @property |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
326 def stream_object(self): |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
327 if self._stream_object is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
328 self._stream_object = self.getSession()["stream_object"] |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
329 if self.server_mode: |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
330 self._stream_object.registerProducer(self.transport, True) |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
331 return self._stream_object |
0 | 332 |
1845
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
333 def getSession(self): |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
334 """Return session associated with this candidate |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
335 |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
336 @return (dict): session data |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
337 """ |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
338 if self.server_mode: |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
339 return self.factory.getSession(self._session_hash) |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
340 else: |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
341 return self.factory.getSession() |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
342 |
0 | 343 def _startNegotiation(self): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
344 log.debug("starting negotiation (client mode)") |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
345 self.state = STATE_CLIENT_AUTH |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
346 self.transport.write(struct.pack("!3B", SOCKS5_VER, 1, AUTHMECH_ANON)) |
0 | 347 |
348 def _parseNegotiation(self): | |
349 try: | |
350 # Parse out data | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
351 ver, nmethod = struct.unpack("!BB", self.buf[:2]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
352 methods = struct.unpack("%dB" % nmethod, self.buf[2 : nmethod + 2]) |
0 | 353 |
354 # Ensure version is correct | |
355 if ver != 5: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
356 self.transport.write(struct.pack("!BB", SOCKS5_VER, AUTHMECH_INVALID)) |
0 | 357 self.transport.loseConnection() |
358 return | |
359 | |
360 # Trim off front of the buffer | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
361 self.buf = self.buf[nmethod + 2 :] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
362 |
0 | 363 # Check for supported auth mechs |
364 for m in self.supportedAuthMechs: | |
365 if m in methods: | |
366 # Update internal state, according to selected method | |
367 if m == AUTHMECH_ANON: | |
368 self.state = STATE_REQUEST | |
369 elif m == AUTHMECH_USERPASS: | |
370 self.state = STATE_AUTH_USERPASS | |
371 # Complete negotiation w/ this method | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
372 self.transport.write(struct.pack("!BB", SOCKS5_VER, m)) |
0 | 373 return |
374 | |
375 # No supported mechs found, notify client and close the connection | |
3028 | 376 log.warning("Unsupported authentication mechanism") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
377 self.transport.write(struct.pack("!BB", SOCKS5_VER, AUTHMECH_INVALID)) |
0 | 378 self.transport.loseConnection() |
379 except struct.error: | |
380 pass | |
381 | |
382 def _parseUserPass(self): | |
383 try: | |
384 # Parse out data | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
385 ver, ulen = struct.unpack("BB", self.buf[:2]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
386 uname, = struct.unpack("%ds" % ulen, self.buf[2 : ulen + 2]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
387 plen, = struct.unpack("B", self.buf[ulen + 2]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
388 password, = struct.unpack("%ds" % plen, self.buf[ulen + 3 : ulen + 3 + plen]) |
0 | 389 # Trim off fron of the buffer |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
390 self.buf = self.buf[3 + ulen + plen :] |
0 | 391 # Fire event to authenticate user |
392 if self.authenticateUserPass(uname, password): | |
393 # Signal success | |
394 self.state = STATE_REQUEST | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
395 self.transport.write(struct.pack("!BB", SOCKS5_VER, 0x00)) |
0 | 396 else: |
397 # Signal failure | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
398 self.transport.write(struct.pack("!BB", SOCKS5_VER, 0x01)) |
0 | 399 self.transport.loseConnection() |
400 except struct.error: | |
401 pass | |
402 | |
403 def sendErrorReply(self, errorcode): | |
404 # Any other address types are not supported | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
405 result = struct.pack("!BBBBIH", SOCKS5_VER, errorcode, 0, 1, 0, 0) |
0 | 406 self.transport.write(result) |
407 self.transport.loseConnection() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
408 |
0 | 409 def _parseRequest(self): |
410 try: | |
411 # Parse out data and trim buffer accordingly | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
412 ver, cmd, rsvd, self.addressType = struct.unpack("!BBBB", self.buf[:4]) |
0 | 413 |
414 # Ensure we actually support the requested address type | |
415 if self.addressType not in self.supportedAddrs: | |
416 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED) | |
417 return | |
418 | |
419 # Deal with addresses | |
420 if self.addressType == ADDR_IPV4: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
421 addr, port = struct.unpack("!IH", self.buf[4:10]) |
0 | 422 self.buf = self.buf[10:] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
423 elif self.addressType == ADDR_DOMAINNAME: |
3028 | 424 nlen = self.buf[4] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
425 addr, port = struct.unpack("!%dsH" % nlen, self.buf[5:]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
426 self.buf = self.buf[7 + len(addr) :] |
0 | 427 else: |
428 # Any other address types are not supported | |
429 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED) | |
430 return | |
431 | |
432 # Ensure command is supported | |
433 if cmd not in self.enabledCommands: | |
434 # Send a not supported error | |
435 self.sendErrorReply(REPLY_CMD_NOT_SUPPORTED) | |
436 return | |
437 | |
438 # Process the command | |
439 if cmd == CMD_CONNECT: | |
440 self.connectRequested(addr, port) | |
441 elif cmd == CMD_BIND: | |
442 self.bindRequested(addr, port) | |
443 else: | |
444 # Any other command is not supported | |
445 self.sendErrorReply(REPLY_CMD_NOT_SUPPORTED) | |
446 | |
941
c6d8fc63b1db
core, plugins: host.getClient now raise an exception instead of returning None when no profile is found, plugins have been adapted consequently and a bit cleaned
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
447 except struct.error: |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
448 # The buffer is probably not complete, we need to wait more |
0 | 449 return None |
450 | |
451 def _makeRequest(self): | |
3028 | 452 hash_ = self._session_hash.encode('utf-8') |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
453 request = struct.pack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
454 "!5B%dsH" % len(hash_), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
455 SOCKS5_VER, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
456 CMD_CONNECT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
457 0, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
458 ADDR_DOMAINNAME, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
459 len(hash_), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
460 hash_, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
461 0, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
462 ) |
0 | 463 self.transport.write(request) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
464 self.state = STATE_CLIENT_REQUEST |
0 | 465 |
466 def _parseRequestReply(self): | |
467 try: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
468 ver, rep, rsvd, self.addressType = struct.unpack("!BBBB", self.buf[:4]) |
0 | 469 # Ensure we actually support the requested address type |
470 if self.addressType not in self.supportedAddrs: | |
471 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED) | |
472 return | |
473 | |
474 # Deal with addresses | |
475 if self.addressType == ADDR_IPV4: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
476 addr, port = struct.unpack("!IH", self.buf[4:10]) |
0 | 477 self.buf = self.buf[10:] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
478 elif self.addressType == ADDR_DOMAINNAME: |
3028 | 479 nlen = self.buf[4] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
480 addr, port = struct.unpack("!%dsH" % nlen, self.buf[5:]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
481 self.buf = self.buf[7 + len(addr) :] |
0 | 482 else: |
483 # Any other address types are not supported | |
484 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED) | |
485 return | |
486 | |
487 # Ensure reply is OK | |
488 if rep != REPLY_SUCCESS: | |
489 self.loseConnection() | |
490 return | |
491 | |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
492 self.state = STATE_READY |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
493 self.connection.callback(None) |
0 | 494 |
941
c6d8fc63b1db
core, plugins: host.getClient now raise an exception instead of returning None when no profile is found, plugins have been adapted consequently and a bit cleaned
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
495 except struct.error: |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
496 # The buffer is probably not complete, we need to wait more |
0 | 497 return None |
498 | |
499 def connectionMade(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
500 log.debug( |
3028 | 501 "Socks5 connectionMade (mode = {})".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
502 "server" if self.state == STATE_INITIAL else "client" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
503 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
504 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
505 if self.state == STATE_CLIENT_INITIAL: |
0 | 506 self._startNegotiation() |
507 | |
508 def connectRequested(self, addr, port): | |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
536
diff
changeset
|
509 # Check that this session is expected |
3028 | 510 if not self.factory.addToSession(addr.decode('utf-8'), self): |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
536
diff
changeset
|
511 self.sendErrorReply(REPLY_CONN_REFUSED) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
512 log.warning( |
3028 | 513 "Unexpected connection request received from {host}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
514 host=self.transport.getPeer().host |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
515 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
516 ) |
0 | 517 return |
3028 | 518 self._session_hash = addr.decode('utf-8') |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
519 self.connectCompleted(addr, 0) |
0 | 520 |
1581
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
521 def startTransfer(self, chunk_size): |
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
522 """Callback called when the result iq is received |
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
523 |
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
524 @param chunk_size(None, int): size of the buffer, or None for default |
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
525 """ |
1759
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
526 self.active = True |
1581
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
527 if chunk_size is not None: |
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
528 self.CHUNK_SIZE = chunk_size |
3028 | 529 log.debug("Starting file transfer") |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
530 d = self.stream_object.startStream(self.transport) |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
531 d.addCallback(self.streamFinished) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
532 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
533 def streamFinished(self, d): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
956
diff
changeset
|
534 log.info(_("File transfer completed, closing connection")) |
0 | 535 self.transport.loseConnection() |
536 | |
537 def connectCompleted(self, remotehost, remoteport): | |
538 if self.addressType == ADDR_IPV4: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
539 result = struct.pack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
540 "!BBBBIH", SOCKS5_VER, REPLY_SUCCESS, 0, 1, remotehost, remoteport |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
541 ) |
0 | 542 elif self.addressType == ADDR_DOMAINNAME: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
543 result = struct.pack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
544 "!BBBBB%dsH" % len(remotehost), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
545 SOCKS5_VER, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
546 REPLY_SUCCESS, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
547 0, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
548 ADDR_DOMAINNAME, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
549 len(remotehost), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
550 remotehost, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
551 remoteport, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
552 ) |
0 | 553 self.transport.write(result) |
554 self.state = STATE_READY | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
555 |
0 | 556 def bindRequested(self, addr, port): |
557 pass | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
558 |
0 | 559 def authenticateUserPass(self, user, passwd): |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
560 # FIXME: implement authentication and remove the debug printing a password |
3028 | 561 log.debug("User/pass: %s/%s" % (user, passwd)) |
0 | 562 return True |
563 | |
564 def dataReceived(self, buf): | |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
565 if self.state == STATE_READY: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
566 # Everything is set, we just have to write the incoming data |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
567 self.stream_object.write(buf) |
1759
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
568 if not self.active: |
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
569 self.active = True |
1845
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
570 self.getSession()[TIMER_KEY].cancel() |
0 | 571 return |
572 | |
573 self.buf = self.buf + buf | |
574 if self.state == STATE_INITIAL: | |
575 self._parseNegotiation() | |
576 if self.state == STATE_AUTH_USERPASS: | |
577 self._parseUserPass() | |
578 if self.state == STATE_REQUEST: | |
579 self._parseRequest() | |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
580 if self.state == STATE_CLIENT_REQUEST: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
581 self._parseRequestReply() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
582 if self.state == STATE_CLIENT_AUTH: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
583 ver, method = struct.unpack("!BB", buf) |
0 | 584 self.buf = self.buf[2:] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
585 if ver != SOCKS5_VER or method != AUTHMECH_ANON: |
0 | 586 self.transport.loseConnection() |
587 else: | |
588 self._makeRequest() | |
589 | |
590 def connectionLost(self, reason): | |
3028 | 591 log.debug("Socks5 connection lost: {}".format(reason.value)) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
592 if self.state != STATE_READY: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
593 self.connection.errback(reason) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
594 if self.server_mode: |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
595 self.factory.removeFromSession(self._session_hash, self, reason) |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
596 |
0 | 597 |
598 class Socks5ServerFactory(protocol.ServerFactory): | |
599 protocol = SOCKSv5 | |
600 | |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
601 def __init__(self, parent): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
602 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
603 @param parent(XEP_0065): XEP_0065 parent instance |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
604 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
605 self.parent = parent |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
606 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
607 def getSession(self, session_hash): |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
608 return self.parent.getSession(None, session_hash) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
609 |
1581
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
610 def startTransfer(self, session_hash, chunk_size=None): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
611 session = self.getSession(session_hash) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
612 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
613 protocol = session["protocols"][0] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
614 except (KeyError, IndexError): |
3028 | 615 log.error("Can't start file transfer, can't find protocol") |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
616 else: |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
617 session[TIMER_KEY].cancel() |
1581
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
618 protocol.startTransfer(chunk_size) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
619 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
620 def addToSession(self, session_hash, protocol): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
621 """Check is session_hash is valid, and associate protocol with it |
0 | 622 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
623 the session will be associated to the corresponding candidate |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
624 @param session_hash(str): hash of the session |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
625 @param protocol(SOCKSv5): protocol instance |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
626 @param return(bool): True if hash was valid (i.e. expected), False else |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
627 """ |
3028 | 628 assert isinstance(session_hash, str) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
629 try: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
630 session_data = self.getSession(session_hash) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
631 except KeyError: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
632 return False |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
633 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
634 session_data.setdefault("protocols", []).append(protocol) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
635 return True |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
636 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
637 def removeFromSession(self, session_hash, protocol, reason): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
638 """Remove a protocol from session_data |
0 | 639 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
640 There can be several protocol instances while candidates are tried, they |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
641 have removed when candidate connection is closed |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
642 @param session_hash(str): hash of the session |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
643 @param protocol(SOCKSv5): protocol instance |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
644 @param reason(failure.Failure): reason of the removal |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
645 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
646 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
647 protocols = self.getSession(session_hash)["protocols"] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
648 protocols.remove(protocol) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
649 except (KeyError, ValueError): |
3028 | 650 log.error("Protocol not found in session while it should be there") |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
651 else: |
1759
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
652 if protocol.active: |
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
653 # The active protocol has been removed, session is finished |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
654 if reason.check(internet_error.ConnectionDone): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
655 self.getSession(session_hash)[DEFER_KEY].callback(None) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
656 else: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
657 self.getSession(session_hash)[DEFER_KEY].errback(reason) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
658 |
0 | 659 |
660 class Socks5ClientFactory(protocol.ClientFactory): | |
661 protocol = SOCKSv5 | |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
662 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
663 def __init__(self, client, parent, session, session_hash): |
398 | 664 """Init the Client Factory |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
665 |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
666 @param session(dict): session data |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
667 @param session_hash(unicode): hash used for peer_connection |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
668 hash is the same as hostname computed in XEP-0065 § 5.3.2 #1 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
669 """ |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
670 self.session = session |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
671 self.session_hash = session_hash |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
672 self.client = client |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
673 self.connection = defer.Deferred() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
674 self._protocol_instance = None |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
675 self.connector = None |
0 | 676 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
677 def discard(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
678 """Disconnect the client |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
679 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
680 Also set a discarded flag, which avoid to call the session Deferred |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
681 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
682 self.connector.disconnect() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
683 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
684 def getSession(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
685 return self.session |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
686 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
687 def startTransfer(self, __=None, chunk_size=None): |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
688 self.session[TIMER_KEY].cancel() |
1581
8cc7d83141a4
plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
689 self._protocol_instance.startTransfer(chunk_size) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
690 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
691 def clientConnectionFailed(self, connector, reason): |
3028 | 692 log.debug("Connection failed") |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
693 self.connection.errback(reason) |
0 | 694 |
695 def clientConnectionLost(self, connector, reason): | |
3028 | 696 log.debug(_("Socks 5 client connection lost (reason: %s)") % reason.value) |
1759
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
697 if self._protocol_instance.active: |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
698 # This one was used for the transfer, than mean that |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
699 # the Socks5 session is finished |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
700 if reason.check(internet_error.ConnectionDone): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
701 self.getSession()[DEFER_KEY].callback(None) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
702 else: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
703 self.getSession()[DEFER_KEY].errback(reason) |
1759
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
704 self._protocol_instance = None |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
705 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
706 def buildProtocol(self, addr): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
707 log.debug(("Socks 5 client connection started")) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
708 p = self.protocol(session_hash=self.session_hash) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
709 p.factory = self |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
710 p.connection.chainDeferred(self.connection) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
711 self._protocol_instance = p |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
712 return p |
0 | 713 |
714 | |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
715 class XEP_0065(object): |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
716 NAMESPACE = NS_BS |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
717 TYPE_DIRECT = "direct" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
718 TYPE_ASSISTED = "assisted" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
719 TYPE_TUNEL = "tunel" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
720 TYPE_PROXY = "proxy" |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
721 Candidate = Candidate |
20
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
722 |
0 | 723 def __init__(self, host): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
956
diff
changeset
|
724 log.info(_("Plugin XEP_0065 initialization")) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
725 self.host = host |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
726 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
727 # session data |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
728 self.hash_clients_map = {} # key: hash of the transfer session, value: session data |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
729 self._cache_proxies = {} # key: server jid, value: proxy data |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
730 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
731 # misc data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
732 self._server_factory = None |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
733 self._external_port = None |
21
633c5ed65701
parameters: new button type (not finished)
Goffi <goffi@goffi.org>
parents:
20
diff
changeset
|
734 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
735 # plugins shortcuts |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
736 self._ip = self.host.plugins["IP"] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
737 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
738 self._np = self.host.plugins["NAT-PORT"] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
739 except KeyError: |
3028 | 740 log.debug("NAT Port plugin not available") |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
741 self._np = None |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
742 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
743 # parameters |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
744 # XXX: params are not used for now, but they may be used in the futur to force proxy/IP |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
745 # host.memory.updateParams(PARAMS) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
746 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2129
diff
changeset
|
747 def getHandler(self, client): |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
748 return XEP_0065_handler(self) |
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
749 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2129
diff
changeset
|
750 def profileConnected(self, client): |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
751 client.xep_0065_sid_session = {} # key: stream_id, value: session_data(dict) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
752 client._s5b_sessions = {} |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
753 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
754 def getSessionHash(self, from_jid, to_jid, sid): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
755 return getSessionHash(from_jid, to_jid, sid) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
756 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
757 def getSocks5ServerFactory(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
758 """Return server factory |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
759 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
760 The server is created if it doesn't exists yet |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
761 self._server_factory_port is set on server creation |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
762 """ |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
536
diff
changeset
|
763 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
764 if self._server_factory is None: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
765 self._server_factory = Socks5ServerFactory(self) |
3028 | 766 for port in range(SERVER_STARTING_PORT, 65356): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
767 try: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
768 listening_port = reactor.listenTCP(port, self._server_factory) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
769 except internet_error.CannotListenError as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
770 log.debug( |
3028 | 771 "Cannot listen on port {port}: {err_msg}{err_num}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
772 port=port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
773 err_msg=e.socketError.strerror, |
3028 | 774 err_num=" (error code: {})".format(e.socketError.errno), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
775 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
776 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
777 else: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
778 self._server_factory_port = listening_port.getHost().port |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
779 break |
0 | 780 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
781 log.info( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
782 _("Socks5 Stream server launched on port {}").format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
783 self._server_factory_port |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
784 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
785 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
786 return self._server_factory |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
787 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
788 @defer.inlineCallbacks |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
789 def getProxy(self, client, local_jid): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
790 """Return the proxy available for this profile |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
791 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
792 cache is used between clients using the same server |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
793 @param local_jid(jid.JID): same as for [getCandidates] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
794 @return ((D)(ProxyInfos, None)): Found proxy infos, |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
795 or None if not acceptable proxy is found |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
796 @raise exceptions.NotFound: no Proxy found |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
797 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
798 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
799 def notFound(server): |
3028 | 800 log.info("No proxy found on this server") |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
801 self._cache_proxies[server] = None |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
802 raise exceptions.NotFound |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
803 |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
804 server = client.host if client.is_component else client.jid.host |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
805 try: |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
806 defer.returnValue(self._cache_proxies[server]) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
807 except KeyError: |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
808 pass |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
809 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
810 proxy = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
811 yield self.host.findServiceEntities(client, "proxy", "bytestreams") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
812 ).pop() |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
813 except (defer.CancelledError, StopIteration, KeyError): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
814 notFound(server) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
815 iq_elt = client.IQ("get") |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
816 iq_elt["from"] = local_jid.full() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
817 iq_elt["to"] = proxy.full() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
818 iq_elt.addElement((NS_BS, "query")) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
819 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
820 try: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
821 result_elt = yield iq_elt.send() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
822 except jabber_error.StanzaError as failure: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
823 log.warning( |
3028 | 824 "Error while requesting proxy info on {jid}: {error}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
825 proxy.full(), failure |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
826 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
827 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
828 notFound(server) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
829 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
830 try: |
3028 | 831 query_elt = next(result_elt.elements(NS_BS, "query")) |
832 streamhost_elt = next(query_elt.elements(NS_BS, "streamhost")) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
833 host = streamhost_elt["host"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
834 jid_ = streamhost_elt["jid"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
835 port = streamhost_elt["port"] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
836 if not all((host, jid, port)): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
837 raise KeyError |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
838 jid_ = jid.JID(jid_) |
1742
244a605623d6
complete the Exception's list when catching JID error:
souliane <souliane@mailoo.org>
parents:
1675
diff
changeset
|
839 except (StopIteration, KeyError, RuntimeError, jid.InvalidFormat, AttributeError): |
3028 | 840 log.warning("Invalid proxy data received from {}".format(proxy.full())) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
841 notFound(server) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
842 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
843 proxy_infos = self._cache_proxies[server] = ProxyInfos(host, jid_, port) |
3028 | 844 log.info("Proxy found: {}".format(proxy_infos)) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
845 defer.returnValue(proxy_infos) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
846 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
847 @defer.inlineCallbacks |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
848 def _getNetworkData(self, client): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
849 """Retrieve information about network |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
850 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
851 @param client: %(doc_client)s |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
852 @return (D(tuple[local_port, external_port, local_ips, external_ip])): network data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
853 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
854 self.getSocks5ServerFactory() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
855 local_port = self._server_factory_port |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
856 external_ip = yield self._ip.getExternalIP(client) |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
857 local_ips = yield self._ip.getLocalIPs(client) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
858 |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
859 if external_ip is not None and self._external_port is None: |
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
860 if external_ip != local_ips[0]: |
3028 | 861 log.info("We are probably behind a NAT") |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
862 if self._np is None: |
3028 | 863 log.warning("NAT port plugin not available, we can't map port") |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
864 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
865 ext_port = yield self._np.mapPort( |
3028 | 866 local_port, desc="SaT socks5 stream" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
867 ) |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
868 if ext_port is None: |
3028 | 869 log.warning("Can't map NAT port") |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
870 else: |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
871 self._external_port = ext_port |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
872 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
873 defer.returnValue((local_port, self._external_port, local_ips, external_ip)) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
874 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
875 @defer.inlineCallbacks |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
876 def getCandidates(self, client, local_jid): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
877 """Return a list of our stream candidates |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
878 |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
879 @param local_jid(jid.JID): jid to use as local jid |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
880 This is needed for client which can be addressed with a different jid than |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
881 client.jid if a local part is used (e.g. piotr@file.example.net where |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
882 client.jid would be file.example.net) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
883 @return (D(list[Candidate])): list of candidates, ordered by priority |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
884 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
885 server_factory = yield self.getSocks5ServerFactory() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
886 local_port, ext_port, local_ips, external_ip = yield self._getNetworkData(client) |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
887 try: |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
888 proxy = yield self.getProxy(client, local_jid) |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
889 except exceptions.NotFound: |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
890 proxy = None |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
891 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
892 # its time to gather the candidates |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
893 candidates = [] |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
894 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
895 # first the direct ones |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
896 |
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
897 # the preferred direct connection |
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
898 ip = local_ips.pop(0) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
899 candidates.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
900 Candidate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
901 ip, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
902 local_port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
903 XEP_0065.TYPE_DIRECT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
904 PRIORITY_BEST_DIRECT, |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
905 local_jid, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
906 priority_local=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
907 factory=server_factory, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
908 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
909 ) |
1576
d5f59ba166fe
plugins IP: getLocalIPs now return localhost IP instead of an empty list if no non-localhost ip can be found
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
910 for ip in local_ips: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
911 candidates.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
912 Candidate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
913 ip, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
914 local_port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
915 XEP_0065.TYPE_DIRECT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
916 PRIORITY_DIRECT, |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
917 local_jid, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
918 priority_local=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
919 factory=server_factory, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
920 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
921 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
922 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
923 # then the assisted one |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
924 if ext_port is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
925 candidates.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
926 Candidate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
927 external_ip, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
928 ext_port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
929 XEP_0065.TYPE_ASSISTED, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
930 PRIORITY_ASSISTED, |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
931 local_jid, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
932 priority_local=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
933 factory=server_factory, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
934 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
935 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
936 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
937 # finally the proxy |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
938 if proxy: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
939 candidates.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
940 Candidate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
941 proxy.host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
942 proxy.port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
943 XEP_0065.TYPE_PROXY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
944 PRIORITY_PROXY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
945 proxy.jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
946 priority_local=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
947 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
948 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
949 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
950 # should be already sorted, but just in case the priorities get weird |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
951 candidates.sort(key=lambda c: c.priority, reverse=True) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
952 defer.returnValue(candidates) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
953 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
954 def _addConnector(self, connector, candidate): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
955 """Add connector used to connect to candidate, and return client factory's connection Deferred |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
956 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
957 the connector can be used to disconnect the candidate, and returning the factory's connection Deferred allow to wait for connection completion |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
958 @param connector: a connector implementing IConnector |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
959 @param candidate(Candidate): candidate linked to the connector |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
960 @return (D): Deferred fired when factory connection is done or has failed |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
961 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
962 candidate.factory.connector = connector |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
963 return candidate.factory.connection |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
964 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
965 def connectCandidate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
966 self, client, candidate, session_hash, peer_session_hash=None, delay=None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
967 ): |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
968 """Connect to a candidate |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
969 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
970 Connection will be done with a Socks5ClientFactory |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
971 @param candidate(Candidate): candidate to connect to |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
972 @param session_hash(unicode): hash of the session |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
973 hash is the same as hostname computed in XEP-0065 § 5.3.2 #1 |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
974 @param peer_session_hash(unicode, None): hash used with the peer |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
975 None to use session_hash. |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
976 None must be used in 2 cases: |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
977 - when XEP-0065 is used with XEP-0096 |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
978 - when a peer connect to a proxy *he proposed himself* |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
979 in practice, peer_session_hash is only used by tryCandidates |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
980 @param delay(None, float): optional delay to wait before connection, in seconds |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
981 @return (D): Deferred launched when TCP connection + Socks5 connection is done |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
982 """ |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
983 if peer_session_hash is None: |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
984 # for XEP-0065, only one hash is needed |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
985 peer_session_hash = session_hash |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
986 session = self.getSession(client, session_hash) |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
987 factory = Socks5ClientFactory(client, self, session, peer_session_hash) |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
988 candidate.factory = factory |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
989 if delay is None: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
990 d = defer.succeed(candidate.host) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
991 else: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
992 d = sat_defer.DelayedDeferred(delay, candidate.host) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
993 d.addCallback(reactor.connectTCP, candidate.port, factory) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
994 d.addCallback(self._addConnector, candidate) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
995 return d |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
996 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
997 def tryCandidates( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
998 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
999 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1000 candidates, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1001 session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1002 peer_session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1003 connection_cb=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1004 connection_eb=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1005 ): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1006 defers_list = [] |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1007 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1008 for candidate in candidates: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1009 delay = CANDIDATE_DELAY * len(defers_list) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1010 if candidate.type == XEP_0065.TYPE_PROXY: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1011 delay += CANDIDATE_DELAY_PROXY |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1012 d = self.connectCandidate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1013 client, candidate, session_hash, peer_session_hash, delay |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1014 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1015 if connection_cb is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1016 d.addCallback( |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1017 lambda __, candidate=candidate, client=client: connection_cb( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1018 client, candidate |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1019 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1020 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1021 if connection_eb is not None: |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1022 d.addErrback(connection_eb, client, candidate) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1023 defers_list.append(d) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1024 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1025 return defers_list |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1026 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1027 def getBestCandidate(self, client, candidates, session_hash, peer_session_hash=None): |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1028 """Get best candidate (according to priority) which can connect |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1029 |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1030 @param candidates(iterable[Candidate]): candidates to test |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1031 @param session_hash(unicode): hash of the session |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
1032 hash is the same as hostname computed in XEP-0065 § 5.3.2 #1 |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
1033 @param peer_session_hash(unicode, None): hash of the other peer |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
1034 only useful for XEP-0260, must be None for XEP-0065 streamhost candidates |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1035 @return (D(None, Candidate)): best candidate or None if none can connect |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1036 """ |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1037 defer_candidates = None |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1038 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1039 def connectionCb(client, candidate): |
3028 | 1040 log.info("Connection of {} successful".format(str(candidate))) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1041 for idx, other_candidate in enumerate(candidates): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1042 try: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1043 if other_candidate.priority < candidate.priority: |
3028 | 1044 log.debug("Cancelling {}".format(other_candidate)) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1045 defer_candidates[idx].cancel() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1046 except AttributeError: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1047 assert other_candidate is None |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1048 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1049 def connectionEb(failure, client, candidate): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1050 if failure.check(defer.CancelledError): |
3028 | 1051 log.debug("Connection of {} has been cancelled".format(candidate)) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1052 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1053 log.info( |
3028 | 1054 "Connection of {candidate} Failed: {error}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1055 candidate=candidate, error=failure.value |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1056 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1057 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1058 candidates[candidates.index(candidate)] = None |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1059 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1060 def allTested(self): |
3028 | 1061 log.debug("All candidates have been tested") |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1062 good_candidates = [c for c in candidates if c] |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1063 return good_candidates[0] if good_candidates else None |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1064 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1065 defer_candidates = self.tryCandidates( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1066 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1067 candidates, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1068 session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1069 peer_session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1070 connectionCb, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1071 connectionEb, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1072 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1073 d_list = defer.DeferredList(defer_candidates) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1074 d_list.addCallback(allTested) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1075 return d_list |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1076 |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1077 def _timeOut(self, session_hash, client): |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1078 """Called when stream was not started quickly enough |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1079 |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1080 @param session_hash(str): hash as returned by getSessionHash |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1081 @param client: %(doc_client)s |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1082 """ |
3028 | 1083 log.info("Socks5 Bytestream: TimeOut reached") |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1084 session = self.getSession(client, session_hash) |
3132
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
1085 session[DEFER_KEY].errback(exceptions.TimeOutError()) |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1086 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
1087 def killSession(self, failure_, session_hash, sid, client): |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1088 """Clean the current session |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1089 |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1090 @param session_hash(str): hash as returned by getSessionHash |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1091 @param sid(None, unicode): session id |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1092 or None if self.xep_0065_sid_session was not used |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1093 @param client: %(doc_client)s |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
1094 @param failure_(None, failure.Failure): None if eveything was fine, a failure else |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
1095 @return (None, failure.Failure): failure_ is returned |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1096 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1097 log.debug( |
3028 | 1098 "Cleaning session with hash {hash}{id}: {reason}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1099 hash=session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1100 reason="" if failure_ is None else failure_.value, |
3028 | 1101 id="" if sid is None else " (id: {})".format(sid), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1102 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1103 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1104 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1105 try: |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1106 assert self.hash_clients_map[session_hash] == client |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1107 del self.hash_clients_map[session_hash] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1108 except KeyError: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1109 pass |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1110 |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1111 if sid is not None: |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1112 try: |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1113 del client.xep_0065_sid_session[sid] |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1114 except KeyError: |
3028 | 1115 log.warning("Session id {} is unknown".format(sid)) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1116 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1117 try: |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1118 session_data = client._s5b_sessions[session_hash] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1119 except KeyError: |
3028 | 1120 log.warning("There is no session with this hash") |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1121 return |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1122 else: |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1123 del client._s5b_sessions[session_hash] |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1124 |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1125 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1126 session_data["timer"].cancel() |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1127 except (internet_error.AlreadyCalled, internet_error.AlreadyCancelled): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1128 pass |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1129 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
1130 return failure_ |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1131 |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1132 def startStream(self, client, stream_object, local_jid, to_jid, sid): |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1133 """Launch the stream workflow |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1134 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1135 @param streamProducer: stream_object to use |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1136 @param local_jid(jid.JID): same as for [getCandidates] |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1137 @param to_jid: JID of the recipient |
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1138 @param sid: Stream session id |
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1139 @param successCb: method to call when stream successfuly finished |
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1140 @param failureCb: method to call when something goes wrong |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1141 @return (D): Deferred fired when session is finished |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1142 """ |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1143 session_data = self._createSession( |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1144 client, stream_object, local_jid, to_jid, sid, True) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1145 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1146 session_data[client] = client |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1147 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1148 def gotCandidates(candidates): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1149 session_data["candidates"] = candidates |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1150 iq_elt = client.IQ() |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1151 iq_elt["from"] = local_jid.full() |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1152 iq_elt["to"] = to_jid.full() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1153 query_elt = iq_elt.addElement((NS_BS, "query")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1154 query_elt["mode"] = "tcp" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1155 query_elt["sid"] = sid |
398 | 1156 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1157 for candidate in candidates: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1158 streamhost = query_elt.addElement("streamhost") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1159 streamhost["host"] = candidate.host |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1160 streamhost["port"] = str(candidate.port) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1161 streamhost["jid"] = candidate.jid.full() |
3028 | 1162 log.debug("Candidate proposed: {}".format(candidate)) |
398 | 1163 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1164 d = iq_elt.send() |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1165 args = [client, session_data, local_jid] |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1166 d.addCallbacks(self._IQNegotiationCb, self._IQNegotiationEb, args, None, args) |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1167 |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1168 self.getCandidates(client, local_jid).addCallback(gotCandidates) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1169 return session_data[DEFER_KEY] |
0 | 1170 |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1171 def _IQNegotiationCb(self, iq_elt, client, session_data, local_jid): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1172 """Called when the result of open iq is received |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1173 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1174 @param session_data(dict): data of the session |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1175 @param client: %(doc_client)s |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1176 @param iq_elt(domish.Element): <iq> result |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1177 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1178 try: |
3028 | 1179 query_elt = next(iq_elt.elements(NS_BS, "query")) |
1180 streamhost_used_elt = next(query_elt.elements(NS_BS, "streamhost-used")) | |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1181 except StopIteration: |
3028 | 1182 log.warning("No streamhost found in stream query") |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1183 # FIXME: must clean session |
398 | 1184 return |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1185 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1186 streamhost_jid = jid.JID(streamhost_used_elt["jid"]) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1187 try: |
3028 | 1188 candidate = next(( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1189 c for c in session_data["candidates"] if c.jid == streamhost_jid |
3028 | 1190 )) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1191 except StopIteration: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1192 log.warning( |
3028 | 1193 "Candidate [{jid}] is unknown !".format(jid=streamhost_jid.full()) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1194 ) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1195 return |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1196 else: |
3028 | 1197 log.info("Candidate choosed by target: {}".format(candidate)) |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1198 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1199 if candidate.type == XEP_0065.TYPE_PROXY: |
3028 | 1200 log.info("A Socks5 proxy is used") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1201 d = self.connectCandidate(client, candidate, session_data["hash"]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1202 d.addCallback( |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1203 lambda __: candidate.activate( |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1204 client, session_data["id"], session_data["peer_jid"], local_jid |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1205 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1206 ) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1207 d.addErrback(self._activationEb) |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1208 else: |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1209 d = defer.succeed(None) |
398 | 1210 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1211 d.addCallback(lambda __: candidate.startTransfer(session_data["hash"])) |
398 | 1212 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1213 def _activationEb(self, failure): |
3028 | 1214 log.warning("Proxy activation error: {}".format(failure.value)) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1215 |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1216 def _IQNegotiationEb(self, stanza_err, client, session_data, local_jid): |
3028 | 1217 log.warning("Socks5 transfer failed: {}".format(stanza_err.value)) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1218 # FIXME: must clean session |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1219 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1220 def createSession(self, *args, **kwargs): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1221 """like [_createSession] but return the session deferred instead of the whole session |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1222 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1223 session deferred is fired when transfer is finished |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1224 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1225 return self._createSession(*args, **kwargs)[DEFER_KEY] |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1226 |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1227 def _createSession(self, client, stream_object, local_jid, to_jid, sid, |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1228 requester=False): |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1229 """Called when a bytestream is imminent |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1230 |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1231 @param stream_object(iface.IStreamProducer): File object where data will be |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1232 written |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1233 @param to_jid(jid.JId): jid of the other peer |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1234 @param sid(unicode): session id |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1235 @param initiator(bool): if True, this session is create by initiator |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1236 @return (dict): session data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1237 """ |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1238 if sid in client.xep_0065_sid_session: |
3028 | 1239 raise exceptions.ConflictError("A session with this id already exists !") |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1240 if requester: |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1241 session_hash = getSessionHash(local_jid, to_jid, sid) |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1242 session_data = self._registerHash(client, session_hash, stream_object) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1243 else: |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1244 session_hash = getSessionHash(to_jid, local_jid, sid) |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1245 session_d = defer.Deferred() |
1629
a34d7f621944
plugin XEP-0065: renamed _killSession to killSession as it make sense to kill a session from an other plugin
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1246 session_d.addBoth(self.killSession, session_hash, sid, client) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1247 session_data = client._s5b_sessions[session_hash] = { |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1248 DEFER_KEY: session_d, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1249 TIMER_KEY: reactor.callLater( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1250 TIMEOUT, self._timeOut, session_hash, client |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1251 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1252 } |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1253 client.xep_0065_sid_session[sid] = session_data |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1254 session_data.update( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1255 { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1256 "id": sid, |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1257 "local_jid": local_jid, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1258 "peer_jid": to_jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1259 "stream_object": stream_object, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1260 "hash": session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1261 } |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1262 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1263 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1264 return session_data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1265 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1266 def getSession(self, client, session_hash): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1267 """Return session data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1268 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1269 @param session_hash(unicode): hash of the session |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
1270 hash is the same as hostname computed in XEP-0065 § 5.3.2 #1 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1271 @param client(None, SatXMPPClient): client of the peer |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1272 None is used only if client is unknown (this is only the case |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1273 for incoming request received by Socks5ServerFactory). None must |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1274 only be used by Socks5ServerFactory. |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1275 See comments below for details |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1276 @return (dict): session data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1277 """ |
3028 | 1278 assert isinstance(session_hash, str) |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1279 if client is None: |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1280 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1281 client = self.hash_clients_map[session_hash] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1282 except KeyError as e: |
3028 | 1283 log.warning("The requested session doesn't exists !") |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1284 raise e |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1285 return client._s5b_sessions[session_hash] |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1286 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1287 def registerHash(self, *args, **kwargs): |
1845
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1288 """like [_registerHash] but return the session deferred instead of the whole session |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1289 session deferred is fired when transfer is finished |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1290 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1291 return self._registerHash(*args, **kwargs)[DEFER_KEY] |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1292 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1293 def _registerHash(self, client, session_hash, stream_object): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1294 """Create a session_data associated to hash |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1295 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1296 @param session_hash(str): hash of the session |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1297 @param stream_object(iface.IStreamProducer, IConsumer, None): file-like object |
1757
abd6d6f89006
plugins XEP-0065, XEP-0260: fixed session creation order: session is created earlied, and file object associated in a second time if needed
Goffi <goffi@goffi.org>
parents:
1742
diff
changeset
|
1298 None if it will be filled later |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1299 return (dict): session data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1300 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1301 assert session_hash not in client._s5b_sessions |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1302 session_d = defer.Deferred() |
1629
a34d7f621944
plugin XEP-0065: renamed _killSession to killSession as it make sense to kill a session from an other plugin
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1303 session_d.addBoth(self.killSession, session_hash, None, client) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1304 session_data = client._s5b_sessions[session_hash] = { |
1584
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1305 DEFER_KEY: session_d, |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
1306 TIMER_KEY: reactor.callLater(TIMEOUT, self._timeOut, session_hash, client), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1307 } |
1757
abd6d6f89006
plugins XEP-0065, XEP-0260: fixed session creation order: session is created earlied, and file object associated in a second time if needed
Goffi <goffi@goffi.org>
parents:
1742
diff
changeset
|
1308 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1309 if stream_object is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1310 session_data["stream_object"] = stream_object |
1757
abd6d6f89006
plugins XEP-0065, XEP-0260: fixed session creation order: session is created earlied, and file object associated in a second time if needed
Goffi <goffi@goffi.org>
parents:
1742
diff
changeset
|
1311 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1312 assert session_hash not in self.hash_clients_map |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1313 self.hash_clients_map[session_hash] = client |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1314 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1315 return session_data |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1316 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1317 def associateStreamObject(self, client, session_hash, stream_object): |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1318 """Associate a stream object with a session""" |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1319 session_data = self.getSession(client, session_hash) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1320 assert "stream_object" not in session_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1321 session_data["stream_object"] = stream_object |
1757
abd6d6f89006
plugins XEP-0065, XEP-0260: fixed session creation order: session is created earlied, and file object associated in a second time if needed
Goffi <goffi@goffi.org>
parents:
1742
diff
changeset
|
1322 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1323 def streamQuery(self, iq_elt, client): |
3028 | 1324 log.debug("BS stream query") |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1325 |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1326 iq_elt.handled = True |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1327 |
3028 | 1328 query_elt = next(iq_elt.elements(NS_BS, "query")) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1329 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1330 sid = query_elt["sid"] |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1331 except KeyError: |
3028 | 1332 log.warning("Invalid bystreams request received") |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1333 return client.sendError(iq_elt, "bad-request") |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1334 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1335 streamhost_elts = list(query_elt.elements(NS_BS, "streamhost")) |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1336 if not streamhost_elts: |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1337 return client.sendError(iq_elt, "bad-request") |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1338 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1339 try: |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1340 session_data = client.xep_0065_sid_session[sid] |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1341 except KeyError: |
3028 | 1342 log.warning("Ignoring unexpected BS transfer: {}".format(sid)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1343 return client.sendError(iq_elt, "not-acceptable") |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1344 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1345 peer_jid = session_data["peer_jid"] = jid.JID(iq_elt["from"]) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1346 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1347 candidates = [] |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1348 nb_sh = len(streamhost_elts) |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1349 for idx, sh_elt in enumerate(streamhost_elts): |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1350 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1351 host, port, jid_ = sh_elt["host"], sh_elt["port"], jid.JID(sh_elt["jid"]) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1352 except KeyError: |
3028 | 1353 log.warning("malformed streamhost element") |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1354 return client.sendError(iq_elt, "bad-request") |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1355 priority = nb_sh - idx |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1356 if jid_.userhostJID() != peer_jid.userhostJID(): |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1357 type_ = XEP_0065.TYPE_PROXY |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1358 else: |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1359 type_ = XEP_0065.TYPE_DIRECT |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1360 candidates.append(Candidate(host, port, type_, priority, jid_)) |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1361 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1362 for candidate in candidates: |
3028 | 1363 log.info("Candidate proposed: {}".format(candidate)) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1364 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1365 d = self.getBestCandidate(client, candidates, session_data["hash"]) |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1366 d.addCallback(self._ackStream, iq_elt, session_data, client) |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1367 |
1577
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1368 def _ackStream(self, candidate, iq_elt, session_data, client): |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1369 if candidate is None: |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1370 log.info("No streamhost candidate worked, we have to end negotiation") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1371 return client.sendError(iq_elt, "item-not-found") |
3028 | 1372 log.info("We choose: {}".format(candidate)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1373 result_elt = xmlstream.toResponse(iq_elt, "result") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1374 query_elt = result_elt.addElement((NS_BS, "query")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1375 query_elt["sid"] = session_data["id"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1376 streamhost_used_elt = query_elt.addElement("streamhost-used") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1377 streamhost_used_elt["jid"] = candidate.jid.full() |
2129
6a66c8c5a567
core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
1378 client.send(result_elt) |
0 | 1379 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1380 |
3028 | 1381 @implementer(iwokkel.IDisco) |
64 | 1382 class XEP_0065_handler(XMPPHandler): |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1383 |
64 | 1384 def __init__(self, plugin_parent): |
1385 self.plugin_parent = plugin_parent | |
1386 self.host = plugin_parent.host | |
398 | 1387 |
64 | 1388 def connectionInitialized(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1389 self.xmlstream.addObserver( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1390 BS_REQUEST, self.plugin_parent.streamQuery, client=self.parent |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1391 ) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1392 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1393 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
64 | 1394 return [disco.DiscoFeature(NS_BS)] |
1395 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1396 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
64 | 1397 return [] |