HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/local/CyberCP/__pycache__/fastapi_ssh_server.cpython-310.pyc
o

��h��@s�ddlZddlZddlZddlZddlmZmZmZmZddl	m
Z
ddlZddlZddl
Z
ddlmZmZddlZe�ZdZdZeje
dgddgdgd	�d
Zde�d�Zd
d�Zdd�Zdd�Ze�d�ed�ed�fdededefdd��ZdS)�N)�FastAPI�	WebSocket�WebSocketDisconnect�Query)�CORSMiddleware)�jwt�JWTError�+DAsjK2gl50PE09d1N3uZPTQ6JdwwfiuhlyWKMVbUEpc�HS256�*T)�
allow_origins�allow_credentials�
allow_methods�
allow_headers�your_website_userz/home/z/.ssh/authorized_keyscCsDtj�d�}t��}|�|�|��}|���d|����}||fS)Ni� )	�paramiko�RSAKey�generate�io�StringIO�write_private_key�getvalue�get_name�
get_base64)�key�
private_io�private_key�
public_key�r�(/usr/local/CyberCP/fastapi_ssh_server.py�generate_ssh_keypairs
r!cCsLd|�d|�d�}ttd��}|�|�Wd�dS1swYdS)N�from="127.0.0.1,::1" r�
�a)�open�AUTHORIZED_KEYS_PATH�write)r�comment�entry�frrr �add_key_to_authorized_keys(s"�r+cCs~ttd��}|��}Wd�n1swYttd��}|D]}||vr,|�|�q!Wd�dS1s8wYdS)N�r�w)r%r&�	readlinesr')r(r*�lines�linerrr �remove_key_from_authorized_keys.s
�
��"�r1z/ws�	websocket�token�ssh_userc�sj�ztj|ttgd�}|�d�}|s���IdHWdSWnty.���IdHYdSwt�|�j	}t
j�|d�}t
j�|d�}t
j
|dd�t
j�|�set|d��Wd�n1s`wYt
�|t�|�jt�|�j�t
�|d�t
�|t�|�jt�|�j�t
�|d	�t�\}}	d
t
�d�����}
d|	�d
|
�d�}t|d��
}|�|�Wd�n1s�wYtjdd��}
|
�|���|
j}Wd�n1s�wY���IdHd}d�z�z1tjd||gdd�IdH}|jdd�IdH���fdd�}��fdd�}t �!|�|��IdHWnit"�y�}z\z&d|��}t#�$d|���t%|t&��rB|jddd�}��'t(|��IdHWnt"�yh}zt#�$d|���WYd}~nd}~wwz	���IdHWn
t"�y|YnwWYd}~nd}~wwWt|d ��}|�)�}Wd�n	1�s�wYt|d��}|D]
}|
|v�r�|�|��q�Wd�n	1�s�wYt
�*|���rՈ��|�r�|��dSdSt|d ��}|�)�}Wd�n	1�s�wYt|d��}|D]
}|
|v�r|�|��qWd�n	1�swYt
�*|���r,���|�r4|��ww)!N)�
algorithmsr4z.ssh�authorized_keysT)�exist_okr-i�i�zwebterm-�r"rr#r$F)�delete�	localhost)�username�client_keys�known_hosts�xterm)�	term_typec�sJ�z	���IdH}�j�|jddd��qty$�j��YdSw)NT�utf-8�replace��errors)�
receive_bytes�stdinr'�decoder�close)�data��processr2rr �	ws_to_sshds���z%websocket_endpoint.<locals>.ws_to_sshc
�s��zY�j��sX�j�d�IdH}|rPt�dt|��dt|t�r%|dd�n|���t|t�r8��|�IdHnt|t	�rF��
|�IdHn
��
t	|��IdH�j��rWdSWdStyu}zt�d|���WYd}~dSd}~ww)Niz [ssh_to_ws] Sending to WS: type=z	, sample=�(z[ssh_to_ws] Exception: )
�stdout�at_eof�read�logging�debug�type�
isinstance�bytes�
send_bytes�str�	send_text�	Exception�	exception)rH�exrIrr �	ssh_to_wsms"�
0

���z%websocket_endpoint.<locals>.ssh_to_wszConnection error: z [websocket_endpoint] Exception: r@rArBz2[websocket_endpoint] Error sending error message: r,)+rrF�
JWT_SECRET�
JWT_ALGORITHM�getrGr�pwd�getpwnam�pw_dir�os�path�join�makedirs�existsr%�chown�pw_uid�pw_gid�chmodr!�urandom�hexr'�tempfile�NamedTemporaryFile�encode�name�accept�asyncssh�connect�create_process�asyncio�gatherrXrPrYrSrTrWrVr.�remove)r2r3r4�payload�user�home_dir�ssh_dir�authorized_keys_pathrrr(r)r*�keyfile�keyfile_path�connrKr[�e�msgrZr/r0rrIr �websocket_endpoint6s��
��*
���	
������
�

���
��
�

���

�r�)rurrrmrb�fastapirrrr�fastapi.middleware.corsrrrr_�joserrrP�appr\r]�add_middleware�SSH_USERr&r!r+r1r2rVr�rrrr �<module>s8�	*