153 lines
4.4 KiB
Python
153 lines
4.4 KiB
Python
import os
|
|
|
|
from http.server import BaseHTTPRequestHandler
|
|
import socketserver
|
|
import threading
|
|
import pyqrcode
|
|
import socket
|
|
import struct
|
|
import base64
|
|
|
|
|
|
from SuperBeam.RequestHandlers import (
|
|
ChunkTemplateHandler, ThumbHandler, ApkRequestHandler, LegacyListHandler,
|
|
SingleFileHandler, ZipFileHandler, SuperStreamHandler, AssetHandler,
|
|
JsonListHandler
|
|
)
|
|
|
|
|
|
class SuperBeamServer(BaseHTTPRequestHandler):
|
|
"""SuperBeam Server Request Handler
|
|
|
|
Handle incoming requests to serve information to the client.
|
|
|
|
Class inherited `BaseHTTPRequestHandler`.
|
|
"""
|
|
|
|
def __init__(self, debug=False, *args, **kwargs):
|
|
self.debug = debug
|
|
super(SuperBeamServer, self).__init__()
|
|
|
|
def do_GET(self):
|
|
# TODO Add globbing
|
|
if self.debug:
|
|
print(threading.current_thread())
|
|
paths = {
|
|
'index': ChunkTemplateHandler,
|
|
'/': ChunkTemplateHandler,
|
|
'/index.htm': ChunkTemplateHandler, # TODO handler2
|
|
'/getthumb': ThumbHandler,
|
|
'/light': ChunkTemplateHandler, # TODO handler2
|
|
'/superlist': LegacyListHandler,
|
|
'/jsonlist': JsonListHandler,
|
|
'/get/': SingleFileHandler,
|
|
'/getapk': ApkRequestHandler,
|
|
'/getzip': ZipFileHandler,
|
|
'/getstream': SuperStreamHandler,
|
|
'*': AssetHandler,
|
|
}
|
|
|
|
if self.path in paths: # TODO startswith key check
|
|
self.respond(paths[self.path])
|
|
|
|
def respond(self, handler):
|
|
if self.debug:
|
|
print('='*30 + ' DEBUG ' + '='*30)
|
|
print('CONNECTION:', self.connection)
|
|
print('HEADERS :', self.headers)
|
|
print('REQUEST :', self.request)
|
|
print('REQUEST* :', self.requestline)
|
|
print('PATH :', self.path)
|
|
print('COMMAND :', self.command)
|
|
print('HANDLER :', handler)
|
|
print('='*30 + ' DEBUG ' + '='*30)
|
|
handler.handle(self)
|
|
|
|
|
|
def get_primary_ip():
|
|
"""Get primary IP address.
|
|
|
|
Obtain the primary interface IP address. If an error occurs, the default
|
|
loopback adddress '127.0.0.1' is returned.
|
|
|
|
Returns:
|
|
str: IP Address
|
|
"""
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
try:
|
|
# doesn't even have to be reachable
|
|
s.connect(('10.255.255.255', 1))
|
|
IP = s.getsockname()[0]
|
|
except Exception as exc:
|
|
print(exc)
|
|
IP = '127.0.0.1'
|
|
finally:
|
|
s.close()
|
|
return IP
|
|
|
|
|
|
def get_qrcode():
|
|
"""Generate QR Code to be scanned by the SuperBeam Client.
|
|
|
|
Returns:
|
|
pyqrcode.QRCode: SuperBeam QR Code
|
|
"""
|
|
primary_ip = get_primary_ip()
|
|
octets = [1]
|
|
octets.extend([int(_) for _ in primary_ip.split('.')[::-1]])
|
|
url = 'http://superbe.am/q?' + base64.b64encode(
|
|
struct.pack('BBBBB', *octets)
|
|
).decode('utf-8')
|
|
return pyqrcode.create(url), primary_ip
|
|
|
|
|
|
def show_qrcode():
|
|
"""Display QR Code with link for SuperBeam Client.
|
|
|
|
The QR code is generated in function `get_code()`. The QR code is rendered
|
|
in the terminal window.
|
|
"""
|
|
qrcode, primary_ip = get_qrcode()
|
|
print(qrcode.terminal())
|
|
print(f'SuperBeam Server: http:://{primary_ip}:8080/\n')
|
|
|
|
|
|
def build_filelist(paths, recursive=True):
|
|
"""List of files or directories to be served to SuperBeam clients.
|
|
|
|
Args:
|
|
paths (list(str)): list of directories to serve.
|
|
recursive (bool, optional): serve subdirectories of `paths`. Defaults
|
|
to True
|
|
|
|
Returns:
|
|
list: list of files with fullpath to be served.
|
|
"""
|
|
filelist = []
|
|
for path in paths:
|
|
if os.path.isdir(path) and recursive:
|
|
for root, dirs, files in os.walk(path):
|
|
for file_ in files:
|
|
filelist.append(os.path.join(root, file_))
|
|
else:
|
|
filelist.append(path)
|
|
return filelist
|
|
|
|
|
|
def serve_forever(files):
|
|
"""Start the SuperBeam server and serve `files`.
|
|
|
|
Infinite main loop serving `files` via SuperBeam server.
|
|
|
|
The server tries to obtain the primary IP address of the system and binds
|
|
the server to TCP port 8080.
|
|
|
|
Args:
|
|
files (list(str)): a list of files with fullpath information to serve.
|
|
"""
|
|
Handler = SuperBeamServer
|
|
Handler.files = files
|
|
with socketserver.ThreadingTCPServer(('', 8080), Handler) as httpd:
|
|
show_qrcode()
|
|
httpd.serve_forever()
|