File: //usr/lib/python3/dist-packages/twisted/names/__pycache__/server.cpython-38.pyc
U
W[!W � @ sZ d Z ddlmZmZ ddlZddlmZ ddlmZm Z ddl
mZ G dd� dej�Z
dS ) a`
Async DNS server
Future plans:
- Better config file format maybe
- Make sure to differentiate between different classes
- notice truncation bit
Important: No additional processing is done on some of the record types.
This violates the most basic RFC and is just plain annoying
for resolvers to deal with. Fix it.
@author: Jp Calderone
� )�division�absolute_importN)�protocol)�dns�resolve)�logc @ s� e Zd ZdZejZdZejZ d$dd�Z
dd� Zdd � Zd
d� Z
dd
� Zdd� Zejdddfdd�Zdd� Zdd� Zdd� Zdd� Zdd� Zdd� Zdd� Zd%d d!�Zd"d#� ZdS )&�DNSServerFactorya
Server factory and tracker for L{DNSProtocol} connections. This class also
provides records for responses to DNS queries.
@ivar cache: A L{Cache<twisted.names.cache.CacheResolver>} instance whose
C{cacheResult} method is called when a response is received from one of
C{clients}. Defaults to L{None} if no caches are specified. See
C{caches} of L{__init__} for more details.
@type cache: L{Cache<twisted.names.cache.CacheResolver>} or L{None}
@ivar canRecurse: A flag indicating whether this server is capable of
performing recursive DNS resolution.
@type canRecurse: L{bool}
@ivar resolver: A L{resolve.ResolverChain} containing an ordered list of
C{authorities}, C{caches} and C{clients} to which queries will be
dispatched.
@type resolver: L{resolve.ResolverChain}
@ivar verbose: See L{__init__}
@ivar connections: A list of all the connected L{DNSProtocol} instances
using this object as their controller.
@type connections: C{list} of L{DNSProtocol} instances
@ivar protocol: A callable used for building a DNS stream protocol. Called
by L{DNSServerFactory.buildProtocol} and passed the L{DNSServerFactory}
instance as the one and only positional argument. Defaults to
L{dns.DNSProtocol}.
@type protocol: L{IProtocolFactory} constructor
@ivar _messageFactory: A response message constructor with an initializer
signature matching L{dns.Message.__init__}.
@type _messageFactory: C{callable}
Nr c C sn g }|dk r|� |� |dk r(|� |� |dk r:|� |� | | _t�|�| _|| _|rd|d | _g | _dS )a�
@param authorities: Resolvers which provide authoritative answers.
@type authorities: L{list} of L{IResolver} providers
@param caches: Resolvers which provide cached non-authoritative
answers. The first cache instance is assigned to
C{DNSServerFactory.cache} and its C{cacheResult} method will be
called when a response is received from one of C{clients}.
@type caches: L{list} of L{Cache<twisted.names.cache.CacheResolver>} instances
@param clients: Resolvers which are capable of performing recursive DNS
lookups.
@type clients: L{list} of L{IResolver} providers
@param verbose: An integer controlling the verbosity of logging of
queries and responses. Default is C{0} which means no logging. Set
to C{2} to enable logging of full query and response messages.
@type verbose: L{int}
N���)�extend�
canRecurser Z
ResolverChain�resolver�verbose�cache�connections)�selfZauthoritiesZcachesZclientsr
Z resolvers� r �6/usr/lib/python3/dist-packages/twisted/names/server.py�__init__F s
zDNSServerFactory.__init__c O s | j dkrtj||� dS )z�
Log a message only if verbose logging is enabled.
@param args: Positional arguments which will be passed to C{log.msg}
@param kwargs: Keyword arguments which will be passed to C{log.msg}
r N)r
r �msg)r �args�kwargsr r r �_verboseLogj s
zDNSServerFactory._verboseLogc C s | � | �}| |_|S )N)r �factory)r Zaddr�pr r r �
buildProtocolu s
zDNSServerFactory.buildProtocolc C s | j �|� dS )z�
Track a newly connected L{DNSProtocol}.
@param protocol: The protocol instance to be tracked.
@type protocol: L{dns.DNSProtocol}
N)r �append�r r r r r �connectionMade{ s zDNSServerFactory.connectionMadec C s | j �|� dS )z�
Stop tracking a no-longer connected L{DNSProtocol}.
@param protocol: The tracked protocol instance to be which has been
lost.
@type protocol: L{dns.DNSProtocol}
N)r �remover r r r �connectionLost� s zDNSServerFactory.connectionLostc C s� | j dkr�d�dd� |jD ��}d�dd� |jD ��}d�dd� |jD ��}|s\t�d� n*t�d| � t�d | � t�d
| � |dkr�|�|� n|�||� | �dt � � |j
� dS )
a]
Send a response C{message} to a given C{address} via the supplied
C{protocol}.
Message payload will be logged if C{DNSServerFactory.verbose} is C{>1}.
@param protocol: The DNS protocol instance to which to send the message.
@type protocol: L{dns.DNSDatagramProtocol} or L{dns.DNSProtocol}
@param message: The DNS message to be sent.
@type message: L{dns.Message}
@param address: The address to which the message will be sent or L{None}
if C{protocol} is a stream protocol.
@type address: L{tuple} or L{None}
� � c S s g | ]}t |j��qS r ��strZpayload��.0�ar r r �
<listcomp>� s z.DNSServerFactory.sendReply.<locals>.<listcomp>c S s g | ]}t |j��qS r r"