Commit 33b0e123 authored by ibuler's avatar ibuler

[Fixture] 完成web terminal项目拆分,完成代码结构

parent 422cf996
# ~*~ coding: utf-8 ~*~
class Config(object):
pass
\ No newline at end of file
#!/usr/bin/env python
# ~*~ coding: utf-8 ~*~
#
from flask import Flask
from flask_socketio import SocketIO
from apps.ssh_server.utils import AppRequest
from .conf import CONFIG
app = Flask(__name__, template_folder='dist')
app.config.from_object(CONFIG)
socket_io = SocketIO(app)
api = AppRequest(app.config['NAME'])
import os
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
from .luna import app, socket_io
from . import authentication, views
......@@ -5,14 +5,10 @@
from flask import g, request
from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth, MultiAuth
from conf import CONFIG
from utils import get_logger, TerminalApiRequest, Dict
from . import app
from .errors import forbidden, unauthorized
from .errors import unauthorized
NAME = CONFIG.NAME
api = TerminalApiRequest(NAME)
token_auth = HTTPTokenAuth()
basic_auth = HTTPBasicAuth()
auth = MultiAuth(token_auth, basic_auth)
......@@ -20,7 +16,8 @@ auth = MultiAuth(token_auth, basic_auth)
@basic_auth.verify_password
def verify_password(username, password):
user = api.login(username=username, password=password, remote_addr=request.remote_addr)
return True
user = app.user_service.login(username=username, password=password, remote_addr=request.remote_addr)
if not user:
g.current_user = None
return False
......@@ -31,16 +28,17 @@ def verify_password(username, password):
@token_auth.verify_token
def verify_token(token):
return True
if getattr(g, 'token') and g.token == token:
return True
else:
return False
@app.before_request
@auth.login_required
def before_request():
print('Request start')
if g.current_user is None:
print('User is None')
return unauthorized('Invalid credentials')
#@app.before_request
#@auth.login_required
#def before_request():
# print('Request start')
# if g.current_user is None:
# print('User is None')
# return unauthorized('Invalid credentials')
import sys
import os
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
PROJECT_DIR = os.path.dirname(os.path.dirname(BASE_DIR))
sys.path.append(os.path.dirname(BASE_DIR))
sys.path.append(PROJECT_DIR)
# Use main config except define by self
from config import config as main_config, WebTerminalConfig
CONFIG = main_config.get('web_terminal', WebTerminalConfig)
8f562c1c-bdbd-4e92-b6d1-d7b5791ce688:5c30672a-6bfa-42d3-982e-566448c88199
\ No newline at end of file
# ~*~ coding: utf-8 ~*~
import os
import logging
import time
from flask import Flask
from flask_socketio import SocketIO
from jms import AppService, UserService
from jms.mixin import AppMixin
from . import BASE_DIR
__version__ = '0.4.0'
class Luna(Flask, AppMixin):
default_config = dict(Flask.default_config)
default_config.update({
'NAME': 'luna',
'BIND_HOST': '0.0.0.0',
'LISTEN_PORT': 5000,
'JUMPSERVER_ENDPOINT': 'http://localhost:8080',
'ACCESS_KEY': None,
'ACCESS_KEY_ENV': 'LUNA_ACCESS_KEY',
'ACCESS_KEY_STORE': os.path.join(BASE_DIR, 'keys', '.access_key'),
'LOG_LEVEL': 'DEBUG',
'LOG_DIR': os.path.join(BASE_DIR, 'logs'),
'ASSET_LIST_SORT_BY': 'ip',
'HEATBEAT_INTERVAL': 5,
})
app_service = None
user_service = None
def bootstrap(self):
self.app_service = AppService(app_name=self.config['NAME'],
endpoint=self.config['JUMPSERVER_ENDPOINT'])
self.user_service = UserService(app_name=self.config['NAME'],
endpoint=self.config['JUMPSERVER_ENDPOINT'])
self.app_auth()
while True:
if self.check_auth():
logging.info('App auth passed')
break
else:
logging.warn('App auth failed, Access key error or need admin active it')
time.sleep(5)
self.heatbeat()
def run(self, host=None, port=None, debug=None, **options):
self.bootstrap()
print(time.ctime())
print('Luna version %s, more see https://www.jumpserver.org' % __version__)
print('Starting ssh server at %(host)s:%(port)s' % {'host': self.config['BIND_HOST'],
'port': self.config['LISTEN_PORT']})
print('Quit the server with CONTROL-C.')
return Flask.run(self, host=host, port=port, debug=debug, **options)
app = Luna(__name__, template_folder='dist')
socket_io = SocketIO(app)
......@@ -5,7 +5,7 @@
from flask import render_template, send_from_directory, jsonify, g
from flask_socketio import emit
from . import app, socket_io, api
from . import app, socket_io
@app.route('/api/token')
......
flask==0.12
paramiko==2.1.1
python-gssapi==0.6.4
requests==2.11.1
flask-socketio==2.8.2
flask-httpauth==3.2.1
jumpserver-python-sdk
\ No newline at end of file
#!/usr/bin/env python
# ~*~ coding: utf-8 ~*~
from luna import app
from config import Config
app.config.from_object(Config)
if __name__ == '__main__':
app.run(host=app.config['BIND_HOST'], port=app.config['LISTEN_PORT'],
debug=app.config['DEBUG'])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment