68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
|
|
from yuxi.channel.extensions.tlon.utils import is_valid_ship
|
||
|
|
|
||
|
|
|
||
|
|
class TlonSetupWizard:
|
||
|
|
steps = [
|
||
|
|
{
|
||
|
|
"id": "ship",
|
||
|
|
"label": "Ship name",
|
||
|
|
"placeholder": "~sampel-palnet",
|
||
|
|
"validate": "normalize_ship → must be valid ship name",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"id": "url",
|
||
|
|
"label": "Ship URL",
|
||
|
|
"placeholder": "https://your-ship-host",
|
||
|
|
"validate": "validate_urbit_base_url → http/https protocol, no credentials",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"id": "code",
|
||
|
|
"label": "Login code",
|
||
|
|
"placeholder": "lidlut-tabwed-pillex-ridrup",
|
||
|
|
"validate": "required, non-empty string",
|
||
|
|
},
|
||
|
|
]
|
||
|
|
|
||
|
|
def setup_wizard_steps(self) -> list:
|
||
|
|
return self.steps
|
||
|
|
|
||
|
|
def validate_wizard_input(self, step_key: str, value: str) -> str | None:
|
||
|
|
if step_key == "ship":
|
||
|
|
if not is_valid_ship(value):
|
||
|
|
return "Invalid ship name format (expected ~name-name)"
|
||
|
|
elif step_key == "url":
|
||
|
|
if not value.startswith(("http://", "https://")):
|
||
|
|
return "URL must start with http:// or https://"
|
||
|
|
elif step_key == "code":
|
||
|
|
if not value or not value.strip():
|
||
|
|
return "Login code is required"
|
||
|
|
return None
|
||
|
|
|
||
|
|
async def finalize(self, config: dict) -> dict:
|
||
|
|
result = dict(config)
|
||
|
|
|
||
|
|
ship_url = result.get("url", "")
|
||
|
|
private_ranges = ["127.", "10.", "192.168.", "172.16.", "172.17.",
|
||
|
|
"172.18.", "172.19.", "172.20.", "172.21.", "172.22.",
|
||
|
|
"172.23.", "172.24.", "172.25.", "172.26.", "172.27.",
|
||
|
|
"172.28.", "172.29.", "172.30.", "172.31.", "localhost"]
|
||
|
|
|
||
|
|
from urllib.parse import urlparse
|
||
|
|
parsed = urlparse(ship_url)
|
||
|
|
hostname = parsed.hostname or ""
|
||
|
|
is_private = any(hostname.startswith(prefix) for prefix in private_ranges)
|
||
|
|
if is_private:
|
||
|
|
result.setdefault("network", {})["dangerouslyAllowPrivateNetwork"] = True
|
||
|
|
|
||
|
|
result.setdefault("groupChannels", [])
|
||
|
|
result.setdefault("dmAllowlist", [])
|
||
|
|
result.setdefault("groupInviteAllowlist", [])
|
||
|
|
result.setdefault("autoDiscoverChannels", True)
|
||
|
|
result.setdefault("showModelSignature", False)
|
||
|
|
result.setdefault("autoAcceptDmInvites", False)
|
||
|
|
result.setdefault("autoAcceptGroupInvites", False)
|
||
|
|
result.setdefault("ownerShip", result.get("ship", ""))
|
||
|
|
result.setdefault("defaultAuthorizedShips", [])
|
||
|
|
result.setdefault("authorization", {"channelRules": {}})
|
||
|
|
|
||
|
|
return result
|