
    RSi-                         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 dlm	Z	 d dlm
Z
 ddgZ G d de          Z G d	 de          ZdS )
    N)Acceptor)	Connector)debug)logger	ActiveDTP
PassiveDTPc                   :    e Zd ZdZdZdZd
dZd Zd Zd Z	d	 Z
dS )r   a  Creates a socket listening on a local port, dispatching the
    resultant connection to DTPHandler. Used for handling PASV command.

     - (int) timeout: the timeout for a remote client to establish
       connection with the listening socket. Defaults to 30 seconds.

     - (int) backlog: the maximum number of queued connections passed
       to listen(). If a connection request arrives when the queue is
       full the client may raise ECONNRESET. Defaults to 5.
       NFc                    || _         |j        | _        |j        | _        t          j        | |j                   | j         j                                        d         }|| j         j        v r| j         j        |         }n| j         j	        r| j         j	        }nd}| j         j
        j        j        t          j        k    rE|                     |df          }| j                                         |                                  n| j         j        j        }|                     |t          j                   | j         j        |                     |df           n*t)          | j         j                  }|r|                    t-          j        dt1          |          dz
                      }|                                  	 |                     ||f           n# t4          $ r, | j                             d| t6          j                   Y nqt:          $ re}|j        t<          j        k    rE|rY d}~|                     |df           | j                             dt6          j                    n Y d}~nd}~ww xY w|| !                    | j"        p| j         j
        j"                   | j                                        d         }|s`|p|}	|	#                    d          r
|	d	d         }	d
|	$                    dd          |dz  |dz  fz  }
| j         %                    |
           n+| j         %                    dtM          |           d           | j'        r"| (                    | j'        | j)                   dS dS )zInitialize the passive data server.

        - (instance) cmd_channel: the command channel class instance.
        - (bool) extmode: whether use extended passive mode response type.
        ioloopr   N   z#ignoring EPERM when bind()ing port logfunzdCan't find a valid passive port in the configured range. A random kernel-assigned port will be used.z::ffff:   z%227 Entering passive mode (%s,%d,%d)..,   z'229 Entering extended passive mode (|||z|).)*cmd_channelloglog_exceptionr   __init__r   socketgetsocknamemasquerade_address_mapmasquerade_addressserverfamilyAF_INETbind_af_unspecifiedclosedel_channelcreate_socketSOCK_STREAMpassive_portsbindlistpoprandomrandintlenset_reuse_addrPermissionErrorr   r   OSErrorerrno
EADDRINUSEwarninglistenbacklog
startswithreplacerespondinttimeout
call_laterhandle_timeout)selfr   extmodelocal_ipmasqueraded_ipafportsporterripresps              V/home/jrussi/.local/lib/python3.11/site-packages/pyftpdlib/handlers/ftp/dispatchers.pyr   zPassiveDTP.__init__"   s    '?(6${'9::::#*6688;t'>>>!-DXNNN0 	"!-@NN!N")0FNBB))8Q-88BK!(/B2v1222)1 IIxm$$$$)788E yy3u::>!B!BCC##%%%IIx.///4 3 '   $((DdDD%| )         yE$444  $HHHH !IIxm444 ,00!5 (.~	 1           > 	DLCD$4$;$CDDD{&&((+ 	+8B}}Y'' 	 V ;

3$$s
> D
 $$T****$$H#d))HHH   < 	?OODL$*=>>>>>	? 	?s$   G 3I>	I>I95?I99I>c                    | j         j        s|                                 S | j         j        |d         k    r| j         j        s	 |                                 n# t
          $ r Y nw xY wd|d          d|d          d}| j                             |           t          j        	                                r| j         
                    d           dS d|d          d|d          d}| j         
                    |t          j        	           |                                  | j         j        rN| j                             || j                   }|j        r)|| j         _        | j                                          dS dS dS )
z1Called when remote client initiates a connection.r   z2425 Rejected data connection from foreign address :r   r   zGyou can use --permit-foreign-addresses CLI opt to allow this connectionNz1Established data connection with foreign address r   )r   	connectedr!   	remote_ippermit_foreign_addressesr.   respond_w_warningsysstdoutisattyr   r   r1   dtp_handlerdata_channel_on_dtp_connection)r;   sockaddrmsghandlers        rE   handle_acceptedzPassiveDTP.handle_accepted   s   ) 	 ::<<
 %a00#< AJJLLLL   D,Aw, ,!%a, , ,   223777:$$&& $((1   ,Aw, ,!%a, , ,   $$S$@@@ 	

% 	6&2249IJJG  607 - 3355555		6 	66 6s   A 
A&%A&c                     | j         j        r&| j                             dt          j                   |                                  d S )Nz#421 Passive data channel timed out.r   )r   rH   r6   r   infor!   r;   s    rE   r:   zPassiveDTP.handle_timeout   sJ    % 	$$5fk %    	

    c                 
   	  # t           $ r( t          j        t          j                               Y nw xY w	 |                                  dS # t           $ r) t          j        t          j                               Y dS w xY wz)Called to handle any uncaught exceptions.N)	Exceptionr   error	traceback
