mirror of https://github.com/tp4a/teleport
开始实现第三方服务可调用的API接口。
parent
c863a37094
commit
9cd7c609f9
|
@ -44,6 +44,18 @@ def tp_convert_to_attr_dict(d):
|
||||||
return ret
|
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):
|
def tp_make_dir(path):
|
||||||
"""
|
"""
|
||||||
创建目录
|
创建目录
|
||||||
|
|
|
@ -15,11 +15,13 @@ import tornado.web
|
||||||
import tornado.platform.asyncio
|
import tornado.platform.asyncio
|
||||||
from app.const import *
|
from app.const import *
|
||||||
from app.base.configs import tp_cfg
|
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.db import get_db
|
||||||
from app.base.logger import log
|
from app.base.logger import log
|
||||||
from app.base.session import tp_session
|
from app.base.session import tp_session
|
||||||
from app.base.cron import tp_cron
|
from app.base.cron import tp_cron
|
||||||
from app.base.stats import tp_stats
|
from app.base.stats import tp_stats
|
||||||
|
from app.app_ver import TP_SERVER_VER
|
||||||
|
|
||||||
|
|
||||||
class WebApp:
|
class WebApp:
|
||||||
|
@ -71,7 +73,13 @@ class WebApp:
|
||||||
def run(self):
|
def run(self):
|
||||||
log.i('\n')
|
log.i('\n')
|
||||||
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))
|
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')
|
log.i('Teleport Web Server starting ...\n')
|
||||||
|
|
||||||
tp_cron().init()
|
tp_cron().init()
|
||||||
|
|
|
@ -220,6 +220,10 @@ TPE_OPENFILE = 300
|
||||||
|
|
||||||
TPE_HTTP_404_NOT_FOUND = 404
|
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_EXPIRED = 10000
|
||||||
TPE_CAPTCHA_MISMATCH = 10001
|
TPE_CAPTCHA_MISMATCH = 10001
|
||||||
TPE_OATH_MISMATCH = 10002
|
TPE_OATH_MISMATCH = 10002
|
||||||
|
|
|
@ -14,6 +14,7 @@ from . import rpc
|
||||||
from . import system
|
from . import system
|
||||||
from . import user
|
from . import user
|
||||||
from . import ws
|
from . import ws
|
||||||
|
from . import api_v1
|
||||||
|
|
||||||
__all__ = ['controllers', 'fix_controller']
|
__all__ = ['controllers', 'fix_controller']
|
||||||
|
|
||||||
|
@ -286,6 +287,10 @@ controllers = [
|
||||||
# ws-client call 'http://ip:7190/ws/action/'
|
# ws-client call 'http://ip:7190/ws/action/'
|
||||||
(r'/ws/(.*)', ws.WebSocketHandler),
|
(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),
|
(r'/.*', index.CatchAllHandler),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -442,3 +442,46 @@ def get_group_with_member(sql_filter, sql_order, sql_limit):
|
||||||
g['members'].append(u)
|
g['members'].append(u)
|
||||||
|
|
||||||
return err, sg.total_count, sg.recorder
|
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
|
||||||
|
|
Loading…
Reference in New Issue