
    h)A                         S r SSKrSSKrSSKJr  SSKJrJrJr  SSK	J
r
  SSKJrJr  SSKJr  SS jr " S	 S
\R"                  5      r " S S\5      r " S S\5      rg)z
Authoritative resolvers.
    N)defer)commondnserror)failure)execfilenativeString)FilePathc                    [         R                  " S5      n[        R                  " S5      n [        R                  R                  U 5      (       d)  [        U S5       nUR                  US-   5        SSS5        [        R                  " U5        [        U 5       nUR                  5       R                  5       u  pVSSS5        WU:H  =(       a    [        W5      S-   =(       d    Sn[        U S5       nUR                  SX4-  5        SSS5        US	U4-  -   nU$ ! , (       d  f       N= f! [        R                  " U5        f = f! , (       d  f       N= f! , (       d  f       NV= f)
a  
Return a monotonically increasing (across program runs) integer.

State is stored in the given file.  If it does not exist, it is
created with rw-/---/--- permissions.

This manipulates process-global state by calling C{os.umask()}, so it isn't
thread-safe.

@param filename: Path to a file that is used to store the state across
    program runs.
@type filename: L{str}

@return: a monotonically increasing number
@rtype: L{str}
z%Y%m%d   wz 0N   r   z%s %dz%02d)timestrftimeosumaskpathexistsopenwritereadlinesplitint)filenameserialof
serialFile
lastSerialzoneIDs          ^/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/names/authority.py	getSerialr"      s   " ]]8$F
Aww~~h''h$& % 		h:'00288:
 
 F"9Vq>QF	h	
F#334 
 v	)*FM %$ 		
 
	s;   0D. D3D. !E	4E
D+'D. .E	
E
E(c                       \ rS rSrSr\R                  \R                  \R                  4r	\R                  \R                  4rSrSrS rS rS rS rSS jrSS	 jrS
 rSrg)FileAuthority;   a}  
An Authority that is loaded from a file.

This is an abstract class that implements record search logic. To create
a functional resolver, subclass it and override the L{loadFile} method.

@ivar _ADDITIONAL_PROCESSING_TYPES: Record types for which additional
    processing will be done.

@ivar _ADDRESS_TYPES: Record types which are useful for inclusion in the
    additional section generated during additional processing.

@ivar soa: A 2-tuple containing the SOA domain name as a L{bytes} and a
    L{dns.Record_SOA}.

@ivar records: A mapping of domains (as lowercased L{bytes}) to records.
@type records: L{dict} with L{bytes} keys
Nc                 r    [         R                  R                  U 5        U R                  U5        0 U l        g N)r   ResolverBase__init__loadFile_cacheselfr   s     r!   r)   FileAuthority.__init__V   s)    $$T*h    c                     Xl         g r'   )__dict__)r-   states     r!   __setstate__FileAuthority.__setstate__[   s    r/   c                     g)a*  
Load DNS records from a file.

This method populates the I{soa} and I{records} attributes. It must be
overridden in a subclass. It is called once from the initializer.

@param filename: The I{filename} parameter that was passed to the
initilizer.

@returns: L{None} -- the return value is ignored
N r,   s     r!   r*   FileAuthority.loadFile^   s    r/   c              #     #    X-    H  nUR                   U R                  ;   d  M  UR                  R                  R                  nU R                  R                  UR                  5       S5       Hd  nUR                  U R                  ;   d  M  [        R                  " XVR                  [        R                  UR                  =(       d    UUSS9v   Mf     M     g7f)a  
Find locally known information that could be useful to the consumer of
the response and construct appropriate records to include in the
I{additional} section of that response.

Essentially, implement RFC 1034 section 4.3.2 step 6.

@param answer: A L{list} of the records which will be included in the
    I{answer} section of the response.

@param authority: A L{list} of the records which will be included in
    the I{authority} section of the response.

@param ttl: The default TTL for records for which this is not otherwise
    specified.

@return: A generator of L{dns.RRHeader} instances for inclusion in the
    I{additional} section.  These instances represent extra information
    about the records in C{answer} and C{authority}.
