HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/zmq/ssh/__pycache__/tunnel.cpython-310.pyc
o

�N�g�3�@s�dZddlZddlZddlZddlZddlZddlZddlZddlmZm	Z	ddl
mZz$e���e�
de�ddlZejjZWd�n1sJwYWneycdZGdd�de�ZYnwdd	lmZzddlZWney{dZYnwd
d�Ze�dej�Zd%d
d�Zdd�Zdd�Z	d&dd�Zd&dd�Z	d'dd�Z dd�Z!dd�Z"	d'dd �Z#d(d!d"�Z$ej%d#kr�e#Z&ne Z&gd$�Z'dS))zXBasic ssh tunnel utilities, and convenience functions for tunneling
zeromq connections.
�N)�getpass�getuser)�Process�ignorec@seZdZdS)�SSHExceptionN)�__name__�
__module__�__qualname__�r
r
�B/opt/saltstack/salt/lib/python3.10/site-packages/zmq/ssh/tunnel.pyrsr�)�forward_tunnelcCsZg}g}t|�D]}t��}|�d�|�|��d�|�|�q|D]}|��q$|S)z4Select and return n random ports that are available.)�rr)�range�socket�bind�append�getsockname�close)�nZportsZsockets�i�sockr
r
r�select_random_ports*s

rspass(word|phrase):cCs*|dur	tjdk}|st}nt}|||�S)z�Attempt to make an ssh connection without a password.
    This is mainly used for requiring password input only once
    when many tunnels may be connected to the same server.

    If paramiko is None, the default for the platform is chosen.
    N�win32)�sys�platform�_try_passwordless_openssh�_try_passwordless_paramiko)�server�keyfile�paramiko�fr
r
r�try_passwordless_ssh>s

r"cCs�tdurtd��d|}|r|d|7}|d7}tj��}|�dd�d}tj||d�}	z|j|tgd
d�}|dkr?t	d
��WdStj
yKYq,tjyUYd	Sw)z.Try passwordless login with shell ssh command.Nz!pexpect unavailable, use paramikozssh -f z -i z exit�SSH_ASKPASS�,Are you sure you want to continue connecting��envT皙�����?��timeoutr�2The authenticity of the host can't be established.F)�pexpect�ImportError�os�environ�copy�pop�spawn�expect�
_password_patr�TIMEOUT�EOF)rr�cmdr&�
ssh_newkey�prr
r
rrNs0
��	��rc	Cs�tdurd}tjdkr|d7}t|��|d7}t|��t|�\}}}t��}tj�d�}z|�	|�Wn	t
y<Ynwtj�dd�}|rTt
t|�d��}|�|��z|j||||d	d
�WntjykYdSw|��d	S)z%Try passwordless login with paramiko.NzParamiko unavailable, rz=Paramiko is required for ssh tunneled connections on Windows.zuse OpenSSH.z~/.ssh/known_hostsZPYZMQ_PARAMIKO_HOST_KEY_POLICY�PolicyT)�username�key_filename�
look_for_keysF)r rrr,�
_split_server�	SSHClientr-�path�
expanduserZload_host_keys�FileNotFoundErrorr.�get�getattr�set_missing_host_key_policy�connectZAuthenticationExceptionr)	rr�msgr:�port�clientZknown_hostsZpolicy_name�policyr
r
rrls8
��

��r�<c	Cs&t||||||d�\}}|�|�|S)aTConnect a socket to an address via an ssh tunnel.

    This is a wrapper for socket.connect(addr), when addr is not accessible
    from the local machine.  It simply creates an ssh tunnel using the remaining args,
    and calls socket.connect('tcp://localhost:lport') where lport is the randomly
    selected local port of the tunnel.

    )r�passwordr r))�open_tunnelrE)	r�addrrrrKr r)Znew_url�tunnelr
r
r�tunnel_connection�s
�
rOc	Csrtd�d}|�d�\}}|�d�\}}	t|	�}	|dur!tjdk}|r&t}
nt}
|
||	|||||d�}d||fS)	z�Open a tunneled connection from a 0MQ url.

    For use inside tunnel_connection.

    Returns
    -------

    (url, tunnel) : (str, object)
        The 0MQ url that has been forwarded, and the tunnel object
    rrz://�:Nr)�remoteiprrKr)ztcp://127.0.0.1:%i)r�split�intrr�paramiko_tunnel�openssh_tunnel)rMrrrKr r)�lportZ	transport�ip�rportZtunnelfrNr
