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
|
|
|
|
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
|
|
|
|
2019-11-12 14:13:03 +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])
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
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()
|
|
|
|
|
|
2019-11-12 14:13:03 +05:00
|
|
|
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-11-06 14:02:30 +05:00
|
|
|
time.sleep(0.01)
|
|
|
|
|
else:
|
|
|
|
|
return 0
|
2019-11-04 23:05:13 +05:00
|
|
|
except BaseException, 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()
|
|
|
|
|
except BaseException, msg:
|
|
|
|
|
print(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:
|
|
|
|
|
data = json.loads(self.data)
|
|
|
|
|
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']))
|
2019-11-04 23:05:13 +05:00
|
|
|
except:
|
|
|
|
|
pass
|
2019-11-03 19:31:21 +05:00
|
|
|
|
2019-11-04 23:05:13 +05:00
|
|
|
def handleConnected(self):
|
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()
|
2019-11-03 19:31:21 +05:00
|
|
|
|
2019-11-04 23:05:13 +05:00
|
|
|
def handleClose(self):
|
|
|
|
|
try:
|
|
|
|
|
os.remove(self.verifyPath)
|
2019-11-06 14:02:30 +05:00
|
|
|
self.running = 0
|
2019-11-04 23:05:13 +05:00
|
|
|
except:
|
|
|
|
|
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
|
|
|
|
2019-11-12 14:13:03 +05:00
|
|
|
SSHServer.findSSHPort()
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, close_sig_handler)
|
|
|
|
|
server.serveforever()
|