Files
CyberPanel/WebTerminal/CPWebSocket.py

166 lines
4.9 KiB
Python
Raw Normal View History

2020-06-01 13:24:52 +05:00
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
2020-02-08 23:09:18 +05:00
import sys
import os
sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
2019-11-02 19:29:02 +05:00
import paramiko
2019-11-04 23:05:13 +05:00
import os
2019-11-02 19:29:02 +05:00
import json
2019-11-04 23:05:13 +05:00
import threading as multi
import time
2020-06-01 13:24:52 +05:00
import asyncio
2021-03-30 16:16:59 +05:00
from plogical.processUtilities import ProcessUtilities
2020-02-08 23:09:18 +05:00
2019-11-04 23:05:13 +05:00
class SSHServer(multi.Thread):
2019-11-05 14:07:37 +05:00
OKGREEN = '\033[92m'
ENDC = '\033[0m'
2019-11-02 19:29:02 +05:00
DEFAULT_PORT = 22
@staticmethod
def findSSHPort():
try:
2021-03-30 16:16:59 +05:00
sshData = ProcessUtilities.outputExecutioner('cat /etc/ssh/sshd_config').readlines()
for items in sshData:
if items.find('Port') > -1:
if items[0] == 0:
pass
else:
SSHServer.DEFAULT_PORT = int(items.split(' ')[1])
2020-09-02 13:26:46 +05:00
logging.writeToFile('SSH Port for WebTerminal Connection: %s' % (SSHServer.DEFAULT_PORT))
2020-02-08 23:09:18 +05:00
except BaseException as msg:
logging.writeToFile('%s. [SSHServer.findSSHPort]' % (str(msg)))
2019-11-02 19:29:02 +05:00
def loadPublicKey(self):
pubkey = '/root/.ssh/cyberpanel.pub'
data = open(pubkey, 'r').read()
authFile = '/root/.ssh/authorized_keys'
checker = 1
2019-11-03 19:31:21 +05:00
try:
authData = open(authFile, 'r').read()
if authData.find(data) > -1:
checker = 0
except:
pass
2019-11-02 19:29:02 +05:00
if checker:
writeToFile = open(authFile, 'a')
writeToFile.writelines(data)
writeToFile.close()
2019-11-04 23:05:13 +05:00
def __init__(self, websocket):
multi.Thread.__init__(self)
2019-11-02 19:29:02 +05:00
self.sshclient = paramiko.SSHClient()
self.sshclient.load_system_host_keys()
self.sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
k = paramiko.RSAKey.from_private_key_file('/root/.ssh/cyberpanel')
## Load Public Key
self.loadPublicKey()
self.sshclient.connect('127.0.0.1', SSHServer.DEFAULT_PORT, username='root', pkey=k)
2019-11-02 19:29:02 +05:00
self.shell = self.sshclient.invoke_shell(term='xterm')
self.shell.settimeout(0)
2019-11-04 23:05:13 +05:00
self.websocket = websocket
2019-11-05 14:07:37 +05:00
self.color = 0
2019-11-02 19:29:02 +05:00
2019-11-04 23:05:13 +05:00
def recvData(self):
2020-06-01 13:24:52 +05:00
asyncio.set_event_loop(asyncio.new_event_loop())
2019-11-04 23:05:13 +05:00
while True:
2019-11-02 19:29:02 +05:00
try:
2019-11-06 14:02:30 +05:00
if self.websocket.running:
2020-06-01 13:24:52 +05:00
if os.path.exists(self.verifyPath) and self.filePassword == self.password:
if self.shell.recv_ready():
self.websocket.write_message(self.shell.recv(9000).decode("utf-8"))
else:
time.sleep(0.001)
else:
return 0
2019-11-06 14:02:30 +05:00
else:
return 0
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2020-06-01 13:24:52 +05:00
print('%s. [recvData]' % str(msg))
time.sleep(0.001)
2019-11-02 19:29:02 +05:00
2019-11-04 23:05:13 +05:00
def run(self):
2019-11-02 19:29:02 +05:00
try:
2019-11-04 23:05:13 +05:00
self.recvData()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
print('%s. [SSHServer.run]' % (str(msg)))
2019-11-02 19:29:02 +05:00
2020-06-01 13:24:52 +05:00
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
print('connected')
self.running = 1
self.sh = SSHServer(self)
self.shell = self.sh.shell
self.sh.start()
self.init = 1
print('connect ok')
def on_message(self, message):
try:
print('handle message')
data = json.loads(message)
if self.init:
self.sh.verifyPath = str(data['data']['verifyPath'])
self.sh.password = str(data['data']['password'])
self.sh.filePassword = open(self.sh.verifyPath, 'r').read()
self.init = 0
else:
if os.path.exists(self.sh.verifyPath):
if self.sh.filePassword == self.sh.password:
self.shell.send(str(data['data']))
except BaseException as msg:
print('%s. [WebTerminalServer.handleMessage]' % (str(msg)))
def on_close(self):
print('connection closed')
2019-11-02 19:29:02 +05:00
2020-06-01 13:24:52 +05:00
def check_origin(self, origin):
return True
2019-11-02 19:29:02 +05:00
2020-06-01 13:24:52 +05:00
application = tornado.web.Application([
(r'/', WSHandler),
])
2019-11-02 19:29:02 +05:00
2019-11-04 23:05:13 +05:00
if __name__ == "__main__":
2019-11-02 19:29:02 +05:00
2020-06-01 13:24:52 +05:00
pidfile = '/usr/local/CyberCP/WebTerminal/pid'
2019-11-02 19:29:02 +05:00
2020-06-01 13:24:52 +05:00
writeToFile = open(pidfile, 'w')
writeToFile.write(str(os.getpid()))
writeToFile.close()
2020-09-02 13:26:46 +05:00
SSHServer.findSSHPort()
2020-06-01 13:24:52 +05:00
http_server = tornado.httpserver.HTTPServer(application, ssl_options={
"certfile": "/usr/local/lscp/conf/cert.pem",
"keyfile": "/usr/local/lscp/conf/key.pem",
}, )
2020-02-08 23:09:18 +05:00
2020-06-01 13:24:52 +05:00
ADDR = '0.0.0.0'
http_server.listen(5678, ADDR)
print('*** Websocket Server Started at %s***' % ADDR)
2019-11-04 23:05:13 +05:00
2020-06-01 13:24:52 +05:00
import signal
def close_sig_handler(signal, frame):
http_server.stop()
sys.exit()
2019-11-04 23:05:13 +05:00
2020-06-01 13:24:52 +05:00
signal.signal(signal.SIGINT, close_sig_handler)
2020-06-01 13:24:52 +05:00
tornado.ioloop.IOLoop.instance().start()