def test_tenWrappers(self): "stack can be called with ten wrappers." args = [] result = 'a', for x in xrange(10): args.append(wrapperFactory(x)) result = 9 - x, result args.append(nullFactory) fac = parsley.stack(*args) self.assertEqual(fac('a'), result)
def test_tenWrappers(self): "stack can be called with ten wrappers." args = [] result = 'a', for x in range(10): args.append(wrapperFactory(x)) result = 9 - x, result args.append(nullFactory) fac = parsley.stack(*args) self.assertEqual(fac('a'), result)
def __init__(self): source = OMeta(grammar.grammarSource).parseGrammar('Grammar') bindings = grammar.bindings if bindings is None: bindings = {} ParserProtocol.__init__(self, grammar=source, senderFactory=SOCKS5Sender, receiverFactory=stack(SOCKS5AuthDispatcher, SOCKS5Receiver), bindings=bindings)
def auth_addition(self, *a): self.additionArgs = a # PY3KPORT: Py2-3 compatible port using six self.sender.transport.write(six.b('addition!')) self.currentRule = 'authAddition' def authedAddition(self, x): self.additionParsed = x del self.currentRule self.w._sendRequest() AdditionAuthSOCKS5Client = makeProtocol( grammar.grammarSource + authAdditionGrammar, client.SOCKS5Sender, stack(client.SOCKS5AuthDispatcher, AuthAdditionWrapper, client.SOCKS5Receiver), grammar.bindings) class TestSOCKS5Client(unittest.TestCase): def makeProto(self, *a, **kw): protoClass = kw.pop('_protoClass', client.SOCKS5Client) fac = FakeSOCKS5ClientFactory(*a, **kw) fac.protocol = protoClass proto = fac.buildProtocol(None) transport = proto_helpers.StringTransport() transport.abortConnection = lambda: None proto.makeConnection(transport) return fac, proto def test_initialHandshake(self): # PY3KPORT: Py2-3 compatible port using six
def prepareParsing(self, parser): pass def finishParsing(self, reason): pass def netstringFirstHalfReceived(self, string): self.sender.sendNetstring(string) def netstringSecondHalfReceived(self, string): pass pass # begin protocol definition NetstringProtocol = makeProtocol( grammar, stack(NetstringReversalWrapper, NetstringSender), stack(NetstringSplittingWrapper, SplitNetstringReceiver)) class NetstringFactory(Factory): protocol = NetstringProtocol def main(reactor): server = TCP4ServerEndpoint(reactor, 1234) d = server.listen(NetstringFactory()) d.addCallback(lambda p: Deferred()) # listen forever return d react(main, [])
def auth_addition(self, *a): self.additionArgs = a self.sender.transport.write("addition!") self.currentRule = "authAddition" def authedAddition(self, x): self.additionParsed = x del self.currentRule self.w._sendRequest() AdditionAuthSOCKS5Client = makeProtocol( grammar.grammarSource + authAdditionGrammar, client.SOCKS5Sender, stack(client.SOCKS5AuthDispatcher, AuthAdditionWrapper, client.SOCKS5Receiver), grammar.bindings, ) class TestSOCKS5Client(unittest.TestCase): def makeProto(self, *a, **kw): protoClass = kw.pop("_protoClass", client.SOCKS5Client) fac = FakeSOCKS5ClientFactory(*a, **kw) fac.protocol = protoClass proto = fac.buildProtocol(None) transport = proto_helpers.StringTransport() transport.abortConnection = lambda: None proto.makeConnection(transport) return fac, proto
self.sender.join("#colontea") def unknownCommand(self, line): print line def unknownCTCP(self, line, command, params): print line, command, params IRCClient = parsley.makeProtocol( parslirc.ircGrammar, parslirc.IRCSender, parsley.stack( parslirc.IRCDispatcher, parslirc.CTCPDispatcher, parslirc.CAPNegotiator, parslirc.BaseIRCFunctionality, SpewingWrapper, parslirc.NullIRCReceiver, ), parslirc.bindings, ) class IRCClientFactory(Factory): protocol = IRCClient def main(reactor, description): client = clientFromString(reactor, description) d = client.connect(policies.SpewingFactory(IRCClientFactory())) d.addCallback(lambda p: Deferred())
def _sendRequest(self): self.sender.sendRequest(c.CMD_CONNECT, self.factory.host, self.factory.port) self.currentRule = 'SOCKS5ClientState_readResponse' def serverResponse(self, status, address, port): if status != c.SOCKS5_GRANTED: raise e.socks5ErrorMap.get(status)() self.factory.proxyConnectionEstablished(self) self.currentRule = 'SOCKSState_readData' SOCKS5Client = makeProtocol(grammar.grammarSource, SOCKS5Sender, stack(SOCKS5AuthDispatcher, SOCKS5Receiver), grammar.bindings) class SOCKS5ClientFactory(_SOCKSClientFactory): protocol = SOCKS5Client authMethodMap = { 'anonymous': c.AUTH_ANONYMOUS, 'login': c.AUTH_LOGIN, } def __init__(self, host, port, proxiedFactory, methods={'anonymous': ()}): if not methods: raise ValueError('no auth methods were specified') self.host = host
def test_onlyBase(self): "stack can be called with no wrappers." fac = parsley.stack(nullFactory) self.assertEqual(fac('a'), ('a',))
def test_oneWrapper(self): "stack can be called with one wrapper." fac = parsley.stack(wrapperFactory(0), nullFactory) self.assertEqual(fac('a'), (0, ('a',)))
def test_senderFactoriesTakeOneArgument(self): "The callable returned by stack takes exactly one argument." fac = parsley.stack(nullFactory) self.assertRaises(TypeError, fac) self.assertRaises(TypeError, fac, 'a', 'b')
def _sendRequest(self): self.sender.sendRequest( c.CMD_CONNECT, self.factory.host, self.factory.port) self.currentRule = 'SOCKS5ClientState_readResponse' def serverResponse(self, status, address, port): if status != c.SOCKS5_GRANTED: raise e.socks5ErrorMap.get(status)() self.factory.proxyConnectionEstablished(self) self.currentRule = 'SOCKSState_readData' SOCKS5Client = makeProtocol( grammar.grammarSource, SOCKS5Sender, stack(SOCKS5AuthDispatcher, SOCKS5Receiver), grammar.bindings) class SOCKS5ClientFactory(_SOCKSClientFactory): protocol = SOCKS5Client authMethodMap = { 'anonymous': c.AUTH_ANONYMOUS, 'login': c.AUTH_LOGIN, } def __init__(self, host, port, proxiedFactory, methods={'anonymous': ()}): if not methods: raise ValueError('no auth methods were specified') self.host = host self.port = port
def ctcp_ACTION(self, line, arguments): self.w.action( line, IRCUser.fromFull(line.prefix), line.params[0], arguments) class CAPNegotiator(WrapperBase): def connectionMade(self): self.w.sender.sendLine('CAP LS') self.w.connectionMade() def irc_CAP(self, line): if line.params[1] == 'LS': supported = set(line.params[2].split()) toRequest = supported.intersection(self.w.capExtensions) if toRequest: self.w.sender.sendCommand('CAP', ['REQ', ' '.join(toRequest)]) else: self.w.sender.sendLine('CAP END') elif line.params[1] == 'ACK': print line self.w.sender.sendLine('CAP END') else: self.w.unknownCommand(line) IRCClient = parsley.makeProtocol( ircGrammar, IRCSender, parsley.stack(NullIRCReceiver, BaseIRCFunctionality, IRCDispatcher), bindings)