o
    i©¹d/(  ã                   @   s(   d dl Z d dlmZ G dd„ deƒZdS )é    N)ÚSoSMapc                       s„   e Zd ZdZg d¢ZddgZdZdZdZi Z	i Z
dd„ Zd	d
„ Zdd„ Zdd„ Z‡ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚSoSHostnameMapaL  Mapping store for hostnames and domain names

    Hostnames are obfuscated using an incrementing counter based on the total
    number of hosts matched regardless of domain name.

    Domain names are obfuscated based on the host's hostname, plus any user
    defined domains passed in by the `--domains` options.

    Domains are obfuscated as whole units, meaning the domains 'example.com'
    and 'host.foo.example.com' will be separately obfuscated with no relation
    for example as 'obfuscatedomdain1.com' and 'obfuscatedomain2.com'.

    Top-level domains are left untouched.
    )Ú	localhostz.*localdomain.*z^com..*ÚwwwÚapi)
z.yamlz.ymlz.crtz.keyz.pemz.logz.repoz.rulesz.confz.cfgr   c                 C   sä   | j  ¡ D ]f\}}t| d¡ƒdkr | j | | j| d¡d < q| d¡r0| d¡d | j|< qd | d¡dd… ¡}|s?q| j  ¡ D ]&}d | d¡dd… ¡}||krjd | j |  d¡dd… ¡}|| j|< qDq|  	¡  dS )z×Because we use 'intermediary' dicts for host names and domain names
        in this parser, we need to re-inject entries from the map_file into
        these dicts and not just the underlying 'dataset' dict
        Ú.é   r   ÚobfuscateddomainéÿÿÿÿN)
ÚdatasetÚitemsÚlenÚsplitÚhostsÚ
startswithÚ_domainsÚjoinÚkeysÚset_initial_counts)ÚselfÚdomainÚob_pairÚ_domain_to_injectÚexisting_domainÚ	_existingÚ
_ob_domain© r   úC/usr/lib/python3/dist-packages/sos/cleaner/mappings/hostname_map.pyÚload_domains_from_map3   s&   
ÿ
€úz$SoSHostnameMap.load_domains_from_mapc                 C   s"   d|v r
|  dd¡}t |tj¡S )z³Override the base get_regex_result() to provide a regex that, if
        this is an FQDN or a straight domain, will include an underscore
        formatted regex as well.
        r   z(\.|_))ÚreplaceÚreÚcompileÚI)r   Úitemr   r   r   Úget_regex_resultS   s   zSoSHostnameMap.get_regex_resultc                 C   sš   zt | j ¡ ddd  d¡d }t|ƒd | _W n	 ty"   Y nw zt | j ¡ ddd  d¡}t|d  d¡d ƒd | _W dS  tyL   Y dS w )	zSet the initial counter for host and domain obfuscation numbers
        based on what is already present in the mapping.
        T)Úreverser   Úhostr   r   r   N)	Úsortedr   Úvaluesr   ÚintÚ
host_countÚ
IndexErrorr   Údomain_count)r   ÚhÚdr   r   r   r   \   s    þ"þz!SoSHostnameMap.set_initial_countsc                    sj   || j v rdS | d¡}d | d¡dd… ¡‰ t|ƒdkr%|d | jv S t‡ fdd„| j D ƒƒr3dS dS )	zkCheck if a potential domain is in one of the domains we've loaded
        and should be obfuscated
        Tr   r   r
   r   c                    s   g | ]}ˆ   |¡‘qS r   )Úendswith)Ú.0Ú_d©Úno_tldr   r   Ú
<listcomp>{   s    z@SoSHostnameMap.domain_name_in_loaded_domains.<locals>.<listcomp>F)r   r   r   r   r   Úany)r   r   r&   r   r2   r   Údomain_name_in_loaded_domainsp   s   

