开始实现第三方服务可调用的API接口。

dev
Apex Liu 2020-07-01 03:06:00 +08:00
parent c863a37094
commit 9cd7c609f9
5 changed files with 72 additions and 0 deletions

View File

@ -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):
"""
创建目录

View File

@ -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()

View File

@ -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

View File

@ -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),
]

View File

@ -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