本文整理汇总了Python中twisted.names.server.DNSServerFactory方法的典型用法代码示例。如果您正苦于以下问题:Python server.DNSServerFactory方法的具体用法?Python server.DNSServerFactory怎么用?Python server.DNSServerFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.names.server
的用法示例。
在下文中一共展示了server.DNSServerFactory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: messageReceived
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def messageReceived(self, message, proto, address=None):
# Log info.
entry = {}
entry["src_ip"] = address[0]
entry["src_port"] = address[1]
entry["dns_name"] = message.queries[0].name.name
entry["dns_type"] = dns.QUERY_TYPES.get(message.queries[0].type, dns.EXT_QUERIES.get(message.queries[0].type, "UNKNOWN (%d)" % message.queries[0].type))
entry["dns_cls"] = dns.QUERY_CLASSES.get(message.queries[0].cls, "UNKNOWN (%d)" % message.queries[0].cls)
self.log(entry)
# Forward the request to the DNS server only if match set conditions,
# otherwise act as honeypot.
if entry["src_ip"] in self.request_log and (datetime.now() - self.request_log[entry["src_ip"]]["last_seen"]).total_seconds() < self.opts.req_timeout:
if self.request_log[entry["src_ip"]]["count"] < self.opts.req_count:
self.request_log[entry["src_ip"]]["count"] += 1
self.request_log[entry["src_ip"]]["last_seen"] = datetime.now()
return server.DNSServerFactory.messageReceived(self, message, proto, address)
else:
self.request_log[entry["src_ip"]]["last_seen"] = datetime.now()
return
else:
self.request_log[entry["src_ip"]] = {"count": 1, "last_seen": 0, "last_seen": datetime.now()}
return server.DNSServerFactory.messageReceived(self, message, proto, address)
示例2: test_resolverOrder
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_resolverOrder(self):
"""
L{server.DNSServerFactory.resolver} contains an ordered list of
authorities, caches and clients.
"""
# Use classes here so that we can see meaningful names in test results
class DummyAuthority(object):
pass
class DummyCache(object):
pass
class DummyClient(object):
pass
self.assertEqual(
server.DNSServerFactory(
authorities=[DummyAuthority],
caches=[DummyCache],
clients=[DummyClient]).resolver.resolvers,
[DummyAuthority, DummyCache, DummyClient])
示例3: test_messageReceivedAllowQuery
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_messageReceivedAllowQuery(self):
"""
L{server.DNSServerFactory.messageReceived} passes all messages to
L{server.DNSServerFactory.allowQuery} along with the receiving protocol
and origin address.
"""
message = dns.Message()
dummyProtocol = object()
dummyAddress = object()
f = RaisingDNSServerFactory()
e = self.assertRaises(
RaisingDNSServerFactory.AllowQueryArguments,
f.messageReceived,
message=message, proto=dummyProtocol, address=dummyAddress)
args, kwargs = e.args
self.assertEqual(args, (message, dummyProtocol, dummyAddress))
self.assertEqual(kwargs, {})
示例4: test_allowQueryFalse
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_allowQueryFalse(self):
"""
If C{allowQuery} returns C{False},
L{server.DNSServerFactory.messageReceived} calls L{server.sendReply}
with a message whose C{rCode} is L{dns.EREFUSED}.
"""
class SendReplyException(Exception):
pass
class RaisingDNSServerFactory(server.DNSServerFactory):
def allowQuery(self, *args, **kwargs):
return False
def sendReply(self, *args, **kwargs):
raise SendReplyException(args, kwargs)
f = RaisingDNSServerFactory()
e = self.assertRaises(
SendReplyException,
f.messageReceived,
message=dns.Message(), proto=None, address=None)
(proto, message, address), kwargs = e.args
self.assertEqual(message.rCode, dns.EREFUSED)
示例5: test_connectionTracking
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_connectionTracking(self):
"""
The C{connectionMade} and C{connectionLost} methods of
L{DNSServerFactory} cooperate to keep track of all L{DNSProtocol}
objects created by a factory which are connected.
"""
protoA, protoB = object(), object()
factory = server.DNSServerFactory()
factory.connectionMade(protoA)
self.assertEqual(factory.connections, [protoA])
factory.connectionMade(protoB)
self.assertEqual(factory.connections, [protoA, protoB])
factory.connectionLost(protoA)
self.assertEqual(factory.connections, [protoB])
factory.connectionLost(protoB)
self.assertEqual(factory.connections, [])
示例6: test_handleQuery
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_handleQuery(self):
"""
L{server.DNSServerFactory.handleQuery} takes the first query from the
supplied message and dispatches it to
L{server.DNSServerFactory.resolver.query}.
"""
m = dns.Message()
m.addQuery(b'one.example.com')
m.addQuery(b'two.example.com')
f = server.DNSServerFactory()
f.resolver = RaisingResolver()
e = self.assertRaises(
RaisingResolver.QueryArguments,
f.handleQuery,
message=m, protocol=NoopProtocol(), address=None)
(query,), kwargs = e.args
self.assertEqual(query, m.queries[0])
示例7: test_gotResolverResponse
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_gotResolverResponse(self):
"""
L{server.DNSServerFactory.gotResolverResponse} accepts a tuple of
resource record lists and triggers a response message containing those
resource record lists.
"""
f = server.DNSServerFactory()
answers = []
authority = []
additional = []
e = self.assertRaises(
RaisingProtocol.WriteMessageArguments,
f.gotResolverResponse,
(answers, authority, additional),
protocol=RaisingProtocol(), message=dns.Message(), address=None)
(message,), kwargs = e.args
self.assertIs(message.answers, answers)
self.assertIs(message.authority, authority)
self.assertIs(message.additional, additional)
示例8: test_gotResolverResponseCallsResponseFromMessage
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_gotResolverResponseCallsResponseFromMessage(self):
"""
L{server.DNSServerFactory.gotResolverResponse} calls
L{server.DNSServerFactory._responseFromMessage} to generate a response.
"""
factory = NoResponseDNSServerFactory()
factory._responseFromMessage = raiser
request = dns.Message()
request.timeReceived = 1
e = self.assertRaises(
RaisedArguments,
factory.gotResolverResponse,
([], [], []),
protocol=None, message=request, address=None
)
self.assertEqual(
((), dict(message=request, rCode=dns.OK,
answers=[], authority=[], additional=[])),
(e.args, e.kwargs)
)
示例9: test_responseFromMessageRecursionAvailable
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_responseFromMessageRecursionAvailable(self):
"""
L{server.DNSServerFactory._responseFromMessage} generates a response
message whose C{recAV} attribute is L{True} if
L{server.DNSServerFactory.canRecurse} is L{True}.
"""
factory = server.DNSServerFactory()
factory.canRecurse = True
response1 = factory._responseFromMessage(
message=dns.Message(recAv=False))
factory.canRecurse = False
response2 = factory._responseFromMessage(
message=dns.Message(recAv=True))
self.assertEqual(
(True, False),
(response1.recAv, response2.recAv))
示例10: test_responseFromMessageCallsMessageFactory
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_responseFromMessageCallsMessageFactory(self):
"""
L{server.DNSServerFactory._responseFromMessage} calls
C{dns._responseFromMessage} to generate a response
message from the request message. It supplies the request message and
other keyword arguments which should be passed to the response message
initialiser.
"""
factory = server.DNSServerFactory()
self.patch(dns, '_responseFromMessage', raiser)
request = dns.Message()
e = self.assertRaises(
RaisedArguments,
factory._responseFromMessage,
message=request, rCode=dns.OK
)
self.assertEqual(
((), dict(responseConstructor=factory._messageFactory,
message=request, rCode=dns.OK, recAv=factory.canRecurse,
auth=False)),
(e.args, e.kwargs)
)
示例11: test_gotResolverResponseLogging
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_gotResolverResponseLogging(self):
"""
L{server.DNSServerFactory.gotResolverResponse} logs the total number of
records in the response if C{verbose > 0}.
"""
f = NoResponseDNSServerFactory(verbose=1)
answers = [dns.RRHeader()]
authority = [dns.RRHeader()]
additional = [dns.RRHeader()]
assertLogMessage(
self,
["Lookup found 3 records"],
f.gotResolverResponse,
(answers, authority, additional),
protocol=NoopProtocol(), message=dns.Message(), address=None)
示例12: test_gotResolverErrorCallsResponseFromMessage
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_gotResolverErrorCallsResponseFromMessage(self):
"""
L{server.DNSServerFactory.gotResolverError} calls
L{server.DNSServerFactory._responseFromMessage} to generate a response.
"""
factory = NoResponseDNSServerFactory()
factory._responseFromMessage = raiser
request = dns.Message()
request.timeReceived = 1
e = self.assertRaises(
RaisedArguments,
factory.gotResolverError,
failure.Failure(error.DomainError()),
protocol=None, message=request, address=None
)
self.assertEqual(
((), dict(message=request, rCode=dns.ENAME)),
(e.args, e.kwargs)
)
示例13: _assertMessageRcodeForError
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def _assertMessageRcodeForError(self, responseError, expectedMessageCode):
"""
L{server.DNSServerFactory.gotResolver} accepts a L{failure.Failure} and
triggers a response message whose rCode corresponds to the DNS error
contained in the C{Failure}.
@param responseError: The L{Exception} instance which is expected to
trigger C{expectedMessageCode} when it is supplied to
C{gotResolverError}
@type responseError: L{Exception}
@param expectedMessageCode: The C{rCode} which is expected in the
message returned by C{gotResolverError} in response to
C{responseError}.
@type expectedMessageCode: L{int}
"""
f = server.DNSServerFactory()
e = self.assertRaises(
RaisingProtocol.WriteMessageArguments,
f.gotResolverError,
failure.Failure(responseError),
protocol=RaisingProtocol(), message=dns.Message(), address=None)
(message,), kwargs = e.args
self.assertEqual(message.rCode, expectedMessageCode)
示例14: test_gotResolverErrorResetsResponseAttributes
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_gotResolverErrorResetsResponseAttributes(self):
"""
L{server.DNSServerFactory.gotResolverError} does not allow request
attributes to leak into the response ie it sends a response with AD, CD
set to 0 and empty response record sections.
"""
factory = server.DNSServerFactory()
responses = []
factory.sendReply = (
lambda protocol, response, address: responses.append(response)
)
request = dns.Message(authenticData=True, checkingDisabled=True)
request.answers = [object(), object()]
request.authority = [object(), object()]
request.additional = [object(), object()]
factory.gotResolverError(
failure.Failure(error.DomainError()),
protocol=None, message=request, address=None
)
self.assertEqual([dns.Message(rCode=3, answer=True)], responses)
示例15: test_gotResolverResponseResetsResponseAttributes
# 需要导入模块: from twisted.names import server [as 别名]
# 或者: from twisted.names.server import DNSServerFactory [as 别名]
def test_gotResolverResponseResetsResponseAttributes(self):
"""
L{server.DNSServerFactory.gotResolverResponse} does not allow request
attributes to leak into the response ie it sends a response with AD, CD
set to 0 and none of the records in the request answer sections are
copied to the response.
"""
factory = server.DNSServerFactory()
responses = []
factory.sendReply = (
lambda protocol, response, address: responses.append(response)
)
request = dns.Message(authenticData=True, checkingDisabled=True)
request.answers = [object(), object()]
request.authority = [object(), object()]
request.additional = [object(), object()]
factory.gotResolverResponse(
([], [], []),
protocol=None, message=request, address=None
)
self.assertEqual([dns.Message(rCode=0, answer=True)], responses)