<commit_before>from base64 import b64encode
import catcher
import gevent
import gevent.coros
import json
import logging
import requests
import traceback
import zlib

import ajenti
from ajenti.api import *
from ajenti.api.http import *
from ajenti.api.sensors import Sensor
from ajenti.licensing import Licensing
from ajenti.middleware import AuthenticationMiddleware
from ajenti.plugins import manager
from ajenti.profiler import *
from ajenti.users import PermissionProvider, UserManager, SecurityError
from ajenti.ui import *
from ajenti.util import make_report
import ajenti.feedback

from api import SectionPlugin


@plugin
class MainServer (BasePlugin, HttpPlugin):
    @url('/')
    def handle_index(self, context):
        context.add_header('Content-Type', 'text/html')
        if context.session.identity is None:
            context.respond_ok()
            html = self.open_content('static/auth.html').read()
            return html % {
                'license': json.dumps(Licensing.get().get_license_status()),
                'error': json.dumps(context.session.data.pop('login-error', None)),
            }
        context.respond_ok()
        return self.open_content('static/index.html').read()

    @url('/ajenti:auth')
    def handle_auth(self, context):
        username = context.query.getvalue('username', '')
        password = context.query.getvalue('password', '')
        if not AuthenticationMiddleware.get().try_login(
            context, username, password, env=context.env,
        ):
            context.session.data['login-error'] = _('Invalid login or password')
            gevent.sleep(3)
        return context.redirect('/')<commit_msg>Pass authentication token from Ajenti to tty.js<commit_after>from base64 import b64encode
import catcher
import gevent
import gevent.coros
import json
import logging
import requests
import traceback
import zlib

import ajenti
from ajenti.api import *
from ajenti.api.http import *
from ajenti.api.sensors import Sensor
from ajenti.licensing import Licensing
from ajenti.middleware import AuthenticationMiddleware
from ajenti.plugins import manager
from ajenti.profiler import *
from ajenti.users import PermissionProvider, UserManager, SecurityError
from ajenti.ui import *
from ajenti.util import make_report
import ajenti.feedback
import random

from api import SectionPlugin


@plugin
class MainServer (BasePlugin, HttpPlugin):
    # Lup Yuen: Expose terminal.html for launching web terminal.
    @url('/terminal.html')
    def handle_terminal(self, context):
        context.add_header('Content-Type', 'text/html')
        # Must be logged in.
        if context.session.identity is None:
            context.respond_ok()
            html = self.open_content('static/auth.html').read()
            return html % {
                'license': json.dumps(Licensing.get().get_license_status()),
                'error': json.dumps(context.session.data.pop('login-error', None)),
            }
        context.respond_ok()
        # Create a token and pass to tty.js.
        token = str(int(random.random() * 1000000)) + '_' + str(int(random.random() * 1000000)) + '_' + str(int(random.random() * 1000000)) + '_' + str(int(random.random() * 1000000))
        f = open('/tmp/tty' + token, 'w')
        f.write('Token passed by Ajenti to tty.js')
        f.close()
        # tty.js url is http://host:3000?token=...
        host = context.env['HTTP_HOST']
        url = 'http://' + host + ':3000?token=' + token
        result = '<meta http-equiv="refresh" content="0; url=' + url + '">'
        return result

    @url('/')
    def handle_index(self, context):
        context.add_header('Content-Type', 'text/html')
        if context.session.identity is None:
            context.respond_ok()
            html = self.open_content('static/auth.html').read()
            return html % {
                'license': json.dumps(Licensing.get().get_license_status()),
                'error': json.dumps(context.session.data.pop('login-error', None)),
            }
        context.respond_ok()
        return self.open_content('static/index.html').read()

    @url('/ajenti:auth')
    def handle_auth(self, context):
        username = context.query.getvalue('username', '')
        password = context.query.getvalue('password', '')
        if not AuthenticationMiddleware.get().try_login(
            context, username, password, env=context.env,
        ):
            context.session.data['login-error'] = _('Invalid login or password')
            gevent.sleep(3)
        return context.redirect('/')
{'fraction_non_alphanumeric': 0.11958898119807608, 'fraction_numerical': 0.010275470048097944, 'mean_word_length': 2.964471403812825, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2132539', 'n_tokens_mistral': 1323, 'n_tokens_neox': 1264, 'n_words': 320}