Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit d9cc0aa

Browse files
committed
Recoded Base
1 parent 6720264 commit d9cc0aa

File tree

7 files changed

+128
-143
lines changed

7 files changed

+128
-143
lines changed

common/__init__.py

+57-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import asyncio
2+
from asyncio import sleep, Event, create_task
3+
from contextlib import suppress
4+
from getpass import getpass
5+
from itertools import cycle
26
from os import name, system
37
from socket import gethostname
48

@@ -9,10 +13,15 @@
913
from tools.impl import handle as tools_handle
1014

1115

16+
# noinspection PyMethodMayBeStatic
1217
class Console:
13-
@staticmethod
14-
async def banner():
15-
await Console.clear()
18+
def __init__(self):
19+
self.using = None
20+
self.dots = cycle(["|", "/", "-", "\\"])
21+
self.loading_text = ""
22+
23+
async def banner(self):
24+
await self.clear()
1625
await aprint(Colorate.Diagonal(Colors.yellow_to_red, Center.XCenter("""
1726
╔════════════════════════════════════════════════════════════════════════════════╗
1827
║ ooooooooo. ooooooooooooo oooo ║
@@ -28,37 +37,54 @@ async def banner():
2837
║ Coded By : MXTeam - V 1.0 ║
2938
╚═════════════════════════════════╝\n\n\n\n""")))
3039

31-
@staticmethod
32-
async def input(*messages):
33-
return await ainput(' '.join([txt for txt in messages]))
40+
async def loader(self):
41+
while 1:
42+
if not self.using.is_set():
43+
await self.using.wait()
44+
await aprint(Colorate.Horizontal(Colors.rainbow,
45+
"[%s] %s" %
46+
(next(self.dots),
47+
self.loading_text)),
48+
end="\r")
49+
await sleep(.05)
50+
51+
async def input(self, *messages, hide_value=False):
52+
if not hide_value:
53+
return await ainput(' '.join([str(txt) for txt in messages]))
54+
return getpass(' '.join([str(txt) for txt in messages]))
3455

35-
@staticmethod
36-
async def error(*messages):
37-
await aprint(Colorate.Diagonal(Colors.red_to_white, "[X] " + ' '.join([txt for txt in messages])))
56+
async def cinput(self, *messages, hide_value=False):
57+
return await self.input(Colorate.Horizontal(Colors.yellow_to_red,
58+
f"╔═══[{gethostname()}@PyTools]"
59+
f"{(' (' + ' '.join([txt for txt in messages]) + ')') if messages else ''} "
60+
f"\n╚══════> "), hide_value=hide_value)
3861

39-
@staticmethod
40-
async def clear():
41-
await Console.command('cls' if name == 'nt' else 'clear')
62+
async def error(self, *messages):
63+
await aprint(Colorate.Diagonal(Colors.red_to_white, "[X] " + ' '.join([str(txt) for txt in messages])))
4264

43-
@staticmethod
44-
async def run():
65+
async def clear(self):
66+
if self.using:
67+
self.using.clear()
68+
await aprint(' ' * len(self.loading_text), end="\r")
69+
self.loading_text = ''
70+
await self.command('cls' if name == 'nt' else 'clear')
71+
72+
async def run(self):
73+
self.using = Event()
74+
create_task(self.loader())
4575
while 1:
46-
inp = (await Console.input(Colorate.Horizontal(Colors.yellow_to_red,
47-
f"╔═══[{gethostname()}"
48-
f"@PyTools]\n╚══════> ")))
76+
inp = await self.cinput()
4977
if not inp:
5078
pass
51-
await Console.handle(inp.strip())
79+
await self.handle(inp.strip())
80+
self.using.clear()
5281

53-
@staticmethod
54-
async def command(cmd):
55-
try:
82+
async def command(self, cmd):
83+
with suppress(Exception):
5684
return await (await asyncio.create_subprocess_shell(cmd=cmd)).communicate()
57-
except:
58-
system(cmd)
85+
system(cmd)
5986

60-
@staticmethod
61-
async def handle(inp):
87+
async def handle(self, inp):
6288
cmd, args = tools.Patterns.parsecommand(inp)
6389
cmd = cmd.upper()
6490

