|
|
import os |
|
|
import asyncio |
|
|
import asyncssh |
|
|
from flask import Flask |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
SSH_PORT = 2222 |
|
|
SSH_USER = "user" |
|
|
SSH_PASSWORD = "password" |
|
|
|
|
|
async def start_ssh_server(): |
|
|
"""Запускаем SSH-сервер на Python (без root-прав)""" |
|
|
async def handle_client(process): |
|
|
process.stdout.write("Welcome to Python SSH Server!\n") |
|
|
while not process.stdin.at_eof(): |
|
|
line = await process.stdin.readexactly(1) |
|
|
process.stdout.write(line) |
|
|
|
|
|
try: |
|
|
await asyncssh.listen( |
|
|
"", SSH_PORT, |
|
|
server_host_keys=["ssh_host_key"], |
|
|
process_factory=handle_client, |
|
|
authorized_client_keys=None, |
|
|
password_auth=True, |
|
|
username_passwords={SSH_USER: SSH_PASSWORD} |
|
|
) |
|
|
print(f"SSH server running on port {SSH_PORT}") |
|
|
except Exception as e: |
|
|
print(f"Error starting SSH server: {e}") |
|
|
|
|
|
@app.route('/') |
|
|
def home(): |
|
|
"""Пробрасываем SSH через Ngrok""" |
|
|
try: |
|
|
import pyngrok |
|
|
ssh_tunnel = pyngrok.ngrok.connect(SSH_PORT, "tcp") |
|
|
print(f"SSH tunnel established: {ssh_tunnel.public_url}") |
|
|
return f"SSH доступ: {ssh_tunnel.public_url.replace('tcp://', '')}" |
|
|
except Exception as e: |
|
|
print(f"Error starting Ngrok: {e}") |
|
|
return "Error starting Ngrok tunnel." |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
if not os.path.exists("ssh_host_key"): |
|
|
os.system("ssh-keygen -b 2048 -t rsa -f ssh_host_key -N ''") |
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop() |
|
|
loop.create_task(start_ssh_server()) |
|
|
|
|
|
|
|
|
try: |
|
|
app.run(host="0.0.0.0", port=7860) |
|
|
except Exception as e: |
|
|
print(f"Error starting Flask app: {e}") |
|
|
|