This commit is contained in:
2025-05-09 14:19:29 +02:00
parent 95ad49df43
commit 17c6124a57
86 changed files with 1885 additions and 5826 deletions
+21 -74
View File
@@ -1,78 +1,25 @@
import asyncio
import logging
import os
import asyncssh
_A=True
import asyncio,logging,os,asyncssh
asyncssh.set_debug_level(2)
logging.basicConfig(level=logging.DEBUG)
# Configuration for SFTP server
SFTP_ROOT = "." # Directory to serve
USERNAME = "test"
PASSWORD = "woeii"
HOST = "localhost"
PORT = 2225
SFTP_ROOT='.'
USERNAME='test'
PASSWORD='woeii'
HOST='localhost'
PORT=2225
class MySFTPServer(asyncssh.SFTPServer):
def __init__(self, chan):
super().__init__(chan)
self.root = os.path.abspath(SFTP_ROOT)
async def stat(self, path):
"""Handles 'stat' command from SFTP client"""
full_path = os.path.join(self.root, path.lstrip("/"))
return await super().stat(full_path)
async def open(self, path, flags, attrs):
"""Handles file open requests"""
full_path = os.path.join(self.root, path.lstrip("/"))
return await super().open(full_path, flags, attrs)
async def listdir(self, path):
"""Handles directory listing"""
full_path = os.path.join(self.root, path.lstrip("/"))
return await super().listdir(full_path)
def __init__(A,chan):super().__init__(chan);A.root=os.path.abspath(SFTP_ROOT)
async def stat(A,path):"Handles 'stat' command from SFTP client";B=os.path.join(A.root,path.lstrip('/'));return await super().stat(B)
async def open(A,path,flags,attrs):'Handles file open requests';B=os.path.join(A.root,path.lstrip('/'));return await super().open(B,flags,attrs)
async def listdir(A,path):'Handles directory listing';B=os.path.join(A.root,path.lstrip('/'));return await super().listdir(B)
class MySSHServer(asyncssh.SSHServer):
"""Custom SSH server to handle authentication"""
def connection_made(self, conn):
print(f"New connection from {conn.get_extra_info('peername')}")
def connection_lost(self, exc):
print("Client disconnected")
def begin_auth(self, username):
return True # No additional authentication steps
def password_auth_supported(self):
return True # Support password authentication
def validate_password(self, username, password):
print(username, password)
return True
return username == USERNAME and password == PASSWORD
async def start_sftp_server():
os.makedirs(SFTP_ROOT, exist_ok=True) # Ensure the root directory exists
await asyncssh.create_server(
lambda: MySSHServer(),
host=HOST,
port=PORT,
server_host_keys=["ssh_host_key"],
process_factory=MySFTPServer,
)
print(f"SFTP server running on {HOST}:{PORT}")
await asyncio.Future() # Keep running forever
if __name__ == "__main__":
try:
asyncio.run(start_sftp_server())
except (OSError, asyncssh.Error) as e:
print(f"Error starting SFTP server: {e}")
'Custom SSH server to handle authentication'
def connection_made(A,conn):print(f"New connection from {conn.get_extra_info("peername")}")
def connection_lost(A,exc):print('Client disconnected')
def begin_auth(A,username):return _A
def password_auth_supported(A):return _A
def validate_password(C,username,password):A=password;B=username;print(B,A);return _A;return B==USERNAME and A==PASSWORD
async def start_sftp_server():os.makedirs(SFTP_ROOT,exist_ok=_A);await asyncssh.create_server(lambda:MySSHServer(),host=HOST,port=PORT,server_host_keys=['ssh_host_key'],process_factory=MySFTPServer);print(f"SFTP server running on {HOST}:{PORT}");await asyncio.Future()
if __name__=='__main__':
try:asyncio.run(start_sftp_server())
except(OSError,asyncssh.Error)as e:print(f"Error starting SFTP server: {e}")