r
rrL�s&
�	rL�	127.0.0.1cCs�tdurtd��d}|r|d|7}d|vr#|�d�\}}|d|7}|�d|��}	tj|	dd	�\}
}|sft|
|
�d
�d|
�d���}d
|||||f}	tj|	dd	�\}
}|sft�t|	�	ddd��|Sd||||||f}	t
j��}
|
�
dd�d}tj|	|
d�}d}	z|j|tgdd�}|dkr�td��Wn.tjy�Yq�tjy�|jr�t|j�t|j�t|j�td|	��|jYSw|r�td�d}|dur�td|�}|�|�d}q�)aCreate an ssh tunnel using command-line ssh that connects port lport
    on this machine to localhost:rport on server.  The tunnel
    will automatically close when not in use, remaining open
    for a minimum of timeout seconds for an initial connection.

    This creates a tunnel redirecting `localhost:lport` to `remoteip:rport`,
    as seen from `server`.

    keyfile and password may be specified, but ssh config is checked for defaults.

    Parameters
    ----------

    lport : int
        local port for connecting to the tunnel from this machine.
    rport : int
        port on the remote machine to connect to.
    server : str
        The ssh server to connect to. The full ssh server string will be parsed.
        user@server:port
    remoteip : str [Default: 127.0.0.1]
        The remote ip, specifying the destination of the tunnel.
        Default is localhost, which means that the tunnel would redirect
        localhost:lport on this machine to localhost:rport on the *server*.

    keyfile : str; path to public key file
        This specifies a key to be used in ssh login, default None.
        Regular default ssh keys will be used without specifying this argument.
    password : str;
        Your ssh password to the ssh server. Note that if this is left None,
        you will be prompted for it if passwordless key based login is unavailable.
    timeout : int [default: 60]
        The time (in seconds) after which no activity will result in the tunnel
        closing.  This prevents orphaned tunnels from running forever.
    Nz(pexpect unavailable, use paramiko_tunnelzssh z-i rPz -p %sz
 -O check T)Zwithexitstatuss(pid=��)z&%s -O forward -L 127.0.0.1:%i:%s:%i %sz
-O forwardz	-O cancelrz/%s -f -S none -L 127.0.0.1:%i:%s:%i %s sleep %ir#r$r%Fr'r(rr*ztunnel '%s' failed to startzPassword rejected, try again�%s's password: )r+r,rR�runrS�find�atexit�register�_stop_tunnel�replacer-r.r/r0r1r2r3rr4r5�
exitstatus�print�beforeZafter�RuntimeError�pidrZsendline)rVrXrrQrrKr)ZsshrGr6�outputrcrgr&r7rNZfailedrr
r
rrU�s|& ��

��



�	
�rUcCst�|�dS�N)r+r])r6r
r
rra0sracCsNd|vr
|�dd�\}}nt�}d|vr |�d�\}}t|�}nd}|||fS)N�@rrP�)rRrrS)rr:rGr
r
rr=4s

r=cCs^tdurtd��|durt||�std|�}tt||||ft||d�d�}d|_|��|S)a
launch a tunner with paramiko in a subprocess. This should only be used
    when shell ssh is unavailable (e.g. Windows).

    This creates a tunnel redirecting `localhost:lport` to `remoteip:rport`,
    as seen from `server`.

    If you are familiar with ssh tunnels, this creates the tunnel:

    ssh server -L localhost:lport:remoteip:rport

    keyfile and password may be specified, but ssh config is checked for defaults.


    Parameters
    ----------

    lport : int
        local port for connecting to the tunnel from this machine.
    rport : int
        port on the remote machine to connect to.
    server : str
        The ssh server to connect to. The full ssh server string will be parsed.
        user@server:port
    remoteip : str [Default: 127.0.0.1]
        The remote ip, specifying the destination of the tunnel.
        Default is localhost, which means that the tunnel would redirect
        localhost:lport on this machine to localhost:rport on the *server*.

    keyfile : str; path to public key file
        This specifies a key to be used in ssh login, default None.
        Regular default ssh keys will be used without specifying this argument.
    password : str;
        Your ssh password to the ssh server. Note that if this is left None,
        you will be prompted for it if passwordless key based login is unavailable.
    timeout : int [default: 60]
        The time (in seconds) after which no activity will result in the tunnel
        closing.  This prevents orphaned tunnels from running forever.

    NzParamiko not availabler\)rrK)�target�args�kwargsT)	r r,rrr�_paramiko_tunnel�dict�daemon�start)rVrXrrQrrKr)r8r
r
rrTAs*


�rTc

Cst|�\}}}t��}|��|�t���z
|j||||d|d�Wn tyC}	ztd|||	f�t	�
d�WYd}	~	nd}	~	wwt�tjtj
�zt||||���WdStyktd�t	�
d�YdSty�}	ztd|	�t	�
d	�WYd}	~	dSd}	~	ww)
z�Function for actually starting a paramiko tunnel, to be passed
    to multiprocessing.Process(target=this), and not called directly.
    T)r:r;r<rKz"*** Failed to connect to %s:%d: %rrNz'SIGINT: Port forwarding stopped cleanlyrz%Port forwarding stopped uncleanly: %s�)r=r r>Zload_system_host_keysrDZ
WarningPolicyrE�	Exceptionrdr�exit�signal�SIGINT�SIG_IGNr
Z
get_transport�KeyboardInterrupt)
rVrXrrQrrKr:rGrH�er
r
rro|s:
�����ror)rO�
ssh_tunnelrUrTr"ri)NNNrJ)rYNNrJ)NN)(�__doc__r_r-�rervrr�warningsrr�multiprocessingr�catch_warnings�simplefilter�DeprecationWarningr Z
ssh_exceptionrr,rtZforwardr
r+r�compile�
IGNORECASEr3r"rrrOrLrUrar=rTrorr{�__all__r
r
r
r�<module>s\


����
!
�
$
�j
�
;
)