r6   TauthN)type_ADDITIONAL_PROCESSING_TYPESpayloadnamerecordsgetlowerTYPE_ADDRESS_TYPESr   RRHeaderINttl)r-   answer	authorityrF   recordr>   recs          r!   _additionalRecords FileAuthority._additionalRecordsk   s     * (F{{d???~~**//<<++DJJL"=Cxx4#6#66!ll ((CFFCGGNsCd  > )s    CA%CACc                    / n/ n/ n/ n[        U R                  S   R                  U R                  S   R                  5      n	U R                  R                  UR                  5       5      n
U
(       Ga=  U
 GHm  nUR                  b  UR                  nOU	nUR                  [        R                  :X  an  UR                  5       U R                  S   R                  5       :w  a?  UR                  [        R                  " XR                  [        R                  XSS95        ObUR                  U:X  d  U[        R                  :X  a>  UR                  [        R                  " XR                  [        R                  XSS95        UR                  [        R                  :X  d  GM/  UR                  [        R                  " XR                  [        R                  XSS95        GMp     U(       d  UnU R!                  XgU	5      nU(       a  UR#                  U5        OUR#                  U5        U(       de  U(       d^  UR                  [        R                  " U R                  S   [        R$                  [        R                  WU R                  S   SS95        [&        R(                  " XgU45      $ [        R*                  " XR                  S   5      (       a>  [&        R,                  " [.        R0                  " [        R2                  " U5      5      5      $ [&        R,                  " [.        R0                  " [4        R6                  " U5      5      5      $ )a  
Determine a response to a particular DNS query.

@param name: The name which is being queried and for which to lookup a
    response.
@type name: L{bytes}

@param cls: The class which is being queried.  Only I{IN} is
    implemented here and this value is presently disregarded.
@type cls: L{int}

@param type: The type of records being queried.  See the types defined
    in L{twisted.names.dns}.
@type type: L{int}

@param timeout: All processing is done locally and a result is
    available immediately, so the timeout value is ignored.

@return: A L{Deferred} that fires with a L{tuple} of three sets of
    response records (to comprise the I{answer}, I{authority}, and
    I{additional} sections of a DNS response) or with a L{Failure} if
    there is a problem processing the query.
r   r   Fr9   T)maxsoaminimumexpirer?   r@   rA   rF   rB   r   NSappendrD   rE   ALL_RECORDSCNAMErK   extendSOAr   succeed_isSubdomainOffailr   FailureAuthoritativeDomainErrorr   DomainError)r-   r>   clsr;   timeoutcnamesresultsrH   
additionaldefault_ttldomain_recordsrI   rF   additionalInformations                 r!   _lookupFileAuthority._lookup   sS   0 	
$((1+--txx{/A/AB))$**,7(::) **C%C;;#&&(TZZ\TXXa[=N=N=P-P $$T;;RWX [[D(DCOO,CNNT;;RVW ;;#))+MMT;;RVW% )*   %)$;$;K%! 45!!"789   LLSWWcffc488A;T
 =='j!ABB!!$44 zz'//#2N2Nt2T"UVV
 zz'//%2C2CD2I"JKKr/   c                 .   [         R                  " U5      nU R                  S   R                  5       UR                  5       :X  Ga  [	        U R                  S   R
                  U R                  S   R                  5      nU R                  S   R                  b  U R                  S   R                  nOUn[         R                  " U R                  S   [         R                  [         R                  X@R                  S   SS9/nU R                  R                  5        H  u  pgU H}  nUR                  b  UR                  n	OUn	UR                  [         R                  :w  d  M?  UR                  [         R                  " XhR                  [         R                  XSS95        M     M     UR                  US   5        [        R                   " USS45      $ [        R"                  " [$        R&                  " [         R(                  " U5      5      5      $ )Nr   r   Tr9   r6   )r   domainStringrO   rA   rN   rP   rQ   rF   rD   rW   rE   r?   itemsrB   rS   r   rX   rZ   r   r[   r]   )
r-   r>   r_   rc   soa_ttlra   krrJ   rF   s
             r!   
lookupZoneFileAuthority.lookupZone   ss   %88A;$**,.dhhqk11488A;3E3EFKxx{*((1+//%HHQK#&&'88A;TG
 **,Cww*!gg)xx377*LLHHcffcTR  - NN71:&=='2r!233zz'//#//$*?@AAr/   c                     / / / pCnU HT  nUS   (       d  M  UR                  US   S   5        UR                  US   S   5        UR                  US   S   5        MV     X#U4$ )Nr   r      )rV   )r-   ra   ansr:   address         r!   _cbAllRecordsFileAuthority._cbAllRecords   sh    R3C1vv

3q6!9%CF1I&

3q6!9%	 
 #~r/   )r1   r+   r'   )
   )__name__
__module____qualname____firstlineno____doc__r   rU   MXrR   r<   AAAAArC   rO   r?   r)   r3   r*   rK   rf   rn   ru   __static_attributes__r6   r/   r!   r$   r$   ;   se    ( %(IIsvvsvv#> eeSXX&N
CG
<WLrB8r/   r$   c                   *    \ rS rSrSrS rS rS rSrg)PySourceAuthorityi  z;
A FileAuthority that is built up from Python source code.
c                 Z   U R                  5       0 p2[        XU5        SU;  a  [        SU-   5      e0 U l        US    Hj  n[	        US   [
        R                  5      (       a  X@l        U R                  R                  US   R                  5       / 5      R                  US   5        Ml     g )NzonezNo zone defined in r   r   )setupConfigNamespacer   
ValueErrorr?   
isinstancer   
Record_SOArO   
setdefaultrA   rS   )r-   r   glrrs        r!   r*   PySourceAuthority.loadFile  s    ((*B1a ?2X=>>F)B"Q%00LL##BqEKKM26==beD r/   c                    ^ U4S jnU$ )Nc                 @   > [         R                  " U 5      T" U0 UD64$ r'   )r   ri   )r>   argkwr;   s      r!   wrapRecordFunc4PySourceAuthority.wrapRecord.<locals>.wrapRecordFunc  s#    $$T*D#,<,<==r/   r6   )r-   r;   r   s    ` r!   
wrapRecordPySourceAuthority.wrapRecord  s    	> r/   c                    0 n[         R                  R                  5       nU Vs/ s H  o3R                  S5      (       d  M  UPM     sn H4  n[	        [         U5      nU R                  U5      nXaU[        S5      S  '   M6     U$ s  snf )NRecord_)r   r1   keys
startswithgetattrr   len)r-   rm   rj   xrI   r;   r   s          r!   r   &PySourceAuthority.setupConfigNamespace  sw    !!#"'C%Q<<	+Bq%CF3'D%A*+fS^%&' D 	 Ds   BB)r?   rO   N)	rx   ry   rz   r{   r|   r*   r   r   r   r6   r/   r!   r   r     s    
Er/   r   c                   B    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
rg)BindAuthorityi)  z
An Authority that loads U{BIND zone files
<https://en.wikipedia.org/wiki/Zone_file>}.

Supports only C{$ORIGIN} and C{$TTL} directives.
c                     [        U5      n[        UR                  5       S-   5      U l        UR	                  5       R                  S5      nU R                  U5      nU R                  U5      nU R                  U5        g)z]
Load records from C{filename}.

@param filename: file to read from
@type filename: L{bytes}
   .TN)	r
   r	   basenameorigin
getContent
splitlinesstripCommentscollapseContinuations
parseLines)r-   r   fpliness       r!   r*   BindAuthority.loadFile1  si     h #2;;=4#78**40""5)**51r/   c                 Z    S U Vs/ s H  o"R                  5       PM     sn 5       $ s  snf )z
Strip comments from C{lines}.

@param lines: lines to work on
@type lines: iterable of L{bytes}

@return: C{lines} sans comments.
c              3      #    U  H=  nUR                  S 5      S:H  =(       a    U=(       d    USUR                  S 5       v   M?     g7f)   ;N)find).0as     r!   	<genexpr>.BindAuthority.stripComments.<locals>.<genexpr>K  s@      
/ FF4LB$19.AFF4L(99/s   AA)strip)r-   r   bs      r!   r   BindAuthority.stripCommentsB  s*    
)./Aggi/
 	
/s   (c           
         / nSnU H  nUS:X  aO  UR                  S5      S:X  a  UR                  U5        M1  UR                  USUR                  S5       5        SnMX  UR                  S5      S:w  a&  US==   SUSUR                  S5       -   -  ss'   SnM  US==   SU-   -  ss'   M     [        SS U 5       5      $ )	z
Transform multiline statements into single lines.

@param lines: lines to work on
@type lines: iterable of L{bytes}

@return: iterable of continuous lines
r      (r   Nr      )    c              3   @   #    U  H  oR                  5       v   M     g 7fr'   )r   )r   lines     r!   r   6BindAuthority.collapseContinuations.<locals>.<genexpr>h  s     8adZZ\\as   )r   rS   filter)r-   r   r   r2   r   s        r!   r   #BindAuthority.collapseContinuationsP  s     Dz99T?b(HHTNHHT"3DIIdO45E99T?b(bETD):499T?$;;;EEbETD[(E  d8a899r/   c                 (   SnU R                   n0 U l        U Hq  nUS   S:X  a  [        R                  " US   5      nM'  US   S:X  a  US   nM7  US   S:X  a  [	        S5      eUS   S:X  a  [	        S	5      eU R                  X2U5        Ms     X0l         g
)zS
Parse C{lines}.

@param lines: lines to work on
@type lines: iterable of L{bytes}
i0*  r   s   $TTLr   s   $ORIGINs   $INCLUDEz"$INCLUDE directive not implementeds	   $GENERATEz#$GENERATE directive not implementedN)r   r?   r   str2timeNotImplementedErrorparseRecordLine)r-   r   rF   r   r   s        r!   r   BindAuthority.parseLinesj  s     DAw'!ll47+aJ&aaK')*NOOaL()*OPP$$V$7  r/   c                     UR                  S5      (       d  US-   USS -   nOUSS n[        U SU 3S5      nU(       a
  U" X#XF5        g[        SU< S35      e)a  
Add a record to our authority.  Expand domain with origin if necessary.

@param owner: origin?
@type owner: L{bytes}

@param ttl: time to live for the record
@type ttl: L{int}

@param domain: the domain for which the record is to be added
@type domain: L{bytes}

@param type: record type
@type type: L{str}

@param cls: record class
@type cls: L{str}

@param rdata: record data
@type rdata: L{list} of L{bytes}
r   Nr   class_zRecord class  not supported)endswithr   r   )r-   ownerrF   r;   domainr^   rdatar   s           r!   	addRecordBindAuthority.addRecord  sg    , t$$d]U3BZ/FCR[FDF3%.$/c'%cWN&KLLr/   c                 $   [        [        S[        U5       3S5      nU(       aT  U" U6 nXl        U R                  R                  UR                  5       / 5      R                  U5        US:X  a	  X64U l        gg[        S[        U5      < S35      e)z
Simulate a class IN and recurse into the actual class.

@param ttl: time to live for the record
@type ttl: L{int}

@param type: record type
@type type: str

@param domain: the domain
@type domain: bytes

@param rdata:
@type rdata: bytes
r   NrW   zRecord type r   )
r   r   r	   rF   r?   r   rA   rS   rO   r   )r-   rF   r;   r   r   rI   rm   s          r!   class_INBindAuthority.class_IN  s      T(:';<dCAELL##FLLNB7>>qAu}";  &|D14NC r/   c           	      L   [         R                  R                  5        Vs1 s H  oDR                  S5      iM     nn[         R                  R                  5        Vs1 s H  ofR                  S5      iM     nnXW-  nSn	Un
US   S:X  a  USS nUn
O+US   R                  5       (       d  US   U;  a
  US   n
USS nUS   R                  5       (       d	  US   U;   a  U
nUn
O
US   nUSS nUS   U;   a6  US   n	USS nUS   R                  5       (       a  [        US   5      nUSS nO>US   R                  5       (       a&  [        US   5      nUSS nUS   U;   a
  US   n	USS nUS   nUSS nU R                  X[        U5      U[        U	5      U5        gs  snf s  snf )a;  
Parse a C{line} from a zone file respecting C{origin} and C{ttl}.

Add resulting records to authority.

@param origin: starting point for the zone
@type origin: L{bytes}

@param ttl: time to live for the record
@type ttl: L{int}

@param line: zone file line to parse; split by word
@type line: L{list} of L{bytes}
asciis   INr      @r   N)	r   QUERY_CLASSESvaluesencodeQUERY_TYPESisdigitr   r   r	   )r-   r   rF   r   qcqueryClassesqt
queryTypesmarkersr^   r   r   r;   r   s                 r!   r   BindAuthority.parseRecordLine  s    695F5F5M5M5OP5Or		'*5OP36??3I3I3KL3KRii(3K
L+7d?8DEa""tAwg'=GE8D7??Q7 2FE!WF8D7l"q'C8DAw  $q'lABx!W__d1g,C8DAw,&1gABxAwQRu<#5v|C?PRWXM QLs   FF!)r   r?   rO   N)rx   ry   rz   r{   r|   r*   r   r   r   r   r   r   r   r6   r/   r!   r   r   )  s-    "
:46M@:5Yr/   r   )z/tmp/twisted-names.serial)r|   r   r   twisted.internetr   twisted.namesr   r   r   twisted.pythonr   twisted.python.compatr   r	   twisted.python.filepathr
   r"   r(   r$   r   r   r6   r/   r!   <module>r      s\   

 
  " , , " 8 ,$NJF'' JZ BNYM NYr/   