hf*dZ ddlZn #e$rdZYnwxYwddlZddlZddlmZmZddlmZddl m Z m Z m Z m Z mZmZddlmZmZmZmZmZddlmZddlmZdd lmZdd lmZGd d ZGd dZdS)zo maxminddb.reader ~~~~~~~~~~~~~~~~ This module contains the pure Python database reader and related classes. N) IPv4Address IPv6Address)PathLike)AnyAnyStrIOOptionalTupleUnion) MODE_AUTO MODE_MMAP MODE_FILE MODE_MEMORYMODE_FD)Decoder)InvalidDatabaseError) FileBuffer)Recordc^eZdZUdZdZdZeeedfe d<dZ e e e d<e fdeee eefd e d dfd Zdd Zdeeeefd e efdZdeeeefd ee ee ffdZded ee e ffdZde d e fdZde de d e fdZde d efdZddZddZ ddZ!dS) Readerz Instances of this class provide a reader for the MaxMind DB format. IP addresses can be looked up using the ``get`` method. sMaxMind.comz mmap.mmap_bufferN _ipv4_startdatabasemodereturncn|tkrts |tkrt|d5}tj|dtj|_|j|_dddn #1swxYwY|}n|ttfvr5t||_|j|_|}n|tkrZt|d5}| }||_t||_dddn #1swxYwY|}nX|tkr:| |_t|j|_|j}nt!d|d|j|jt'd|jdz }|dkr'|t+d |d |t|jz }t-|j|}||\}} t1|t2st+d |d t5di||_t-|j|jj|jz|_d |_dS)aReader for the MaxMind DB file format Arguments: database -- A path to a valid MaxMind DB file such as a GeoIP2 database file, or a file descriptor in the case of MODE_FD. mode -- mode to open the database with. Valid mode are: * MODE_MMAP - read from memory map. * MODE_FILE - read database as standard file. * MODE_MEMORY - load database into memory. * MODE_AUTO - tries MODE_MMAP and then MODE_FILE. Default. * MODE_FD - the param passed via database is a file descriptor, not a path. This mode implies MODE_MEMORY. rbr)accessNzUnsupported open mode (z]). Only MODE_AUTO, MODE_FILE, MODE_MEMORY and MODE_FD are supported by the pure Python ReaderizError opening database file (z#). Is this a valid MaxMind DB file?z)Error reading metadata in database file (z).F) r mmapr openfileno ACCESS_READrsize _buffer_sizerrrreadlenrname ValueErrorrfind_METADATA_START_MARKERmaxcloserrdecode isinstancedictMetadata _metadatasearch_tree_size_DATA_SECTION_SEPARATOR_SIZE_decoderclosed) selfrrdb_filefilenamebufmetadata_startmetadata_decodermetadata_s p/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib/python3.11/site-packages/maxminddb/reader.py__init__zReader.__init__'s$" I  $ 49+<+<h%% 8#y)9)91TEUVVV $(L$5$5$7$7! 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 HH i+ + +%h//DL $ 1 1 3 3D HH [ h%% -llnn" $'HH! - - - - - - - - - - - - - - - HH W__#==??DL #DL 1 1D }HH$  ++  'Q0AJ0N)O)O   R   JJLLL&3333  #d9:::"4<@@(//?? 1(D)) &HHHHH "--H-- L N +d.O O    s$ABBB0D<<EEr3c|jS)z7Return the metadata associated with the MaxMind DB file)r4r9s rAr?zReader.metadatans ~ ip_addressc6||\}}|S)zReturn the record for the ip_address in the MaxMind DB Arguments: ip_address -- an IP address in the standard string notation )get_with_prefix_len)r9rFrecordr@s rAgetz Reader.getrs ..z::  rEct|trtj|}n|} t |j}n"#t $r}td|d}~wwxYw|jdkr#|j j dkrtd|d| |\}}|r| ||fSd|fS)zReturn a tuple with the record and the associated prefix length Arguments: ip_address -- an IP address in the standard string notation z/argument 1 must be a string or ipaddress objectNzError looking up zD. You attempted to look up an IPv6 address in an IPv4-only database.)r1str ipaddressrF bytearraypackedAttributeError TypeErrorversionr4 ip_versionr+_find_address_in_tree_resolve_data_pointer)r9rFaddresspacked_addressexpointer prefix_lens rArHzReader.get_with_prefix_len|s j# & & !*:66GG G W&w~66NN W W WMNNTV V W ?a  DN$=$B$B J J*  C--g66 B BZsA A" AA"rQcFt|dz}||}|jj}d}||krD||kr>d||dz d|dzz z z}|||}|dz}||kr||k>||krd|fS||kr||fSt d)NrzInvalid node in search tree)r) _start_noder4 node_count _read_noder)r9rQ bit_countnodercibits rArVzReader._find_address_in_treesKK!O  **^. )mmz 1 1va1f~a!e45C??4--DAA)mmz 1 1 :  a4K *  7N"#@AAArElengthc|jjdks|dkrdS|jr|jSd}tdD]*}||jjkrn||d}+||_|S)NrLr`)r4rUrrangercrd)r9rirfr@s rArbzReader._start_nodes > $ ) )Vs]]1   $# #r , ,At~000??4++DD rE node_numberindexc||jjz}|jj}|dkr||dzz}d|j||dzz}n|dkri|d|zz}t |j||dz}|rd|dz|d<ndd|zdz }|d|n3|d kr||dzz}|j||dz}ntd |tj d |dS) Nr`rMr zUnknown record size: s!I) r4node_byte_size record_sizerrPpopinsertrstructunpack)r9rnro base_offsetrxoffset node_bytesmiddles rArdzReader._read_nodes2!DN$AA n0 "   519,F 4<!0C#DDJJ B   1u9,F"4<!0C#DEEJ - $z!} 4 1 !1!11a7!!!V,,,, B   519,Ffvz&9:JJ&'L{'L'LMM M}UJ//22rEr[c||jjz |jjz}||jkrt d|j|\}}|S)Nz,The MaxMind DB file's search tree is corrupt)r4rcr5r'rr7r0)r9r[resolveddatar@s rArWzReader._resolve_data_pointersVT^669XX t( ( (&'UVV VM((22 q rEcj |jn#t$rYnwxYwd|_dS)zBCloses the MaxMind DB file and returns the resources to the systemTN)rr/rRr8rDs rAr/z Reader.closesI  L       D  s  ))c.|dS)N)r/r9argss rA__exit__zReader.__exit__s rEc2|jrtd|S)Nz%Attempt to reopen a closed MaxMind DB)r8r+rDs rA __enter__zReader.__enter__s! ; FDEE E rE)rr3rN)rr)"__name__ __module__ __qualname____doc__r6r-r bytesr__annotations__rr intr rrrrBr?rNrrrrJr rHrPrVrbrdrWr/rrr!rErArrs  $& 7 5*k1 2222!%K#%%%GPEEfc8R78E@CE EEEENeCk$ABxPVGW [+ => x$ %    >BIB%S/BBBB&##"3c3#3#3333,SVrErcXeZdZdZddZedefdZedefdZdZ dS) r3aMetadata for the MaxMind DB reader .. attribute:: binary_format_major_version The major version number of the binary format used when creating the database. :type: int .. attribute:: binary_format_minor_version The minor version number of the binary format used when creating the database. :type: int .. attribute:: build_epoch The Unix epoch for the build time of the database. :type: int .. attribute:: database_type A string identifying the database type, e.g., "GeoIP2-City". :type: str .. attribute:: description A map from locales to text descriptions of the database. :type: dict(str, str) .. attribute:: ip_version The IP version of the data in a database. A value of "4" means the database only supports IPv4. A database with a value of "6" may support both IPv4 and IPv6 lookups. :type: int .. attribute:: languages A list of locale codes supported by the databse. :type: list(str) .. attribute:: node_count The number of nodes in the database. :type: int .. attribute:: record_size The bit size of a record in the search tree. :type: int rNc |d|_|d|_|d|_|d|_|d|_|d|_|d|_|d|_|d |_d S) zACreates new Metadata object. kwargs are key/value pairs from specrcrxrU database_type languagesbinary_format_major_versionbinary_format_minor_version build_epoch descriptionN) rcrxrUrrrrrr)r9kwargss rArBzMetadata.__init__0s~!.!-0 .#O4 ,+12O+P(+12O+P(!-0!-0rEc|jdzS)z8The size of a node in bytes :type: int rM)rxrDs rArwzMetadata.node_byte_size>s 1$$rEc |j|jzS)z8The size of the search tree :type: int )rcrwrDs rAr5zMetadata.search_tree_sizeFs !444rEcdd|jD}|jd|jjd|dS)Nz, c3*K|]\}}|d|VdS)=Nr!).0kvs rA z$Metadata.__repr__..Os0HH$!QAHHHHHHrE.())join__dict__itemsr __class__rrs rA__repr__zMetadata.__repr__NsVyyHH$-2E2E2G2GHHHHH/EEDN$;EEdEEEErEr) rrrrrBpropertyrrwr5rr!rErAr3r3s==@ 1 1 1 1%%%%X%5#555X5FFFFFrEr3) rr" ImportErrorrOr{rrosrtypingrrrr r r maxminddb.constr r rrrmaxminddb.decoderrmaxminddb.errorsrmaxminddb.filermaxminddb.typesrrr3r!rErArsKKKK DDD ........::::::::::::::::QQQQQQQQQQQQQQ%%%%%%111111%%%%%%""""""QQQQQQQQhaFaFaFaFaFaFaFaFaFaFs