Skip to content

Commit c9ffdf5

Browse files
committed
update 4.2 (no pypi)
1 parent 527094f commit c9ffdf5

16 files changed

Lines changed: 954 additions & 187 deletions

File tree

demo/server.py

Lines changed: 112 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22
import socket
33
import time
44
import shlex
5-
from damp11113 import TextFormatter
5+
from damp11113 import TextFormatter, sort_files, allfiles
66
import cv2
77
import traceback
8+
import requests
9+
from bs4 import BeautifulSoup
810

9-
from PyserSSH import Server, AccountManager, Send, wait_input, wait_inputkey
11+
from PyserSSH import Server, AccountManager, Send, wait_input, wait_inputkey, wait_choose, Clear, Title
1012
from PyserSSH.system.info import system_banner
1113
from PyserSSH.extensions.processbar import indeterminateStatus, LoadingProgress
14+
from PyserSSH.extensions.dialog import MenuDialog, TextDialog, TextInputDialog
15+
from PyserSSH.extensions.moredisplay import clickable_url
1216

1317
useraccount = AccountManager()
1418
useraccount.add_account("admin", "") # create user without password
19+
useraccount.add_account("test", "test") # create user without password
1520

1621
ssh = Server(useraccount, system_commands=True, system_message=False)
1722

@@ -22,89 +27,115 @@
2227
Authorizedmessage = """You have successfully connected to the server.
2328
Enjoy your session and remember to follow security protocols."""
2429

30+
loading = ["PyserSSH", "Extensions"]
31+
2532
@ssh.on_user("connect")
26-
def connect(channel, client):
33+
def connect(client):
34+
Title(client, "PyserSSH")
2735
#print(client["windowsize"])
2836
if client['current_user'] == "":
2937
warningmessage = nonamewarning
3038
else:
3139
warningmessage = Authorizedmessage
3240

33-
3441
wm = f"""*********************************************************************************************
3542
Hello {client['current_user']},
3643
3744
{warningmessage}
3845
46+
Visit: {clickable_url("https://damp11113.xyz", "DPCloudev")}
47+
3948
{system_banner}
4049
*********************************************************************************************"""
4150

42-
for char in wm:
43-
Send(channel, char, ln=False)
44-
time.sleep(0.005) # Adjust the delay as needed
45-
Send(channel, '\n') # Send newline after each line
51+
if client['current_user'] != "test":
52+
for i in loading:
53+
P = indeterminateStatus(client, f"Starting {i}", f"[ OK ] Started {i}")
54+
P.start()
55+
56+
time.sleep(len(i) / 20)
57+
58+
P.stop()
59+
60+
Di1 = TextDialog(client, "PyserSSH Extension", "Welcome!\n to PyserSSH test server")
61+
Di1.render()
62+
63+
for char in wm:
64+
Send(client, char, ln=False)
65+
# time.sleep(0.005) # Adjust the delay as needed
66+
Send(client, '\n') # Send newline after each line
4667

4768
@ssh.on_user("error")
48-
def error(channel, error, client):
69+
def error(client, error):
4970
if isinstance(error, socket.error):
5071
pass
5172
else:
52-
Send(channel, traceback.format_exc())
73+
Send(client, traceback.format_exc())
74+
75+
76+
#@ssh.on_user("onrawtype")
77+
#def onrawtype(client, key):
78+
# print(key)
5379

