Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0065.py @ 4242:8acf46ed7f36
frontends: remote control implementation:
This is the frontends common part of remote control implementation. It handle the creation
of WebRTC session, and management of inputs. For now the reception use freedesktop.org
Desktop portal, and works mostly with Wayland based Desktop Environments.
rel 436
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 11 May 2024 13:52:43 +0200 |
parents | b86912d3fd33 |
children | 0d7bb4df2343 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
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) |
3479 | 9 # 2009-2021 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 |
3401 | 57 import struct |
58 import hashlib | |
59 import uuid | |
60 from collections import namedtuple | |
61 from zope.interface import implementer | |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 from twisted.internet import defer |
3401 | 69 from wokkel import disco, iwokkel |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
70 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
71 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
72 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
73 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
74 from libervia.backend.tools import sat_defer |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
75 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
76 |
3401 | 77 log = getLogger(__name__) |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
78 |
0 | 79 |
80 PLUGIN_INFO = { | |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 C.PI_HANDLER: "yes", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 C.PI_DESCRIPTION: _("""Implementation of SOCKS5 Bytestreams"""), |
0 | 91 } |
92 | |
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
|
93 IQ_SET = '/iq[@type="set"]' |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 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
|
95 BS_REQUEST = IQ_SET + '/query[@xmlns="' + NS_BS + '"]' |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 TIMER_KEY = "timer" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 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
|
98 SERVER_STARTING_PORT = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 0 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 ) # 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
|
101 |
b57b4683dc33
plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents:
1583
diff
changeset
|
102 # 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
|
103 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
|
104 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
|
105 PRIORITY_ASSISTED = 1000 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
106 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
|
107 CANDIDATE_DELAY = 0.2 # see XEP-0260 §4 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
108 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
|
109 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
110 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
|
111 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
112 # 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
|
113 # 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
|
114 # PARAMS = """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
115 # <params> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
116 # <general> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
117 # <category name="File Transfer"> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
118 # <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
|
119 # <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
|
120 # </category> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
121 # </general> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
122 # <individual> |
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 Proxy" value="" 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 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
|
126 # <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
|
127 # </category> |
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 # </params> |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
130 # """ |
0 | 131 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
132 ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
133 STATE_INITIAL, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
134 STATE_AUTH, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
135 STATE_REQUEST, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
136 STATE_READY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
137 STATE_AUTH_USERPASS, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
138 STATE_CLIENT_INITIAL, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
139 STATE_CLIENT_AUTH, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
140 STATE_CLIENT_REQUEST, |
3028 | 141 ) = range(8) |
0 | 142 |
143 SOCKS5_VER = 0x05 | |
144 | |
145 ADDR_IPV4 = 0x01 | |
146 ADDR_DOMAINNAME = 0x03 | |
147 ADDR_IPV6 = 0x04 | |
148 | |
149 CMD_CONNECT = 0x01 | |
150 CMD_BIND = 0x02 | |
151 CMD_UDPASSOC = 0x03 | |
152 | |
153 AUTHMECH_ANON = 0x00 | |
154 AUTHMECH_USERPASS = 0x02 | |
155 AUTHMECH_INVALID = 0xFF | |
156 | |
157 REPLY_SUCCESS = 0x00 | |
158 REPLY_GENERAL_FAILUR = 0x01 | |
159 REPLY_CONN_NOT_ALLOWED = 0x02 | |
160 REPLY_NETWORK_UNREACHABLE = 0x03 | |
161 REPLY_HOST_UNREACHABLE = 0x04 | |
162 REPLY_CONN_REFUSED = 0x05 | |
163 REPLY_TTL_EXPIRED = 0x06 | |
164 REPLY_CMD_NOT_SUPPORTED = 0x07 | |
165 REPLY_ADDR_NOT_SUPPORTED = 0x08 | |
166 | |
167 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
168 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
|
169 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
170 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
171 class Candidate(object): |
3040 | 172 def __init__(self, host, port, type_, priority, jid_, id_=None, priority_local=False, |
173 factory=None,): | |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
174 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
175 @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
|
176 @param port(int): port |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
177 @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
|
178 @param priority(int): priority |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
179 @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
|
180 @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
|
181 @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
|
182 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
|
183 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
184 assert isinstance(jid_, jid.JID) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
185 self.host, self.port, self.type, self.jid = (host, int(port), type_, jid_) |
3028 | 186 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
|
187 if priority_local: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
188 self._local_priority = int(priority) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
189 self._priority = self.calculate_priority() |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
190 else: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
191 self._local_priority = 0 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
192 self._priority = int(priority) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
193 self.factory = factory |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
194 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
195 def discard(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
196 """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
|
197 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
198 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
|
199 """ |
3028 | 200 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
|
201 try: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
202 self.factory.discard() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
203 except AttributeError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
204 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
|
205 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
206 @property |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
207 def local_priority(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
208 return self._local_priority |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
209 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
210 @property |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
211 def priority(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
212 return self._priority |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
213 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
214 def __str__(self): |
3028 | 215 return "Candidate ({0.priority}): host={0.host} port={0.port} jid={0.jid} type={0.type}{id}".format( |
216 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
|
217 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
218 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
219 def __eq__(self, other): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
220 # 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
|
221 # 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
|
222 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
223 return ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
224 self.host == other.host |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
225 and self.port == other.port |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
226 and self.jid == other.jid |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
227 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
228 except (AttributeError, TypeError): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
229 return False |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
230 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
231 def __ne__(self, other): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
232 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
|
233 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
234 def calculate_priority(self): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
235 """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
|
236 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
237 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
238 @return (int): priority |
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 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
|
241 multiplier = 126 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
242 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
|
243 multiplier = 120 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
244 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
|
245 multiplier = 110 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
246 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
|
247 multiplier = 10 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
248 else: |
3028 | 249 raise exceptions.InternalError("Unknown {} type !".format(self.type)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
250 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
|
251 |
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
|
252 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
|
253 """Activate the proxy candidate |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
254 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
255 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
|
256 Must only be used with proxy candidates |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
257 @param sid(unicode): session id (same as for get_session_hash) |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
258 @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
|
259 @return (D(domish.Element)): IQ result (or error) |
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 assert self.type == XEP_0065.TYPE_PROXY |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
262 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
|
263 iq_elt["from"] = local_jid.full() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
264 iq_elt["to"] = self.jid.full() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
265 query_elt = iq_elt.addElement((NS_BS, "query")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
266 query_elt["sid"] = sid |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
267 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
|
268 return iq_elt.send() |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
269 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
270 def start_transfer(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
|
271 if self.type == XEP_0065.TYPE_PROXY: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
272 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
|
273 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
|
274 chunk_size = None |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
275 self.factory.start_transfer(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
|
276 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
277 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
278 def get_session_hash(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
|
279 """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
|
280 |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
281 @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
|
282 @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
|
283 @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
|
284 @return (str): hash |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
285 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
286 return hashlib.sha1( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
287 (sid + requester_jid.full() + target_jid.full()).encode("utf-8") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
288 ).hexdigest() |
0 | 289 |
290 | |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
291 class SOCKSv5(protocol.Protocol): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
292 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
|
293 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
294 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
|
295 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
296 @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
|
297 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
|
298 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
299 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
|
300 if session_hash is not None: |
3028 | 301 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
|
302 self.server_mode = False |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
303 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
|
304 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
|
305 else: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
306 self.server_mode = True |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
307 self.state = STATE_INITIAL |
3028 | 308 self.buf = b"" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
309 self.supportedAuthMechs = [AUTHMECH_ANON] |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
310 self.supportedAddrs = [ADDR_DOMAINNAME] |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
311 self.enabledCommands = [CMD_CONNECT] |
0 | 312 self.peersock = None |
313 self.addressType = 0 | |
314 self.requestType = 0 | |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
315 self._stream_object = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
316 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
|
317 # 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
|
318 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
319 @property |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
320 def stream_object(self): |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
321 if self._stream_object is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
322 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
|
323 if self.server_mode: |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
324 self._stream_object.registerProducer(self.transport, True) |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
325 return self._stream_object |
0 | 326 |
1845
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
327 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
|
328 """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
|
329 |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
330 @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
|
331 """ |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
332 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
|
333 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
|
334 else: |
2bde6fc7aabd
plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
335 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
|
336 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
337 def _start_negotiation(self): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
338 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
|
339 self.state = STATE_CLIENT_AUTH |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
340 self.transport.write(struct.pack("!3B", SOCKS5_VER, 1, AUTHMECH_ANON)) |
0 | 341 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
342 def _parse_negotiation(self): |
0 | 343 try: |
344 # Parse out data | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
345 ver, nmethod = struct.unpack("!BB", self.buf[:2]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
346 methods = struct.unpack("%dB" % nmethod, self.buf[2 : nmethod + 2]) |
0 | 347 |
348 # Ensure version is correct | |
349 if ver != 5: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
350 self.transport.write(struct.pack("!BB", SOCKS5_VER, AUTHMECH_INVALID)) |
0 | 351 self.transport.loseConnection() |
352 return | |
353 | |
354 # Trim off front of the buffer | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
355 self.buf = self.buf[nmethod + 2 :] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
356 |
0 | 357 # Check for supported auth mechs |
358 for m in self.supportedAuthMechs: | |
359 if m in methods: | |
360 # Update internal state, according to selected method | |
361 if m == AUTHMECH_ANON: | |
362 self.state = STATE_REQUEST | |
363 elif m == AUTHMECH_USERPASS: | |
364 self.state = STATE_AUTH_USERPASS | |
365 # Complete negotiation w/ this method | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
366 self.transport.write(struct.pack("!BB", SOCKS5_VER, m)) |
0 | 367 return |
368 | |
369 # No supported mechs found, notify client and close the connection | |
3028 | 370 log.warning("Unsupported authentication mechanism") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
371 self.transport.write(struct.pack("!BB", SOCKS5_VER, AUTHMECH_INVALID)) |
0 | 372 self.transport.loseConnection() |
373 except struct.error: | |
374 pass | |
375 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
376 def _parse_user_pass(self): |
0 | 377 try: |
378 # Parse out data | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
379 ver, ulen = struct.unpack("BB", self.buf[:2]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
380 uname, = struct.unpack("%ds" % ulen, self.buf[2 : ulen + 2]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
381 plen, = struct.unpack("B", self.buf[ulen + 2]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
382 password, = struct.unpack("%ds" % plen, self.buf[ulen + 3 : ulen + 3 + plen]) |
0 | 383 # Trim off fron of the buffer |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
384 self.buf = self.buf[3 + ulen + plen :] |
0 | 385 # Fire event to authenticate user |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
386 if self.authenticate_user_pass(uname, password): |
0 | 387 # Signal success |
388 self.state = STATE_REQUEST | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
389 self.transport.write(struct.pack("!BB", SOCKS5_VER, 0x00)) |
0 | 390 else: |
391 # Signal failure | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
392 self.transport.write(struct.pack("!BB", SOCKS5_VER, 0x01)) |
0 | 393 self.transport.loseConnection() |
394 except struct.error: | |
395 pass | |
396 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
397 def send_error_reply(self, errorcode): |
0 | 398 # Any other address types are not supported |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
399 result = struct.pack("!BBBBIH", SOCKS5_VER, errorcode, 0, 1, 0, 0) |
0 | 400 self.transport.write(result) |
401 self.transport.loseConnection() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
402 |
0 | 403 def _parseRequest(self): |
404 try: | |
405 # Parse out data and trim buffer accordingly | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
406 ver, cmd, rsvd, self.addressType = struct.unpack("!BBBB", self.buf[:4]) |
0 | 407 |
408 # Ensure we actually support the requested address type | |
409 if self.addressType not in self.supportedAddrs: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
410 self.send_error_reply(REPLY_ADDR_NOT_SUPPORTED) |
0 | 411 return |
412 | |
413 # Deal with addresses | |
414 if self.addressType == ADDR_IPV4: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
415 addr, port = struct.unpack("!IH", self.buf[4:10]) |
0 | 416 self.buf = self.buf[10:] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
417 elif self.addressType == ADDR_DOMAINNAME: |
3028 | 418 nlen = self.buf[4] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
419 addr, port = struct.unpack("!%dsH" % nlen, self.buf[5:]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
420 self.buf = self.buf[7 + len(addr) :] |
0 | 421 else: |
422 # Any other address types are not supported | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
423 self.send_error_reply(REPLY_ADDR_NOT_SUPPORTED) |
0 | 424 return |
425 | |
426 # Ensure command is supported | |
427 if cmd not in self.enabledCommands: | |
428 # Send a not supported error | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
429 self.send_error_reply(REPLY_CMD_NOT_SUPPORTED) |
0 | 430 return |
431 | |
432 # Process the command | |
433 if cmd == CMD_CONNECT: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
434 self.connect_requested(addr, port) |
0 | 435 elif cmd == CMD_BIND: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
436 self.bind_requested(addr, port) |
0 | 437 else: |
438 # Any other command is not supported | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
439 self.send_error_reply(REPLY_CMD_NOT_SUPPORTED) |
0 | 440 |
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
|
441 except struct.error: |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
442 # The buffer is probably not complete, we need to wait more |
0 | 443 return None |
444 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
445 def _make_request(self): |
3028 | 446 hash_ = self._session_hash.encode('utf-8') |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
447 request = struct.pack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
448 "!5B%dsH" % len(hash_), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
449 SOCKS5_VER, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
450 CMD_CONNECT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
451 0, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
452 ADDR_DOMAINNAME, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
453 len(hash_), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
454 hash_, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
455 0, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
456 ) |
0 | 457 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
|
458 self.state = STATE_CLIENT_REQUEST |
0 | 459 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
460 def _parse_request_reply(self): |
0 | 461 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
462 ver, rep, rsvd, self.addressType = struct.unpack("!BBBB", self.buf[:4]) |
0 | 463 # Ensure we actually support the requested address type |
464 if self.addressType not in self.supportedAddrs: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
465 self.send_error_reply(REPLY_ADDR_NOT_SUPPORTED) |
0 | 466 return |
467 | |
468 # Deal with addresses | |
469 if self.addressType == ADDR_IPV4: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
470 addr, port = struct.unpack("!IH", self.buf[4:10]) |
0 | 471 self.buf = self.buf[10:] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
472 elif self.addressType == ADDR_DOMAINNAME: |
3028 | 473 nlen = self.buf[4] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
474 addr, port = struct.unpack("!%dsH" % nlen, self.buf[5:]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
475 self.buf = self.buf[7 + len(addr) :] |
0 | 476 else: |
477 # Any other address types are not supported | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
478 self.send_error_reply(REPLY_ADDR_NOT_SUPPORTED) |
0 | 479 return |
480 | |
481 # Ensure reply is OK | |
482 if rep != REPLY_SUCCESS: | |
483 self.loseConnection() | |
484 return | |
485 | |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
486 self.state = STATE_READY |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
487 self.connection.callback(None) |
0 | 488 |
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
|
489 except struct.error: |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
490 # The buffer is probably not complete, we need to wait more |
0 | 491 return None |
492 | |
493 def connectionMade(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
494 log.debug( |
3028 | 495 "Socks5 connectionMade (mode = {})".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
496 "server" if self.state == STATE_INITIAL else "client" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
497 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
498 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
499 if self.state == STATE_CLIENT_INITIAL: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
500 self._start_negotiation() |
0 | 501 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
502 def connect_requested(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
|
503 # Check that this session is expected |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
504 if not self.factory.add_to_session(addr.decode('utf-8'), self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
505 log.warning( |
3028 | 506 "Unexpected connection request received from {host}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
507 host=self.transport.getPeer().host |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
508 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
509 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
510 self.send_error_reply(REPLY_CONN_REFUSED) |
0 | 511 return |
3028 | 512 self._session_hash = addr.decode('utf-8') |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
513 self.connect_completed(addr, 0) |
0 | 514 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
515 def start_transfer(self, chunk_size): |
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
|
516 """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
|
517 |
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
|
518 @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
|
519 """ |
1759
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
520 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
|
521 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
|
522 self.CHUNK_SIZE = chunk_size |
3028 | 523 log.debug("Starting file transfer") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
524 d = self.stream_object.start_stream(self.transport) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
525 d.addCallback(self.stream_finished) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
526 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
527 def stream_finished(self, d): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
956
diff
changeset
|
528 log.info(_("File transfer completed, closing connection")) |
0 | 529 self.transport.loseConnection() |
530 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
531 def connect_completed(self, remotehost, remoteport): |
0 | 532 if self.addressType == ADDR_IPV4: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
533 result = struct.pack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
534 "!BBBBIH", SOCKS5_VER, REPLY_SUCCESS, 0, 1, remotehost, remoteport |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
535 ) |
0 | 536 elif self.addressType == ADDR_DOMAINNAME: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
537 result = struct.pack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
538 "!BBBBB%dsH" % len(remotehost), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
539 SOCKS5_VER, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
540 REPLY_SUCCESS, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
541 0, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
542 ADDR_DOMAINNAME, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
543 len(remotehost), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
544 remotehost, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
545 remoteport, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
546 ) |
0 | 547 self.transport.write(result) |
548 self.state = STATE_READY | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
549 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
550 def bind_requested(self, addr, port): |
0 | 551 pass |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
552 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
553 def authenticate_user_pass(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
|
554 # FIXME: implement authentication and remove the debug printing a password |
3028 | 555 log.debug("User/pass: %s/%s" % (user, passwd)) |
0 | 556 return True |
557 | |
558 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
|
559 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
|
560 # 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
|
561 self.stream_object.write(buf) |
1759
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
562 if not self.active: |
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
563 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
|
564 self.getSession()[TIMER_KEY].cancel() |
0 | 565 return |
566 | |
567 self.buf = self.buf + buf | |
568 if self.state == STATE_INITIAL: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
569 self._parse_negotiation() |
0 | 570 if self.state == STATE_AUTH_USERPASS: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
571 self._parse_user_pass() |
0 | 572 if self.state == STATE_REQUEST: |
573 self._parseRequest() | |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
574 if self.state == STATE_CLIENT_REQUEST: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
575 self._parse_request_reply() |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
576 if self.state == STATE_CLIENT_AUTH: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
577 ver, method = struct.unpack("!BB", buf) |
0 | 578 self.buf = self.buf[2:] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
579 if ver != SOCKS5_VER or method != AUTHMECH_ANON: |
0 | 580 self.transport.loseConnection() |
581 else: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
582 self._make_request() |
0 | 583 |
584 def connectionLost(self, reason): | |
3028 | 585 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
|
586 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
|
587 self.connection.errback(reason) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
588 if self.server_mode: |
3401 | 589 try: |
590 session_hash = self._session_hash | |
591 except AttributeError: | |
592 log.debug("no session has been received yet") | |
593 else: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
594 self.factory.remove_from_session(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
|
595 |
0 | 596 |
597 class Socks5ServerFactory(protocol.ServerFactory): | |
598 protocol = SOCKSv5 | |
599 | |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
600 def __init__(self, parent): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
601 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
602 @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
|
603 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
604 self.parent = parent |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
605 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
606 def getSession(self, session_hash): |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
607 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
|
608 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
609 def start_transfer(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
|
610 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
|
611 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
612 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
|
613 except (KeyError, IndexError): |
3028 | 614 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
|
615 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
|
616 session[TIMER_KEY].cancel() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
617 protocol.start_transfer(chunk_size) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
618 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
619 def add_to_session(self, session_hash, protocol): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
620 """Check is session_hash is valid, and associate protocol with it |
0 | 621 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
622 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
|
623 @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
|
624 @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
|
625 @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
|
626 """ |
3028 | 627 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
|
628 try: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
629 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
|
630 except KeyError: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
631 return False |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
632 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
633 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
|
634 return True |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
635 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
636 def remove_from_session(self, session_hash, protocol, reason): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
637 """Remove a protocol from session_data |
0 | 638 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
639 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
|
640 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
|
641 @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
|
642 @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
|
643 @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
|
644 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
645 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
646 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
|
647 protocols.remove(protocol) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
648 except (KeyError, ValueError): |
3028 | 649 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
|
650 else: |
1759
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
651 if protocol.active: |
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
652 # 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
|
653 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
|
654 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
|
655 else: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
656 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
|
657 |
0 | 658 |
659 class Socks5ClientFactory(protocol.ClientFactory): | |
660 protocol = SOCKSv5 | |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
661 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
662 def __init__(self, client, parent, session, session_hash): |
398 | 663 """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
|
664 |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
665 @param session(dict): session data |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
666 @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
|
667 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
|
668 """ |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
669 self.session = session |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
670 self.session_hash = session_hash |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
671 self.client = client |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
672 self.connection = defer.Deferred() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
673 self._protocol_instance = None |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
674 self.connector = None |
0 | 675 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
676 def discard(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
677 """Disconnect the client |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
678 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
679 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
|
680 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
681 self.connector.disconnect() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
682 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
683 def getSession(self): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
684 return self.session |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
685 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
686 def start_transfer(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
|
687 self.session[TIMER_KEY].cancel() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
688 self._protocol_instance.start_transfer(chunk_size) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
689 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
690 def clientConnectionFailed(self, connector, reason): |
3028 | 691 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
|
692 self.connection.errback(reason) |
0 | 693 |
694 def clientConnectionLost(self, connector, reason): | |
3028 | 695 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
|
696 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
|
697 # 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
|
698 # 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
|
699 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
|
700 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
|
701 else: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
702 self.getSession()[DEFER_KEY].errback(reason) |
1759
81923b3f8b14
plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents:
1758
diff
changeset
|
703 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
|
704 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
705 def buildProtocol(self, addr): |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
706 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
|
707 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
|
708 p.factory = self |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
709 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
|
710 self._protocol_instance = p |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
711 return p |
0 | 712 |
713 | |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
714 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
|
715 NAMESPACE = NS_BS |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
716 TYPE_DIRECT = "direct" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
717 TYPE_ASSISTED = "assisted" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
718 TYPE_TUNEL = "tunel" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
719 TYPE_PROXY = "proxy" |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
720 Candidate = Candidate |
20
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
721 |
0 | 722 def __init__(self, host): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
956
diff
changeset
|
723 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
|
724 self.host = host |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
725 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
726 # session data |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
727 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
|
728 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
|
729 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
730 # misc data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
731 self._server_factory = None |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
732 self._external_port = None |
21
633c5ed65701
parameters: new button type (not finished)
Goffi <goffi@goffi.org>
parents:
20
diff
changeset
|
733 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
734 # plugins shortcuts |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
735 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
|
736 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
737 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
|
738 except KeyError: |
3028 | 739 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
|
740 self._np = None |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
741 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
742 # parameters |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
743 # XXX: params are not used for now, but they may be used in the futur to force proxy/IP |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
744 # host.memory.update_params(PARAMS) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
745 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
746 def get_handler(self, client): |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
747 return XEP_0065_handler(self) |
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
748 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
749 def profile_connected(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
|
750 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
|
751 client._s5b_sessions = {} |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
752 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
753 def get_session_hash(self, from_jid, to_jid, sid): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
754 return get_session_hash(from_jid, to_jid, sid) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
755 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
756 def get_socks_5_server_factory(self): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
757 """Return server factory |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
758 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
759 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
|
760 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
|
761 """ |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
536
diff
changeset
|
762 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
763 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
|
764 self._server_factory = Socks5ServerFactory(self) |
3028 | 765 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
|
766 try: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
767 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
|
768 except internet_error.CannotListenError as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
769 log.debug( |
3028 | 770 "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
|
771 port=port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
772 err_msg=e.socketError.strerror, |
3028 | 773 err_num=" (error code: {})".format(e.socketError.errno), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
774 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
775 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
776 else: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
777 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
|
778 break |
0 | 779 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
780 log.info( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
781 _("Socks5 Stream server launched on port {}").format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
782 self._server_factory_port |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
783 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
784 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
785 return self._server_factory |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
786 |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
787 async def get_proxy(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
|
788 """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
|
789 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
790 cache is used between clients using the same server |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
791 @param local_jid(jid.JID): same as for [get_candidates] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
792 @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
|
793 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
|
794 @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
|
795 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
796 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
797 def notFound(server): |
3028 | 798 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
|
799 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
|
800 raise exceptions.NotFound |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
801 |
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 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
|
803 try: |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
804 return self._cache_proxies[server] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
805 except KeyError: |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
806 pass |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
807 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
808 proxy = ( |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
809 await self.host.find_service_entities(client, "proxy", "bytestreams") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
810 ).pop() |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
811 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
|
812 notFound(server) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
813 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
|
814 iq_elt["from"] = local_jid.full() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
815 iq_elt["to"] = proxy.full() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
816 iq_elt.addElement((NS_BS, "query")) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
817 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
818 try: |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
819 result_elt = await iq_elt.send() |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
820 except jabber_error.StanzaError as failure: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
821 log.warning( |
3028 | 822 "Error while requesting proxy info on {jid}: {error}".format( |
3401 | 823 jid=proxy.full(), error=failure |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
824 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
825 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
826 notFound(server) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
827 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
828 try: |
3028 | 829 query_elt = next(result_elt.elements(NS_BS, "query")) |
830 streamhost_elt = next(query_elt.elements(NS_BS, "streamhost")) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
831 host = streamhost_elt["host"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
832 jid_ = streamhost_elt["jid"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
833 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
|
834 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
|
835 raise KeyError |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
836 jid_ = jid.JID(jid_) |
1742
244a605623d6
complete the Exception's list when catching JID error:
souliane <souliane@mailoo.org>
parents:
1675
diff
changeset
|
837 except (StopIteration, KeyError, RuntimeError, jid.InvalidFormat, AttributeError): |
3028 | 838 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
|
839 notFound(server) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
840 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
841 proxy_infos = self._cache_proxies[server] = ProxyInfos(host, jid_, port) |
3028 | 842 log.info("Proxy found: {}".format(proxy_infos)) |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
843 return proxy_infos |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
844 |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
845 async def _get_network_data(self, client): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
846 """Retrieve information about network |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
847 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
848 @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
|
849 @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
|
850 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
851 self.get_socks_5_server_factory() |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
852 local_port = self._server_factory_port |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
853 external_ip = await self._ip.get_external_ip(client) |
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
854 local_ips = await self._ip.get_local_ips(client) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
855 |
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
|
856 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
|
857 if external_ip != local_ips[0]: |
3028 | 858 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
|
859 if self._np is None: |
3028 | 860 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
|
861 else: |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
862 ext_port = await self._np.map_port( |
3028 | 863 local_port, desc="SaT socks5 stream" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
864 ) |
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
|
865 if ext_port is None: |
3028 | 866 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
|
867 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
|
868 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
|
869 |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
870 return (local_port, self._external_port, local_ips, external_ip) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
871 |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
872 async def get_candidates(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
|
873 """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
|
874 |
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
|
875 @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
|
876 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
|
877 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
|
878 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
|
879 @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
|
880 """ |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
881 server_factory = self.get_socks_5_server_factory() |
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
882 local_port, ext_port, local_ips, external_ip = await self._get_network_data(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
|
883 try: |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
884 proxy = await self.get_proxy(client, local_jid) |
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
|
885 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
|
886 proxy = None |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
887 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
888 # 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
|
889 candidates = [] |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
890 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
891 # 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
|
892 |
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
|
893 # 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
|
894 ip = local_ips.pop(0) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
895 candidates.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
896 Candidate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
897 ip, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
898 local_port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
899 XEP_0065.TYPE_DIRECT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
900 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
|
901 local_jid, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
902 priority_local=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
903 factory=server_factory, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
904 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
905 ) |
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
|
906 for ip in local_ips: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
907 candidates.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
908 Candidate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
909 ip, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
910 local_port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
911 XEP_0065.TYPE_DIRECT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
912 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
|
913 local_jid, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
914 priority_local=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
915 factory=server_factory, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
916 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
917 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
918 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
919 # then the assisted one |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
920 if ext_port is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
921 candidates.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
922 Candidate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
923 external_ip, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
924 ext_port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
925 XEP_0065.TYPE_ASSISTED, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
926 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
|
927 local_jid, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
928 priority_local=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
929 factory=server_factory, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
930 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
931 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
932 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
933 # finally the proxy |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
934 if proxy: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
935 candidates.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
936 Candidate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
937 proxy.host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
938 proxy.port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
939 XEP_0065.TYPE_PROXY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
940 PRIORITY_PROXY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
941 proxy.jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
942 priority_local=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
943 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
944 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
945 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
946 # 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
|
947 candidates.sort(key=lambda c: c.priority, reverse=True) |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
948 return candidates |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
949 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
950 def _add_connector(self, connector, candidate): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
951 """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
|
952 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
953 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
|
954 @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
|
955 @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
|
956 @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
|
957 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
958 candidate.factory.connector = connector |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
959 return candidate.factory.connection |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
960 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
961 def connect_candidate( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
962 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
|
963 ): |
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
|
964 """Connect to a candidate |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
965 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
966 Connection will be done with a Socks5ClientFactory |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
967 @param candidate(Candidate): candidate to connect to |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
968 @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
|
969 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
|
970 @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
|
971 None to use session_hash. |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
972 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
|
973 - 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
|
974 - when a peer connect to a proxy *he proposed himself* |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
975 in practice, peer_session_hash is only used by try_candidates |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
976 @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
|
977 @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
|
978 """ |
1758
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
979 if peer_session_hash is None: |
a66d34353f34
plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents:
1757
diff
changeset
|
980 # 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
|
981 peer_session_hash = session_hash |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
982 session = self.getSession(client, session_hash) |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
983 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
|
984 candidate.factory = factory |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
985 if delay is None: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
986 d = defer.succeed(candidate.host) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
987 else: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
988 d = sat_defer.DelayedDeferred(delay, candidate.host) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
989 d.addCallback(reactor.connectTCP, candidate.port, factory) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
990 d.addCallback(self._add_connector, candidate) |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
991 return d |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1569
diff
changeset
|
992 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
993 def try_candidates( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
994 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
995 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
996 candidates, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
997 session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
998 peer_session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
999 connection_cb=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1000 connection_eb=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1001 ): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1002 defers_list = [] |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1003 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1004 for candidate in candidates: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1005 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
|
1006 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
|
1007 delay += CANDIDATE_DELAY_PROXY |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1008 d = self.connect_candidate( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1009 client, candidate, session_hash, peer_session_hash, delay |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1010 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1011 if connection_cb is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1012 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
|
1013 lambda __, candidate=candidate, client=client: connection_cb( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1014 client, candidate |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1015 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1016 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1017 if connection_eb is not None: |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1018 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
|
1019 defers_list.append(d) |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1020 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1021 return defers_list |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1022 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1023 def get_best_candidate(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
|
1024 """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
|
1025 |
d04d7402b8e9
plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents:
1576
diff
changeset
|
1026 @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
|
1027 @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
|
1028 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
|
1029 @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
|
1030 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
|
1031 @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
|
1032 """ |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1033 defer_candidates = None |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1034 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1035 def connection_cb(client, candidate): |
3028 | 1036 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
|
1037 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
|
1038 try: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1039 if other_candidate.priority < candidate.priority: |
3028 | 1040 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
|
1041 defer_candidates[idx].cancel() |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1042 except AttributeError: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1043 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
|
1044 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1045 def connection_eb(failure, client, candidate): |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1046 if failure.check(defer.CancelledError): |
3028 | 1047 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
|
1048 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1049 log.info( |
3028 | 1050 "Connection of {candidate} Failed: {error}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1051 candidate=candidate, error=failure.value |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1052 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1053 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1054 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
|
1055 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1056 def all_tested(__): |
3028 | 1057 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
|
1058 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
|
1059 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
|
1060 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1061 defer_candidates = self.try_candidates( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1062 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1063 candidates, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1064 session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1065 peer_session_hash, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1066 connection_cb, |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1067 connection_eb, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1068 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1069 d_list = defer.DeferredList(defer_candidates) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1070 d_list.addCallback(all_tested) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1071 return d_list |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1072 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1073 def _time_out(self, session_hash, 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
|
1074 """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
|
1075 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1076 @param session_hash(str): hash as returned by get_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
|
1077 @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
|
1078 """ |
3028 | 1079 log.info("Socks5 Bytestream: TimeOut reached") |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1080 session = self.getSession(client, session_hash) |
3132
b64dd7c1496d
plugin ip: fixed IP detection with external website
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
1081 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
|
1082 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1083 def kill_session(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
|
1084 """Clean the current session |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1085 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1086 @param session_hash(str): hash as returned by get_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
|
1087 @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
|
1088 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
|
1089 @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
|
1090 @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
|
1091 @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
|
1092 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1093 log.debug( |
3028 | 1094 "Cleaning session with hash {hash}{id}: {reason}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1095 hash=session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1096 reason="" if failure_ is None else failure_.value, |
3028 | 1097 id="" if sid is None else " (id: {})".format(sid), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1098 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1099 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1100 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1101 try: |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1102 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
|
1103 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
|
1104 except KeyError: |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1105 pass |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1106 |
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
|
1107 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
|
1108 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
|
1109 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
|
1110 except KeyError: |
3028 | 1111 log.warning("Session id {} is unknown".format(sid)) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1112 |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1113 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
|
1114 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
|
1115 except KeyError: |
3028 | 1116 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
|
1117 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
|
1118 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
|
1119 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
|
1120 |
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 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1122 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
|
1123 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
|
1124 pass |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1125 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
1126 return failure_ |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1127 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1128 def start_stream(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
|
1129 """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
|
1130 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1131 @param streamProducer: stream_object to use |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1132 @param local_jid(jid.JID): same as for [get_candidates] |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1133 @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
|
1134 @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
|
1135 @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
|
1136 @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
|
1137 @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
|
1138 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1139 session_data = self._create_session( |
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
|
1140 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
|
1141 |
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
|
1142 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
|
1143 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1144 def got_candidates(candidates): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1145 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
|
1146 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
|
1147 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
|
1148 iq_elt["to"] = to_jid.full() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1149 query_elt = iq_elt.addElement((NS_BS, "query")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1150 query_elt["mode"] = "tcp" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1151 query_elt["sid"] = sid |
398 | 1152 |
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
|
1153 for candidate in candidates: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1154 streamhost = query_elt.addElement("streamhost") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1155 streamhost["host"] = candidate.host |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1156 streamhost["port"] = str(candidate.port) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1157 streamhost["jid"] = candidate.jid.full() |
3028 | 1158 log.debug("Candidate proposed: {}".format(candidate)) |
398 | 1159 |
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
|
1160 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
|
1161 args = [client, session_data, local_jid] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1162 d.addCallbacks(self._iq_negotiation_cb, self._iq_negotiation_eb, args, None, args) |
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
|
1163 |
4180
b86912d3fd33
plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1164 defer.ensureDeferred(self.get_candidates(client, local_jid)).addCallback(got_candidates) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1165 return session_data[DEFER_KEY] |
0 | 1166 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1167 def _iq_negotiation_cb(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
|
1168 """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
|
1169 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1170 @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
|
1171 @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
|
1172 @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
|
1173 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1174 try: |
3028 | 1175 query_elt = next(iq_elt.elements(NS_BS, "query")) |
1176 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
|
1177 except StopIteration: |
3028 | 1178 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
|
1179 # FIXME: must clean session |
398 | 1180 return |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1181 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1182 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
|
1183 try: |
3028 | 1184 candidate = next(( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1185 c for c in session_data["candidates"] if c.jid == streamhost_jid |
3028 | 1186 )) |
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 except StopIteration: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1188 log.warning( |
3028 | 1189 "Candidate [{jid}] is unknown !".format(jid=streamhost_jid.full()) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
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 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
|
1192 else: |
3028 | 1193 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
|
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 if candidate.type == XEP_0065.TYPE_PROXY: |
3028 | 1196 log.info("A Socks5 proxy is used") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1197 d = self.connect_candidate(client, candidate, session_data["hash"]) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1198 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
|
1199 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
|
1200 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
|
1201 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1202 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1203 d.addErrback(self._activation_eb) |
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
|
1204 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
|
1205 d = defer.succeed(None) |
398 | 1206 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1207 d.addCallback(lambda __: candidate.start_transfer(session_data["hash"])) |
398 | 1208 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1209 def _activation_eb(self, failure): |
3028 | 1210 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
|
1211 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1212 def _iq_negotiation_eb(self, stanza_err, client, session_data, local_jid): |
3028 | 1213 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
|
1214 # 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
|
1215 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1216 def create_session(self, *args, **kwargs): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1217 """like [_create_session] 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
|
1218 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1219 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
|
1220 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1221 return self._create_session(*args, **kwargs)[DEFER_KEY] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1222 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1223 def _create_session(self, client, stream_object, local_jid, to_jid, 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
|
1224 requester=False): |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1225 """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
|
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 @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
|
1228 written |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1229 @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
|
1230 @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
|
1231 @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
|
1232 @return (dict): session data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1233 """ |
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
|
1234 if sid in client.xep_0065_sid_session: |
3028 | 1235 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
|
1236 if requester: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1237 session_hash = get_session_hash(local_jid, to_jid, sid) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1238 session_data = self._register_hash(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
|
1239 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1240 session_hash = get_session_hash(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
|
1241 session_d = defer.Deferred() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1242 session_d.addBoth(self.kill_session, 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
|
1243 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
|
1244 DEFER_KEY: session_d, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1245 TIMER_KEY: reactor.callLater( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1246 TIMEOUT, self._time_out, session_hash, client |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1247 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1248 } |
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
|
1249 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
|
1250 session_data.update( |
2624
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 "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
|
1253 "local_jid": local_jid, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1254 "peer_jid": to_jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1255 "stream_object": stream_object, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1256 "hash": session_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1257 } |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1258 ) |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1259 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1260 return session_data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1261 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1262 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
|
1263 """Return session data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1264 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1265 @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
|
1266 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
|
1267 @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
|
1268 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
|
1269 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
|
1270 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
|
1271 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
|
1272 @return (dict): session data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1273 """ |
3028 | 1274 assert isinstance(session_hash, str) |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1275 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
|
1276 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1277 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
|
1278 except KeyError as e: |
3028 | 1279 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
|
1280 raise e |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1281 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
|
1282 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1283 def register_hash(self, *args, **kwargs): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1284 """like [_register_hash] 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
|
1285 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
|
1286 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1287 return self._register_hash(*args, **kwargs)[DEFER_KEY] |
1559
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1288 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1289 def _register_hash(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
|
1290 """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
|
1291 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1292 @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
|
1293 @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
|
1294 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
|
1295 return (dict): session data |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1296 """ |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1297 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
|
1298 session_d = defer.Deferred() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1299 session_d.addBoth(self.kill_session, 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
|
1300 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
|
1301 DEFER_KEY: session_d, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1302 TIMER_KEY: reactor.callLater(TIMEOUT, self._time_out, session_hash, client), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1303 } |
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
|
1304 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1305 if stream_object is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1306 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
|
1307 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1308 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
|
1309 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
|
1310 |
7cc29634b6ef
plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
1311 return session_data |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1312 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1313 def associate_stream_object(self, client, session_hash, stream_object): |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1314 """Associate a stream object with a session""" |
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1315 session_data = self.getSession(client, session_hash) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1316 assert "stream_object" not in session_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1317 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
|
1318 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1319 def stream_query(self, iq_elt, client): |
3028 | 1320 log.debug("BS stream query") |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1321 |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
1322 iq_elt.handled = True |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1323 |
3028 | 1324 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
|
1325 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1326 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
|
1327 except KeyError: |
3028 | 1328 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
|
1329 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
|
1330 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1331 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
|
1332 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
|
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 |
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
|
1335 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
|
1336 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
|
1337 except KeyError: |
3028 | 1338 log.warning("Ignoring unexpected BS transfer: {}".format(sid)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1339 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
|
1340 |
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
|
1341 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
|
1342 |
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
|
1343 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
|
1344 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
|
1345 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
|
1346 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1347 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
|
1348 except KeyError: |
3028 | 1349 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
|
1350 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
|
1351 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
|
1352 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
|
1353 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
|
1354 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
|
1355 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
|
1356 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
|
1357 |
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
|
1358 for candidate in candidates: |
3028 | 1359 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
|
1360 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1361 d = self.get_best_candidate(client, candidates, session_data["hash"]) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1362 d.addCallback(self._ack_stream, 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
|
1363 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1364 def _ack_stream(self, candidate, iq_elt, session_data, 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
|
1365 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
|
1366 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
|
1367 return client.sendError(iq_elt, "item-not-found") |
3028 | 1368 log.info("We choose: {}".format(candidate)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1369 result_elt = xmlstream.toResponse(iq_elt, "result") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1370 query_elt = result_elt.addElement((NS_BS, "query")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1371 query_elt["sid"] = session_data["id"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1372 streamhost_used_elt = query_elt.addElement("streamhost-used") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1373 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
|
1374 client.send(result_elt) |
0 | 1375 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1376 |
3028 | 1377 @implementer(iwokkel.IDisco) |
3401 | 1378 class XEP_0065_handler(xmlstream.XMPPHandler): |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1379 |
64 | 1380 def __init__(self, plugin_parent): |
1381 self.plugin_parent = plugin_parent | |
1382 self.host = plugin_parent.host | |
398 | 1383 |
64 | 1384 def connectionInitialized(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1385 self.xmlstream.addObserver( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1386 BS_REQUEST, self.plugin_parent.stream_query, client=self.parent |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1387 ) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
1388 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1389 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
64 | 1390 return [disco.DiscoFeature(NS_BS)] |
1391 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1392 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
64 | 1393 return [] |