Skip to content

Commit

Permalink
Fix bug in instantiation of deferred and endpoint
Browse files Browse the repository at this point in the history
lots of thanks to nextime for hunting it down
  • Loading branch information
hellais committed Mar 26, 2012
1 parent cc81a59 commit a0cb238
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/socksclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SOCKSv4ClientProtocol(Protocol):
handshakeDone = None
buf = ''

def SOCKSConnect(self, host, port):
def SOCKSConnect(self, host, port):
# only socksv4a for now
ver = 4
cmd = 1 # stream connection
Expand Down Expand Up @@ -58,8 +58,11 @@ def connectionMade(self):

def dataReceived(self, data):
if self.isSuccess(data):
import traceback
print "--------- I have success!!!"
#traceback.print_stack()
# Build protocol from provided factory and transfer control to it.
self.transport.protocol = self.postHandshakeFactory.buildProtocol(
self.transport.protocol = self.postHandshakeFactory.buildProtocol(
self.transport.getHost())
self.transport.protocol.transport = self.transport
self.transport.protocol.connectionMade()
Expand All @@ -69,6 +72,13 @@ def dataReceived(self, data):
class SOCKSv4ClientFactory(ClientFactory):
protocol = SOCKSv4ClientProtocol

def buildProtocol(self, addr):
r=ClientFactory.buildProtocol(self, addr)
r.postHandshakeEndpoint = self.postHandshakeEndpoint
r.postHandshakeFactory = self.postHandshakeFactory
r.handshakeDone = self.handshakeDone
return r


class SOCKSWrapper(object):
implements(IStreamClientEndpoint)
Expand All @@ -78,7 +88,7 @@ def __init__(self, reactor, host, port, endpoint):
self._host = host
self._port = port
self._reactor = reactor
self._endpoint = endpoint
self._endpoint = endpoint

def connect(self, protocolFactory):
"""
Expand All @@ -88,18 +98,18 @@ def connect(self, protocolFactory):
def _canceller(deferred):
connector.stopConnecting()
deferred.errback(
error.ConnectingCancelledError(connector.getDestination()))
error.ConnectingCancelledError(connector.getDestination()))

try:
# Connect with an intermediate SOCKS factory/protocol,
# which then hands control to the provided protocolFactory
# once a SOCKS connection has been established.
f = self.factory()
f.protocol.postHandshakeEndpoint = self._endpoint
f.protocol.postHandshakeFactory = protocolFactory
f.protocol.handshakeDone = defer.Deferred()
f.postHandshakeEndpoint = self._endpoint
f.postHandshakeFactory = protocolFactory
f.handshakeDone = defer.Deferred()
wf = _WrappingFactory(f, _canceller)
self._reactor.connectTCP(self._host, self._port, wf)
return f.protocol.handshakeDone
except:
return defer.fail()
except:
return defer.fail()

0 comments on commit a0cb238

Please sign in to comment.