Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Implement basic parsing of SOCKS messages with parsley
* Does not parse authenticated messages * XXX Currently parsing of reply messages and methods is broken because of https://bugs.launchpad.net/parsley/+bug/1085492 Update parser and add unittests Refactoring of SOCKSClient to not depend on qbuf. * Adapt unittests to support the changes in design * XXX disable some unittests that are no longer needed Finish implementation of SOCKS5 proxy client with example usage.
- Loading branch information
Showing
with
504 additions
and 148 deletions.
- +39 −0 example.py
- +21 −0 scripts/codegen.py
- +22 −9 txsocksx/auth.py
- +93 −53 txsocksx/client.py
- +59 −14 txsocksx/errors.py
- +112 −0 txsocksx/parser.py
- +20 −72 txsocksx/test/test_client.py
- +138 −0 txsocksx/test/test_parser.py
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| @@ -0,0 +1,39 @@ | ||
| from twisted.internet import reactor | ||
| from twisted.internet.protocol import Protocol, Factory | ||
| from twisted.internet.endpoints import TCP4ClientEndpoint | ||
|
|
||
| from txsocksx.client import SOCKS5ClientEndpoint | ||
|
|
||
| class GETSlash(Protocol): | ||
| def connectionMade(self): | ||
| self.transport.write("GET / HTTP/1.1\n\r\n\r") | ||
|
|
||
| def buildProtocol(self): | ||
| return self | ||
|
|
||
| def dataReceived(self, data): | ||
| print "Got this as a response" | ||
| print data | ||
|
|
||
| class GETSlashFactory(Factory): | ||
| def buildProtocol(self, addr): | ||
| print "Building protocol towards" | ||
| return GETSlash() | ||
|
|
||
| socks_addr = '127.0.0.1' | ||
| socks_port = 9050 | ||
| TCPPoint = TCP4ClientEndpoint(reactor, socks_addr, socks_port) | ||
|
|
||
| dst_addr = 'checkip.dyndns.com' | ||
| dst_port = 80 | ||
| SOCKSPoint = SOCKS5ClientEndpoint(dst_addr, | ||
| dst_port, TCPPoint) | ||
|
|
||
| d = SOCKSPoint.connect(GETSlashFactory()) | ||
| @d.addErrback | ||
| def _gotError(error): | ||
| print "Error in connection" | ||
| reactor.stop() | ||
|
|
||
| reactor.run() | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| @@ -0,0 +1,21 @@ | ||
|
|
||
|
|
||
| def makeTestFailure(): | ||
| l = ['ServerFailure', | ||
| 'ConnectionNotAllowed', | ||
| 'NetworkUnreachable', | ||
| 'HostUnreachable', | ||
| 'ConnectionRefused', | ||
| 'TTLExpired', | ||
| 'CommandNotSupported', | ||
| 'AddressNotSupported'] | ||
|
|
||
| base = """ | ||
| def test_ServerReply%(error_name)s(self): | ||
| p = SOCKSGrammar(dummyServerReplyFail%(idx)sIPV4) | ||
| failure, addr, port = p.serverReply() | ||
| self.assertIs(failure, e.%(error_name)s) | ||
| """ | ||
| for i, v in enumerate(l): | ||
| print base % {'idx': i+1, 'error_name': v} | ||
| makeTestFailure() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| @@ -1,28 +1,41 @@ | ||
| from twisted.internet import defer | ||
| from txsocksx.errors import SocksError | ||
| from txsocksx.errors import SOCKSError | ||
|
|
||
| class Anonymous(object): | ||
| """ | ||
| ( 0 ) | ||
| """ | ||
| method = '\x00' | ||
|
|
||
| def negotiate(self, proto): | ||
| self.negotiated = True | ||
| return defer.succeed(None) | ||
|
|
||
| class UsernamePasswordAuthFailed(SocksError): | ||
| class GSSAPI(object): | ||
| """ | ||
| ( 1 ) | ||
| """ | ||
| method = '\x01' | ||
| def negotiate(self, proto): | ||
| raise NotImplemented | ||
|
|
||
| class UsernamePasswordAuthFailed(SOCKSError): | ||
| pass | ||
|
|
||
| class UsernamePassword(object): | ||
| """ | ||
| ( 2 ) | ||
| """ | ||
| method = '\x02' | ||
|
|
||
| def __init__(self, uname, passwd): | ||
| self.uname = uname | ||
| self.passwd = passwd | ||
|
|
||
| @defer.inlineCallbacks | ||
| def negotiate(self, proto): | ||
| proto.transport.write( | ||
| '\x01' | ||
| self.method | ||
| + chr(len(self.uname)) + self.uname | ||
| + chr(len(self.passwd)) + self.passwd) | ||
| resp, = yield proto.unpack('!xB') | ||
| if resp != 0: | ||
| raise UsernamePasswordAuthFailed(resp) | ||
| # XXX implement the reading of the response and make sure | ||
| # authentication suceeded | ||
| return defer.succeed(None) | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Oops, something went wrong.