5480
@ssh.on_user("command")
55-
def command(channel, command: str, client):
81+
def command(client, command: str):
5682
if command == "passtest":
57-
user = wait_input(channel, "username: ")
58-
password = wait_input(channel, "password: ", password=True)
59-
Send(channel, f"username: {user} | password: {password}")
83+
user = wait_input(client, "username: ")
84+
password = wait_input(client, "password: ", password=True)
85+
Send(client, f"username: {user} | password: {password}")
6086
elif command == "colortest":
6187
for i in range(0, 255, 5):
62-
Send(channel, TextFormatter.format_text_truecolor(" ", background=f"{i};0;0"), ln=False)
63-
Send(channel, "")
88+
Send(client, TextFormatter.format_text_truecolor(" ", background=f"{i};0;0"), ln=False)
89+
Send(client, "")
6490
for i in range(0, 255, 5):
65-
Send(channel, TextFormatter.format_text_truecolor(" ", background=f"0;{i};0"), ln=False)
66-
Send(channel, "")
91+
Send(client, TextFormatter.format_text_truecolor(" ", background=f"0;{i};0"), ln=False)
92+
Send(client, "")
6793
for i in range(0, 255, 5):
68-
Send(channel, TextFormatter.format_text_truecolor(" ", background=f"0;0;{i}"), ln=False)
69-
Send(channel, "")
70-
71-
Send(channel, "TrueColors 24-Bit")
94+
Send(client, TextFormatter.format_text_truecolor(" ", background=f"0;0;{i}"), ln=False)
95+
Send(client, "")
96+
Send(client, "TrueColors 24-Bit")
7297
elif command == "keytest":
73-
user = wait_inputkey(channel, "press any key", raw=True)
74-
Send(channel, "")
75-
Send(channel, f"key: {user}")
98+
user = wait_inputkey(client, "press any key", raw=True)
99+
Send(client, "")
100+
Send(client, f"key: {user}")
76101
for i in range(10):
77-
user = wait_inputkey(channel, "press any key", raw=True)
78-
Send(channel, "")
79-
Send(channel, f"key: {user}")
102+
user = wait_inputkey(client, "press any key", raw=True)
103+
Send(client, "")
104+
Send(client, f"key: {user}")
80105
elif command.startswith("typing"):
81106
args = shlex.split(command)
82107
messages = args[1]
83108
speed = float(args[2])
84109
for w in messages:
85-
Send(channel, w, ln=False)
110+
Send(client, w, ln=False)
86111
time.sleep(speed)
87-
Send(channel, "")
88-
elif command == "renimtest":
89-
image = cv2.imread(r"opensource.png", cv2.IMREAD_COLOR)
112+
Send(client, "")
113+
elif command.startswith("renimtest"):
114+
args = shlex.split(command)
115+
Clear(client)
116+
image = cv2.imread(f"opensource.png", cv2.IMREAD_COLOR)
90117
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
91118

92-
width, height = client['windowsize']["width"], client['windowsize']["height"]
119+
width, height = client['windowsize']["width"]-5, client['windowsize']["height"]-5
93120

94121
# resize image
95122
resized = cv2.resize(image, (width, height))
123+
t = ""
96124

97125
# Scan all pixels
98126
for y in range(0, height):
99127
for x in range(0, width):
100128
pixel_color = resized[y, x]
101-
#PyserSSH.Send(channel, f"Pixel color at ({x}, {y}): {pixel_color}")
129+
# PyserSSH.Send(channel, f"Pixel color at ({x}, {y}): {pixel_color}")
102130
if pixel_color.tolist() != [0, 0, 0]:
103-
Send(channel, TextFormatter.format_text_truecolor(" ", background=f"{pixel_color[0]};{pixel_color[1]};{pixel_color[2]}"), ln=False)
131+
t += TextFormatter.format_text_truecolor(" ", background=f"{pixel_color[0]};{pixel_color[1]};{pixel_color[2]}")
104132
else:
105-
Send(channel, " ", ln=False)
133+
t += " "
134+
135+
Send(client, t, ln=False)
136+
Send(client, "")
137+
t = ""
106138

