annotate src/plugins/plugin_xep_0065.py @ 1818:7e6342de71fb

core (xml_tools): unescape HTML sequences to avoid parsing error in ElementParser
author Goffi <goffi@goffi.org>
date Fri, 22 Jan 2016 20:24:17 +0100
parents d17772b0fe22
children 2bde6fc7aabd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #!/usr/bin/python
goffi@necton2
parents:
diff changeset
2 #-*- coding: utf-8 -*-
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
3
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
4 # SAT plugin for managing xep-0065
0
goffi@necton2
parents:
diff changeset
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)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1759
diff changeset
9 # 2009-2016 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
23
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
24 # --
0
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
30
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
31 # --
0
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
56
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 742
diff changeset
57 from sat.core.i18n import _
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 956
diff changeset
58 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 956
diff changeset
59 log = getLogger(__name__)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
60 from sat.core.constants import Const as C
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
61 from sat.core import exceptions
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
62 from sat.tools import sat_defer
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
63 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
64 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
65 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
66 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
67 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
68 from twisted.words.protocols.jabber import xmlstream
0
goffi@necton2
parents:
diff changeset
69 from twisted.protocols.basic import FileSender
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
70 from twisted.internet import defer
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
71 from collections import namedtuple
0
goffi@necton2
parents:
diff changeset
72 import struct
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
73 import hashlib
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
74 import uuid
0
goffi@necton2
parents:
diff changeset
75
15
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
76 from zope.interface import implements
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
77
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
78 try:
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
79 from twisted.words.protocols.xmlstream import XMPPHandler
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
80 except ImportError:
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
81 from wokkel.subprotocols import XMPPHandler
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
82
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
83 from wokkel import disco, iwokkel
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
84
0
goffi@necton2
parents:
diff changeset
85
goffi@necton2
parents:
diff changeset
86 PLUGIN_INFO = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
87 "name": "XEP 0065 Plugin",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
88 "import_name": "XEP-0065",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
89 "type": "XEP",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
90 "protocols": ["XEP-0065"],
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
91 "dependencies": ["IP"],
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
92 "recommendations": ["NAT-PORT"],
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
93 "main": "XEP_0065",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
94 "handler": "yes",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
95 "description": _("""Implementation of SOCKS5 Bytestreams""")
0
goffi@necton2
parents:
diff changeset
96 }
goffi@necton2
parents:
diff changeset
97
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
98 IQ_SET = '/iq[@type="set"]'
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
99 NS_BS = 'http://jabber.org/protocol/bytestreams'
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
100 BS_REQUEST = IQ_SET + '/query[@xmlns="' + NS_BS + '"]'
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 TIMER_KEY = 'timer'
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 DEFER_KEY = 'finished' # key of the deferred used to track session end
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 SERVER_STARTING_PORT = 0 # starting number for server port search (0 to ask automatic attribution)
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
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 # 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
106 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
107 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
108 PRIORITY_ASSISTED = 1000
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
109 PRIORITY_PROXY = 0.2 # proxy is the last option for s5b
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
110 CANDIDATE_DELAY = 0.2 # see XEP-0260 §4
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 CANDIDATE_DELAY_PROXY = 0.2 # additional time for proxy types (see XEP-0260 §4 note 3)
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
112
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
113 TIMEOUT = 300 # maxium time between session creation and stream start
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
114
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
115 # 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
116 # 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
117 # PARAMS = """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
118 # <params>
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
119 # <general>
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
120 # <category name="File Transfer">
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
121 # <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
122 # <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
123 # </category>
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
124 # </general>
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
125 # <individual>
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
126 # <category name="File Transfer">
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
127 # <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
128 # <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
129 # <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
130 # </category>
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
131 # </individual>
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
132 # </params>
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
133 # """
0
goffi@necton2
parents:
diff changeset
134
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
135 (STATE_INITIAL,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
136 STATE_AUTH,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
137 STATE_REQUEST,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
138 STATE_READY,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
139 STATE_AUTH_USERPASS,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
140 STATE_CLIENT_INITIAL,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
141 STATE_CLIENT_AUTH,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
142 STATE_CLIENT_REQUEST,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
143 ) = xrange(8)
0
goffi@necton2
parents:
diff changeset
144
goffi@necton2
parents:
diff changeset
145 SOCKS5_VER = 0x05
goffi@necton2
parents:
diff changeset
146
goffi@necton2
parents:
diff changeset
147 ADDR_IPV4 = 0x01
goffi@necton2
parents:
diff changeset
148 ADDR_DOMAINNAME = 0x03
goffi@necton2
parents:
diff changeset
149 ADDR_IPV6 = 0x04
goffi@necton2
parents:
diff changeset
150
goffi@necton2
parents:
diff changeset
151 CMD_CONNECT = 0x01
goffi@necton2
parents:
diff changeset
152 CMD_BIND = 0x02
goffi@necton2
parents:
diff changeset
153 CMD_UDPASSOC = 0x03
goffi@necton2
parents:
diff changeset
154
goffi@necton2
parents:
diff changeset
155 AUTHMECH_ANON = 0x00
goffi@necton2
parents:
diff changeset
156 AUTHMECH_USERPASS = 0x02
goffi@necton2
parents:
diff changeset
157 AUTHMECH_INVALID = 0xFF
goffi@necton2
parents:
diff changeset
158
goffi@necton2
parents:
diff changeset
159 REPLY_SUCCESS = 0x00
goffi@necton2
parents:
diff changeset
160 REPLY_GENERAL_FAILUR = 0x01
goffi@necton2
parents:
diff changeset
161 REPLY_CONN_NOT_ALLOWED = 0x02
goffi@necton2
parents:
diff changeset
162 REPLY_NETWORK_UNREACHABLE = 0x03
goffi@necton2
parents:
diff changeset
163 REPLY_HOST_UNREACHABLE = 0x04
goffi@necton2
parents:
diff changeset
164 REPLY_CONN_REFUSED = 0x05
goffi@necton2
parents:
diff changeset
165 REPLY_TTL_EXPIRED = 0x06
goffi@necton2
parents:
diff changeset
166 REPLY_CMD_NOT_SUPPORTED = 0x07
goffi@necton2
parents:
diff changeset
167 REPLY_ADDR_NOT_SUPPORTED = 0x08
goffi@necton2
parents:
diff changeset
168
goffi@necton2
parents:
diff changeset
169
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
170 ProxyInfos = namedtuple("ProxyInfos", ['host', 'jid', 'port'])
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
171
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
172
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
173 class Candidate(object):
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 def __init__(self, host, port, type_, priority, jid_, id_=None, priority_local=False, factory=None):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
176 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
177 @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
178 @param port(int): port
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
179 @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
180 @param priority(int): priority
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
181 @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
182 @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
183 @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
184 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
185 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
186 assert isinstance(jid_, jid.JID)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
187 self.host, self.port, self.type, self.jid = (
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
188 host, int(port), type_, jid_)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
189 self.id = id_ if id_ is not None else unicode(uuid.uuid4())
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
190 if priority_local:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
191 self._local_priority = int(priority)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
192 self._priority = self.calculatePriority()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
193 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
194 self._local_priority = 0
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
195 self._priority = int(priority)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
196 self.factory = factory
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 def discard(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
199 """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
200
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
201 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
202 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
203 log.debug(u"Discarding {}".format(self))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
204 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
205 self.factory.discard()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
206 except AttributeError:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
207 pass # no discard for Socks5ServerFactory
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
208
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
209 @property
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
210 def local_priority(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
211 return self._local_priority
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
212
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
213 @property
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
214 def priority(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
215 return self._priority
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
216
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
217 def __str__(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
218 # similar to __unicode__ but we don't show jid and we encode id
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
219 return "Candidate ({0.priority}): host={0.host} port={0.port} type={0.type}{id}".format(
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
220 self,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
221 id=u" id={}".format(self.id if self.id is not None else u'').encode('utf-8', 'ignore'),
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
222 )
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
223
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
224 def __unicode__(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
225 return u"Candidate ({0.priority}): host={0.host} port={0.port} jid={0.jid} type={0.type}{id}".format(
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
226 self,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
227 id=u" id={}".format(self.id if self.id is not None else u''),
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
228 )
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
229
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
230 def __eq__(self, other):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
231 # 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
232 # 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
233 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
234 return (self.host == other.host and
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
235 self.port == other.port and
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
236 self.jid == other.jid)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
237 except (AttributeError, TypeError):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
238 return False
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
239
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
240 def __ne__(self, other):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
241 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
242
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
243 def calculatePriority(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
244 """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
245
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
246
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
247 @return (int): priority
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
248 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
249 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
250 multiplier = 126
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
251 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
252 multiplier = 120
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
253 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
254 multiplier = 110
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
255 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
256 multiplier = 10
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
257 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
258 raise exceptions.InternalError(u"Unknown {} type !".format(self.type))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
259 return 2**16 * multiplier + self._local_priority
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
260
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
261 def activate(self, sid, peer_jid, client):
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
262 """Activate the proxy candidate
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
263
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
264 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
265 Must only be used with proxy candidates
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
266 @param sid(unicode): session id (same as for getSessionHash)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
267 @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
268 @return (D(domish.Element)): IQ result (or error)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
269 """
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
270 assert self.type == XEP_0065.TYPE_PROXY
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
271 iq_elt = client.IQ()
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
272 iq_elt['to'] = self.jid.full()
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
273 query_elt = iq_elt.addElement((NS_BS, 'query'))
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
274 query_elt['sid'] = sid
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
275 query_elt.addElement('activate', content=peer_jid.full())
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
276 return iq_elt.send()
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
277
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
278 def startTransfer(self, session_hash=None):
1581
8cc7d83141a4 plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents: 1577
diff changeset
279 if self.type == XEP_0065.TYPE_PROXY:
8cc7d83141a4 plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents: 1577
diff changeset
280 chunk_size = 4096 # Prosody's proxy reject bigger chunks by 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
281 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
282 chunk_size = None
8cc7d83141a4 plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents: 1577
diff changeset
283 self.factory.startTransfer(session_hash, chunk_size=chunk_size)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
284
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
285
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
286 def getSessionHash(requester_jid, target_jid, sid):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
287 """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
288
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
289 @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
290 @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
291 @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
292 @return (str): hash
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
293 """
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
294 return hashlib.sha1((sid + requester_jid.full() + target_jid.full()).encode('utf-8')).hexdigest()
0
goffi@necton2
parents:
diff changeset
295
goffi@necton2
parents:
diff changeset
296
goffi@necton2
parents:
diff changeset
297 class SOCKSv5(protocol.Protocol, FileSender):
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
298 CHUNK_SIZE = 2**16
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
299
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
300 def __init__(self, session_hash=None):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
301 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
302 @param session_hash(str): hash of the session
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
303 must only be used in client mode
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
304 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
305 self.connection = defer.Deferred() # called when connection/auth is done
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
306 if session_hash is not None:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
307 self.server_mode = False
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
308 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
309 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
310 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
311 self.server_mode = True
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
312 self.state = STATE_INITIAL
0
goffi@necton2
parents:
diff changeset
313 self.buf = ""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
314 self.supportedAuthMechs = [AUTHMECH_ANON]
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
315 self.supportedAddrs = [ADDR_DOMAINNAME]
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
316 self.enabledCommands = [CMD_CONNECT]
0
goffi@necton2
parents:
diff changeset
317 self.peersock = None
goffi@necton2
parents:
diff changeset
318 self.addressType = 0
goffi@necton2
parents:
diff changeset
319 self.requestType = 0
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
320 self._file_obj = None
1759
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
321 self.active = False # set to True when protocol is actually used for transfer
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
322 # 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
323
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
324 @property
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
325 def file_obj(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
326 if self._file_obj is None:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
327 if self.server_mode:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
328 self._file_obj = self.factory.getSession(self._session_hash)["file"]
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
329 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
330 self._file_obj = self.factory.getSession()['file']
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
331 return self._file_obj
0
goffi@necton2
parents:
diff changeset
332
goffi@necton2
parents:
diff changeset
333 def _startNegotiation(self):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
334 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
335 self.state = STATE_CLIENT_AUTH
0
goffi@necton2
parents:
diff changeset
336 self.transport.write(struct.pack('!3B', SOCKS5_VER, 1, AUTHMECH_ANON))
goffi@necton2
parents:
diff changeset
337
goffi@necton2
parents:
diff changeset
338 def _parseNegotiation(self):
goffi@necton2
parents:
diff changeset
339 try:
goffi@necton2
parents:
diff changeset
340 # Parse out data
goffi@necton2
parents:
diff changeset
341 ver, nmethod = struct.unpack('!BB', self.buf[:2])
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
342 methods = struct.unpack('%dB' % nmethod, self.buf[2:nmethod + 2])
0
goffi@necton2
parents:
diff changeset
343
goffi@necton2
parents:
diff changeset
344 # Ensure version is correct
goffi@necton2
parents:
diff changeset
345 if ver != 5:
goffi@necton2
parents:
diff changeset
346 self.transport.write(struct.pack('!BB', SOCKS5_VER, AUTHMECH_INVALID))
goffi@necton2
parents:
diff changeset
347 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
348 return
goffi@necton2
parents:
diff changeset
349
goffi@necton2
parents:
diff changeset
350 # Trim off front of the buffer
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
351 self.buf = self.buf[nmethod + 2:]
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
352
0
goffi@necton2
parents:
diff changeset
353 # Check for supported auth mechs
goffi@necton2
parents:
diff changeset
354 for m in self.supportedAuthMechs:
goffi@necton2
parents:
diff changeset
355 if m in methods:
goffi@necton2
parents:
diff changeset
356 # Update internal state, according to selected method
goffi@necton2
parents:
diff changeset
357 if m == AUTHMECH_ANON:
goffi@necton2
parents:
diff changeset
358 self.state = STATE_REQUEST
goffi@necton2
parents:
diff changeset
359 elif m == AUTHMECH_USERPASS:
goffi@necton2
parents:
diff changeset
360 self.state = STATE_AUTH_USERPASS
goffi@necton2
parents:
diff changeset
361 # Complete negotiation w/ this method
goffi@necton2
parents:
diff changeset
362 self.transport.write(struct.pack('!BB', SOCKS5_VER, m))
goffi@necton2
parents:
diff changeset
363 return
goffi@necton2
parents:
diff changeset
364
goffi@necton2
parents:
diff changeset
365 # No supported mechs found, notify client and close the connection
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
366 log.warning(u"Unsupported authentication mechanism")
0
goffi@necton2
parents:
diff changeset
367 self.transport.write(struct.pack('!BB', SOCKS5_VER, AUTHMECH_INVALID))
goffi@necton2
parents:
diff changeset
368 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
369 except struct.error:
goffi@necton2
parents:
diff changeset
370 pass
goffi@necton2
parents:
diff changeset
371
goffi@necton2
parents:
diff changeset
372 def _parseUserPass(self):
goffi@necton2
parents:
diff changeset
373 try:
goffi@necton2
parents:
diff changeset
374 # Parse out data
goffi@necton2
parents:
diff changeset
375 ver, ulen = struct.unpack('BB', self.buf[:2])
goffi@necton2
parents:
diff changeset
376 uname, = struct.unpack('%ds' % ulen, self.buf[2:ulen + 2])
goffi@necton2
parents:
diff changeset
377 plen, = struct.unpack('B', self.buf[ulen + 2])
goffi@necton2
parents:
diff changeset
378 password, = struct.unpack('%ds' % plen, self.buf[ulen + 3:ulen + 3 + plen])
goffi@necton2
parents:
diff changeset
379 # Trim off fron of the buffer
goffi@necton2
parents:
diff changeset
380 self.buf = self.buf[3 + ulen + plen:]
goffi@necton2
parents:
diff changeset
381 # Fire event to authenticate user
goffi@necton2
parents:
diff changeset
382 if self.authenticateUserPass(uname, password):
goffi@necton2
parents:
diff changeset
383 # Signal success
goffi@necton2
parents:
diff changeset
384 self.state = STATE_REQUEST
goffi@necton2
parents:
diff changeset
385 self.transport.write(struct.pack('!BB', SOCKS5_VER, 0x00))
goffi@necton2
parents:
diff changeset
386 else:
goffi@necton2
parents:
diff changeset
387 # Signal failure
goffi@necton2
parents:
diff changeset
388 self.transport.write(struct.pack('!BB', SOCKS5_VER, 0x01))
goffi@necton2
parents:
diff changeset
389 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
390 except struct.error:
goffi@necton2
parents:
diff changeset
391 pass
goffi@necton2
parents:
diff changeset
392
goffi@necton2
parents:
diff changeset
393 def sendErrorReply(self, errorcode):
goffi@necton2
parents:
diff changeset
394 # Any other address types are not supported
goffi@necton2
parents:
diff changeset
395 result = struct.pack('!BBBBIH', SOCKS5_VER, errorcode, 0, 1, 0, 0)
goffi@necton2
parents:
diff changeset
396 self.transport.write(result)
goffi@necton2
parents:
diff changeset
397 self.transport.loseConnection()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
398
0
goffi@necton2
parents:
diff changeset
399 def _parseRequest(self):
goffi@necton2
parents:
diff changeset
400 try:
goffi@necton2
parents:
diff changeset
401 # Parse out data and trim buffer accordingly
goffi@necton2
parents:
diff changeset
402 ver, cmd, rsvd, self.addressType = struct.unpack('!BBBB', self.buf[:4])
goffi@necton2
parents:
diff changeset
403
goffi@necton2
parents:
diff changeset
404 # Ensure we actually support the requested address type
goffi@necton2
parents:
diff changeset
405 if self.addressType not in self.supportedAddrs:
goffi@necton2
parents:
diff changeset
406 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
407 return
goffi@necton2
parents:
diff changeset
408
goffi@necton2
parents:
diff changeset
409 # Deal with addresses
goffi@necton2
parents:
diff changeset
410 if self.addressType == ADDR_IPV4:
goffi@necton2
parents:
diff changeset
411 addr, port = struct.unpack('!IH', self.buf[4:10])
goffi@necton2
parents:
diff changeset
412 self.buf = self.buf[10:]
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
413 elif self.addressType == ADDR_DOMAINNAME:
0
goffi@necton2
parents:
diff changeset
414 nlen = ord(self.buf[4])
goffi@necton2
parents:
diff changeset
415 addr, port = struct.unpack('!%dsH' % nlen, self.buf[5:])
goffi@necton2
parents:
diff changeset
416 self.buf = self.buf[7 + len(addr):]
goffi@necton2
parents:
diff changeset
417 else:
goffi@necton2
parents:
diff changeset
418 # Any other address types are not supported
goffi@necton2
parents:
diff changeset
419 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
420 return
goffi@necton2
parents:
diff changeset
421
goffi@necton2
parents:
diff changeset
422 # Ensure command is supported
goffi@necton2
parents:
diff changeset
423 if cmd not in self.enabledCommands:
goffi@necton2
parents:
diff changeset
424 # Send a not supported error
goffi@necton2
parents:
diff changeset
425 self.sendErrorReply(REPLY_CMD_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
426 return
goffi@necton2
parents:
diff changeset
427
goffi@necton2
parents:
diff changeset
428 # Process the command
goffi@necton2
parents:
diff changeset
429 if cmd == CMD_CONNECT:
goffi@necton2
parents:
diff changeset
430 self.connectRequested(addr, port)
goffi@necton2
parents:
diff changeset
431 elif cmd == CMD_BIND:
goffi@necton2
parents:
diff changeset
432 self.bindRequested(addr, port)
goffi@necton2
parents:
diff changeset
433 else:
goffi@necton2
parents:
diff changeset
434 # Any other command is not supported
goffi@necton2
parents:
diff changeset
435 self.sendErrorReply(REPLY_CMD_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
436
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
437 except struct.error:
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
438 # The buffer is probably not complete, we need to wait more
0
goffi@necton2
parents:
diff changeset
439 return None
goffi@necton2
parents:
diff changeset
440
goffi@necton2
parents:
diff changeset
441 def _makeRequest(self):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
442 hash_ = self._session_hash
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
443 request = struct.pack('!5B%dsH' % len(hash_), SOCKS5_VER, CMD_CONNECT, 0, ADDR_DOMAINNAME, len(hash_), hash_, 0)
0
goffi@necton2
parents:
diff changeset
444 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
445 self.state = STATE_CLIENT_REQUEST
0
goffi@necton2
parents:
diff changeset
446
goffi@necton2
parents:
diff changeset
447 def _parseRequestReply(self):
goffi@necton2
parents:
diff changeset
448 try:
goffi@necton2
parents:
diff changeset
449 ver, rep, rsvd, self.addressType = struct.unpack('!BBBB', self.buf[:4])
goffi@necton2
parents:
diff changeset
450 # Ensure we actually support the requested address type
goffi@necton2
parents:
diff changeset
451 if self.addressType not in self.supportedAddrs:
goffi@necton2
parents:
diff changeset
452 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
453 return
goffi@necton2
parents:
diff changeset
454
goffi@necton2
parents:
diff changeset
455 # Deal with addresses
goffi@necton2
parents:
diff changeset
456 if self.addressType == ADDR_IPV4:
goffi@necton2
parents:
diff changeset
457 addr, port = struct.unpack('!IH', self.buf[4:10])
goffi@necton2
parents:
diff changeset
458 self.buf = self.buf[10:]
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
459 elif self.addressType == ADDR_DOMAINNAME:
0
goffi@necton2
parents:
diff changeset
460 nlen = ord(self.buf[4])
goffi@necton2
parents:
diff changeset
461 addr, port = struct.unpack('!%dsH' % nlen, self.buf[5:])
goffi@necton2
parents:
diff changeset
462 self.buf = self.buf[7 + len(addr):]
goffi@necton2
parents:
diff changeset
463 else:
goffi@necton2
parents:
diff changeset
464 # Any other address types are not supported
goffi@necton2
parents:
diff changeset
465 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
466 return
goffi@necton2
parents:
diff changeset
467
goffi@necton2
parents:
diff changeset
468 # Ensure reply is OK
goffi@necton2
parents:
diff changeset
469 if rep != REPLY_SUCCESS:
goffi@necton2
parents:
diff changeset
470 self.loseConnection()
goffi@necton2
parents:
diff changeset
471 return
goffi@necton2
parents:
diff changeset
472
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
473 self.state = STATE_READY
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
474 self.connection.callback(None)
0
goffi@necton2
parents:
diff changeset
475
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
476 except struct.error:
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
477 # The buffer is probably not complete, we need to wait more
0
goffi@necton2
parents:
diff changeset
478 return None
goffi@necton2
parents:
diff changeset
479
goffi@necton2
parents:
diff changeset
480 def connectionMade(self):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
481 log.debug(u"Socks5 connectionMade (mode = {})".format("server" if self.state == STATE_INITIAL else "client"))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
482 if self.state == STATE_CLIENT_INITIAL:
0
goffi@necton2
parents:
diff changeset
483 self._startNegotiation()
goffi@necton2
parents:
diff changeset
484
goffi@necton2
parents:
diff changeset
485 def connectRequested(self, addr, port):
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
486 # Check that this session is expected
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
487 if not self.factory.addToSession(addr, self):
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
488 self.sendErrorReply(REPLY_CONN_REFUSED)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
489 log.warning(u"Unexpected connection request received from {host}"
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
490 .format(host=self.transport.getPeer().host))
0
goffi@necton2
parents:
diff changeset
491 return
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
492 self._session_hash = addr
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
493 self.connectCompleted(addr, 0)
0
goffi@necton2
parents:
diff changeset
494
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
495 def startTransfer(self, chunk_size):
8cc7d83141a4 plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents: 1577
diff changeset
496 """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
497
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
498 @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
499 """
1759
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
500 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
501 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
502 self.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
503 log.debug(u"Starting file transfer")
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
504 d = self.beginFileTransfer(self.file_obj, self.transport)
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
505 d.addCallback(self.fileTransfered)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
506
0
goffi@necton2
parents:
diff changeset
507 def fileTransfered(self, d):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 956
diff changeset
508 log.info(_("File transfer completed, closing connection"))
0
goffi@necton2
parents:
diff changeset
509 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
510
goffi@necton2
parents:
diff changeset
511 def connectCompleted(self, remotehost, remoteport):
goffi@necton2
parents:
diff changeset
512 if self.addressType == ADDR_IPV4:
goffi@necton2
parents:
diff changeset
513 result = struct.pack('!BBBBIH', SOCKS5_VER, REPLY_SUCCESS, 0, 1, remotehost, remoteport)
goffi@necton2
parents:
diff changeset
514 elif self.addressType == ADDR_DOMAINNAME:
goffi@necton2
parents:
diff changeset
515 result = struct.pack('!BBBBB%dsH' % len(remotehost), SOCKS5_VER, REPLY_SUCCESS, 0,
goffi@necton2
parents:
diff changeset
516 ADDR_DOMAINNAME, len(remotehost), remotehost, remoteport)
goffi@necton2
parents:
diff changeset
517 self.transport.write(result)
goffi@necton2
parents:
diff changeset
518 self.state = STATE_READY
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
519
0
goffi@necton2
parents:
diff changeset
520 def bindRequested(self, addr, port):
goffi@necton2
parents:
diff changeset
521 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
522
0
goffi@necton2
parents:
diff changeset
523 def authenticateUserPass(self, user, passwd):
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
524 # FIXME: implement authentication and remove the debug printing a password
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
525 log.debug(u"User/pass: %s/%s" % (user, passwd))
0
goffi@necton2
parents:
diff changeset
526 return True
goffi@necton2
parents:
diff changeset
527
goffi@necton2
parents:
diff changeset
528 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
529 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
530 # Everything is set, we just have to write the incoming data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
531 self.file_obj.write(buf)
1759
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
532 if not self.active:
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
533 self.active = True
0
goffi@necton2
parents:
diff changeset
534 return
goffi@necton2
parents:
diff changeset
535
goffi@necton2
parents:
diff changeset
536 self.buf = self.buf + buf
goffi@necton2
parents:
diff changeset
537 if self.state == STATE_INITIAL:
goffi@necton2
parents:
diff changeset
538 self._parseNegotiation()
goffi@necton2
parents:
diff changeset
539 if self.state == STATE_AUTH_USERPASS:
goffi@necton2
parents:
diff changeset
540 self._parseUserPass()
goffi@necton2
parents:
diff changeset
541 if self.state == STATE_REQUEST:
goffi@necton2
parents:
diff changeset
542 self._parseRequest()
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
543 if self.state == STATE_CLIENT_REQUEST:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
544 self._parseRequestReply()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
545 if self.state == STATE_CLIENT_AUTH:
0
goffi@necton2
parents:
diff changeset
546 ver, method = struct.unpack('!BB', buf)
goffi@necton2
parents:
diff changeset
547 self.buf = self.buf[2:]
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
548 if ver != SOCKS5_VER or method != AUTHMECH_ANON:
0
goffi@necton2
parents:
diff changeset
549 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
550 else:
goffi@necton2
parents:
diff changeset
551 self._makeRequest()
goffi@necton2
parents:
diff changeset
552
goffi@necton2
parents:
diff changeset
553 def connectionLost(self, reason):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
554 log.debug(u"Socks5 connection lost: {}".format(reason.value))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
555 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
556 self.connection.errback(reason)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
557 if self.server_mode :
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
558 self.factory.removeFromSession(self._session_hash, self, reason)
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
559
0
goffi@necton2
parents:
diff changeset
560
goffi@necton2
parents:
diff changeset
561 class Socks5ServerFactory(protocol.ServerFactory):
goffi@necton2
parents:
diff changeset
562 protocol = SOCKSv5
goffi@necton2
parents:
diff changeset
563
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
564 def __init__(self, parent):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
565 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
566 @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
567 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
568 self.parent = parent
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
569
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
570 def getSession(self, session_hash):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
571 return self.parent.getSession(session_hash, None)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
572
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
573 def startTransfer(self, session_hash, chunk_size=None):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
574 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
575 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
576 protocol = session['protocols'][0]
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
577 except (KeyError, IndexError):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
578 log.error(u"Can't start file transfer, can't find protocol")
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
579 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
580 session[TIMER_KEY].cancel()
1581
8cc7d83141a4 plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents: 1577
diff changeset
581 protocol.startTransfer(chunk_size)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
582
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
583 def addToSession(self, session_hash, protocol):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
584 """Check is session_hash is valid, and associate protocol with it
0
goffi@necton2
parents:
diff changeset
585
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
586 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
587 @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
588 @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
589 @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
590 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
591 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
592 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
593 except KeyError:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
594 return False
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
595 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
596 session_data.setdefault('protocols', []).append(protocol)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
597 return True
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
598
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
599 def removeFromSession(self, session_hash, protocol, reason):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
600 """Remove a protocol from session_data
0
goffi@necton2
parents:
diff changeset
601
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
602 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
603 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
604 @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
605 @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
606 @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
607 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
608 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
609 protocols = self.getSession(session_hash)['protocols']
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
610 protocols.remove(protocol)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
611 except (KeyError, ValueError):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
612 log.error(u"Protocol not found in session while it should be there")
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
613 else:
1759
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
614 if protocol.active:
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
615 # 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
616 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
617 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
618 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
619 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
620
0
goffi@necton2
parents:
diff changeset
621
goffi@necton2
parents:
diff changeset
622 class Socks5ClientFactory(protocol.ClientFactory):
goffi@necton2
parents:
diff changeset
623 protocol = SOCKSv5
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
624
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
625 def __init__(self, parent, session, session_hash, profile):
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
626 """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
627
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
628 @param session(dict): session data
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
629 @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
630 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
631 @param profile(unciode): %(doc_profile)s
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
632 """
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
633 self.session = session
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
634 self.session_hash = session_hash
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
635 self.profile = profile
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
636 self.connection = defer.Deferred()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
637 self._protocol_instance = None
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
638 self.connector = None
0
goffi@necton2
parents:
diff changeset
639
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
640 def discard(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
641 """Disconnect the client
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
642
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
643 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
644 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
645 self.connector.disconnect()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
646
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
647 def getSession(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
648 return self.session
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
649
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
650 def startTransfer(self, dummy=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
651 self.session[TIMER_KEY].cancel()
1581
8cc7d83141a4 plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
Goffi <goffi@goffi.org>
parents: 1577
diff changeset
652 self._protocol_instance.startTransfer(chunk_size)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
653
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
654 def clientConnectionFailed(self, connector, reason):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
655 log.debug(u"Connection failed")
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
656 self.connection.errback(reason)
0
goffi@necton2
parents:
diff changeset
657
goffi@necton2
parents:
diff changeset
658 def clientConnectionLost(self, connector, reason):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
659 log.debug(_(u"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
660 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
661 # 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
662 # 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
663 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
664 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
665 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
666 self.getSession()[DEFER_KEY].errback(reason)
1759
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
667 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
668
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
669 def buildProtocol(self, addr):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
670 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
671 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
672 p.factory = self
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
673 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
674 self._protocol_instance = p
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
675 return p
0
goffi@necton2
parents:
diff changeset
676
goffi@necton2
parents:
diff changeset
677
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
678 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
679 NAMESPACE = NS_BS
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
680 TYPE_DIRECT = 'direct'
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
681 TYPE_ASSISTED = 'assisted'
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
682 TYPE_TUNEL = 'tunel'
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
683 TYPE_PROXY = 'proxy'
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
684 Candidate = Candidate
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
685
0
goffi@necton2
parents:
diff changeset
686 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 956
diff changeset
687 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
688 self.host = host
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
689
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
690 # session data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
691 self.hash_profiles_map = {} # key: hash of the transfer session, value: session data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
692 self._cache_proxies = {} # key: server jid, value: proxy data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
693
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
694 # misc data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
695 self._server_factory = None
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
696 self._external_port = None
21
633c5ed65701 parameters: new button type (not finished)
Goffi <goffi@goffi.org>
parents: 20
diff changeset
697
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
698 # plugins shortcuts
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
699 self._ip = self.host.plugins['IP']
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
700 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
701 self._np = self.host.plugins['NAT-PORT']
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
702 except KeyError:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
703 log.debug(u"NAT Port plugin not available")
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
704 self._np = None
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
705
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
706 # parameters
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
707 # XXX: params are not used for now, but they may be used in the futur to force proxy/IP
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
708 # host.memory.updateParams(PARAMS)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
709
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 69
diff changeset
710 def getHandler(self, profile):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
711 return XEP_0065_handler(self)
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
712
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
713 def profileConnected(self, profile):
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
714 client = self.host.getClient(profile)
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
715 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
716 client._s5b_sessions = {}
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
717
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
718 def getSessionHash(self, from_jid, to_jid, sid):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
719 return getSessionHash(from_jid, to_jid, sid)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
720
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
721 def getSocks5ServerFactory(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
722 """Return server factory
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
723
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
724 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
725 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
726 """
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
727
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
728 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
729 self._server_factory = Socks5ServerFactory(self)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
730 for port in xrange(SERVER_STARTING_PORT, 65356):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
731 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
732 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
733 except internet_error.CannotListenError as e:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
734 log.debug(u"Cannot listen on port {port}: {err_msg}{err_num}".format(
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
735 port=port,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
736 err_msg=e.socketError.strerror,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
737 err_num=u' (error code: {})'.format(e.socketError.errno),
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
738 ))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
739 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
740 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
741 break
0
goffi@necton2
parents:
diff changeset
742
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
743 log.info(_("Socks5 Stream server launched on port {}").format(self._server_factory_port))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
744 return self._server_factory
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
745
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
746 @defer.inlineCallbacks
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
747 def getProxy(self, profile):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
748 """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
749
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
750 cache is used between profiles using the same server
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
751 @param profile: %(doc_profile)s
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
752 @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
753 or None if not acceptable proxy is found
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
754 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
755 def notFound(server):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
756 log.info(u"No proxy found on this server")
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
757 self._cache_proxies[server] = None
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
758 defer.returnValue(None)
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
759 client = self.host.getClient(profile)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
760 server = client.jid.host
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
761 try:
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
762 defer.returnValue(self._cache_proxies[server])
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
763 except KeyError:
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
764 pass
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
765 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
766 proxy = (yield self.host.findServiceEntities('proxy', 'bytestreams', profile=profile)).pop()
1675
fd143578fe89 plugin XEP-0065: fixed bad error handling
Goffi <goffi@goffi.org>
parents: 1629
diff changeset
767 except (defer.CancelledError, StopIteration):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
768 notFound(server)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
769 iq_elt = client.IQ('get')
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
770 iq_elt['to'] = proxy.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
771 iq_elt.addElement((NS_BS, 'query'))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
772
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
773 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
774 result_elt = yield iq_elt.send()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
775 except jabber_error.StanzaError as failure:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
776 log.warning(u"Error while requesting proxy info on {jid}: {error}"
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
777 .format(proxy.full(), failure))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
778 notFound(server)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
779
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
780 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
781 query_elt = result_elt.elements(NS_BS, 'query').next()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
782 streamhost_elt = query_elt.elements(NS_BS, 'streamhost').next()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
783 host = streamhost_elt['host']
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
784 jid_ = streamhost_elt['jid']
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
785 port = streamhost_elt['port']
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
786 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
787 raise KeyError
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
788 jid_ = jid.JID(jid_)
1742
244a605623d6 complete the Exception's list when catching JID error:
souliane <souliane@mailoo.org>
parents: 1675
diff changeset
789 except (StopIteration, KeyError, RuntimeError, jid.InvalidFormat, AttributeError):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
790 log.warning(u"Invalid proxy data received from {}".format(proxy.full()))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
791 notFound(server)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
792
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
793 proxy_infos = self._cache_proxies[server] = ProxyInfos(host, jid_, port)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
794 log.info(u"Proxy found: {}".format(proxy_infos))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
795 defer.returnValue(proxy_infos)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
796
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
797 @defer.inlineCallbacks
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
798 def _getNetworkData(self, client):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
799 """Retrieve information about network
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
800
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
801 @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
802 @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
803 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
804 self.getSocks5ServerFactory()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
805 local_port = self._server_factory_port
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
806 external_ip = yield self._ip.getExternalIP(client.profile)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
807 local_ips = yield self._ip.getLocalIPs(client.profile)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
808
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
809 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
810 if external_ip != local_ips[0]:
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
811 log.info(u"We are probably behind a NAT")
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
812 if self._np 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
813 log.warning(u"NAT port plugin not available, we can't map port")
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
814 else:
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
815 ext_port = yield self._np.mapPort(local_port, desc=u"SaT socks5 stream")
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
816 if ext_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
817 log.warning(u"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
818 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
819 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
820
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
821 defer.returnValue((local_port, self._external_port, local_ips, external_ip))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
822
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
823 @defer.inlineCallbacks
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
824 def getCandidates(self, profile):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
825 """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
826
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
827 @param profile: %(doc_profile)s
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
828 @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
829 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
830 client = self.host.getClient(profile)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
831 server_factory = yield self.getSocks5ServerFactory()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
832 local_port, ext_port, local_ips, external_ip = yield self._getNetworkData(client)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
833 proxy = yield self.getProxy(profile)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
834
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
835 # 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
836 candidates = []
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
837
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
838 # 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
839
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
840 # 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
841 ip = local_ips.pop(0)
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
842 candidates.append(Candidate(ip, local_port, XEP_0065.TYPE_DIRECT, PRIORITY_BEST_DIRECT, client.jid, priority_local=True, factory=server_factory))
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
843 for ip in local_ips:
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
844 candidates.append(Candidate(ip, local_port, XEP_0065.TYPE_DIRECT, PRIORITY_DIRECT, client.jid, priority_local=True, factory=server_factory))
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
845
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
846 # then the assisted one
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
847 if ext_port is not None:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
848 candidates.append(Candidate(external_ip, ext_port, XEP_0065.TYPE_ASSISTED, PRIORITY_ASSISTED, client.jid, priority_local=True, factory=server_factory))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
849
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
850 # finally the proxy
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
851 if proxy:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
852 candidates.append(Candidate(proxy.host, proxy.port, XEP_0065.TYPE_PROXY, PRIORITY_PROXY, proxy.jid, priority_local=True))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
853
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
854 # 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
855 candidates.sort(key=lambda c: c.priority, reverse=True)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
856 defer.returnValue(candidates)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
857
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
858 def _addConnector(self, connector, candidate):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
859 """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
860
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
861 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
862 @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
863 @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
864 @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
865 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
866 candidate.factory.connector = connector
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
867 return candidate.factory.connection
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
868
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
869 def connectCandidate(self, candidate, session_hash, peer_session_hash=None, delay=None, profile=C.PROF_KEY_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
870 """Connect to a candidate
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
871
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
872 Connection will be done with a Socks5ClientFactory
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
873 @param candidate(Candidate): candidate to connect to
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
874 @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
875 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
876 @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
877 None to use session_hash.
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
878 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
879 - 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
880 - when a peer connect to a proxy *he proposed himself*
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
881 in practice, peer_session_hash is only used by tryCandidates
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
882 @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
883 @param profile: %(doc_profile)s
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
884 @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
885 """
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
886 if peer_session_hash is None:
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
887 # 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
888 peer_session_hash = session_hash
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
889 session = self.getSession(session_hash, profile)
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
890 factory = Socks5ClientFactory(self, session, peer_session_hash, profile)
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
891 candidate.factory = factory
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
892 if delay is None:
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
893 d = defer.succeed(candidate.host)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
894 else:
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
895 d = sat_defer.DelayedDeferred(delay, candidate.host)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
896 d.addCallback(reactor.connectTCP, candidate.port, factory)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
897 d.addCallback(self._addConnector, candidate)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
898 return d
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
899
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
900 def tryCandidates(self, candidates, session_hash, peer_session_hash, connection_cb=None, connection_eb=None, profile=C.PROF_KEY_NONE):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
901 defers_list = []
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
902
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
903 for candidate in candidates:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
904 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
905 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
906 delay += CANDIDATE_DELAY_PROXY
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
907 d = self.connectCandidate(candidate, session_hash, peer_session_hash, delay, profile)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
908 if connection_cb is not None:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
909 d.addCallback(lambda dummy, candidate=candidate, profile=profile: connection_cb(candidate, profile))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
910 if connection_eb is not None:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
911 d.addErrback(connection_eb, candidate, profile)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
912 defers_list.append(d)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
913
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
914 return defers_list
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
915
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
916 def getBestCandidate(self, candidates, session_hash, peer_session_hash=None, profile=C.PROF_KEY_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
917 """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
918
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
919 @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
920 @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
921 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
922 @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
923 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
924 @param profile: %(doc_profile)s
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
925 @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
926 """
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
927 defer_candidates = None
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
928
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
929 def connectionCb(candidate, profile):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
930 log.info(u"Connection of {} successful".format(unicode(candidate)))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
931 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
932 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
933 if other_candidate.priority < candidate.priority:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
934 log.debug(u"Cancelling {}".format(other_candidate))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
935 defer_candidates[idx].cancel()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
936 except AttributeError:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
937 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
938
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
939 def connectionEb(failure, candidate, profile):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
940 if failure.check(defer.CancelledError):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
941 log.debug(u"Connection of {} has been cancelled".format(candidate))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
942 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
943 log.info(u"Connection of {candidate} Failed: {error}".format(
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
944 candidate = candidate,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
945 error = failure.value))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
946 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
947
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
948 def allTested(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
949 log.debug(u"All candidates have been tested")
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
950 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
951 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
952
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
953 defer_candidates = self.tryCandidates(candidates, session_hash, peer_session_hash, connectionCb, connectionEb, profile)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
954 d_list = defer.DeferredList(defer_candidates)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
955 d_list.addCallback(allTested)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
956 return d_list
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
957
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
958 def _timeOut(self, session_hash, client):
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
959 """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
960
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
961 @param session_hash(str): hash as returned by getSessionHash
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
962 @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
963 """
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 log.info(u"Socks5 Bytestream: TimeOut reached")
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
965 session = self.getSession(session_hash, client.profile)
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
966 session[DEFER_KEY].errback(exceptions.TimeOutError)
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
967
1629
a34d7f621944 plugin XEP-0065: renamed _killSession to killSession as it make sense to kill a session from an other plugin
Goffi <goffi@goffi.org>
parents: 1584
diff changeset
968 def killSession(self, reason, 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
969 """Clean the current session
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
970
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
971 @param session_hash(str): hash as returned by getSessionHash
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
972 @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
973 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
974 @param client: %(doc_client)s
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
975 @param reason(None, failure.Failure): None if eveything was fine, a failure 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
976 @return (None, failure.Failure): reason is returned
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
977 """
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
978 log.debug(u'Cleaning session with hash {hash}{id}: {reason}'.format(
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
979 hash=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
980 reason='' if reason is None else reason.value,
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
981 id='' if sid is None else u' (id: {})'.format(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
982 ))
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
983
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
984 try:
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
985 assert self.hash_profiles_map[session_hash] == client.profile
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
986 del self.hash_profiles_map[session_hash]
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
987 except KeyError:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
988 pass
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
989
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
990 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
991 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
992 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
993 except KeyError:
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
994 log.warning(u"Session id {} is unknown".format(sid))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
995
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
996 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
997 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
998 except KeyError:
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
999 log.warning(u"There is no session with this 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
1000 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
1001 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
1002 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
1003
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
1004 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
1005 session_data['timer'].cancel()
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
1006 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
1007 pass
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1008
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
1009 return reason
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
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 def startStream(self, file_obj, to_jid, sid, profile=C.PROF_KEY_NONE):
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1012 """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
1013
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1014 @param file_obj: file_obj to send
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1015 @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
1016 @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
1017 @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
1018 @param failureCb: method to call when something goes wrong
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1019 @param profile: %(doc_profile)s
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
1020 @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
1021 """
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
1022 client = self.host.getClient(profile)
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
1023 session_data = self._createSession(file_obj, to_jid, sid, True, client.profile)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1024
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
1025 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
1026
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
1027 def gotCandidates(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
1028 session_data['candidates'] = 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
1029 iq_elt = client.IQ()
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1030 iq_elt["from"] = client.jid.full()
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 iq_elt["to"] = to_jid.full()
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 query_elt = iq_elt.addElement((NS_BS, 'query'))
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1033 query_elt['mode'] = 'tcp'
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1034 query_elt['sid'] = sid
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
1035
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
1036 for candidate in 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
1037 streamhost = query_elt.addElement('streamhost')
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1038 streamhost['host'] = candidate.host
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1039 streamhost['port'] = str(candidate.port)
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1040 streamhost['jid'] = candidate.jid.full()
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
1041 log.debug(u"Candidate proposed: {}".format(candidate))
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
1042
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
1043 d = iq_elt.send()
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1044 args = [session_data, client]
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1045 d.addCallbacks(self._IQNegotiationCb, self._IQNegotiationEb, args, None, args)
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1046
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1047 self.getCandidates(profile).addCallback(gotCandidates)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1048 return session_data[DEFER_KEY]
0
goffi@necton2
parents:
diff changeset
1049
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
1050 def _IQNegotiationCb(self, iq_elt, session_data, client):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1051 """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
1052
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1053 @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
1054 @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
1055 @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
1056 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1057 try:
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
1058 query_elt = iq_elt.elements(NS_BS, 'query').next()
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1059 streamhost_used_elt = query_elt.elements(NS_BS, 'streamhost-used').next()
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1060 except StopIteration:
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1061 log.warning(u"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
1062 # FIXME: must clean session
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
1063 return
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1064
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
1065 streamhost_jid = jid.JID(streamhost_used_elt['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
1066 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
1067 candidate = (c for c in session_data['candidates'] if c.jid == streamhost_jid).next()
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1068 except StopIteration:
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1069 log.warning(u"Candidate [{jid}] is unknown !".format(jid=streamhost_jid.full()))
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1070 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
1071 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
1072 log.info(u"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
1073
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
1074 if candidate.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
1075 log.info(u"A Socks5 proxy is used")
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1076 d = self.connectCandidate(candidate, session_data['hash'], profile=client.profile)
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1077 d.addCallback(lambda dummy: candidate.activate(session_data['id'], session_data['peer_jid'], client))
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1078 d.addErrback(self._activationEb)
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1079 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
1080 d = defer.succeed(None)
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
1081
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
1082 d.addCallback(lambda dummy: candidate.startTransfer(session_data['hash']))
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
1083
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
1084 def _activationEb(self, failure):
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1085 log.warning(u"Proxy activation error: {}".format(failure.value))
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1086
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1087 def _IQNegotiationEb(self, stanza_err, session_data, client):
1583
d46aae87c03a plugins XEP-0047, XEP-0065, XEP-0096: fixed use of failure.condition insteand of failure.value + removed gettext use for local logs
Goffi <goffi@goffi.org>
parents: 1581
diff changeset
1088 log.warning(u"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
1089 # 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
1090
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1091 def createSession(self, *args, **kwargs):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1092 """like [_createSession] but return the session deferred instead of the whole session
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1093
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1094 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
1095 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1096 return self._createSession(*args, **kwargs)[DEFER_KEY]
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1097
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
1098 def _createSession(self, file_obj, to_jid, sid, requester=False, profile=C.PROF_KEY_NONE):
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1099 """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
1100
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1101 @param file_obj(file): File object where data will be written
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1102 @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
1103 @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
1104 @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
1105 @param profile: %(doc_profile)s
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1106 @return (dict): session data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1107 """
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
1108 client = self.host.getClient(profile)
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
1109 if sid in client.xep_0065_sid_session:
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1110 raise exceptions.ConflictError(u'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
1111 if requester:
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1112 session_hash = getSessionHash(client.jid, to_jid, 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
1113 session_data = self._registerHash(session_hash, file_obj, profile)
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1114 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
1115 session_hash = getSessionHash(to_jid, client.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
1116 session_d = defer.Deferred()
1629
a34d7f621944 plugin XEP-0065: renamed _killSession to killSession as it make sense to kill a session from an other plugin
Goffi <goffi@goffi.org>
parents: 1584
diff changeset
1117 session_d.addBoth(self.killSession, session_hash, sid, client)
1577
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1118 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
1119 DEFER_KEY: session_d,
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
1120 TIMER_KEY: reactor.callLater(TIMEOUT, self._timeOut, session_hash, 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
1121 }
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1122 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
1123 session_data.update(
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1124 {'id': 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
1125 'peer_jid': to_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
1126 'file': file_obj,
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1127 'hash': session_hash,
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1128 })
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1129
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1130 return session_data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1131
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1132 def getSession(self, session_hash, profile):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1133 """Return session data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1134
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1135 @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
1136 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
1137 @param profile(None, unicode): profile of the peer
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1138 None is used only if profile is unknown (this is only the case
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1139 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
1140 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
1141 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
1142 @return (dict): session data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1143 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1144 if profile is None:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1145 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1146 profile = self.hash_profiles_map[session_hash]
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1147 except KeyError as e:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1148 log.warning(u"The requested session doesn't exists !")
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1149 raise e
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1150 client = self.host.getClient(profile)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1151 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
1152
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1153 def registerHash(self, *args, **kwargs):
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
1154 """like [_registerHash] but resturn 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
1155 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
1156 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1157 return self._registerHash(*args, **kwargs)[DEFER_KEY]
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1158
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1159 def _registerHash(self, session_hash, file_obj, profile):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1160 """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
1161
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1162 @param session_hash(str): hash of the session
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
1163 @param file_obj(file, None): file-like object
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
1164 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
1165 @param profile: %(doc_profile)s
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1166 return (dict): session data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1167 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1168 client = self.host.getClient(profile)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1169 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
1170 session_d = defer.Deferred()
1629
a34d7f621944 plugin XEP-0065: renamed _killSession to killSession as it make sense to kill a session from an other plugin
Goffi <goffi@goffi.org>
parents: 1584
diff changeset
1171 session_d.addBoth(self.killSession, session_hash, None, client)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1172 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
1173 DEFER_KEY: session_d,
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
1174 TIMER_KEY: reactor.callLater(TIMEOUT, self._timeOut, session_hash, client),
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1175 }
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
1176
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
1177 if file_obj is not None:
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
1178 session_data['file'] = file_obj
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
1179
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
1180 assert session_hash not in self.hash_profiles_map
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
1181 self.hash_profiles_map[session_hash] = profile
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1182
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1183 return session_data
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
1184
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
1185 def associateFileObj(self, session_hash, file_obj, profile):
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
1186 """Associate a file obj with a session"""
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
1187 session_data = self.getSession(session_hash, profile)
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
1188 assert 'file' not in session_data
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
1189 session_data['file'] = file_obj
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
1190
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1191 def streamQuery(self, iq_elt, profile):
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
1192 log.debug(u"BS stream query")
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
1193 client = self.host.getClient(profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
1194
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1195 iq_elt.handled = True
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
1196
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
1197 query_elt = iq_elt.elements(NS_BS, 'query').next()
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1198 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
1199 sid = query_elt['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
1200 except KeyError:
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1201 log.warning(u"Invalid bystreams request received")
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1202 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
1203
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 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
1205 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
1206 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
1207
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
1208 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
1209 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
1210 except KeyError:
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 log.warning(u"Ignoring unexpected BS transfer: {}".format(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
1212 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
1213
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 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
1215
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
1216 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
1217 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
1218 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
1219 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
1220 host, port, jid_ = sh_elt['host'], sh_elt['port'], jid.JID(sh_elt['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
1221 except KeyError:
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1222 log.warning(u"malformed streamhost element")
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1223 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
1224 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
1225 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
1226 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
1227 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
1228 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
1229 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
1230
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 for candidate in 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
1232 log.info(u"Candidate proposed: {}".format(candidate))
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1233
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
1234 d = self.getBestCandidate(candidates, session_data['hash'], profile=profile)
1577
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1235 d.addCallback(self._ackStream, iq_elt, session_data, client)
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1236
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
1237 def _ackStream(self, candidate, iq_elt, session_data, client):
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1238 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
1239 log.info("No streamhost candidate worked, we have to end negotiation")
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1240 return client.sendError(iq_elt, 'item-not-found')
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 log.info(u"We choose: {}".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
1242 result_elt = xmlstream.toResponse(iq_elt, 'result')
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 query_elt = result_elt.addElement((NS_BS, 'query'))
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1244 query_elt['sid'] = session_data['id']
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1245 streamhost_used_elt = query_elt.addElement('streamhost-used')
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1246 streamhost_used_elt['jid'] = candidate.jid.full()
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1247 client.xmlstream.send(result_elt)
0
goffi@necton2
parents:
diff changeset
1248
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
1249
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1250 class XEP_0065_handler(XMPPHandler):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1251 implements(iwokkel.IDisco)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
1252
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1253 def __init__(self, plugin_parent):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1254 self.plugin_parent = plugin_parent
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1255 self.host = plugin_parent.host
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
1256
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1257 def connectionInitialized(self):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1258 self.xmlstream.addObserver(BS_REQUEST, self.plugin_parent.streamQuery, profile=self.parent.profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
1259
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1260 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1261 return [disco.DiscoFeature(NS_BS)]
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1262
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1263 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1264 return []