]>
code.delx.au - pymsnt/blob - src/tlib/proxy.py
1 # Copyright 2004 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
4 from twisted
.internet
.ssl
import ClientContextFactory
5 from twisted
.internet
.protocol
import Protocol
, ClientFactory
6 from twisted
.internet
import reactor
8 if(utils
.checkTwisted()):
9 from twisted
.web
.http
import HTTPClient
11 from twisted
.protocols
.http
import HTTPClient
14 class ProxyFactory(ClientFactory
):
15 def __init__(self
, server
, serverport
, otherfac
):
16 self
.server
, self
.serverport
, self
.otherfac
= server
, serverport
, otherfac
18 def buildProtocol(self
, addr
):
20 p
.server
, p
.serverport
, p
.otherfac
= self
.server
, self
.serverport
, self
.otherfac
23 class ProxyProtocol(HTTPClient
):
24 def connectionMade(self
):
25 self
.ctxFactory
= ClientContextFactory()
26 self
.sendCommand("CONNECT", self
.server
+ ":" + str(self
.serverport
))
28 debug
.log("proxy_connect_ssl: Sent CONNECT")
30 def handleStatus(self
, version
, status
, message
):
31 if(status
== "200"): # Excellent
32 # Must flip over into SSL now
33 debug
.log("proxy_connect_ssl: Flipping into SSL mode")
34 self
.transport
.startTLS(self
.ctxFactory
)
35 p
= self
.otherfac
.buildProtocol(self
.transport
.getPeer())
36 p
.makeConnection(self
.transport
)
37 self
.transport
.protocol
= p
39 debug
.log("proxy_connect_ssl: Flipped into SSL mode successfully. Handed over to real protocol.")
42 debug
.log("proxy_connect_ssl: Error with proxy")
43 self
.otherfac
.clientConnectionFailed(self
.transport
.connector
, "Proxy connection failed")
45 def handleHeader(self
, key
, val
):
48 def handleEndHeaders(self
):
51 def handleResponse(self
, r
):
54 def proxy_connect_ssl(proxy
, proxyport
, server
, serverport
, factory
):
55 debug
.log("proxy_connect_ssl")
56 pfac
= ProxyFactory(server
, serverport
, factory
)
57 reactor
.connectTCP(proxy
, proxyport
, pfac
)