o
    zqf)  ã                   @   sÌ   d dl Z d dlZd dlZd dlZd dlZd dlZe  e¡ZG dd„ dƒZ	G dd„ de	ƒZ
G dd„ de
ƒZG dd	„ d	e	ƒZG d
d„ de	ƒZG dd„ deƒZG dd„ de	ƒZG dd„ de	ƒZG dd„ de	ƒZdS )é    Nc                   @   ó   e Zd Zdd„ Zdd„ ZdS )Ú
BasePluginc                 C   s
   || _ d S ©N)Úsource©ÚselfÚsrc© r	   úW/root/parts/websockify/install/lib/python3.10/site-packages/websockify/token_plugins.pyÚ__init__   s   
zBasePlugin.__init__c                 C   s   d S r   r	   ©r   Útokenr	   r	   r
   Úlookup   s   zBasePlugin.lookupN)Ú__name__Ú
__module__Ú__qualname__r   r   r	   r	   r	   r
   r      s    r   c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚReadOnlyTokenFilec                    s   t ƒ j|i |¤Ž d | _d S r   )Úsuperr   Ú_targets©r   ÚargsÚkwargs©Ú	__class__r	   r
   r      s   
zReadOnlyTokenFile.__init__c              
      sÎ   t j ˆ j¡r‡ fdd„t  ˆ j¡D ƒ}nˆ jg}i ˆ _d}|D ]D}dd„ t|ƒ ¡ D ƒD ]6}|r_| d¡s_zt	 
d|¡\}}| ¡  dd¡ˆ j|< W n ty^   t dˆ j|f ¡ Y nw |d7 }q-q d S )	Nc                    s   g | ]
}t j ˆ j|¡‘qS r	   )ÚosÚpathÚjoinr   )Ú.0Úf©r   r	   r
   Ú
<listcomp>   s    ÿz3ReadOnlyTokenFile._load_targets.<locals>.<listcomp>é   c                 S   s   g | ]}|  ¡ ‘qS r	   )Ústrip)r   Úlr	   r	   r
   r    %   s    ú#z:\sú:zSyntax error in %s on line %d)r   r   Úisdirr   Úlistdirr   ÚopenÚ	readlinesÚ
startswithÚreÚsplitr"   ÚrsplitÚ
ValueErrorÚloggerÚerror)r   Ú	cfg_filesÚindexr   ÚlineÚtokÚtargetr	   r   r
   Ú_load_targets   s&   

ÿÿ
ùÿzReadOnlyTokenFile._load_targetsc                 C   s*   | j d u r	|  ¡  || j v r| j | S d S r   )r   r6   r   r	   r	   r
   r   .   s
   


zReadOnlyTokenFile.lookup)r   r   r   r   r6   r   Ú__classcell__r	   r	   r   r
   r      s    r   c                       s   e Zd Z‡ fdd„Z‡  ZS )Ú	TokenFilec                    s   |   ¡  tƒ  |¡S r   )r6   r   r   r   r   r	   r
   r   ?   s   zTokenFile.lookup)r   r   r   r   r7   r	   r	   r   r
   r8   ;   s    r8   c                       ó$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚTokenFileNamec                    s$   t ƒ  |¡ tj |¡stdƒ‚d S )Nz)TokenFileName plugin requires a directory)r   r   r   r   r&   Ú	Exceptionr   r   r	   r
   r   H   s   ÿzTokenFileName.__init__c                 C   sB   t j |¡}t j | j|¡}t j |¡rt|ƒ ¡  ¡  	d¡S d S )Nr%   )
r   r   Úbasenamer   r   Úexistsr(   Úreadr"   r,   )r   r   r   r	   r	   r
   r   M   s
   zTokenFileName.lookup©r   r   r   r   r   r7   r	   r	   r   r
   r:   D   s    r:   c                   @   r   )ÚBaseTokenAPIc                 C   s$   |j  d¡\}}| dd¡}||gS )Nr%   ÚasciiÚignore)Útextr,   Úencode)r   ÚrespÚhostÚportr	   r	   r
   Úprocess_result]   s   zBaseTokenAPI.process_resultc                 C   s,   dd l }| | j| ¡}|jr|  |¡S d S )Nr   )ÚrequestsÚgetr   ÚokrH   )r   r   rI   rE   r	   r	   r
   r   b   s
   
zBaseTokenAPI.lookupN)r   r   r   rH   r   r	   r	   r	   r
   r@   V   s    r@   c                   @   ó   e Zd Zdd„ ZdS )ÚJSONTokenApic                 C   s   |  ¡ }|d |d fS )NrF   rG   )Újson)r   rE   Ú	resp_jsonr	   r	   r
   rH   q   s   zJSONTokenApi.process_resultN)r   r   r   rH   r	   r	   r	   r
   rM   m   ó    rM   c                   @   rL   )ÚJWTTokenApic              
   C   sâ  záddl m}m} dd l}| ¡ }zt| jdƒ}| ¡ }W d   ƒ n1 s'w   Y  W n tyJ } zt	 
