Ejemplo n.º 1
0
    def send(self, c_data):
        try:
            if not c_data: return
            if not self._thread:
                s_file = '/www/server/panel/config/t_info.json'
                ssh_info = None
                if os.path.exists(s_file):
                    ssh_info = json.loads(
                        public.en_hexb(public.readFile(s_file)))

                if not 'host' in c_data:
                    host = "127.0.0.1"
                    if ssh_info:
                        c_data = ssh_info
                        host = c_data['host']
                else:
                    host = c_data['host']
                if not host:
                    if not ssh_info:
                        return socketio.emit(
                            self._room, "\rWrong user name or password!\r")
                    c_data = ssh_info
                key = 'ssh_' + host
                if 'password' in c_data:
                    session[key] = c_data
                if not key in session:
                    return socketio.emit(self._room,
                                         "\rWrong user name or password!\r")
                result = self.run(session[key])
            else:
                if len(c_data) > 10:
                    if c_data == 'new_bt_terminal':
                        if not self._send_last: self.last_send()
                        self._send_last = False
                        return
                    if c_data.find('resize_pty') != -1:
                        if self.resize(c_data): return
                if type(c_data) == dict: return
                if self._host in self._my_terms:
                    self._my_terms[self._host].last_time = time.time()
                    self._my_terms[self._host].tty.send(c_data)
                else:
                    return
        except:
            self.close()
            socketio.emit(self._room, '\rConnection failure!\r')
            print(public.get_error_info())
Ejemplo n.º 2
0
    def is_local(self):
        '''
            @name 处理本地连接
            @author hwliang<2020-08-07>
            @ps 如果host为127.0.0.1或localhost,则尝试自动使用publicKey登录
            @return void
        '''

        if self._pass: return
        if self._pkey: return
        if self._host in ['127.0.0.1', 'localhost']:
            try:
                self._port = public.get_ssh_port()
                self.set_sshd_config()
                s_file = '/www/server/panel/config/t_info.json'
                if os.path.exists(s_file):
                    ssh_info = json.loads(
                        public.en_hexb(public.readFile(s_file)))
                    self._host = ssh_info['host'].strip()
                    if 'username' in ssh_info:
                        self._user = ssh_info['username']
                    if 'pkey' in ssh_info:
                        self._pkey = ssh_info['pkey']
                    if 'password' in ssh_info:
                        self._pass = ssh_info['password']
                    self._old_conf = True
                    return

                login_user = self.get_login_user()
                if self._user == 'root' and login_user == 'root':
                    id_rsa_file = ['/root/.ssh/id_rsa', '/root/.ssh/id_rsa_bt']
                    for ifile in id_rsa_file:
                        if os.path.exists(ifile):
                            self._pkey = public.readFile(ifile)
                            host_path = self._save_path + self._host
                            if not os.path.exists(host_path):
                                os.makedirs(host_path, 384)
                            return

                if not self._pass or not self._pkey or not self._user:
                    home_path = '/home/' + login_user
                    if login_user == 'root':
                        home_path = '/root'
                    self._user = login_user
                    id_rsa_file = [
                        home_path + '/.ssh/id_rsa',
                        home_path + '/.ssh/id_rsa_bt'
                    ]
                    for ifile in id_rsa_file:
                        if os.path.exists(ifile):
                            self._pkey = public.readFile(ifile)
                            return

                    self._pass = '******'
                    return
                    # _ssh_ks = home_path + '/.ssh'
                    # if not  os.path.exists(_ssh_ks):
                    #     os.makedirs(_ssh_ks,384)
                    # os.system("ssh-keygen -t rsa -P '' -f {}/.ssh/id_rsa |echo y".format(home_path))
                    # pub_file = home_path + '/.ssh/id_rsa.pub'
                    # az_file = home_path + '/.ssh/authorized_keys'
                    # rsa_file = home_path + '/.ssh/id_rsa'
                    # public.ExecShell('cat {} >> {} && chmod 600 {} {}'.format(pub_file, az_file, az_file,rsa_file))
                    # os.remove(pub_file)
                    # public.ExecShell("chown -R {}:{} {}".format(self._user,self._user,_ssh_ks))
                    # public.ExecShell("chmod -R 600 {}".format(_ssh_ks))
                    # self._pkey = public.readFile(rsa_file)

            except:
                return