2023-10-19 00:06:14 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import msgspec
|
|
|
|
|
|
|
|
|
|
|
|
def readconf() -> dict:
|
|
|
|
p = Path.home() / ".droppy/config" # Hardcoded in Droppy
|
|
|
|
cf = msgspec.json.decode((p / "config.json").read_bytes())
|
|
|
|
db = msgspec.json.decode((p / "db.json").read_bytes())
|
|
|
|
cf["path"] = p.parent / "files"
|
|
|
|
cf["listen"] = _droppy_listeners(cf)
|
|
|
|
return cf | db
|
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-19 00:06:14 +01:00
|
|
|
def _droppy_listeners(cf):
|
|
|
|
"""Convert Droppy listeners to our format, for typical cases but not in full."""
|
|
|
|
for listener in cf["listeners"]:
|
|
|
|
try:
|
|
|
|
if listener["protocol"] == "https":
|
|
|
|
# TODO: Add support for TLS
|
|
|
|
continue
|
|
|
|
socket = listener.get("socket")
|
|
|
|
if socket:
|
2023-10-26 15:18:59 +01:00
|
|
|
if isinstance(socket, list):
|
|
|
|
socket = socket[0]
|
2023-10-19 00:06:14 +01:00
|
|
|
return f"{socket}"
|
|
|
|
port = listener["port"]
|
2023-10-26 15:18:59 +01:00
|
|
|
if isinstance(port, list):
|
|
|
|
port = port[0]
|
2023-10-19 00:06:14 +01:00
|
|
|
host = listener["host"]
|
2023-10-26 15:18:59 +01:00
|
|
|
if isinstance(host, list):
|
|
|
|
host = host[0]
|
2023-11-08 20:38:40 +00:00
|
|
|
except (KeyError, IndexError):
|
|
|
|
continue
|
|
|
|
else:
|
2023-10-26 15:18:59 +01:00
|
|
|
if host in ("127.0.0.1", "::", "localhost"):
|
|
|
|
return f":{port}"
|
2023-10-19 00:06:14 +01:00
|
|
|
return f"{host}:{port}"
|
2023-11-08 20:38:40 +00:00
|
|
|
|
2023-10-19 00:06:14 +01:00
|
|
|
# If none matched, fallback to Droppy default
|
2023-10-26 15:18:59 +01:00
|
|
|
return "0.0.0.0:8989"
|