Files
CyberPanel/WebTerminal/CPWebSocket.py

161 lines
5.1 KiB
Python
Raw Normal View History

2020-02-08 23:09:18 +05:00
#!/usr/local/CyberCP/bin/python
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-04 23:05:13 +05:00
import signal
import sys
import ssl
from SimpleWebSocketServer import WebSocket, SimpleSSLWebSocketServer
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
2019-11-02 19:29:02 +05:00
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:
sshData = open('/etc/ssh/sshd_config', 'r').readlines()
for items in sshData:
if items.find('Port') > -1:
if items[0] == 0:
pass
else:
SSHServer.DEFAULT_PORT = int(items.split(' ')[1])
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):
while True:
2019-11-02 19:29:02 +05:00
try:
2019-11-06 14:02:30 +05:00
if self.websocket.running:
if os.path.exists(self.websocket.verifyPath):
if self.websocket.filePassword == self.websocket.password:
if self.shell.recv_ready():
if self.color == 0:
text = '%sEnjoy your accelerated Internet by CyberPanel and LiteSpeed%s' % (SSHServer.OKGREEN, SSHServer.ENDC)
nText = 'Enjoy your accelerated Internet by CyberPanel'
self.websocket.sendMessage(self.shell.recv(9000).decode("utf-8").replace(nText, text))
self.color = 1
else:
self.websocket.sendMessage(self.shell.recv(9000).decode("utf-8"))
2019-11-05 14:07:37 +05:00
else:
2019-12-11 10:10:20 +05:00
time.sleep(0.01)
2019-11-06 14:02:30 +05:00
else:
return 0
2019-12-10 15:09:10 +05:00
except BaseException as msg:
print(str(msg))
2019-11-05 14:07:37 +05:00
time.sleep(0.1)
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
2019-11-04 23:05:13 +05:00
class WebTerminalServer(WebSocket):
2019-11-02 19:29:02 +05:00
2019-11-04 23:05:13 +05:00
def handleMessage(self):
try:
print('handle message')
2019-11-04 23:05:13 +05:00
data = json.loads(self.data)
print(str(data))
2019-11-04 23:05:13 +05:00
if str(self.data).find('"tp":"init"') > -1:
self.verifyPath = str(data['data']['verifyPath'])
2019-11-05 14:07:37 +05:00
self.password = str(data['data']['password'])
self.filePassword = open(self.verifyPath, 'r').read()
2019-11-04 23:05:13 +05:00
else:
if os.path.exists(self.verifyPath):
2019-11-06 14:02:30 +05:00
if self.filePassword == self.password:
2019-11-05 14:07:37 +05:00
self.shell.send(str(data['data']))
except BaseException as msg:
print('%s. [WebTerminalServer.handleMessage]' % (str(msg)))
2019-11-03 19:31:21 +05:00
2019-11-04 23:05:13 +05:00
def handleConnected(self):
print('connected')
2019-11-06 14:02:30 +05:00
self.running = 1
2019-11-04 23:05:13 +05:00
self.sh = SSHServer(self)
self.shell = self.sh.shell
self.sh.start()
print('connect ok')
2019-11-03 19:31:21 +05:00
2019-11-04 23:05:13 +05:00
def handleClose(self):
try:
try:
os.remove(self.verifyPath)
except:
pass
2019-11-06 14:02:30 +05:00
self.running = 0
except BaseException as msg:
print('%s. [WebTerminalServer.handleClose]' % (str(msg)))
2019-11-04 23:05:13 +05:00
pass
2019-11-02 19:29:02 +05:00
2019-11-04 23:05:13 +05:00
if __name__ == "__main__":
pidfile = '/usr/local/CyberCP/WebTerminal/pid'
2019-11-02 19:29:02 +05:00
2019-11-04 23:05:13 +05:00
writeToFile = open(pidfile, 'w')
writeToFile.write(str(os.getpid()))
writeToFile.close()
2019-11-02 19:29:02 +05:00
SSHServer.findSSHPort()
2020-02-08 23:09:18 +05:00
print ('SSH Port is set to: %s' % (str(SSHServer.DEFAULT_PORT)))
2019-11-04 23:05:13 +05:00
server = SimpleSSLWebSocketServer('0.0.0.0', '5678', WebTerminalServer, '/usr/local/lscp/conf/cert.pem', '/usr/local/lscp/conf/key.pem', version=ssl.PROTOCOL_TLSv1)
def close_sig_handler(signal, frame):
server.close()
sys.exit()
print('server started')
2019-11-04 23:05:13 +05:00
signal.signal(signal.SIGINT, close_sig_handler)
server.serveforever()