f
This commit is contained in:
@@ -681,60 +681,120 @@ class ConfigModal(ModalScreen):
|
||||
if not stype: return
|
||||
|
||||
if stype == "zerotier":
|
||||
# Push a discovery screen
|
||||
# Push a discovery wizard
|
||||
from TUI.modalscreen.selection_modal import SelectionModal
|
||||
from API import zerotier as zt
|
||||
|
||||
# Find all joined networks
|
||||
# 1. Choose Network
|
||||
joined = zt.list_networks()
|
||||
if not joined:
|
||||
self.notify("Error: Join a ZeroTier network first in 'Networking'", severity="error")
|
||||
return
|
||||
|
||||
self.notify("Scanning ZeroTier networks for peers...")
|
||||
|
||||
all_peers = []
|
||||
central_token = self.config_data.get("networking", {}).get("zerotier", {}).get("api_key")
|
||||
|
||||
for net in joined:
|
||||
probes = zt.discover_services_on_network(net.id, ports=[999, 45869, 5000], api_token=central_token)
|
||||
for p in probes:
|
||||
label = f"{p.service_hint or 'service'} @ {p.address}:{p.port} ({net.name})"
|
||||
all_peers.append((label, p, net.id))
|
||||
|
||||
if not all_peers:
|
||||
self.notify("No services found on port 999. Use manual setup.", severity="warning")
|
||||
else:
|
||||
options = [p[0] for p in all_peers]
|
||||
|
||||
def on_peer_selected(choice: str):
|
||||
if not choice: return
|
||||
# Find the probe data
|
||||
match = next((p for p in all_peers if p[0] == choice), None)
|
||||
if not match: return
|
||||
label, probe, net_id = match
|
||||
|
||||
# Create a specific name based on host
|
||||
safe_host = str(probe.address).replace(".", "_")
|
||||
new_name = f"zt_{safe_host}"
|
||||
|
||||
if "store" not in self.config_data: self.config_data["store"] = {}
|
||||
store_cfg = self.config_data["store"].setdefault("zerotier", {})
|
||||
|
||||
new_config = {
|
||||
"NAME": new_name,
|
||||
"NETWORK_ID": net_id,
|
||||
"HOST": probe.address,
|
||||
"PORT": probe.port,
|
||||
"SERVICE": "hydrus" if probe.service_hint == "hydrus" else "remote"
|
||||
}
|
||||
store_cfg[new_name] = new_config
|
||||
self.editing_item_type = "store-zerotier"
|
||||
self.editing_item_name = new_name
|
||||
self.refresh_view()
|
||||
net_options = [f"{n.name or 'Network'} ({n.id})" for n in joined]
|
||||
|
||||
self.app.push_screen(SelectionModal("Discovered ZeroTier Services", options), callback=on_peer_selected)
|
||||
return
|
||||
def on_net_selected(net_choice: str):
|
||||
if not net_choice: return
|
||||
net_id = net_choice.split("(")[-1].rstrip(")")
|
||||
|
||||
# 2. Host or Connect?
|
||||
def on_mode_selected(mode: str):
|
||||
if not mode: return
|
||||
|
||||
if mode == "Host (Share a local store)":
|
||||
# 3a. Select Local Store to Share
|
||||
local_stores = []
|
||||
for s_type, s_data in self.config_data.get("store", {}).items():
|
||||
if s_type == "zerotier": continue
|
||||
for s_name in s_data.keys():
|
||||
local_stores.append(f"{s_name} ({s_type})")
|
||||
|
||||
if not local_stores:
|
||||
self.notify("No local stores available to share.", severity="error")
|
||||
return
|
||||
|
||||
def on_share_selected(share_choice: str):
|
||||
if not share_choice: return
|
||||
share_name = share_choice.split(" (")[0]
|
||||
|
||||
# Update networking config
|
||||
if "networking" not in self.config_data: self.config_data["networking"] = {}
|
||||
zt_net = self.config_data["networking"].setdefault("zerotier", {})
|
||||
zt_net["serve"] = share_name
|
||||
zt_net["network_id"] = net_id
|
||||
if not zt_net.get("port"):
|
||||
zt_net["port"] = "999"
|
||||
|
||||
self.refresh_view()
|
||||
self.notify(f"ZeroTier configured to share '{share_name}' on network {net_id}")
|
||||
self.notify("CLICK 'SAVE' to start the server.")
|
||||
|
||||
self.app.push_screen(SelectionModal("Select Local Store to Share", local_stores), callback=on_share_selected)
|
||||
|
||||
else:
|
||||
# 3b. Connect to Remote Peer
|
||||
# Discover Peers (Port 999 only)
|
||||
central_token = self.config_data.get("networking", {}).get("zerotier", {}).get("api_key")
|
||||
self.notify(f"Scanning Network {net_id} for peers...")
|
||||
|
||||
try:
|
||||
probes = zt.discover_services_on_network(net_id, ports=[999], api_token=central_token)
|
||||
except Exception as e:
|
||||
self.notify(f"Discovery error: {e}", severity="error")
|
||||
return
|
||||
|
||||
if not probes:
|
||||
self.notify("No peers found on port 999. Use manual setup.", severity="warning")
|
||||
# Create empty template
|
||||
new_name = f"zt_remote"
|
||||
if "store" not in self.config_data: self.config_data["store"] = {}
|
||||
store_cfg = self.config_data["store"].setdefault("zerotier", {})
|
||||
store_cfg[new_name] = {
|
||||
"NAME": new_name,
|
||||
"NETWORK_ID": net_id,
|
||||
"HOST": "",
|
||||
"PORT": "999",
|
||||
"SERVICE": "remote"
|
||||
}
|
||||
self.editing_item_type = "store-zerotier"
|
||||
self.editing_item_name = new_name
|
||||
self.refresh_view()
|
||||
return
|
||||
|
||||
peer_options = [f"{p.address} ({p.service_hint or 'service'})" for p in probes]
|
||||
|
||||
def on_peer_selected(peer_choice: str):
|
||||
if not peer_choice: return
|
||||
p_addr = peer_choice.split(" ")[0]
|
||||
match = next((p for p in probes if p.address == p_addr), None)
|
||||
|
||||
new_name = f"zt_{p_addr.replace('.', '_')}"
|
||||
if "store" not in self.config_data: self.config_data["store"] = {}
|
||||
store_cfg = self.config_data["store"].setdefault("zerotier", {})
|
||||
|
||||
new_config = {
|
||||
"NAME": new_name,
|
||||
"NETWORK_ID": net_id,
|
||||
"HOST": p_addr,
|
||||
"PORT": "999",
|
||||
"SERVICE": "remote"
|
||||
}
|
||||
if match and match.service_hint == "hydrus":
|
||||
new_config["SERVICE"] = "hydrus"
|
||||
new_config["PORT"] = "45869"
|
||||
|
||||
store_cfg[new_name] = new_config
|
||||
self.editing_item_type = "store-zerotier"
|
||||
self.editing_item_name = new_name
|
||||
self.refresh_view()
|
||||
self.notify(f"Configured ZeroTier store '{new_name}'")
|
||||
|
||||
self.app.push_screen(SelectionModal("Select Remote Peer", peer_options), callback=on_peer_selected)
|
||||
|
||||
self.app.push_screen(SelectionModal("ZeroTier Mode", ["Host (Share a local store)", "Connect (Use a remote store)"]), callback=on_mode_selected)
|
||||
|
||||
self.app.push_screen(SelectionModal("Select ZeroTier Network", net_options), callback=on_net_selected)
|
||||
return
|
||||
|
||||
new_name = f"new_{stype}"
|
||||
if "store" not in self.config_data:
|
||||
|
||||
Reference in New Issue
Block a user