z,SoSHostnameMap.domain_name_in_loaded_domainsc              	      sâ  d}d}d }|  d¡r||d 7 }|dd … }|  d¡s| d¡r2||d 7 }|dd… }| d¡s!|| jv r<| j| S |  | ¡ ¡sKd |||g¡S | | j¡rdd| d¡d  }| |d¡}||7 }|| j 	¡ vrát
| j 	¡ ddd	„ d
D ]i}d}| |¡}| d¡}	|	d r”|	d | j 	¡ v r”d}t|ƒdksž|d sŸqw|s·|d  d¡s­| |¡r·tt| ƒ |¡} n*| |d ¡ràz| |d ¡d }
|d tt| ƒ |
¡ }W  n
 tyß   Y qww qw|sëtt| ƒ |¡}|| | S )NÚ )r   Ú_r   r   r
   r   Tc                 S   s   t | ƒS )N)r   )Úxr   r   r   Ú<lambda>   s    z$SoSHostnameMap.get.<locals>.<lambda>)r%   ÚkeyF)r   r/   r   r6   Úlowerr   Ú
strip_extsr   r   r   r'   r   r   Úsuperr   ÚgetÚ	Exception)r   r#   ÚprefixÚsuffixÚfinalÚextr   Ú_host_substrÚ_testÚ_hÚitm©Ú	__class__r   r   r?   €   s^   

þ

þ


ÿ

ÿþùzSoSHostnameMap.getc           	      C   sà   |  d¡}t|ƒdkr|  |d  ¡ ¡S t|ƒdkr.|  |¡}tdd„ |D ƒƒr,| ¡ }|S t|ƒdkrn|d }|dd … }t|ƒdkrL|  | ¡ ¡}nd}|  |¡}|| j|< d ||g¡}tdd„ |D ƒƒrl| ¡ }|S d S )	Nr   r   r   é   c                 S   ó   g | ]}|  ¡ ‘qS r   ©Úisupper©r0   r-   r   r   r   r4   Ä   ó    z0SoSHostnameMap.sanitize_item.<locals>.<listcomp>Úunknownc                 S   rL   r   rM   rO   r   r   r   r4   ×   rP   )	r   r   Úsanitize_short_namer<   Úsanitize_domainÚallÚupperr   r   )	r   r#   r&   ÚdnameÚhostnamer   Úob_hostnameÚ	ob_domainÚ_fqdnr   r   r   Úsanitize_item¼   s*   



îzSoSHostnameMap.sanitize_itemc                 C   s\   |r|| j v r	|S || jvr)d| j }|| j|< |  jd7  _|| j|< |  |¡ | j| S )z„Obfuscate the short name of the host with an incremented counter
        based on the total number of obfuscated host names
        zhost%sr   )Ú	skip_keysr   r*   r   Úadd_regex_item)r   rW   Úob_hostr   r   r   rR   Û   s   





z"SoSHostnameMap.sanitize_short_namec                 C   sz   | j D ]}t |d |¡¡rd |¡  S q|d  ¡ }d |dd… ¡ ¡ }|  |¡}d ||g¡}|| jd |¡< |S )zeObfuscate the domainname, broken out into subdomains. Top-level
        domains are ignored.
        r   r
   r   )Úignore_matchesr    Úmatchr   r<   Ú_new_obfuscated_domainr   )r   r   Ú_skipÚ
top_domainrV   rY   r   r   r   rS   é   s   
ÿ
zSoSHostnameMap.sanitize_domainc                 C   s2   || j vrd| j | j |< |  jd7  _| j | S )zDGenerate an obfuscated domain for each subdomain name given
        zobfuscateddomain%sr   )r   r,   )r   rV   r   r   r   ra   ø   s   

z%SoSHostnameMap._new_obfuscated_domain)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r_   r\   r=   r*   r,   r   r   r   r$   r   r6   r?   r[   rR   rS   ra   Ú__classcell__r   r   rI   r   r      s(    þ 	<r   )r    Úsos.cleaner.mappingsr   r   r   r   r   r   Ú<module>   s   
