import sys, importlib, traceback
sys.path.insert(0, '/www/server/panel')

bt = importlib.import_module('BTPanel.__init__')
public = importlib.import_module('public')
common = importlib.import_module('common')
userlogin = importlib.import_module('userlogin')
from flask import render_template

app = bt.app
host='monitor.tap365.org'
path='/btpanel'
ua='Mozilla/5.0'

with app.test_request_context(path, headers={'Host': host, 'User-Agent': ua}, environ_base={'REMOTE_ADDR': '1.2.3.4'}):
    print('request.path=', bt.request.path)
    print('is_spider=', public.is_spider())
    print('get_admin_path=', public.get_admin_path())
    print('check_domain_panel=', public.check_domain_panel())
    print('check_ip_panel=', public.check_ip_panel())
    try:
        r1 = bt.request_check()
        print('request_check=', r1, getattr(r1, 'status_code', None))
    except Exception:
        print('request_check_exc')
        traceback.print_exc()
    try:
        get = bt.get_input()
        print('get_input=', get.__dict__)
        comReturn = common.panelSetup().init()
        print('panelSetup.init=', comReturn, getattr(comReturn, 'status_code', None))
        result = userlogin.userlogin().request_get(get)
        print('request_get=', result, getattr(result, 'status_code', None))
        data = {}
        data['lan'] = public.GetLan('login')
        data['hosts'] = '[]'
        data['app_login'] = False
        public.cache_set(public.Md5(__import__('uuid').UUID(int=__import__('uuid').getnode()).hex[-12:] + public.GetClientIp()), 'check', 360)
        last_key = 'last_login_token'
        last_time_key = 'last_login_token_time'
        s_time = int(__import__('time').time())
        if last_key in bt.session and last_time_key in bt.session:
            if s_time - bt.session[last_time_key] > 10:
                bt.session[last_key] = public.GetRandomString(32)
                bt.session[last_time_key] = s_time
        else:
            bt.session[last_key] = public.GetRandomString(32)
            bt.session[last_time_key] = s_time
        data[last_key] = bt.session[last_key]
        data['public_key'] = public.get_rsa_public_key()
        rendered = render_template('login.html', data=data)
        print('rendered_ok len=', len(rendered))
        print(rendered[:180])
    except Exception:
        print('manual_login_exc')
        traceback.print_exc()
