verification server changes
* show message on startup * add help to argparser options * make hostname optional * set hostname to addr if not specified * make sure hostname is not `0.0.0.0`
This commit is contained in:
parent
192313ac71
commit
0c44cc7344
|
@ -2,6 +2,7 @@
|
|||
import aputils
|
||||
import argparse
|
||||
import json
|
||||
import socket
|
||||
|
||||
from functools import cached_property, lru_cache
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
@ -11,6 +12,16 @@ from urllib.error import HTTPError
|
|||
from urllib.request import Request, urlopen
|
||||
|
||||
|
||||
def get_machine_addr() -> str:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
|
||||
try:
|
||||
sock.connect(("10.254.254.255", 1))
|
||||
return sock.getsockname()[0]
|
||||
|
||||
except Exception as e:
|
||||
return "127.0.0.1"
|
||||
|
||||
|
||||
class ClientError(Exception):
|
||||
...
|
||||
|
||||
|
@ -261,13 +272,26 @@ parser = argparse.ArgumentParser(
|
|||
description = "Starts a server for validating HTTP signatures"
|
||||
)
|
||||
|
||||
parser.add_argument("hostname")
|
||||
parser.add_argument("--addr", "-a", default = "0.0.0.0")
|
||||
parser.add_argument("--port", "-p", default = 8080, type = int)
|
||||
parser.add_argument("--size-limit", "-s", default = 1024 * 1024, type = int)
|
||||
parser.add_argument("--protocol", "-r", default = "https", choices = ["https", "http"])
|
||||
parser.add_argument("--hostname", "-n",
|
||||
help = "Domain or address this server will be hosted on (defaults to --addr value)")
|
||||
|
||||
parser.add_argument("--addr", "-a", default = "0.0.0.0",
|
||||
help = "IP address to listen on")
|
||||
|
||||
parser.add_argument("--port", "-p", default = 8080, type = int,
|
||||
help = "TCP port to listen on")
|
||||
|
||||
parser.add_argument("--size-limit", "-s", default = 1024 * 1024, type = int,
|
||||
help = "Max size of incoming or outgoing messages in bytes")
|
||||
|
||||
parser.add_argument("--protocol", "-r", default = "http", choices = ["https", "http"],
|
||||
help = "Supported protocol to advertise via the actor at /actor")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.hostname is None:
|
||||
args.hostname = args.addr if args.addr != "0.0.0.0" else get_machine_addr()
|
||||
|
||||
URL = f"{args.protocol}://{args.hostname}"
|
||||
keyid = URL + "/actor#main-key"
|
||||
|
||||
|
@ -278,5 +302,11 @@ if not (key := Path("privkey.pem")).exists():
|
|||
else:
|
||||
signer = aputils.Signer(key, keyid)
|
||||
|
||||
print(f"Starting server on {args.protocol}://{args.hostname} ({args.addr}:{args.port})")
|
||||
server = ThreadingHTTPServer((args.addr, args.port), RequestHandler)
|
||||
|
||||
try:
|
||||
server.serve_forever()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
|
Loading…
Reference in a new issue