format_excr!   criticalrY   s    rE   handle_errorzPassiveDTP.handle_error   s    	1 	1 	1 	1L-//00000	1	4JJLLLLL 	4 	4 	4OI022333333	4s    /55A /BBc                 P    t          d|            t          j        |            d S Nzcall: close())inst)r   r   r!   rY   s    rE   r!   zPassiveDTP.close   s+    oD))))trZ   )F)__name__
__module____qualname____doc__r8   r3   r   rV   r:   rb   r!    rZ   rE   r   r      s        	 	 GG^? ^? ^? ^?D,6 ,6 ,6\  	4 	4 	4    rZ   c                   @    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
S )r   zConnects to remote client and dispatches the resulting connection
    to DTPHandler. Used for handling PORT command.

     - (int) timeout: the timeout for us to establish connection with
       the client's listening data socket.
    r
   c                 Z   t          j        | |j                   || _        |j        | _        |j        | _        d| _        | j        r1| j                            | j        | j	        | j
                  | _        |                    d          dk    rd| _        | d| | _        nd| _        d	| d
| | _        | j        j                                        d         }	 |                     ||f|df           dS # t          j        t$          f$ r |                                  Y dS w xY w)zInitialize the active data channel attempting to connect
        to remote data socket.

         - (str) ip: the remote IP address.
         - (int) port: the remote port.
         - (instance) cmd_channel: the command channel class instance.
        r   N)_errbackr      PORTrG   EPRT[z]:r   )r   r   r   r   r   r   _idlerr8   r9   r:   rb   count_cmd_normalized_addrr   r   connect_af_unspecifiedgaierrorr.   handle_close)r;   rC   rA   r   	source_ips        rE   r   zActiveDTP.__init__   sK    	4(:;;;;&?(6< 	+00d1D<M 1  DK 88C==ADI')NNDNND!!DI$4$4$4d$4$4D!$+7799!<		 ''T
YNCCCCC) 	  	  	 	 s    C< <*D*)D*c                     dS )NFrj   rY   s    rE   readablezActiveDTP.readable   s    urZ   c                    |                                   | j        %| j        j        s| j                                         | j        j        s|                                 S | j                            t          j	        t          j
                  }|dk    rt          |          d}| j                            d|z              | j                            | j        | j        d|           | j        j        s|                                 S | j                            | j        | j                  }|| j        _        | j                                         dS )z&Called when connection is established.Nr   z#Active data connection established.z200    )r"   rr   	cancelledcancelr   rH   r!   r   
getsockopt
SOL_SOCKETSO_ERRORr.   r6   log_cmdrt   ru   rO   rP   rQ   )r;   rB   rT   rU   s       rE   handle_connectzActiveDTP.handle_connect   s(   ;"4;+@"K   ) 	 ::<<k$$V%6HH!88#,,3  #...  D,A3LLL) 	 ::<<"..t{D<LMM(/%++-----rZ   c                     | j         j        rRd}| j                             d|z   t          j                   | j                             | j        | j        d|           |                                  d S )NzActive data channel timed out.z421 r   i  )	r   rH   r6   r   rX   r   rt   ru   r!   r;   rT   s     rE   r:   zActiveDTP.handle_timeout  ss    % 	2C$$Vc\&+$FFF$$	40#s   	

rZ   c                     | j         sh|                                  | j        j        rJd}| j                            d|z              | j                            | j        | j        d|           d S d S d S )Nz#Can't connect to specified address.z425 i  )_closedr!   r   rH   r6   r   rt   ru   r   s     rE   rx   zActiveDTP.handle_close  s     | 	JJLLL) ; ((#666 ((It4c3    	 	 rZ   c                    	  # t           j        t          f$ r Y n$t          $ r |                     |            Y nw xY w	 |                                  dS # t          $ r) t          j        t          j	                               Y dS w xY wr\   )
r   rw   r.   r]   r   rx   r   ra   r_   r`   rY   s    rE   rb   zActiveDTP.handle_error!  s    	%) 	 	 	D 	% 	% 	%t$$$$$	%	4 	4 	4 	4OI022333333	4s"    <<< A /B	B	c                     t          d|            | j        sBt          j        |            | j        )| j        j        s| j                                         d S d S d S d S rd   )r   r   r   r!   rr   r~   r   rY   s    rE   r!   zActiveDTP.close.  sq    oD))))| 	%OD!!!{&t{/D&""$$$$$	% 	%&&&&rZ   N)rf   rg   rh   ri   r8   r   r{   r   r:   rx   rb   r!   rj   rZ   rE   r   r      s          G     @  . . .*    4 4 4% % % % %rZ   )r/   r)   r   rL   r_   pyftpdlib.ioloopr   r   pyftpdlib.logr   r   __all__r   r   rj   rZ   rE   <module>r      s   
    



     % % % % % % & & & & & &                  
%s s s s s s s slj% j% j% j% j%	 j% j% j% j% j%rZ   