@@ -68,16 +94,15 @@ async def handle(inp):
6894
elif {cmd} & {"EXIT", "QUIT", "LOGOUT", "EXIT()"}:
6995
exit(-1)
7096
elif {cmd} & {"CLEAR", "CLS"}:
71-
await Console.banner()
72-
elif await tools_handle(cmd, *args):
97+
await self.banner()
98+
elif await tools_handle(self, cmd, *args):
7399
pass
74100
elif cmd:
75-
await Console.error("\"%s\" command not found" % cmd.lower())
76-
101+
await self.error("\"%s\" command not found" % cmd.lower())
77102

78103
except Exception as e:
79-
await Console.error(str(e) or repr(e))
104+
self.using.clear()
105+
await self.error((str(e) or repr(e)) + ' ' * 50)
80106

81-
@staticmethod
82-
async def info(*messages):
107+
async def info(self, *messages):
83108
await aprint(Colorate.Diagonal(Colors.blue_to_white, "[!] " + ' '.join([txt for txt in messages])))

main.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from common import Console
55

66
if __name__ == '__main__':
7-
run(Console.banner())
7+
console = Console()
8+
run(console.banner())
89
while 1:
910
with suppress(KeyboardInterrupt):
1011
print()
11-
run(Console.run())
12+
run(console.run())

tools/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Callable, Any, List
1010
from time import time
1111

12-
__all__ = ["Patterns", "Random", "Tool", "Math"]
12+
__all__ = ["Patterns", "Random", "Tool", "Math", "Timer"]
1313

1414

1515
class Random:
@@ -67,7 +67,8 @@ def parsecommand(cmd: str):
6767

6868

6969
class Tool:
70-
async def run(self, *args):
70+
@staticmethod
71+
async def run(console, *args):
7172
raise NotImplementedError
7273

7374

@@ -78,6 +79,7 @@ class Math:
7879
def ping_sizer(lists):
7980
return (min(lists, key=int), max(lists, key=int), round(sum(lists) / len(lists), 2)) if lists else (0, 0, 0)
8081

82+
8183
class Timer:
8284
_start: time
8385
_done: time

tools/impl/Pinger.py

+29-46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from asyncio import sleep, open_connection, TimeoutError, wait_for, create_task
1+
from asyncio import sleep, open_connection, TimeoutError, wait_for
22
from contextlib import suppress
3-
from itertools import cycle
43

54
from aioconsole import aprint
65
from aiohttp import ClientSession
@@ -10,15 +9,11 @@
109

1110
from tools import Tool, Timer
1211

13-
dots = cycle(["|", "/", "-", "\\"])
14-
1512

1613
# noinspection PyUnusedLocal
1714
class Pinger(Tool):
18-
using = False
19-
2015
@staticmethod
21-
async def run(*args):
16+
async def run(console, *args):
2217
assert len(args) == 2, "bad args"
2318

2419
bad = 0
@@ -33,33 +28,33 @@ async def run(*args):
3328
assert method, "invalid method"
3429

3530
address = target.human_repr() if method == Pinger.HTTP else target.host
36-
try:
37-
with suppress(KeyboardInterrupt):
38-
Pinger.using = True
39-
create_task(Pinger.amim(args[0].upper(), address, port))
40-
41-
while 1:
42-
request = await method(address, port)
43-
if not request[1] and request[1] != 408:
44-
bad += 1
45-
46-
counter += 1
47-
48-
await sleep(0.5)
49-
pings.append(request[0])
50-
51-
await aprint(
52-
Colorate.Horizontal(
53-
Colors.green_to_cyan if request[1] and request[1] != 408 else Colors.red_to_purple,
54-
"[%s] Reply from %s%sstatus %s protocol %s time: %sms" % (
55-
counter,
56-
address,
57-
(f" port {port} " if method not in [Pinger.ICMP, Pinger.HTTP] else " "),
58-
Pinger.status(request, method),
59-
args[0].upper(),
60-
request[0])))
61-
finally:
62-
Pinger.using = False
31+
with suppress(KeyboardInterrupt):
32+
console.using.set()
33+
console.loading_text = "Pinging %s%susing %s protocol" % (address,
34+
(f" port {port} " if args[0].upper() not in [
35+
"ICMP", "HTTP"] else " "),
36+
args[0].upper())
37+
38+
while 1:
39+
request = await method(address, port)
40+
if not request[1] and request[1] != 408:
41+
bad += 1
42+
43+
counter += 1
44+
45+
await sleep(0.5)
46+
pings.append(request[0])
47+
48+
await aprint(
49+
Colorate.Horizontal(
50+
Colors.green_to_cyan if request[1] and request[1] != 408 else Colors.red_to_purple,
51+
"[%s] Reply from %s%sstatus %s protocol %s time: %sms" % (
52+
counter,
53+
address,
54+
(f" port {port} " if method not in [Pinger.ICMP, Pinger.HTTP] else " "),
55+
Pinger.status(request, method),
56+
args[0].upper(),
57+
request[0])))
6358

