# Copyright (c) Aaron Gallagher <_@habnab.it>
# See COPYING for details.
from parsley import makeProtocol, stack
from twisted.internet.error import ConnectionLost, ConnectionRefusedError
from twisted.internet import defer, protocol
from twisted.python import failure, log
from twisted.trial import unittest
from twisted.test import proto_helpers
from txsocksx.test.util import FakeEndpoint
from txsocksx import client, errors, grammar
import txsocksx.constants as c
connectionLostFailure = failure.Failure(ConnectionLost())
connectionRefusedFailure = failure.Failure(ConnectionRefusedError())
class FakeSOCKS5ClientFactory(protocol.ClientFactory):
protocol = client.SOCKS5Client
def __init__(self, host='', port=0, methods={c.AUTH_ANONYMOUS: ()}):
self.host = host
self.port = port
self.methods = methods
self.reason = None
self.accum = proto_helpers.AccumulatingProtocol()
self.expectingReason = False
def proxyConnectionFailed(self, reason):
if self.expectingReason:
self.reason = reason
else:
log.err(reason)
def proxyConnectionEstablished(self, proxyProtocol):
proxyProtocol.proxyEstablished(self.accum)
class FakeSOCKS4ClientFactory(protocol.ClientFactory):
protocol = client.SOCKS4Client
def __init__(self, host='', port=0, user=''):
self.host = host
self.port = port
self.user = user
self.reason = None
self.accum = proto_helpers.AccumulatingProtocol()
self.expectingReason = False
def proxyConnectionFailed(self, reason):
if self.expectingReason:
self.reason = reason
else:
log.err(reason)
def proxyConnectionEstablished(self, proxyProtocol):
proxyProtocol.proxyEstablished(self.accum)
authAdditionGrammar = """
authAddition = 'addition' anything:x -> receiver.authedAddition(x)
"""
class AuthAdditionWrapper(object):
def __init__(self, wrapped):
self.w = wrapped
def __getattr__(self, attr):
return getattr(self.w, attr)
authMethodMap = {
c.AUTH_ANONYMOUS: 'anonymous',
c.AUTH_LOGIN: 'login',
'A': 'addition',
}
additionArgs = additionParsed = None
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
def test_initialHandshake(self):
fac, proto = self.makeProto()
self.assertEqual(proto.transport.value(), '\x05\x01\x00')
fac, proto = self.makeProto(methods={c.AUTH_ANONYMOUS: (), c.AUTH_LOGIN: ()})
self.assertEqual(proto.transport.value(), '\x05\x02\x00\x02')
fac, proto = self.makeProto(methods={c.AUTH_LOGIN: ()})
self.assertEqual(proto.transport.value(), '\x05\x01\x02')
def test_failedMethodSelection(self):
fac, proto = self.makeProto()
fac.expectingReason = True
proto.dataReceived('\x05\xff')
self.failIfEqual(fac.reason, None)
self.failUnlessIsInstance(
fac.reason.value, errors.MethodsNotAcceptedError)
self.assertEqual(fac.reason.value.args[2], '\xff')
def test_loginAuth(self):
fac, proto = self.makeProto(methods={c.AUTH_LOGIN: ('spam', 'eggs')})
proto.transport.clear()
proto.dataReceived('\x05\x02')
self.assertEqual(proto.transport.value(), '\x01\x04spam\x04eggs')
def test_loginAuthAccepted(self):
fac, proto = self.makeProto(methods={c.AUTH_LOGIN: ('spam', 'eggs')})
proto.dataReceived('\x05\x02')
proto.transport.clear()
proto.dataReceived('\x01\x00')
self.assert_(proto.transport.value())
def test_loginAuthFailed(self):
fac, proto = self.makeProto(methods={c.AUTH_LOGIN: ('spam', 'eggs')})
fac.expectingReason = True
proto.dataReceived('\x05\x02\x01\x01')
self.failIfEqual(fac.reason, None)
self.failUnlessIsInstance(
fac.reason.value, errors.LoginAuthenticationFailed)
def test_connectionRequest(self):
fac, proto = self.makeProto('host', 0x47)
proto.transport.clear()
proto.dataReceived('\x05\x00')
self.assertEqual(proto.transport.value(),
'\x05\x01\x00\x03\x04host\x00\x47')
fac, proto = self.makeProto('longerhost', 0x9494)
proto.transport.clear()
proto.dataReceived('\x05\x00')
self.assertEqual(proto.transport.value(),
'\x05\x01\x00\x03\x0alongerhost\x94\x94')
def test_handshakeEatsEnoughBytes(self):
fac, proto = self.makeProto()
proto.dataReceived('\x05\x00\x05\x00\x00\x01444422xxxxx')
self.assertEqual(fac.accum.data, 'xxxxx')
fac, proto = self.makeProto()
proto.dataReceived('\x05\x00\x05\x00\x00\x04666666666666666622xxxxx')
self.assertEqual(fac.accum.data, 'xxxxx')
fac, proto = self.makeProto()
proto.dataReceived('\x05\x00\x05\x00\x00\x03\x08somehost22xxxxx')
self.assertEqual(fac.accum.data, 'xxxxx')
fac, proto = self.makeProto()
proto.dataReceived('\x05\x00\x05\x00\x00\x03\x0022xxxxx')
self.assertEqual(fac.accum.data, 'xxxxx')
def test_connectionRequestError(self):
fac, proto = self.makeProto()
fac.expectingReason = True
proto.dataReceived('\x05\x00\x05\x01\x00\x03\x0022')
self.failIfEqual(fac.reason, None)
self.failUnlessIsInstance(fac.reason.value, errors.ServerFailure)
def test_buffering(self):
fac, proto = self.makeProto()
for c in '\x05\x00\x05\x00\x00\x01444422xxxxx':
proto.dataReceived(c)
self.assertEqual(fac.accum.data, 'xxxxx')
def test_connectionLostEarly(self):
wholeRequest = '\x05\x00\x05\x00\x00\x01444422'
for e in xrange(len(wholeRequest)):
partialRequest = wholeRequest[:e]
fac, proto = self.makeProto()
fac.expectingReason = True
if partialRequest:
proto.dataReceived(partialRequest)
proto.connectionLost(connectionLostFailure)
self.failUnlessIsInstance(fac.reason.value, ConnectionLost)
def test_connectionLostAfterNegotiation(self):
fac, proto = self.makeProto()
proto.dataReceived('\x05\x00\x05\x00\x00\x01444422')
proto.connectionLost(connectionLostFailure)
self.assertEqual(fac.accum.closedReason, connectionLostFailure)
fac, proto = self.makeProto()
proto.dataReceived('\x05\x00\x05\x00\x00\x01444422xxxxx')
proto.connectionLost(connectionLostFailure)
self.assertEqual(fac.accum.closedReason, connectionLostFailure)
self.assertEqual(fac.accum.data, 'xxxxx')
def test_authAddition(self):
fac, proto = self.makeProto(
_protoClass=AdditionAuthSOCKS5Client, methods={'A': ('x', 'y')})
proto.transport.clear()
proto.dataReceived('\x05A')
self.assertEqual(proto.transport.value(), 'addition!')
self.assertEqual(proto.receiver.additionArgs, ('x', 'y'))
proto.dataReceived('additionz')
self.assertEqual(
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
影视检索系统 mongodb数据地址: 8.129.178.143:27017 账号:movie 密码:movie 数据已经备份,请放心查看(没有比较好的下载下来的方式,所以直接放数据库账号密码 数据大小1.4gb 数据大概在1000w左右 演员20w 评分880w 用户85w 电影10w
资源推荐
资源详情
资源评论
收起资源包目录
videoSearchSystem-main.zip (36个子文件)
videoSearchSystem-main
video_scrapy
txsocksx
http.py 4KB
tls.py 1KB
__init__.py 233B
ssl.py 2KB
grammar.py 3KB
test
__init__.py 73B
test_http.py 3KB
util.py 6KB
test_tls.py 3KB
test_grammar.py 4KB
test_client.py 22KB
client.py 13KB
__pycache__
client.cpython-37.pyc 16KB
tls.cpython-37.pyc 2KB
constants.cpython-37.pyc 894B
errors.cpython-37.pyc 4KB
grammar.cpython-37.pyc 3KB
__init__.cpython-37.pyc 334B
http.cpython-37.pyc 4KB
constants.py 645B
errors.py 2KB
scrapy.cfg 267B
video_scrapy
__init__.py 0B
pipelines.py 1KB
spiders
__init__.py 161B
movie_spider.py 5KB
__pycache__
movie_spider.cpython-37.pyc 4KB
__init__.cpython-37.pyc 173B
items.py 891B
settings.py 8KB
__pycache__
items.cpython-37.pyc 681B
pipelines.cpython-37.pyc 2KB
middlewares.cpython-37.pyc 7KB
settings.cpython-37.pyc 5KB
__init__.cpython-37.pyc 165B
middlewares.py 7KB
共 36 条
- 1
资源评论
博士僧小星
- 粉丝: 1936
- 资源: 5901
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功