annotate src/plugins/plugin_xep_0065.py @ 2502:7ad5f2c4e34a

XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments: huge patch sorry :) many things are improved by this patch, notably: - updated to last protocol changes (urn:xmpp:jingle:apps:file-transfer:5 and urn:xmpp:hashes:2) - XEP-0234: file request implementation - XEP-0234: methods to parse and generate <file> element (can be used by other plugins easily) - XEP-0234: range data is now in a namedtuple - path and namespace can be specified when sending/requesting a file (not standard, but needed for file sharing) - better error/termination handling - trigger points to handle file requests by other plugins - preparation to use file plugins with components
author Goffi <goffi@goffi.org>
date Wed, 28 Feb 2018 18:28:39 +0100
parents e2a7bb875957
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1845
diff changeset
1 #!/usr/bin/env python2
0
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)
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
9 # 2009-2018 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
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
69 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
70 from collections import namedtuple
0
goffi@necton2
parents:
diff changeset
71 import struct
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
72 import hashlib
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
73 import uuid
0
goffi@necton2
parents:
diff changeset
74
15
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
75 from zope.interface import implements
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
76
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
77 try:
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
78 from twisted.words.protocols.xmlstream import XMPPHandler
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
79 except ImportError:
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
80 from wokkel.subprotocols import XMPPHandler
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
81
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
82 from wokkel import disco, iwokkel
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
83
0
goffi@necton2
parents:
diff changeset
84
goffi@necton2
parents:
diff changeset
85 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
86 C.PI_NAME: "XEP 0065 Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
87 C.PI_IMPORT_NAME: "XEP-0065",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
88 C.PI_TYPE: "XEP",
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
89 C.PI_MODES: C.PLUG_MODE_BOTH,
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
90 C.PI_PROTOCOLS: ["XEP-0065"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
91 C.PI_DEPENDENCIES: ["IP"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
92 C.PI_RECOMMENDATIONS: ["NAT-PORT"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
93 C.PI_MAIN: "XEP_0065",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
94 C.PI_HANDLER: "yes",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
95 C.PI_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
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
297 class SOCKSv5(protocol.Protocol):
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
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
320 self._stream_object = 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
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
325 def stream_object(self):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
326 if self._stream_object is None:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
327 self._stream_object = self.getSession()['stream_object']
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
328 if self.server_mode:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
329 self._stream_object.registerProducer(self.transport, True)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
330 return self._stream_object
0
goffi@necton2
parents:
diff changeset
331
1845
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
332 def getSession(self):
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
333 """Return session associated with this candidate
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
334
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
335 @return (dict): session data
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
336 """
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
337 if self.server_mode:
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
338 return self.factory.getSession(self._session_hash)
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
339 else:
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
340 return self.factory.getSession()
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
341
0
goffi@necton2
parents:
diff changeset
342 def _startNegotiation(self):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
343 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
344 self.state = STATE_CLIENT_AUTH
0
goffi@necton2
parents:
diff changeset
345 self.transport.write(struct.pack('!3B', SOCKS5_VER, 1, AUTHMECH_ANON))
goffi@necton2
parents:
diff changeset
346
goffi@necton2
parents:
diff changeset
347 def _parseNegotiation(self):
goffi@necton2
parents:
diff changeset
348 try:
goffi@necton2
parents:
diff changeset
349 # Parse out data
goffi@necton2
parents:
diff changeset
350 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
351 methods = struct.unpack('%dB' % nmethod, self.buf[2:nmethod + 2])
0
goffi@necton2
parents:
diff changeset
352
goffi@necton2
parents:
diff changeset
353 # Ensure version is correct
goffi@necton2
parents:
diff changeset
354 if ver != 5:
goffi@necton2
parents:
diff changeset
355 self.transport.write(struct.pack('!BB', SOCKS5_VER, AUTHMECH_INVALID))
goffi@necton2
parents:
diff changeset
356 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
357 return
goffi@necton2
parents:
diff changeset
358
goffi@necton2
parents:
diff changeset
359 # Trim off front of the buffer
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
360 self.buf = self.buf[nmethod + 2:]
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
361
0
goffi@necton2
parents:
diff changeset
362 # Check for supported auth mechs
goffi@necton2
parents:
diff changeset
363 for m in self.supportedAuthMechs:
goffi@necton2
parents:
diff changeset
364 if m in methods:
goffi@necton2
parents:
diff changeset
365 # Update internal state, according to selected method
goffi@necton2
parents:
diff changeset
366 if m == AUTHMECH_ANON:
goffi@necton2
parents:
diff changeset
367 self.state = STATE_REQUEST
goffi@necton2
parents:
diff changeset
368 elif m == AUTHMECH_USERPASS:
goffi@necton2
parents:
diff changeset
369 self.state = STATE_AUTH_USERPASS
goffi@necton2
parents:
diff changeset
370 # Complete negotiation w/ this method
goffi@necton2
parents:
diff changeset
371 self.transport.write(struct.pack('!BB', SOCKS5_VER, m))
goffi@necton2
parents:
diff changeset
372 return
goffi@necton2
parents:
diff changeset
373
goffi@necton2
parents:
diff changeset
374 # 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
375 log.warning(u"Unsupported authentication mechanism")
0
goffi@necton2
parents:
diff changeset
376 self.transport.write(struct.pack('!BB', SOCKS5_VER, AUTHMECH_INVALID))
goffi@necton2
parents:
diff changeset
377 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
378 except struct.error:
goffi@necton2
parents:
diff changeset
379 pass
goffi@necton2
parents:
diff changeset
380
goffi@necton2
parents:
diff changeset
381 def _parseUserPass(self):
goffi@necton2
parents:
diff changeset
382 try:
goffi@necton2
parents:
diff changeset
383 # Parse out data
goffi@necton2
parents:
diff changeset
384 ver, ulen = struct.unpack('BB', self.buf[:2])
goffi@necton2
parents:
diff changeset
385 uname, = struct.unpack('%ds' % ulen, self.buf[2:ulen + 2])
goffi@necton2
parents:
diff changeset
386 plen, = struct.unpack('B', self.buf[ulen + 2])
goffi@necton2
parents:
diff changeset
387 password, = struct.unpack('%ds' % plen, self.buf[ulen + 3:ulen + 3 + plen])
goffi@necton2
parents:
diff changeset
388 # Trim off fron of the buffer
goffi@necton2
parents:
diff changeset
389 self.buf = self.buf[3 + ulen + plen:]
goffi@necton2
parents:
diff changeset
390 # Fire event to authenticate user
goffi@necton2
parents:
diff changeset
391 if self.authenticateUserPass(uname, password):
goffi@necton2
parents:
diff changeset
392 # Signal success
goffi@necton2
parents:
diff changeset
393 self.state = STATE_REQUEST
goffi@necton2
parents:
diff changeset
394 self.transport.write(struct.pack('!BB', SOCKS5_VER, 0x00))
goffi@necton2
parents:
diff changeset
395 else:
goffi@necton2
parents:
diff changeset
396 # Signal failure
goffi@necton2
parents:
diff changeset
397 self.transport.write(struct.pack('!BB', SOCKS5_VER, 0x01))
goffi@necton2
parents:
diff changeset
398 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
399 except struct.error:
goffi@necton2
parents:
diff changeset
400 pass
goffi@necton2
parents:
diff changeset
401
goffi@necton2
parents:
diff changeset
402 def sendErrorReply(self, errorcode):
goffi@necton2
parents:
diff changeset
403 # Any other address types are not supported
goffi@necton2
parents:
diff changeset
404 result = struct.pack('!BBBBIH', SOCKS5_VER, errorcode, 0, 1, 0, 0)
goffi@necton2
parents:
diff changeset
405 self.transport.write(result)
goffi@necton2
parents:
diff changeset
406 self.transport.loseConnection()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
407
0
goffi@necton2
parents:
diff changeset
408 def _parseRequest(self):
goffi@necton2
parents:
diff changeset
409 try:
goffi@necton2
parents:
diff changeset
410 # Parse out data and trim buffer accordingly
goffi@necton2
parents:
diff changeset
411 ver, cmd, rsvd, self.addressType = struct.unpack('!BBBB', self.buf[:4])
goffi@necton2
parents:
diff changeset
412
goffi@necton2
parents:
diff changeset
413 # Ensure we actually support the requested address type
goffi@necton2
parents:
diff changeset
414 if self.addressType not in self.supportedAddrs:
goffi@necton2
parents:
diff changeset
415 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
416 return
goffi@necton2
parents:
diff changeset
417
goffi@necton2
parents:
diff changeset
418 # Deal with addresses
goffi@necton2
parents:
diff changeset
419 if self.addressType == ADDR_IPV4:
goffi@necton2
parents:
diff changeset
420 addr, port = struct.unpack('!IH', self.buf[4:10])
goffi@necton2
parents:
diff changeset
421 self.buf = self.buf[10:]
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
422 elif self.addressType == ADDR_DOMAINNAME:
0
goffi@necton2
parents:
diff changeset
423 nlen = ord(self.buf[4])
goffi@necton2
parents:
diff changeset
424 addr, port = struct.unpack('!%dsH' % nlen, self.buf[5:])
goffi@necton2
parents:
diff changeset
425 self.buf = self.buf[7 + len(addr):]
goffi@necton2
parents:
diff changeset
426 else:
goffi@necton2
parents:
diff changeset
427 # Any other address types are not supported
goffi@necton2
parents:
diff changeset
428 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
429 return
goffi@necton2
parents:
diff changeset
430
goffi@necton2
parents:
diff changeset
431 # Ensure command is supported
goffi@necton2
parents:
diff changeset
432 if cmd not in self.enabledCommands:
goffi@necton2
parents:
diff changeset
433 # Send a not supported error
goffi@necton2
parents:
diff changeset
434 self.sendErrorReply(REPLY_CMD_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
435 return
goffi@necton2
parents:
diff changeset
436
goffi@necton2
parents:
diff changeset
437 # Process the command
goffi@necton2
parents:
diff changeset
438 if cmd == CMD_CONNECT:
goffi@necton2
parents:
diff changeset
439 self.connectRequested(addr, port)
goffi@necton2
parents:
diff changeset
440 elif cmd == CMD_BIND:
goffi@necton2
parents:
diff changeset
441 self.bindRequested(addr, port)
goffi@necton2
parents:
diff changeset
442 else:
goffi@necton2
parents:
diff changeset
443 # Any other command is not supported
goffi@necton2
parents:
diff changeset
444 self.sendErrorReply(REPLY_CMD_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
445
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
446 except struct.error:
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
447 # The buffer is probably not complete, we need to wait more
0
goffi@necton2
parents:
diff changeset
448 return None
goffi@necton2
parents:
diff changeset
449
goffi@necton2
parents:
diff changeset
450 def _makeRequest(self):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
451 hash_ = self._session_hash
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
452 request = struct.pack('!5B%dsH' % len(hash_), SOCKS5_VER, CMD_CONNECT, 0, ADDR_DOMAINNAME, len(hash_), hash_, 0)
0
goffi@necton2
parents:
diff changeset
453 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
454 self.state = STATE_CLIENT_REQUEST
0
goffi@necton2
parents:
diff changeset
455
goffi@necton2
parents:
diff changeset
456 def _parseRequestReply(self):
goffi@necton2
parents:
diff changeset
457 try:
goffi@necton2
parents:
diff changeset
458 ver, rep, rsvd, self.addressType = struct.unpack('!BBBB', self.buf[:4])
goffi@necton2
parents:
diff changeset
459 # Ensure we actually support the requested address type
goffi@necton2
parents:
diff changeset
460 if self.addressType not in self.supportedAddrs:
goffi@necton2
parents:
diff changeset
461 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
462 return
goffi@necton2
parents:
diff changeset
463
goffi@necton2
parents:
diff changeset
464 # Deal with addresses
goffi@necton2
parents:
diff changeset
465 if self.addressType == ADDR_IPV4:
goffi@necton2
parents:
diff changeset
466 addr, port = struct.unpack('!IH', self.buf[4:10])
goffi@necton2
parents:
diff changeset
467 self.buf = self.buf[10:]
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
468 elif self.addressType == ADDR_DOMAINNAME:
0
goffi@necton2
parents:
diff changeset
469 nlen = ord(self.buf[4])
goffi@necton2
parents:
diff changeset
470 addr, port = struct.unpack('!%dsH' % nlen, self.buf[5:])
goffi@necton2
parents:
diff changeset
471 self.buf = self.buf[7 + len(addr):]
goffi@necton2
parents:
diff changeset
472 else:
goffi@necton2
parents:
diff changeset
473 # Any other address types are not supported
goffi@necton2
parents:
diff changeset
474 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
475 return
goffi@necton2
parents:
diff changeset
476
goffi@necton2
parents:
diff changeset
477 # Ensure reply is OK
goffi@necton2
parents:
diff changeset
478 if rep != REPLY_SUCCESS:
goffi@necton2
parents:
diff changeset
479 self.loseConnection()
goffi@necton2
parents:
diff changeset
480 return
goffi@necton2
parents:
diff changeset
481
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
482 self.state = STATE_READY
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
483 self.connection.callback(None)
0
goffi@necton2
parents:
diff changeset
484
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
485 except struct.error:
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
486 # The buffer is probably not complete, we need to wait more
0
goffi@necton2
parents:
diff changeset
487 return None
goffi@necton2
parents:
diff changeset
488
goffi@necton2
parents:
diff changeset
489 def connectionMade(self):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
490 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
491 if self.state == STATE_CLIENT_INITIAL:
0
goffi@necton2
parents:
diff changeset
492 self._startNegotiation()
goffi@necton2
parents:
diff changeset
493
goffi@necton2
parents:
diff changeset
494 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
495 # 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
496 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
497 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
498 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
499 .format(host=self.transport.getPeer().host))
0
goffi@necton2
parents:
diff changeset
500 return
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
501 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
502 self.connectCompleted(addr, 0)
0
goffi@necton2
parents:
diff changeset
503
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
504 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
505 """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
506
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
507 @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
508 """
1759
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
509 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
510 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
511 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
512 log.debug(u"Starting file transfer")
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
513 d = self.stream_object.startStream(self.transport)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
514 d.addCallback(self.streamFinished)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
515
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
516 def streamFinished(self, d):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 956
diff changeset
517 log.info(_("File transfer completed, closing connection"))
0
goffi@necton2
parents:
diff changeset
518 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
519
goffi@necton2
parents:
diff changeset
520 def connectCompleted(self, remotehost, remoteport):
goffi@necton2
parents:
diff changeset
521 if self.addressType == ADDR_IPV4:
goffi@necton2
parents:
diff changeset
522 result = struct.pack('!BBBBIH', SOCKS5_VER, REPLY_SUCCESS, 0, 1, remotehost, remoteport)
goffi@necton2
parents:
diff changeset
523 elif self.addressType == ADDR_DOMAINNAME:
goffi@necton2
parents:
diff changeset
524 result = struct.pack('!BBBBB%dsH' % len(remotehost), SOCKS5_VER, REPLY_SUCCESS, 0,
goffi@necton2
parents:
diff changeset
525 ADDR_DOMAINNAME, len(remotehost), remotehost, remoteport)
goffi@necton2
parents:
diff changeset
526 self.transport.write(result)
goffi@necton2
parents:
diff changeset
527 self.state = STATE_READY
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
528
0
goffi@necton2
parents:
diff changeset
529 def bindRequested(self, addr, port):
goffi@necton2
parents:
diff changeset
530 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
531
0
goffi@necton2
parents:
diff changeset
532 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
533 # 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
534 log.debug(u"User/pass: %s/%s" % (user, passwd))
0
goffi@necton2
parents:
diff changeset
535 return True
goffi@necton2
parents:
diff changeset
536
goffi@necton2
parents:
diff changeset
537 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
538 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
539 # Everything is set, we just have to write the incoming data
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
540 self.stream_object.write(buf)
1759
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
541 if not self.active:
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
542 self.active = True
1845
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
543 self.getSession()[TIMER_KEY].cancel()
0
goffi@necton2
parents:
diff changeset
544 return
goffi@necton2
parents:
diff changeset
545
goffi@necton2
parents:
diff changeset
546 self.buf = self.buf + buf
goffi@necton2
parents:
diff changeset
547 if self.state == STATE_INITIAL:
goffi@necton2
parents:
diff changeset
548 self._parseNegotiation()
goffi@necton2
parents:
diff changeset
549 if self.state == STATE_AUTH_USERPASS:
goffi@necton2
parents:
diff changeset
550 self._parseUserPass()
goffi@necton2
parents:
diff changeset
551 if self.state == STATE_REQUEST:
goffi@necton2
parents:
diff changeset
552 self._parseRequest()
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
553 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
554 self._parseRequestReply()
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_CLIENT_AUTH:
0
goffi@necton2
parents:
diff changeset
556 ver, method = struct.unpack('!BB', buf)
goffi@necton2
parents:
diff changeset
557 self.buf = self.buf[2:]
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
558 if ver != SOCKS5_VER or method != AUTHMECH_ANON:
0
goffi@necton2
parents:
diff changeset
559 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
560 else:
goffi@necton2
parents:
diff changeset
561 self._makeRequest()
goffi@necton2
parents:
diff changeset
562
goffi@necton2
parents:
diff changeset
563 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
564 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
565 if self.state != STATE_READY:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
566 self.connection.errback(reason)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
567 if self.server_mode :
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
568 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
569
0
goffi@necton2
parents:
diff changeset
570
goffi@necton2
parents:
diff changeset
571 class Socks5ServerFactory(protocol.ServerFactory):
goffi@necton2
parents:
diff changeset
572 protocol = SOCKSv5
goffi@necton2
parents:
diff changeset
573
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
574 def __init__(self, parent):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
575 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
576 @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
577 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
578 self.parent = parent
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
579
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
580 def getSession(self, session_hash):
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
581 return self.parent.getSession(None, session_hash)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
582
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
583 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
584 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
585 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
586 protocol = session['protocols'][0]
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
587 except (KeyError, IndexError):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
588 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
589 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
590 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
591 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
592
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
593 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
594 """Check is session_hash is valid, and associate protocol with it
0
goffi@necton2
parents:
diff changeset
595
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
596 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
597 @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
598 @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
599 @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
600 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
601 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
602 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
603 except KeyError:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
604 return False
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
605 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
606 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
607 return True
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
608
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
609 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
610 """Remove a protocol from session_data
0
goffi@necton2
parents:
diff changeset
611
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
612 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
613 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
614 @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
615 @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
616 @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
617 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
618 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
619 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
620 protocols.remove(protocol)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
621 except (KeyError, ValueError):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
622 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
623 else:
1759
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
624 if protocol.active:
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
625 # 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
626 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
627 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
628 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
629 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
630
0
goffi@necton2
parents:
diff changeset
631
goffi@necton2
parents:
diff changeset
632 class Socks5ClientFactory(protocol.ClientFactory):
goffi@necton2
parents:
diff changeset
633 protocol = SOCKSv5
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
634
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
635 def __init__(self, client, parent, session, session_hash):
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
636 """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
637
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
638 @param session(dict): session data
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
639 @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
640 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
641 """
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
642 self.session = session
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
643 self.session_hash = session_hash
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
644 self.client = client
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
645 self.connection = defer.Deferred()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
646 self._protocol_instance = None
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
647 self.connector = None
0
goffi@necton2
parents:
diff changeset
648
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
649 def discard(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
650 """Disconnect the client
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
651
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
652 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
653 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
654 self.connector.disconnect()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
655
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
656 def getSession(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
657 return self.session
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
658
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
659 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
660 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
661 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
662
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
663 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
664 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
665 self.connection.errback(reason)
0
goffi@necton2
parents:
diff changeset
666
goffi@necton2
parents:
diff changeset
667 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
668 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
669 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
670 # 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
671 # 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
672 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
673 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
674 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
675 self.getSession()[DEFER_KEY].errback(reason)
1759
81923b3f8b14 plugin XEP-0065: better handling of finished Deferred
Goffi <goffi@goffi.org>
parents: 1758
diff changeset
676 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
677
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
678 def buildProtocol(self, addr):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
679 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
680 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
681 p.factory = self
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
682 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
683 self._protocol_instance = p
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
684 return p
0
goffi@necton2
parents:
diff changeset
685
goffi@necton2
parents:
diff changeset
686
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
687 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
688 NAMESPACE = NS_BS
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
689 TYPE_DIRECT = 'direct'
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
690 TYPE_ASSISTED = 'assisted'
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
691 TYPE_TUNEL = 'tunel'
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
692 TYPE_PROXY = 'proxy'
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
693 Candidate = Candidate
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
694
0
goffi@necton2
parents:
diff changeset
695 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 956
diff changeset
696 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
697 self.host = host
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
698
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
699 # session data
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
700 self.hash_clients_map = {} # key: hash of the transfer session, value: session data
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
701 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
702
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
703 # misc data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
704 self._server_factory = None
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
705 self._external_port = None
21
633c5ed65701 parameters: new button type (not finished)
Goffi <goffi@goffi.org>
parents: 20
diff changeset
706
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
707 # plugins shortcuts
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
708 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
709 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
710 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
711 except KeyError:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
712 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
713 self._np = None
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
714
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
715 # parameters
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
716 # 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
717 # host.memory.updateParams(PARAMS)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
718
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
719 def getHandler(self, client):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
720 return XEP_0065_handler(self)
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
721
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
722 def profileConnected(self, client):
1577
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
723 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
724 client._s5b_sessions = {}
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
725
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
726 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
727 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
728
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
729 def getSocks5ServerFactory(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
730 """Return server factory
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
731
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
732 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
733 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
734 """
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 536
diff changeset
735
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
736 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
737 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
738 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
739 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
740 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
741 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
742 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
743 port=port,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
744 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
745 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
746 ))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
747 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
748 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
749 break
0
goffi@necton2
parents:
diff changeset
750
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
751 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
752 return self._server_factory
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
753
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
754 @defer.inlineCallbacks
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
755 def getProxy(self, client):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
756 """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
757
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
758 cache is used between clients using the same server
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
759 @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
760 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
761 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
762 def notFound(server):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
763 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
764 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
765 defer.returnValue(None)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
766 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
767 try:
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
768 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
769 except KeyError:
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
770 pass
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
771 try:
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
772 proxy = (yield self.host.findServiceEntities(client, 'proxy', 'bytestreams')).pop()
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
773 except (defer.CancelledError, StopIteration, KeyError):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
774 notFound(server)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
775 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
776 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
777 iq_elt.addElement((NS_BS, 'query'))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
778
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
779 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
780 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
781 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
782 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
783 .format(proxy.full(), failure))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
784 notFound(server)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
785
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
786 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
787 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
788 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
789 host = streamhost_elt['host']
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
790 jid_ = streamhost_elt['jid']
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
791 port = streamhost_elt['port']
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
792 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
793 raise KeyError
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
794 jid_ = jid.JID(jid_)
1742
244a605623d6 complete the Exception's list when catching JID error:
souliane <souliane@mailoo.org>
parents: 1675
diff changeset
795 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
796 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
797 notFound(server)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
798
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
799 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
800 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
801 defer.returnValue(proxy_infos)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
802
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
803 @defer.inlineCallbacks
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
804 def _getNetworkData(self, client):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
805 """Retrieve information about network
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
806
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
807 @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
808 @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
809 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
810 self.getSocks5ServerFactory()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
811 local_port = self._server_factory_port
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
812 external_ip = yield self._ip.getExternalIP(client)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
813 local_ips = yield self._ip.getLocalIPs(client)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
814
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
815 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
816 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
817 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
818 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
819 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
820 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
821 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
822 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
823 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
824 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
825 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
826
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
827 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
828
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
829 @defer.inlineCallbacks
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
830 def getCandidates(self, client):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
831 """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
832
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
833 @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
834 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
835 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
836 local_port, ext_port, local_ips, external_ip = yield self._getNetworkData(client)
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
837 proxy = yield self.getProxy(client)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
838
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
839 # 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
840 candidates = []
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
841
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
842 # 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
843
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 # 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
845 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
846 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
847 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
848 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
849
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
850 # then the assisted one
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
851 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
852 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
853
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
854 # finally the proxy
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
855 if proxy:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
856 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
857
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
858 # 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
859 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
860 defer.returnValue(candidates)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
861
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
862 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
863 """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
864
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
865 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
866 @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
867 @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
868 @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
869 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
870 candidate.factory.connector = connector
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
871 return candidate.factory.connection
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
872
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
873 def connectCandidate(self, client, candidate, session_hash, peer_session_hash=None, delay=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
874 """Connect to a candidate
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
875
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
876 Connection will be done with a Socks5ClientFactory
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
877 @param candidate(Candidate): candidate to connect to
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
878 @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
879 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
880 @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
881 None to use session_hash.
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
882 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
883 - 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
884 - 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
885 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
886 @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
887 @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
888 """
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
889 if peer_session_hash is None:
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
890 # 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
891 peer_session_hash = session_hash
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
892 session = self.getSession(client, session_hash)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
893 factory = Socks5ClientFactory(client, self, session, peer_session_hash)
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
894 candidate.factory = factory
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
895 if delay is None:
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
896 d = defer.succeed(candidate.host)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
897 else:
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
898 d = sat_defer.DelayedDeferred(delay, candidate.host)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
899 d.addCallback(reactor.connectTCP, candidate.port, factory)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
900 d.addCallback(self._addConnector, candidate)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
901 return d
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1569
diff changeset
902
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
903 def tryCandidates(self, client, candidates, session_hash, peer_session_hash, connection_cb=None, connection_eb=None):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
904 defers_list = []
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
905
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
906 for candidate in candidates:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
907 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
908 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
909 delay += CANDIDATE_DELAY_PROXY
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
910 d = self.connectCandidate(client, candidate, session_hash, peer_session_hash, delay)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
911 if connection_cb is not None:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
912 d.addCallback(lambda dummy, candidate=candidate, client=client: connection_cb(client, candidate))
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
913 if connection_eb is not None:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
914 d.addErrback(connection_eb, client, candidate)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
915 defers_list.append(d)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
916
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
917 return defers_list
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
918
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
919 def getBestCandidate(self, client, candidates, session_hash, peer_session_hash=None):
1577
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
920 """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
921
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
922 @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
923 @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
924 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
925 @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
926 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
927 @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
928 """
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
929 defer_candidates = None
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
930
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
931 def connectionCb(client, candidate):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
932 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
933 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
934 try:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
935 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
936 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
937 defer_candidates[idx].cancel()
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
938 except AttributeError:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
939 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
940
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
941 def connectionEb(failure, client, candidate):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
942 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
943 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
944 else:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
945 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
946 candidate = candidate,
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
947 error = failure.value))
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
948 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
949
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
950 def allTested(self):
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
951 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
952 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
953 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
954
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
955 defer_candidates = self.tryCandidates(client, candidates, session_hash, peer_session_hash, connectionCb, connectionEb)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
956 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
957 d_list.addCallback(allTested)
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
958 return d_list
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
959
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
960 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
961 """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
962
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
963 @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
964 @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
965 """
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 log.info(u"Socks5 Bytestream: TimeOut reached")
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
967 session = self.getSession(client, 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
968 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
969
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
970 def killSession(self, failure_, session_hash, sid, client):
1584
b57b4683dc33 plugin XEP-0065: session cleaning and timeout + log choosed candidate when using SI File Transfer
Goffi <goffi@goffi.org>
parents: 1583
diff changeset
971 """Clean the current session
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
972
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
973 @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
974 @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
975 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
976 @param client: %(doc_client)s
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
977 @param failure_(None, failure.Failure): None if eveything was fine, a failure else
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
978 @return (None, failure.Failure): failure_ is returned
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
979 """
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
980 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
981 hash=session_hash,
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
982 reason='' if failure_ is None else failure_.value,
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
983 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
984 ))
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
985
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
986 try:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
987 assert self.hash_clients_map[session_hash] == client
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
988 del self.hash_clients_map[session_hash]
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
989 except KeyError:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
990 pass
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
991
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
992 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
993 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
994 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
995 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
996 log.warning(u"Session id {} is unknown".format(sid))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
997
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
998 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
999 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
1000 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
1001 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
1002 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
1003 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
1004 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
1005
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 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
1007 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
1008 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
1009 pass
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1010
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
1011 return failure_
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1012
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1013 def startStream(self, client, stream_object, to_jid, sid):
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1014 """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
1015
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1016 @param streamProducer: stream_object to use
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1017 @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
1018 @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
1019 @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
1020 @param failureCb: method to call when something goes wrong
1577
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1021 @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
1022 """
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1023 session_data = self._createSession(client, stream_object, to_jid, sid, True)
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
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
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1047 self.getCandidates(client).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")
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1076 d = self.connectCandidate(client, candidate, session_data['hash'])
1577
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
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
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1098 def _createSession(self, client, stream_object, to_jid, sid, requester=False):
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
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1101 @param stream_object(iface.IStreamProducer): File object where data will be written
1559
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 @return (dict): session data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1106 """
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
1107 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
1108 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
1109 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
1110 session_hash = getSessionHash(client.jid, to_jid, sid)
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1111 session_data = self._registerHash(client, session_hash, stream_object)
1577
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1112 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
1113 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
1114 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
1115 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
1116 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
1117 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
1118 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
1119 }
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1120 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
1121 session_data.update(
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1122 {'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
1123 'peer_jid': to_jid,
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1124 'stream_object': stream_object,
1577
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1125 '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
1126 })
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1127
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1128 return session_data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1129
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1130 def getSession(self, client, session_hash):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1131 """Return session data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1132
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1133 @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
1134 hash is the same as hostname computed in XEP-0065 § 5.3.2 #1
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1135 @param client(None, SatXMPPClient): client of the peer
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1136 None is used only if client is unknown (this is only the case
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1137 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
1138 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
1139 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
1140 @return (dict): session data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1141 """
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1142 if client is None:
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1143 try:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1144 client = self.hash_clients_map[session_hash]
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1145 except KeyError as e:
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1146 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
1147 raise e
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1148 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
1149
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1150 def registerHash(self, *args, **kwargs):
1845
2bde6fc7aabd plugin XEP-0065: fixed timer cancellation when a profile is receiving a file
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1151 """like [_registerHash] but return the session deferred instead of the whole session
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1152 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
1153 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1154 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
1155
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1156 def _registerHash(self, client, session_hash, stream_object):
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1157 """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
1158
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1159 @param session_hash(str): hash of the session
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1160 @param stream_object(iface.IStreamProducer, IConsumer, None): file-like object
1757
abd6d6f89006 plugins XEP-0065, XEP-0260: fixed session creation order: session is created earlied, and file object associated in a second time if needed
Goffi <goffi@goffi.org>
parents: 1742
diff changeset
1161 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
1162 return (dict): session data
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1163 """
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1164 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
1165 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
1166 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
1167 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
1168 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
1169 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
1170 }
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
1171
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1172 if stream_object is not None:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1173 session_data['stream_object'] = stream_object
1757
abd6d6f89006 plugins XEP-0065, XEP-0260: fixed session creation order: session is created earlied, and file object associated in a second time if needed
Goffi <goffi@goffi.org>
parents: 1742
diff changeset
1174
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1175 assert session_hash not in self.hash_clients_map
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1176 self.hash_clients_map[session_hash] = client
1559
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1177
7cc29634b6ef plugin XEP-0065, XEP-0096: preparation for plugin XEP-0260 implementation:
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
1178 return session_data
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
1179
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1180 def associateStreamObject(self, client, session_hash, stream_object):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1181 """Associate a stream object with a session"""
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1182 session_data = self.getSession(client, session_hash)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1183 assert 'stream_object' not in session_data
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1184 session_data['stream_object'] = stream_object
1757
abd6d6f89006 plugins XEP-0065, XEP-0260: fixed session creation order: session is created earlied, and file object associated in a second time if needed
Goffi <goffi@goffi.org>
parents: 1742
diff changeset
1185
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1186 def streamQuery(self, iq_elt, 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
1187 log.debug(u"BS stream query")
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
1188
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
1189 iq_elt.handled = True
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
1190
1577
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1191 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
1192 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
1193 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
1194 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
1195 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
1196 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
1197
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
1198 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
1199 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
1200 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
1201
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
1202 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
1203 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
1204 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
1205 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
1206 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
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 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
1209
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
1210 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
1211 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
1212 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
1213 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
1214 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
1215 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
1216 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
1217 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
1218 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
1219 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
1220 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
1221 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
1222 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
1223 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
1224
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
1225 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
1226 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
1227
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1228 d = self.getBestCandidate(client, candidates, session_data['hash'])
1577
d04d7402b8e9 plugins XEP-0020, XEP-0065, XEP-0095, XEP-0096: fixed file copy with Stream Initiation:
Goffi <goffi@goffi.org>
parents: 1576
diff changeset
1229 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
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 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
1232 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
1233 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
1234 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
1235 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
1236 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
1237 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
1238 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
1239 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
1240 streamhost_used_elt['jid'] = candidate.jid.full()
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
1241 client.send(result_elt)
0
goffi@necton2
parents:
diff changeset
1242
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
1243
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1244 class XEP_0065_handler(XMPPHandler):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1245 implements(iwokkel.IDisco)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
1246
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1247 def __init__(self, plugin_parent):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1248 self.plugin_parent = plugin_parent
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1249 self.host = plugin_parent.host
398
cb0285372818 File transfer:
Goffi <goffi@goffi.org>
parents: 394
diff changeset
1250
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1251 def connectionInitialized(self):
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
1252 self.xmlstream.addObserver(BS_REQUEST, self.plugin_parent.streamQuery, client=self.parent)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
1253
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1254 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1255 return [disco.DiscoFeature(NS_BS)]
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1256
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1257 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
1258 return []