dt|ƒ ¡ W Y d }~W d S d }~ww z| |¡ W n   z|j| d¡dd W n   t	 
d¡ Y Y W d S Y zS|j||d	}| |j¡}	d
|	v r‹|j||jd	}| |j¡}
d|
v r¦t ¡ |
d k r¦t	 d¡ W W d S d|
v r»t ¡ |
d kr»t	 d¡ W W d S |
d |
d fW W S  tyá } zt	 
dt|ƒ ¡ W Y d }~W d S d }~ww  tyð   t	 
d¡ Y d S w )Nr   )ÚjwtÚjwkÚrbzError loading key file: %súutf-8Úoct)ÚkÚktyz#Failed to correctly parse key data!)ÚkeyrR   ÚencÚnbfzToken can not be used yet!ÚexpzToken has expired!rF   rG   zFailed to parse token: %szGpackage jwcrypto not found, are you sure you've installed it correctly?)ÚjwcryptorR   rS   rN   ÚJWKr(   r   r>   r;   r/   r0   ÚstrÚimport_from_pemÚ
import_keyÚdecodeÚJWTÚloadsÚheaderÚclaimsÚtimeÚwarningÚImportError)r   r   rR   rS   rN   rY   Úkey_fileÚkey_dataÚeÚparsed_headerÚparsedr	   r	   r
   r   z   s^   
ÿ€€þ

ý

€þ
þzJWTTokenApi.lookupN)r   r   r   r   r	   r	   r	   r
   rQ   v   rP   rQ   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
TokenRedisaž  Token plugin based on the Redis in-memory data store.

    The token source is in the format:

        host[:port[:db[:password]]]

    where port, db and password are optional. If port or db are left empty
    they will take its default value, ie. 6379 and 0 respectively.

    If your redis server is using the default port (6379) then you can use:

        my-redis-host

    In case you need to authenticate with the redis server and you are using
    the default database and port you can use:

        my-redis-host:::verysecretpass

    In the more general case you will use:

        my-redis-host:6380:1:verysecretpass

    The TokenRedis plugin expects the format of the target in one of these two
    formats:

    - JSON

        {"host": "target-host:target-port"}

    - Plain text

        target-host:target-port

    Prepare data with:

        redis-cli set my-token '{"host": "127.0.0.1:5000"}'

    Verify with:

        redis-cli --raw get my-token

    Spawn a test "server" using netcat

        nc -l 5000 -v

    Note: This Token Plugin depends on the 'redis' module, so you have
    to install it before using this plugin:

          pip install redis
    c                 C   sl  zdd l }W n ty   t d¡ t ¡  Y nw d| _d| _d | _z~| 	d¡}t
|ƒdkr4|d | _nSt
|ƒdkrG|\| _| _| jsFd| _n@t
|ƒdkrb|\| _| _| _| js[d| _| jsad| _n%t
|ƒdkr…|\| _| _| _| _| jsxd| _| js~d| _| js„d | _nt‚t| jƒ| _t| jƒ| _t d	| j| jf ¡ W d S  tyµ   t d
| ¡ t ¡  Y d S w )Nr   zUnable to load redis moduleië  r%   r!   é   é   é   z%TokenRedis backend initilized (%s:%s)zbThe provided --token-source='%s' is not in the expected format <host>[:<port>[:<db>[:<password>]]])Úredisri   r/   r0   ÚsysÚexitÚ_portÚ_dbÚ	_passwordr,   ÚlenÚ_serverr.   ÚintÚinfo)r   r   rs   Úfieldsr	   r	   r
   r   ã   s\   
þ
€€€
ÿþüzTokenRedis.__init__c           	      C   sB  zdd l }W n ty   t d¡ t ¡  Y nw t d| ¡ |j| j| j	| j
| jd}| |¡}|d u r8d S | d¡ ¡ }t d| ¡ | d¡r}zt |¡}|d  d	¡\}}W n8 tyl   t d
| ¡ Y d S  ty|   t d| ¡ Y d S w t d|¡r‹| d	¡\}}n	t d| ¡ d S t d||f ¡ ||gS )Nr   zFpackage redis not found, are you sure you've installed them correctly?zresolving token '%s')rF   rG   ÚdbÚpasswordrU   zresponse from redis : %sÚ{rF   r%   zUnable to decode JSON token: %sz+Unable to find 'host' key in JSON token: %sz\S+:\S+zUnable to parse token: %szhost: %s, port: %s)rs   ri   r/   r0   rt   ru   r|   ÚRedisrz   rv   rw   rx   rJ   rb   r"   Údebugr*   rN   rd   r,   r.   ÚKeyErrorr+   Úmatch)	r   r   rs   ÚclientÚstuffÚresponseStrÚcomborF   rG   r	   r	   r
   r     sH   
þÿ


ÿÿýzTokenRedis.lookupN)r   r   r   Ú__doc__r   r   r	   r	   r	   r
   ro   °   s    2,ro   c                       r9   )ÚUnixDomainSocketDirectoryc                    s&   t ƒ j|i |¤Ž tj | j¡| _d S r   )r   r   r   r   Úabspathr   Ú	_dir_pathr   r   r	   r
   r   5  s   z"UnixDomainSocketDirectory.__init__c              
   C   s´   z=dd l }tj | j¡sW d S tj tj | j|¡¡}| | j¡s$W d S tj |¡s-W d S | 	t  |¡j
¡s9W d S d|gW S  tyY } zt dt|ƒ ¡ W Y d }~d S d }~ww )Nr   Úunix_socketz$Error finding unix domain socket: %s)Ústatr   r   r&   rŒ   r‹   r   r*   r=   ÚS_ISSOCKÚst_moder;   r/   r0   r_   )r   r   rŽ   Úuds_pathrl   r	   r	   r
   r   9  s"   
€þz UnixDomainSocketDirectory.lookupr?   r	   r	   r   r
   rŠ   4  s    rŠ   )Úloggingr   rt   rg   r+   rN   Ú	getLoggerr   r/   r   r   r8   r:   r@   rM   rQ   ro   rŠ   r	   r	   r	   r
   Ú<module>   s"    
(		: 