6459
@staticmethod
6560
async def ICMP(ip, port):
@@ -116,15 +111,3 @@ def status(request, method):
116111
else:
117112
s = "OVERLOAD"
118113
return s
119-
120-
@staticmethod
121-
async def amim(method, target, port):
122-
while Pinger.using:
123-
await aprint(
124-
"[%s] Pinging %s%susing %s protocol" %
125-
(next(dots),
126-
target,
127-
(f" port {port} " if method not in ["ICMP", "HTTP"] else " "),
128-
method),
129-
end="\r")
130-
await sleep(.05)

tools/impl/SSH.py

+14-22
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
from contextlib import suppress
2-
from getpass import getpass
32
from aioconsole import aprint
43
from asyncssh import SSHClientConnection, connect
54
from tools import Tool
65

76

87
class SSH(Tool):
9-
using = False
10-
118
@staticmethod
12-
async def run(*args):
9+
async def run(console, *args):
1310
assert len(args) == 2, "bad args"
14-
ip, username = str(args[0]).split(":"), args[1] # SSH 185.855.855.690:22 root
15-
password = getpass("Enter Password: ")
16-
await aprint(f"Connecting To {ip[0]}")
17-
try:
18-
with suppress(KeyboardInterrupt):
19-
SSH.using = True
20-
try:
21-
Connection: SSHClientConnection
11+
ip, username = str(args[0]).split(":"), args[1]
12+
password = await console.cinput("Enter Password", hide_value=True)
13+
console.using.set()
14+
console.loading_text = f"Connecting To {ip[0]}"
15+
with suppress(KeyboardInterrupt):
16+
Connection: SSHClientConnection
2217

23-
async with connect(ip[0], port=int(ip[1]), username=username, password=password,
24-
known_hosts=None, connect_timeout=5) as Connection:
25-
while True:
26-
inputcmd = input(f"{username}@{ip[0]}> ")
27-
output = await Connection.run(inputcmd)
28-
await aprint(output.stdout)
29-
except Exception as e:
30-
await aprint(str(e))
31-
finally:
32-
SSH.using = False
18+
async with connect(ip[0], port=int(ip[1]), username=username, password=password,
19+
known_hosts=None, connect_timeout=5) as Connection:
20+
await console.banner()
21+
while True:
22+
inputcmd = await console.cinput(f"{username}@{ip[0]}")
23+
output = await Connection.run(inputcmd)
24+
await aprint(output.stdout)

tools/impl/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
__all__ = ["handle"]
66

77

8-
async def handle(cmd, *args):
8+
async def handle(console, cmd, *args):
99
if {cmd} & {"PING", "PINGER"}:
10-
await Pinger.run(*args)
10+
await Pinger.run(console, *args)
1111
return True
1212
elif {cmd} & {"CFX"}:
13-
await Cfxfinder.run(*args)
13+
await Cfxfinder.run(console, *args)
1414
return True
1515
elif {cmd} & {"SSH"}:
16-
await SSH.run(*args)
16+
await SSH.run(console, *args)
1717
return True
1818
return False

0 commit comments

Comments
 (0)