Merge pull request #349 from cdvv7788/ui-enhancements

This commit is contained in:
Nick Sweeting 2020-07-02 02:56:05 -04:00 committed by GitHub
commit 844dcd3885
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 8 deletions

View file

@ -133,6 +133,16 @@ DEFAULT_CLI_COLORS = {
}
ANSI = {k: '' for k in DEFAULT_CLI_COLORS.keys()}
COLOR_DICT = {
'00': [(0, 0, 0), (0, 0, 0)],
'31': [(255, 0, 0), (128, 0, 0)],
'32': [(0, 200, 0), (0, 128, 0)],
'33': [(255, 255, 0), (128, 128, 0)],
'34': [(0, 0, 255), (0, 0, 128)],
'35': [(255, 0, 255), (128, 0, 128)],
'36': [(0, 255, 255), (0, 128, 128)],
}
STATICFILE_EXTENSIONS = {
# 99.999% of the time, URLs ending in these extensions are static files
# that can be downloaded as-is, not html pages that need to be rendered

View file

@ -8,6 +8,9 @@ from django.conf import settings
from core.models import Snapshot
from contextlib import redirect_stdout
from io import StringIO
from ..index import load_main_index, load_main_index_meta
from ..config import (
OUTPUT_DIR,
@ -16,7 +19,7 @@ from ..config import (
PUBLIC_INDEX,
PUBLIC_SNAPSHOTS,
)
from ..util import base_url
from ..util import base_url, ansi_to_html
from .. main import add
@ -54,13 +57,24 @@ class AddLinks(View):
def post(self, request):
url = request.POST['url']
print(f'[+] Adding URL: {url}')
add(
import_str=url,
update_all=False,
out_dir=OUTPUT_DIR,
)
return redirect('/')
if url:
print(f'[+] Adding URL: {url}')
add_stdout = StringIO()
with redirect_stdout(add_stdout):
extracted_links = add(
import_str=url,
update_all=False,
out_dir=OUTPUT_DIR,
)
print(add_stdout.getvalue())
context = {
"stdout": ansi_to_html(add_stdout.getvalue())
}
else:
context = {"stdout": "Please enter a URL"}
return render(template_name=self.template, request=request, context=context)
class LinkDetails(View):

View file

@ -197,6 +197,7 @@
</div>
</header>
<center>
{{ stdout | safe }}
<br/><br/>
<form action="?" method="POST">{% csrf_token %}
Add new links...<br/>
@ -204,6 +205,8 @@
<button role="submit">Add</button>
</form>
</center>
<a href="{% url 'admin:core_snapshot_changelist' %}">Go back to Snapshot list</a>
</body>
</html>

View file

@ -20,6 +20,7 @@ from .config import (
CHECK_SSL_VALIDITY,
WGET_USER_AGENT,
CHROME_OPTIONS,
COLOR_DICT
)
try:
@ -69,6 +70,8 @@ URL_REGEX = re.compile(
re.IGNORECASE,
)
COLOR_REGEX = re.compile(r'\[(?P<arg_1>\d+)(;(?P<arg_2>\d+)(;(?P<arg_3>\d+))?)?m')
def enforce_types(func):
"""
@ -195,6 +198,27 @@ def chrome_args(**options) -> List[str]:
return cmd_args
def ansi_to_html(text):
"""
Based on: https://stackoverflow.com/questions/19212665/python-converting-ansi-color-codes-to-html
"""
TEMPLATE = '<span style="color: rgb{}"><br>'
text = text.replace('[m', '</span>')
def single_sub(match):
argsdict = match.groupdict()
if argsdict['arg_3'] is None:
if argsdict['arg_2'] is None:
bold, color = 0, argsdict['arg_1']
else:
bold, color = argsdict['arg_1'], argsdict['arg_2']
else:
bold, color = argsdict['arg_3'], argsdict['arg_2']
return TEMPLATE.format(COLOR_DICT[color][0])
return COLOR_REGEX.sub(single_sub, text)
class ExtendedEncoder(pyjson.JSONEncoder):
"""