File size: 1,915 Bytes
e7e8666 cc69415 f36614e e7e8666 a92df86 e7e8666 a92df86 cc69415 e7e8666 f36614e cc69415 f3cd87b f36614e f3cd87b e7e8666 f36614e cc69415 f36614e cc69415 f36614e e7e8666 f36614e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import os
import asyncio
import asyncssh
from flask import Flask
app = Flask(__name__)
SSH_PORT = 2222 # Локальный порт для SSH
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__":
# Генерируем SSH-ключи (если их нет)
if not os.path.exists("ssh_host_key"):
os.system("ssh-keygen -b 2048 -t rsa -f ssh_host_key -N ''")
# Запускаем SSH сервер асинхронно
loop = asyncio.get_event_loop()
loop.create_task(start_ssh_server())
# Запускаем Flask
try:
app.run(host="0.0.0.0", port=7860)
except Exception as e:
print(f"Error starting Flask app: {e}")
|