11"""
2- PyserSSH - A SSH server. For more info visit https://github.com/damp11113/PyserSSH
2+ PyserSSH - A Scriptable SSH server. For more info visit https://github.com/damp11113/PyserSSH
33Copyright (C) 2023-2024 damp11113 (MIT)
44
55Visit https://github.com/damp11113/PyserSSH
2626"""
2727
2828import pickle
29+ import time
30+ import atexit
31+ import threading
2932
3033class AccountManager :
31- def __init__ (self , anyuser = False , historylimit = 10 ):
34+ def __init__ (self , anyuser = False , historylimit = 10 , autosave = False , autosavedelay = 60 , autoload = False , autoloadfile = "autosave_session.ses" ):
3235 self .accounts = {}
3336 self .anyuser = anyuser
3437 self .historylimit = historylimit
38+ self .autosavedelay = autosavedelay
39+
40+ self .__autosavework = False
41+ self .__autosaveworknexttime = 0
3542
3643 if self .anyuser :
3744 print ("history system can't work if 'anyuser' is enable" )
3845
46+ if autoload :
47+ self .load (autoloadfile )
48+
49+ if autosave :
50+ self .__autosavethread = threading .Thread (target = self .__autosave )
51+ self .__autosavethread .start ()
52+ atexit .register (self .__saveexit )
53+
54+ def __autosave (self ):
55+ self .save ("autosave_session.ses" )
56+ self .__autosaveworknexttime = time .time () + self .autosavedelay
57+ self .__autosavework = True
58+ while self .__autosavework :
59+ if int (self .__autosaveworknexttime ) == int (time .time ()):
60+ self .save ("autosave_session.ses" )
61+ self .__autosaveworknexttime = time .time () + self .autosavedelay
62+
63+ def __saveexit (self ):
64+ self .__autosavework = False
65+ self .save ("autosave_session.ses" )
66+ self .__autosavethread .join ()
67+
3968 def validate_credentials (self , username , password ):
4069 if username in self .accounts and self .accounts [username ]["password" ] == password or self .anyuser :
4170 return True
@@ -66,11 +95,11 @@ def set_permissions(self, username, new_permissions):
6695 if username in self .accounts :
6796 self .accounts [username ]["permissions" ] = new_permissions
6897
69- def save_to_file (self , filename ):
98+ def save (self , filename = "session.ssh" ):
7099 with open (filename , 'wb' ) as file :
71100 pickle .dump (self .accounts , file )
72101
73- def load_from_file (self , filename ):
102+ def load (self , filename ):
74103 try :
75104 with open (filename , 'rb' ) as file :
76105 self .accounts = pickle .load (file )
@@ -114,12 +143,24 @@ def get_user_sftp_path(self, username):
114143 def get_user_timeout (self , username ):
115144 if username in self .accounts and "timeout" in self .accounts [username ]:
116145 return self .accounts [username ]["timeout" ]
117- return 0
146+ return None
118147
119- def set_user_timeout (self , username , timeout = 0 ):
148+ def set_user_timeout (self , username , timeout = None ):
120149 if username in self .accounts :
121150 self .accounts [username ]["timeout" ] = timeout
122151
152+ def get_user_last_login (self , username ):
153+ if username in self .accounts and "lastlogin" in self .accounts [username ]:
154+ return self .accounts [username ]["lastlogin" ]
155+ return None
156+
157+ def set_user_last_login (self , username , ip , timelogin = time .time ()):
158+ if username in self .accounts :
159+ self .accounts [username ]["lastlogin" ] = {
160+ "ip" : ip ,
161+ "time" : timelogin
162+ }
163+
123164 def add_history (self , username , command ):
124165 if not self .anyuser :
125166 if username in self .accounts :
@@ -158,4 +199,4 @@ def get_lastcommand(self, username):
158199 if username in self .accounts and "lastcommand" in self .accounts [username ]:
159200 command = self .accounts [username ]["lastcommand" ]
160201 return command
161- return None # User or history not found
202+ return None # User or history not found
0 commit comments