
    &i4                         d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 dZ
ddededz  fdZ G d	 d
          Z	 ddee         dedz  dedz  fdZdedz  defdZdedefdZdS )    N)Path)urlopen)zhttps://api.ipify.orgzhttps://icanhazip.comzhttps://ifconfig.me/ip      @timeout_secreturnc                 &   t           D ]}	 t          ||           5 }|                                                    d                                          }|r|cd d d            c S 	 d d d            n# 1 swxY w Y   y# t
          $ r Y w xY wd S )N)timeoutzutf-8)_IP_SERVICESr   readdecodestrip	Exception)r   urlrespips       3/var/www/html/ScheduledJobs/PythonScripts/wg_vpn.pyfetch_public_ipr      s     	k222 dYY[[''006688                          	 	 	H	4s:   B=A5B)B5A9	9B<A9	=B
BBc                       e Zd Zddddddeez  dededed	ed
dfdZded
ee         fdZ	dee         d
dfdZ
ded
dfdZded
dfdZd
efdZd
efdZddedz  d
efdZdded
edz  fdZddZdS )WireGuardRotatorg       @Tzwg-quick)cooldown_secshuffleuse_sudowg_quick
config_dirr   r   r   r   r   Nc                    t          |          | _        || _        || _        || _        |                     |          | _        d| _        d | _        d S )N)r   )	r   r   r   r   r   _load_configs_configs_indexcurrent_config)selfr   r   r   r   r   s         r   __init__zWireGuardRotator.__init__   sV     z**(  **7*;;+/    c                   | j                                         st          d| j                    t          | j                             d                    }|st          d| j                    |rt          j        |           |S )NzWG config dir not found: z*.confzNo .conf files found in )r   existsFileNotFoundErrorsortedglobrandomr   )r!   r   configss      r   r   zWireGuardRotator._load_configs/   s    %%'' 	S#$Q$Q$QRRR--h7788 	R#$Pt$P$PQQQ 	$N7###r#   argsc                     | j         g|z   }| j        rdg|z   }t          j        dd                    |                     t          j        |d           |                                  d S )Nsudoz
WG cmd: %s T)check)r   r   logginginfojoin
subprocessrunlog_public_ip)r!   r+   cmds      r   _runzWireGuardRotator._run9   sn    }o$= 	!(S.C\388C==111s$''''r#   config_pathc                     |                      dt          |          g           || _        | j        rt	          j        | j                   d S d S )Nup)r7   strr    r   timesleepr!   r8   s     r   r:   zWireGuardRotator.upA   sW    		4[))*+++) 	*Jt()))))	* 	*r#   c                     	 |                      dt          |          g           n%# t          $ r t          j        d|           Y nw xY w| j        rt          j        | j                   d S d S )Ndownz!WG down failed for %s; continuing)r7   r;   r   r0   warningr   r<   r=   r>   s     r   r@   zWireGuardRotator.downG   s    	NIIvs;//01111 	N 	N 	NO?MMMMM	N 	*Jt()))))	* 	*s   $' A	A	c                 n    | j         dz   t          | j                  z  | _         | j        | j                  S )N   )r   lenr   r!   s    r   next_configzWireGuardRotator.next_configO   s-    {Q#dm*<*<<}T[))r#   c                 t    | j         | j         S |                                 }|                     |           |S N)r    rF   r:   )r!   configs     r   	ensure_upzWireGuardRotator.ensure_upS   s9    *&&!!##r#   reasonc                     |rt          j        d|           | j        |                     | j                   |                                 }|                     |           |S )NzRotating VPN: %s)r0   rA   r    r@   rF   r:   )r!   rK   rI   s      r   rotatezWireGuardRotator.rotateZ   s`     	8O.777*IId)***!!##r#   VPN IPlabelc                 ~    t                      }|rt          j        d||           nt          j        d|           |S )Nz%s: %sz%s: unavailable)r   r0   r1   rA   )r!   rO   r   s      r   r5   zWireGuardRotator.log_public_ipc   sD     	6L5"----O-u555	r#   c                 Z    | j         #|                     | j                    d | _         d S d S rH   )r    r@   rE   s    r   shutdownzWireGuardRotator.shutdownk   s6    *IId)***"&D +*r#   rH   )rN   )r   N)__name__
__module____qualname__r;   r   floatboolr"   listr   r7   r:   r@   rF   rJ   rM   r5   rR    r#   r   r   r      s       
 ""0 0 0$J0 	0
 0 0 0 
0 0 0 0" d    c t    *d *t * * * ** * * * * **T * * * *4     S4Z 4     3 cDj    ' ' ' ' ' 'r#   r   logs
target_urlc                 T   d }| D ]!}	 t          j        |                    dd                                        di           }|                    d          dk    rZ|                    di           }|                    d          dk    r|                    di           }|                    d	          }|r+t          |t                    r|                    |          s|                    d
          }t          |t                    r|c S # t          $ r Y w xY w|S )Nmessagez{}methodzNetwork.responseReceivedparamstypeDocumentresponser   status)jsonloadsget
isinstancer;   
startswithintr   )rZ   r[   rc   entryr]   r_   rb   r   s           r   +parse_document_status_from_performance_logsrk   q   s;    F  	j9d!;!;<<@@BOOG{{8$$(BBB[[2..Fzz&!!Z//zz*b11H,,u%%C jc22 3>>*;U;U \\(++F&#&&  	 	 	H	Ms%   AD/DAD'+D
D%$D%status_codec                     | d uo| dk    S )N   rY   )rl   s    r   should_rotate_on_statusro      s    d"9{c'99r#   htmlc                 F      sdS d}t           fd|D                       S )NF)ERR_NAME_NOT_RESOLVEDDNS_PROBE_POSSIBLEDNS_PROBE_FINISHED_NXDOMAINdnsDefinitionu   Адресу DNSu   IP-адресу сервераc              3       K   | ]}|v V  	d S rH   rY   ).0mrp   s     r   	<genexpr>z$is_dns_error_html.<locals>.<genexpr>   s'      **QqDy******r#   )any)rp   markerss   ` r   is_dns_error_htmlr|      s<     uG ****'******r#   )r   rH   )rd   r0   osr)   r3   r<   pathlibr   urllib.requestr   r
   rV   r;   r   r   rX   dictri   rk   rW   ro   r|   rY   r#   r   <module>r      s[     				             " " " " " "	 	 	t 	 	 	 	Q' Q' Q' Q' Q' Q' Q' Q'l " 
t*d
 	4Z   2:t : : : : :+C +D + + + + + +r#   