From 56e0ad7a96125f6895a1ce30323f93813fd43d6a Mon Sep 17 00:00:00 2001 From: tomk Date: Mon, 28 Jul 2008 18:53:00 +0000 Subject: [PATCH] basic proxy authentication for BOSH connections implemented --- src/common/connection.py | 1 - src/common/xmpp/bosh.py | 22 ++++++++++++-------- src/common/xmpp/client_nb.py | 16 ++++++++------- src/common/xmpp/transports_nb.py | 35 ++++++++++++++++++++------------ 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/common/connection.py b/src/common/connection.py index 7df8b4a71..fc6a71a56 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -431,7 +431,6 @@ class Connection(ConnectionHandlers): proxy = {} proxyptr = gajim.config.get_per('proxies',p) for key in proxyptr.keys(): proxy[key]=proxyptr[key][1] - print proxy elif gajim.config.get_per('accounts', self.name, 'use_env_http_proxy'): try: diff --git a/src/common/xmpp/bosh.py b/src/common/xmpp/bosh.py index c29fa7294..062084d1c 100644 --- a/src/common/xmpp/bosh.py +++ b/src/common/xmpp/bosh.py @@ -20,7 +20,7 @@ In TCP-derived transports it is file descriptor of socket''' class NonBlockingBOSH(NonBlockingTransport): def __init__(self, raise_event, on_disconnect, idlequeue, xmpp_server, domain, - bosh_dict): + bosh_dict, proxy_creds): NonBlockingTransport.__init__(self, raise_event, on_disconnect, idlequeue) self.bosh_sid = None @@ -42,7 +42,9 @@ class NonBlockingBOSH(NonBlockingTransport): self.bosh_uri = bosh_dict['bosh_uri'] self.bosh_port = bosh_dict['bosh_port'] self.bosh_content = bosh_dict['bosh_content'] - + self.over_proxy = bosh_dict['bosh_useproxy'] + self.use_proxy_auth = bosh_dict['useauth'] + self.proxy_creds = proxy_creds self.wait_cb_time = None self.http_socks = [] self.stanza_buffer = [] @@ -289,8 +291,6 @@ class NonBlockingBOSH(NonBlockingTransport): return t - - def connect_and_flush(self, socket): socket.connect( conn_5tuple = self.conn_5tuple, @@ -334,15 +334,21 @@ class NonBlockingBOSH(NonBlockingTransport): def get_new_http_socket(self): + http_dict = {'http_uri': self.bosh_uri, + 'http_port': self.bosh_port, + 'http_version': self.http_version, + 'http_persistent': self.http_persistent, + 'over_proxy': self.over_proxy} + if self.use_proxy_auth: + http_dict['proxy_user'], http_dict['proxy_pass'] = self.proxy_creds + + s = NonBlockingHTTPBOSH( raise_event=self.raise_event, on_disconnect=self.disconnect, idlequeue = self.idlequeue, on_http_request_possible = self.on_http_request_possible, - http_uri = self.bosh_uri, - http_port = self.bosh_port, - http_version = self.http_version, - http_persistent = self.http_persistent, + http_dict = http_dict, on_persistent_fallback = self.on_persistent_fallback) s.onreceive(self.on_received_http) s.set_stanza_build_cb(self.build_stanza) diff --git a/src/common/xmpp/client_nb.py b/src/common/xmpp/client_nb.py index eb44d4665..8e15f3672 100644 --- a/src/common/xmpp/client_nb.py +++ b/src/common/xmpp/client_nb.py @@ -71,6 +71,7 @@ class NBCommonClient: self.connected='' log.debug('Client disconnected..') + print 'ffffffffffffffffff' for i in reversed(self.disconnect_handlers): log.debug('Calling disconnect handler %s' % i) i() @@ -393,10 +394,11 @@ class NonBlockingClient(NBCommonClient): if proxy['type'] == 'bosh': self.socket = bosh.NonBlockingBOSH( - on_disconnect=self.on_disconnect, + on_disconnect = self.on_disconnect, raise_event = self.raise_event, idlequeue = self.idlequeue, - xmpp_server=(xmpp_hostname, self.Port), + proxy_creds = (proxy_user, proxy_pass), + xmpp_server = (xmpp_hostname, self.Port), domain = self.Server, bosh_dict = proxy) self.protocol_type = 'BOSH' @@ -408,19 +410,19 @@ class NonBlockingClient(NBCommonClient): elif proxy['type'] == 'http': proxy_class = transports_nb.NBHTTPProxySocket self.socket = proxy_class( - on_disconnect=self.on_disconnect, + on_disconnect = self.on_disconnect, raise_event = self.raise_event, idlequeue = self.idlequeue, - proxy_creds=(proxy_user, proxy_pass), - xmpp_server=(xmpp_hostname, self.Port)) + proxy_creds = (proxy_user, proxy_pass), + xmpp_server = (xmpp_hostname, self.Port)) else: self._on_tcp_failure = self._on_connect_failure tcp_host=xmpp_hostname tcp_port=self.Port self.socket = transports_nb.NonBlockingTCP( + on_disconnect = self.on_disconnect, raise_event = self.raise_event, - idlequeue = self.idlequeue, - on_disconnect = self.on_disconnect) + idlequeue = self.idlequeue) self.socket.PlugIn(self) diff --git a/src/common/xmpp/transports_nb.py b/src/common/xmpp/transports_nb.py index f110e4371..29b95124f 100644 --- a/src/common/xmpp/transports_nb.py +++ b/src/common/xmpp/transports_nb.py @@ -480,19 +480,24 @@ class NonBlockingHTTP(NonBlockingTCP): ''' def __init__(self, raise_event, on_disconnect, idlequeue, on_http_request_possible, - http_uri, http_port, on_persistent_fallback, http_version='HTTP/1.1', - http_persistent=False): + on_persistent_fallback, http_dict): NonBlockingTCP.__init__(self, raise_event, on_disconnect, idlequeue) - self.http_protocol, self.http_host, self.http_path = urisplit(http_uri) + self.http_protocol, self.http_host, self.http_path = urisplit(http_dict['http_uri']) if self.http_protocol is None: self.http_protocol = 'http' if self.http_path == '': - http_path = '/' - self.http_port = http_port - self.http_version = http_version - self.http_persistent = http_persistent + self.http_path = '/' + self.http_port = http_dict['http_port'] + self.http_version = http_dict['http_version'] + self.http_persistent = http_dict['http_persistent'] + self.over_proxy = http_dict['over_proxy'] + if http_dict.has_key('proxy_user') and http_dict.has_key('proxy_pass'): + self.proxy_user, self.proxy_pass = http_dict['proxy_user'], http_dict['proxy_pass'] + else: + self.proxy_user, self.proxy_pass = None, None + # buffer for partial responses self.recvbuff = '' self.expected_length = 0 @@ -574,11 +579,15 @@ class NonBlockingHTTP(NonBlockingTCP): headers = ['%s %s %s' % (method, absolute_uri, self.http_version), 'Host: %s:%s' % (self.http_host, self.http_port), 'Content-Type: text/xml; charset=utf-8', - 'Content-Length: %s' % len(str(httpbody)), - 'Proxy-Connection: keep-alive', - 'Pragma: no-cache', - 'Accept-Encoding: gzip, deflate', - '\r\n'] + 'Content-Length: %s' % len(str(httpbody))] + if self.over_proxy: + headers.append('Proxy-Connection: keep-alive') + headers.append('Pragma: no-cache') + if self.proxy_user and self.proxy_pass: + credentials = '%s:%s' % (self.proxy_user, self.proxy_pass) + credentials = base64.encodestring(credentials).strip() + headers.append('Proxy-Authorization: Basic %s' % credentials) + headers.append('\r\n') headers = '\r\n'.join(headers) return('%s%s\r\n' % (headers, httpbody)) @@ -678,7 +687,7 @@ class NBHTTPProxySocket(NBProxySocket): 'Proxy-Connection: Keep-Alive', 'Pragma: no-cache', 'Host: %s:%s' % self.xmpp_server, - 'User-Agent: HTTPPROXYsocket/v0.1'] + 'User-Agent: Gajim'] if self.proxy_user and self.proxy_pass: credentials = '%s:%s' % (self.proxy_user, self.proxy_pass) credentials = base64.encodestring(credentials).strip()