1+ import tornado .httpserver
2+ import tornado .websocket
3+ import tornado .ioloop
4+ import tornado .web
5+ import socket #!/usr/local/CyberCP/bin/python
6+ import sys
7+ import os
8+ sys .path .append ('/usr/local/CyberCP' )
9+ os .environ .setdefault ("DJANGO_SETTINGS_MODULE" , "CyberCP.settings" )
10+ from plogical .CyberCPLogFileWriter import CyberCPLogFileWriter as logging
11+ import paramiko
12+ import os
13+ import json
14+ import threading as multi
15+ import time
16+ import asyncio
17+ '''
18+ This is a simple Websocket Echo server that uses the Tornado websocket handler.
19+ Please run `pip install tornado` with python of version 2.7.9 or greater to install tornado.
20+ This program will echo back the reverse of whatever it recieves.
21+ Messages are output to the terminal for debuggin purposes.
22+ '''
23+
24+
25+ class SSHServer (multi .Thread ):
26+ OKGREEN = '\033 [92m'
27+ ENDC = '\033 [0m'
28+
29+ DEFAULT_PORT = 22
30+
31+ @staticmethod
32+ def findSSHPort ():
33+ try :
34+ sshData = open ('/etc/ssh/sshd_config' , 'r' ).readlines ()
35+
36+ for items in sshData :
37+ if items .find ('Port' ) > - 1 :
38+ if items [0 ] == 0 :
39+ pass
40+ else :
41+ SSHServer .DEFAULT_PORT = int (items .split (' ' )[1 ])
42+ except BaseException as msg :
43+ logging .writeToFile ('%s. [SSHServer.findSSHPort]' % (str (msg )))
44+
45+ def loadPublicKey (self ):
46+ pubkey = '/root/.ssh/cyberpanel.pub'
47+ data = open (pubkey , 'r' ).read ()
48+ authFile = '/root/.ssh/authorized_keys'
49+
50+ checker = 1
51+
52+ try :
53+ authData = open (authFile , 'r' ).read ()
54+ if authData .find (data ) > - 1 :
55+ checker = 0
56+ except :
57+ pass
58+
59+ if checker :
60+ writeToFile = open (authFile , 'a' )
61+ writeToFile .writelines (data )
62+ writeToFile .close ()
63+
64+ def __init__ (self , websocket ):
65+ multi .Thread .__init__ (self )
66+ self .sshclient = paramiko .SSHClient ()
67+ self .sshclient .load_system_host_keys ()
68+ self .sshclient .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
69+ k = paramiko .RSAKey .from_private_key_file ('/root/.ssh/cyberpanel' )
70+
71+ ## Load Public Key
72+ self .loadPublicKey ()
73+
74+ self .sshclient .connect ('127.0.0.1' , SSHServer .DEFAULT_PORT , username = 'root' , pkey = k )
75+ self .shell = self .sshclient .invoke_shell (term = 'xterm' )
76+ self .shell .settimeout (0 )
77+
78+ self .websocket = websocket
79+ self .color = 0
80+
81+ def recvData (self ):
82+ asyncio .set_event_loop (asyncio .new_event_loop ())
83+ while True :
84+ try :
85+ if self .websocket .running :
86+ if os .path .exists (self .verifyPath ) and self .filePassword == self .password :
87+ if self .shell .recv_ready ():
88+ self .websocket .write_message (self .shell .recv (9000 ).decode ("utf-8" ))
89+ else :
90+ time .sleep (0.001 )
91+ else :
92+ return 0
93+ else :
94+ return 0
95+ except BaseException as msg :
96+ print ('%s. [recvData]' % str (msg ))
97+ time .sleep (0.001 )
98+
99+ def run (self ):
100+ try :
101+ self .recvData ()
102+ except BaseException as msg :
103+ print ('%s. [SSHServer.run]' % (str (msg )))
104+
105+
106+ class WSHandler (tornado .websocket .WebSocketHandler ):
107+
108+ def open (self ):
109+ print ('connected' )
110+ self .running = 1
111+ self .sh = SSHServer (self )
112+ self .shell = self .sh .shell
113+ self .sh .start ()
114+ self .init = 1
115+ print ('connect ok' )
116+
117+ def on_message (self , message ):
118+ try :
119+ print ('handle message' )
120+ data = json .loads (message )
121+
122+ if self .init :
123+ self .sh .verifyPath = str (data ['data' ]['verifyPath' ])
124+ self .sh .password = str (data ['data' ]['password' ])
125+ self .sh .filePassword = open (self .sh .verifyPath , 'r' ).read ()
126+ self .init = 0
127+ else :
128+ if os .path .exists (self .sh .verifyPath ):
129+ if self .sh .filePassword == self .sh .password :
130+ self .shell .send (str (data ['data' ]))
131+
132+ except BaseException as msg :
133+ print ('%s. [WebTerminalServer.handleMessage]' % (str (msg )))
134+
135+ def on_close (self ):
136+ print ('connection closed' )
137+
138+ def check_origin (self , origin ):
139+ return True
140+
141+
142+ application = tornado .web .Application ([
143+ (r'/' , WSHandler ),
144+ ])
145+
146+ if __name__ == "__main__" :
147+ http_server = tornado .httpserver .HTTPServer (application , ssl_options = {
148+ "certfile" : "/usr/local/lscp/conf/cert.pem" ,
149+ "keyfile" : "/usr/local/lscp/conf/key.pem" ,
150+ }, )
151+ ADDR = '0.0.0.0'
152+ http_server .listen (5678 , ADDR )
153+ print ('*** Websocket Server Started at %s***' % ADDR )
154+ tornado .ioloop .IOLoop .instance ().start ()
0 commit comments