107-
Send(channel, "")
108139
elif command == "errortest":
109140
raise Exception("hello error")
110141
elif command == "inloadtest":
@@ -117,8 +148,50 @@ def command(channel, command: str, client):
117148
l.start()
118149
for i in range(101):
119150
l.current = i
120-
l.desc = "loading..."
151+
l.status = f"loading {i}"
121152
time.sleep(0.05)
122153
l.stop()
154+
elif command == "dialogtest":
155+
Di1 = TextDialog(client, "PyserSSH Extension", "Hello Dialog!")
156+
Di1.render()
157+
elif command == "dialogtest2":
158+
Di2 = MenuDialog(client, ["H1", "H2", "H3"], "PyserSSH Extension", "Hello world")
159+
Di2.render()
160+
Send(client, f"selected index: {Di2.output()}")
161+
elif command == "dialogtest3":
162+
Di3 = TextInputDialog(client, "PyserSSH Extension")
163+
Di3.render()
164+
Send(client, f"input: {Di3.output()}")
165+
elif command == "passdialogtest3":
166+
Di3 = TextInputDialog(client, "PyserSSH Extension", inputtitle="Password Here", password=True)
167+
Di3.render()
168+
Send(client, f"password: {Di3.output()}")
169+
elif command == "choosetest":
170+
cindex = wait_choose(client, ["H1", "H2", "H3"], "select: ")
171+
Send(client, f"selected index: {cindex}")
172+
elif command.startswith("vieweb"):
173+
args = shlex.split(command)
174+
url = args[1]
175+
loading = indeterminateStatus(client, desc=f"requesting {url}...")
176+
loading.start()
177+
try:
178+
content = requests.get(url).content
179+
except:
180+
loading.stopfail()
181+
return
182+
loading.stop()
183+
loading = indeterminateStatus(client, desc=f"parsing html {url}...")
184+
loading.start()
185+
try:
186+
soup = BeautifulSoup(content, 'html.parser')
187+
# Extract only the text content
188+
text_content = soup.get_text()
189+
except:
190+
loading.stopfail()
191+
return
192+
loading.stop()
193+
Di1 = TextDialog(client, url, text_content)
194+
Di1.render()
195+
123196

124-
ssh.run(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'private_key.pem'))
197+
ssh.run(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'private_key.pem'))

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='PyserSSH',
8-
version='4.0',
8+
version='4.2.1', # update pypi (no update for 4.3)
99
license='MIT',
1010
author='damp11113',
1111
author_email='[email protected]',
@@ -17,5 +17,8 @@
1717
long_description_content_type='text/markdown',
1818
install_requires=[
1919
"paramiko"
20-
]
20+
],
21+
extras_require={
22+
"fullsyscom": ["damp11113"]
23+
}
2124
)

src/PyserSSH/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,44 @@
2525
SOFTWARE.
2626
"""
2727

28+
"""
29+
note
30+
31+
ansi cursor arrow
32+
up - \x1b[A
33+
down - \x1b[B
34+
left - \x1b[D
35+
right - \x1b[C
36+
37+
https://en.wikipedia.org/wiki/ANSI_escape_code
38+
"""
39+
import os
40+
import logging
41+
2842
from .interactive import *
2943
from .server import Server
3044
from .account import AccountManager
45+
from .system.info import system_banner
46+
47+
48+
try:
49+
os.environ["pyserssh_systemmessage"]
50+
except:
51+
os.environ["pyserssh_systemmessage"] = "YES"
52+
53+
try:
54+
os.environ["pyserssh_enable_damp11113"]
55+
except:
56+
os.environ["pyserssh_enable_damp11113"] = "YES"
57+
58+
try:
59+
os.environ["pyserssh_log"]
60+
except:
61+
os.environ["pyserssh_log"] = "NO"
62+
63+
if os.environ["pyserssh_log"]:
64+
logger = logging.getLogger("PyserSSH")
65+
logger.disabled = True
66+
67+
if os.environ["pyserssh_systemmessage"] == "YES":
68+
print(system_banner)

src/PyserSSH/account.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ def get_user_sftp_path(self, username):
111111
return self.accounts[username]["sftp_path"]
112112
return ""
113113

114+
def get_user_timeout(self, username):
115+
if username in self.accounts and "timeout" in self.accounts[username]:
116+
return self.accounts[username]["timeout"]
117+
return 0
118+
119+
def set_user_timeout(self, username, timeout=0):
120+
if username in self.accounts:
121+
self.accounts[username]["timeout"] = timeout
122+
114123
def add_history(self, username, command):
115124
if not self.anyuser:
116125
if username in self.accounts:

0 commit comments

Comments
 (0)