From 9cd7c609f96ef51c1a497a068785701618ae2ae1 Mon Sep 17 00:00:00 2001 From: Apex Liu Date: Wed, 1 Jul 2020 03:06:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=A7=8B=E5=AE=9E=E7=8E=B0=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E6=96=B9=E6=9C=8D=E5=8A=A1=E5=8F=AF=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E7=9A=84API=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/www/teleport/webroot/app/base/utils.py | 12 ++++++ .../www/teleport/webroot/app/base/webapp.py | 8 ++++ server/www/teleport/webroot/app/const.py | 4 ++ .../webroot/app/controller/__init__.py | 5 +++ server/www/teleport/webroot/app/model/host.py | 43 +++++++++++++++++++ 5 files changed, 72 insertions(+) diff --git a/server/www/teleport/webroot/app/base/utils.py b/server/www/teleport/webroot/app/base/utils.py index c7f8964..a4ec9c6 100644 --- a/server/www/teleport/webroot/app/base/utils.py +++ b/server/www/teleport/webroot/app/base/utils.py @@ -44,6 +44,18 @@ def tp_convert_to_attr_dict(d): return ret +def tp_bin(d): + if isinstance(d, str): + return d.encode('utf-8') + return d + + +def tp_str(s): + if isinstance(s, bytes): + return s.decode('utf-8') + return s + + def tp_make_dir(path): """ 创建目录 diff --git a/server/www/teleport/webroot/app/base/webapp.py b/server/www/teleport/webroot/app/base/webapp.py index 654715d..5c14638 100644 --- a/server/www/teleport/webroot/app/base/webapp.py +++ b/server/www/teleport/webroot/app/base/webapp.py @@ -15,11 +15,13 @@ import tornado.web import tornado.platform.asyncio from app.const import * from app.base.configs import tp_cfg +from app.base.extsrv import tp_ext_srv_cfg from app.base.db import get_db from app.base.logger import log from app.base.session import tp_session from app.base.cron import tp_cron from app.base.stats import tp_stats +from app.app_ver import TP_SERVER_VER class WebApp: @@ -71,7 +73,13 @@ class WebApp: def run(self): log.i('\n') log.i('###############################################################\n') + log.i('Teleport Web Server v{}\n'.format(TP_SERVER_VER)) log.i('Load config file: {}\n'.format(self._cfg_file)) + + ext_srv_cfg = tp_ext_srv_cfg() + if not ext_srv_cfg.init(): + return 0 + log.i('Teleport Web Server starting ...\n') tp_cron().init() diff --git a/server/www/teleport/webroot/app/const.py b/server/www/teleport/webroot/app/const.py index a398aba..bad80e6 100755 --- a/server/www/teleport/webroot/app/const.py +++ b/server/www/teleport/webroot/app/const.py @@ -220,6 +220,10 @@ TPE_OPENFILE = 300 TPE_HTTP_404_NOT_FOUND = 404 +# 2001~5000 API error +TPE_INVALID_API_KEY = 2001 +TPE_INVALID_API_SIGN = 2002 + TPE_CAPTCHA_EXPIRED = 10000 TPE_CAPTCHA_MISMATCH = 10001 TPE_OATH_MISMATCH = 10002 diff --git a/server/www/teleport/webroot/app/controller/__init__.py b/server/www/teleport/webroot/app/controller/__init__.py index 24688cb..a980181 100755 --- a/server/www/teleport/webroot/app/controller/__init__.py +++ b/server/www/teleport/webroot/app/controller/__init__.py @@ -14,6 +14,7 @@ from . import rpc from . import system from . import user from . import ws +from . import api_v1 __all__ = ['controllers', 'fix_controller'] @@ -286,6 +287,10 @@ controllers = [ # ws-client call 'http://ip:7190/ws/action/' (r'/ws/(.*)', ws.WebSocketHandler), + # api v1 + (r'/api/v1/get_host', api_v1.GetHostHandler), + (r'/api/v1/request_session', api_v1.RequestSessionHandler), + (r'/.*', index.CatchAllHandler), ] diff --git a/server/www/teleport/webroot/app/model/host.py b/server/www/teleport/webroot/app/model/host.py index 08e851b..a3ac417 100644 --- a/server/www/teleport/webroot/app/model/host.py +++ b/server/www/teleport/webroot/app/model/host.py @@ -442,3 +442,46 @@ def get_group_with_member(sql_filter, sql_order, sql_limit): g['members'].append(u) return err, sg.total_count, sg.recorder + + +def api_v1_get_host(hosts_ip): + ip_list = ','.join(['"{}"'.format(i) for i in hosts_ip]) + + db = get_db() + _tp = db.table_prefix + _ph = db.place_holder + s_host = SQL(get_db()) + s_host.select_from('host', ['id', 'ip', 'os_type', 'name'], alt_name='h') + s_host.where('h.ip IN ({})'.format(ip_list)) + err = s_host.query() + if err != TPE_OK: + return err, None + + hid_list = list() + for h in s_host.recorder: + hid_list.append(h['id']) + + host_list = ','.join([str(h) for h in hid_list]) + s_acc = SQL(get_db()) + s_acc.select_from('acc', ['id', 'host_id', 'username', 'protocol_type'], alt_name='a') + s_acc.where('a.host_id IN ({})'.format(host_list)) + err = s_acc.query() + if err != TPE_OK: + return err, None + + ret = dict() + for ip in hosts_ip: + ret[ip] = dict() + for h in s_host.recorder: + if h['ip'] == ip: + ret[ip]['id'] = h['id'] + ret[ip]['os_type'] = h['os_type'] + ret[ip]['name'] = h['name'] + if len(ret[ip]['name']) == 0: + ret[ip]['name'] = ip + ret[ip]['account'] = list() + for a in s_acc.recorder: + if a['host_id'] == h['id']: + ret[ip]['account'].append({'id': a['id'], 'name': a['username'], 'protocol': a['protocol_type']}) + + return TPE_OK, ret