Last active
September 29, 2022 13:54
-
-
Save zsrinivas/9b3e0a7daecdc0206305 to your computer and use it in GitHub Desktop.
lighty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from subprocess import call | |
import tempfile | |
import os.path | |
import socket | |
import sys | |
import mimetypes | |
from netifaces import interfaces, ifaddresses, AF_INET | |
types_map = { | |
'.json': 'text/json', | |
'.cpp': 'text/c++', | |
'.c++': 'text/c++', | |
'.cc': 'text/c++', | |
'.cxx': 'text/c++', | |
} | |
types_map.update(mimetypes.types_map) | |
startport = 9009 if len(sys.argv) <= 1 else sys.argv[1] | |
for testport in range(startport, startport + 1000): | |
port = testport | |
if socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect_ex(('0.0.0.0', testport)) != 0: | |
break | |
def ip4_addresses(): | |
ip_list = [] | |
for interface in interfaces(): | |
try: | |
for link in ifaddresses(interface)[AF_INET]: | |
try: | |
ip_list.append(link['addr']) | |
except: | |
pass | |
except: | |
pass | |
return ip_list | |
pwd = __import__('os').getcwd() | |
stylecss = open('/home/eightnoteight/.config/lighty/style.css', 'r').read() | |
config = [ | |
'server.modules = ( )', | |
'server.document-root = "{path}"'.format(path=pwd), | |
'server.port = {port}'.format(port=port), | |
'dir-listing.activate = "enable"', | |
'dir-listing.external-css = "/{csspath}"', | |
'server.indexfiles = ( "index.html" )', | |
'mimetype.assign = ( {} )'.format(', '.join([ | |
'"{}" => "{}"'.format(key, value) for key, value in types_map.items() | |
])), | |
'dir-listing.set-footer = " "', | |
'# dir-listing.external-css = "{path}"'.format(path='/tmp/tmp.css'), | |
'# debug.log-request-handling = "enable"', | |
'# debug.log-response-header = "enable"', | |
'# debug.log-request-header = "enable"', | |
'# server.kbytes-per-second = 10240', | |
'# connection.kbytes-per-second = 1024', | |
] | |
print('\n'.join( | |
['server starting at \033[91;1m{host}:{port}\033[0m'.format( | |
port=port, host=host) for host in ip4_addresses()])) | |
with tempfile.NamedTemporaryFile(prefix='lighty-', suffix=".css", dir=pwd, delete=True, mode='w+') as tempcssf: | |
print(stylecss, file=tempcssf, flush=True) | |
config[4] = config[4].format(csspath=os.path.split(tempcssf.name)[-1]) | |
with tempfile.NamedTemporaryFile(prefix='lighty-', delete=True, mode='w+') as tempf: | |
print('\n'.join(config), file=tempf, flush=True) | |
try: | |
call(["/home/eightnoteight/testbed/lighttpd-1.4.x/sbin/lighttpd", '-D', "-f", tempf.name, '-m', '/home/eightnoteight/testbed/lighttpd-1.4.x/lib']) | |
except KeyboardInterrupt as e: | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment