♻️ refactoring
This commit is contained in:
parent
95037bb56c
commit
053268c75d
1 changed files with 19 additions and 33 deletions
|
@ -9,6 +9,7 @@ import socketserver
|
||||||
import qrcode
|
import qrcode
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
|
|
||||||
def get_current_ip_address():
|
def get_current_ip_address():
|
||||||
|
@ -19,12 +20,10 @@ def get_current_ip_address():
|
||||||
return ip_address
|
return ip_address
|
||||||
|
|
||||||
|
|
||||||
# Function to generate a default username
|
|
||||||
def generate_default_username():
|
def generate_default_username():
|
||||||
return "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
|
return "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
|
||||||
|
|
||||||
|
|
||||||
# Function to generate a default password
|
|
||||||
def generate_default_password():
|
def generate_default_password():
|
||||||
return "".join(random.choices(string.ascii_letters + string.digits, k=12))
|
return "".join(random.choices(string.ascii_letters + string.digits, k=12))
|
||||||
|
|
||||||
|
@ -59,21 +58,18 @@ parser.add_argument(
|
||||||
help="Directory to serve. Defaults to the current directory.",
|
help="Directory to serve. Defaults to the current directory.",
|
||||||
)
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
arguments = parser.parse_args()
|
||||||
|
|
||||||
# Expand the directory path (e.g., if it contains ~)
|
|
||||||
expanded_directory = os.path.expanduser(args.directory)
|
|
||||||
|
|
||||||
# Change the working directory to serve the specified directory
|
# Change the working directory to serve the specified directory
|
||||||
os.chdir(expanded_directory)
|
os.chdir(os.path.expanduser(arguments.directory))
|
||||||
|
|
||||||
|
|
||||||
# Define a request handler with basic authentication
|
# Define a request handler with basic authentication
|
||||||
class AuthHandler(http.server.SimpleHTTPRequestHandler):
|
class AuthHandler(http.server.SimpleHTTPRequestHandler):
|
||||||
def __init__(self, username, passsword, *args, **kwargs):
|
def __init__(self, username, passsword, *args, **kwargs):
|
||||||
encoded_bytes = base64.b64encode(f"{username}:{passsword}".encode("utf-8"))
|
self.base64_encoded_string = base64.b64encode(
|
||||||
# Convert bytes back to string
|
f"{username}:{passsword}".encode("utf-8")
|
||||||
self.encoded_str = encoded_bytes.decode("utf-8")
|
).decode("utf-8")
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def do_AUTHHEAD(self):
|
def do_AUTHHEAD(self):
|
||||||
|
@ -92,40 +88,30 @@ class AuthHandler(http.server.SimpleHTTPRequestHandler):
|
||||||
|
|
||||||
def check_auth(self, auth_header):
|
def check_auth(self, auth_header):
|
||||||
encoded_credentials = auth_header.split()[1]
|
encoded_credentials = auth_header.split()[1]
|
||||||
return encoded_credentials == self.encoded_str
|
return encoded_credentials == self.base64_encoded_string
|
||||||
# decoded_credentials = base64.b64decode(encoded_credentials).decode("utf-8")
|
|
||||||
# username, password = decoded_credentials.split(":")
|
|
||||||
# return username == args.username and password == args.password
|
|
||||||
|
|
||||||
|
|
||||||
def save_qr_code(url):
|
def save_qr_code(url):
|
||||||
# Generate the QR code
|
qr_code = qrcode.QRCode(
|
||||||
qr = qrcode.QRCode(
|
|
||||||
version=1,
|
version=1,
|
||||||
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
||||||
box_size=10,
|
box_size=10,
|
||||||
border=4,
|
border=4,
|
||||||
)
|
)
|
||||||
qr.add_data(url)
|
qr_code.add_data(url)
|
||||||
qr.make(fit=True)
|
qr_code.make(fit=True)
|
||||||
# Create an image from the QR Code instance
|
image = qr_code.make_image(fill_color="black", back_color="white").convert("RGB")
|
||||||
img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
|
image.save("qrcode.png")
|
||||||
# Save the image
|
|
||||||
img.save("qrcode.png")
|
|
||||||
|
|
||||||
|
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
# Set up the server with the AuthHandler and the defined port
|
# Set up the server with the AuthHandler and the defined port
|
||||||
auth_handler = partial(AuthHandler, args.username, args.password)
|
auth_handler = partial(AuthHandler, arguments.username, arguments.password)
|
||||||
with socketserver.TCPServer(("", args.port), auth_handler) as httpd:
|
with socketserver.TCPServer(("", arguments.port), auth_handler) as httpd:
|
||||||
print("qrcode.png is saved in the current directory.")
|
print("qrcode.png is saved in the current directory.")
|
||||||
ip_address = get_current_ip_address()
|
ip_address = get_current_ip_address()
|
||||||
print(
|
server_url = f"http://{arguments.username}:{arguments.password}@{ip_address}:{arguments.port}"
|
||||||
f"http://{args.username}:{args.password}@{ip_address}:{args.port}/ < the page"
|
print(f"{server_url} < server url")
|
||||||
)
|
print(f"{server_url}/qrcode.png < qrcode")
|
||||||
print(
|
print("press Ctr-C to stop server")
|
||||||
f"http://{args.username}:{args.password}@{ip_address}:{args.port}/qrcode.png < qrcode"
|
save_qr_code(server_url)
|
||||||
)
|
|
||||||
save_qr_code(f"http://{args.username}:{args.password}@{ip_address}:{args.port}/")
|
|
||||||
httpd.serve_forever()
|
httpd.serve_forever()
|
||||||
|
|
Loading…
Reference in a new issue