automatic handshake and connect
This commit is contained in:
1 parent
dbc460f0bb
commit
da446f5ab0
7 files changed
+283
-10
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
@@ -9,6 +10,23 @@ def _csv_list(val: str) -> List[str]:
|
||||
return [x.strip() for x in (val or "").split(",") if x.strip()]
|
||||
|
||||
|
||||
def _json_value(val: str, fallback):
|
||||
if not val:
|
||||
return fallback
|
||||
try:
|
||||
return json.loads(val)
|
||||
except Exception:
|
||||
return fallback
|
||||
|
||||
|
||||
def _as_list(value):
|
||||
if isinstance(value, list):
|
||||
return value
|
||||
if value is None:
|
||||
return []
|
||||
return [value]
|
||||
|
||||
|
||||
# Handshake / network config driven by env
|
||||
NODE_PRIVACY = os.getenv("NODE_PRIVACY", NODE_TYPE_PUBLIC).strip().lower()
|
||||
if NODE_PRIVACY not in (NODE_TYPE_PUBLIC, NODE_TYPE_PRIVATE):
|
||||
@@ -30,6 +48,12 @@ NETWORK_TLS_VERIFY = int(os.getenv("NETWORK_TLS_VERIFY", "1")) == 1
|
||||
HANDSHAKE_TS_TOLERANCE_SEC = int(os.getenv("HANDSHAKE_TS_TOLERANCE_SEC", "300"))
|
||||
HANDSHAKE_RATE_LIMIT_PER_MIN = int(os.getenv("HANDSHAKE_RATE_LIMIT_PER_MIN", "60"))
|
||||
|
||||
# IPFS discovery/peering
|
||||
IPFS_PRIVATE_BOOTSTRAP_ADDRESSES = _as_list(_json_value(os.getenv("IPFS_PRIVATE_BOOTSTRAP"), []))
|
||||
IPFS_PEERING_PEERS = _json_value(os.getenv("IPFS_PEERING_PEERS"), [])
|
||||
IPFS_ANNOUNCE_ADDRESSES = _as_list(_json_value(os.getenv("IPFS_ANNOUNCE_ADDRESSES"), []))
|
||||
IPFS_NOANNOUNCE_ADDRESSES = _as_list(_json_value(os.getenv("IPFS_NOANNOUNCE_ADDRESSES"), []))
|
||||
|
||||
# Capabilities
|
||||
NODE_IS_BOOTSTRAP = int(os.getenv("NODE_IS_BOOTSTRAP", "0")) == 1
|
||||
MAX_CONTENT_SIZE_MB = int(os.getenv("MAX_CONTENT_SIZE_MB", "512"))
|
||||
|
||||
@@ -19,9 +19,16 @@ from app.core.models.node_storage import StoredContent
|
||||
from app.core.storage import db_session
|
||||
from .constants import CURRENT_PROTOCOL_VERSION
|
||||
from .nodes import list_known_public_nodes
|
||||
from .config import PUBLIC_HOST, NODE_PRIVACY, NODE_IS_BOOTSTRAP, MAX_CONTENT_SIZE_MB
|
||||
from .config import (
|
||||
PUBLIC_HOST,
|
||||
NODE_PRIVACY,
|
||||
NODE_IS_BOOTSTRAP,
|
||||
MAX_CONTENT_SIZE_MB,
|
||||
IPFS_ANNOUNCE_ADDRESSES,
|
||||
)
|
||||
from app.core._config import ALLOWED_CONTENT_TYPES
|
||||
from .constants import NODE_TYPE_PUBLIC
|
||||
from app.core.ipfs_client import id_info
|
||||
|
||||
|
||||
START_TS = time.time()
|
||||
@@ -60,7 +67,30 @@ def _sign(obj: Dict[str, Any]) -> str:
|
||||
return signer.sign(blob)
|
||||
|
||||
|
||||
async def _local_ipfs_payload() -> Dict[str, Any]:
|
||||
try:
|
||||
info = await id_info()
|
||||
multiaddrs = info.get("Addresses") or []
|
||||
peer_id = info.get("ID")
|
||||
agent = info.get("AgentVersion")
|
||||
except Exception:
|
||||
multiaddrs = IPFS_ANNOUNCE_ADDRESSES or []
|
||||
peer_id = None
|
||||
agent = None
|
||||
if not multiaddrs:
|
||||
multiaddrs = IPFS_ANNOUNCE_ADDRESSES or []
|
||||
payload: Dict[str, Any] = {}
|
||||
if multiaddrs:
|
||||
payload["multiaddrs"] = multiaddrs
|
||||
if peer_id:
|
||||
payload["peer_id"] = peer_id
|
||||
if agent:
|
||||
payload["agent_version"] = agent
|
||||
return payload
|
||||
|
||||
|
||||
async def build_handshake_payload(session) -> Dict[str, Any]:
|
||||
ipfs_payload = await _local_ipfs_payload()
|
||||
payload = {
|
||||
"version": CURRENT_PROTOCOL_VERSION,
|
||||
"public_key": b58encode(hot_pubkey).decode(),
|
||||
@@ -77,6 +107,8 @@ async def build_handshake_payload(session) -> Dict[str, Any]:
|
||||
"timestamp": int(datetime.utcnow().timestamp()),
|
||||
"nonce": secrets.token_hex(16),
|
||||
}
|
||||
if ipfs_payload.get("multiaddrs"):
|
||||
payload["ipfs"] = ipfs_payload
|
||||
try:
|
||||
payload["known_public_nodes"] = await list_known_public_nodes(session)
|
||||
except Exception:
|
||||
@@ -86,6 +118,7 @@ async def build_handshake_payload(session) -> Dict[str, Any]:
|
||||
|
||||
|
||||
async def compute_node_info(session) -> Dict[str, Any]:
|
||||
ipfs_payload = await _local_ipfs_payload()
|
||||
node_info = {
|
||||
"id": b58encode(hot_pubkey).decode(),
|
||||
"public_key": b58encode(hot_pubkey).decode(),
|
||||
@@ -100,6 +133,8 @@ async def compute_node_info(session) -> Dict[str, Any]:
|
||||
"max_content_size_mb": MAX_CONTENT_SIZE_MB,
|
||||
},
|
||||
}
|
||||
if ipfs_payload.get("multiaddrs"):
|
||||
node_info["ipfs"] = ipfs_payload
|
||||
return node_info
|
||||
|
||||
def sign_response(data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
|
||||
@@ -13,6 +13,7 @@ from app.core.logger import make_log
|
||||
from app.core.models.my_network import KnownNode
|
||||
from app.core.storage import db_session
|
||||
from app.core._secrets import hot_pubkey
|
||||
from app.core.ipfs_client import swarm_connect
|
||||
from .config import (
|
||||
HANDSHAKE_INTERVAL_SEC,
|
||||
UNSUPPORTED_RECHECK_INTERVAL_SEC,
|
||||
@@ -21,6 +22,8 @@ from .config import (
|
||||
BOOTSTRAP_TIMEOUT_SEC,
|
||||
NODE_PRIVACY,
|
||||
NETWORK_TLS_VERIFY,
|
||||
IPFS_PRIVATE_BOOTSTRAP_ADDRESSES,
|
||||
IPFS_PEERING_PEERS,
|
||||
)
|
||||
from .constants import NODE_TYPE_PRIVATE
|
||||
from .semver import compatibility
|
||||
@@ -77,6 +80,44 @@ async def upsert_known_node(session, host: str, port: int, public_key: str, meta
|
||||
return node
|
||||
|
||||
|
||||
def _ipfs_meta_from_payload(payload: Dict[str, Any]) -> Dict[str, Any]:
|
||||
data = payload or {}
|
||||
ipfs = data.get("ipfs") or data # allow direct ipfs dict
|
||||
multiaddrs = ipfs.get("multiaddrs") if isinstance(ipfs, dict) else []
|
||||
if not isinstance(multiaddrs, list):
|
||||
multiaddrs = [multiaddrs]
|
||||
normalized_multiaddrs = [str(addr) for addr in multiaddrs if addr]
|
||||
meta: Dict[str, Any] = {}
|
||||
if normalized_multiaddrs:
|
||||
meta["multiaddrs"] = normalized_multiaddrs
|
||||
peer_id = ipfs.get("peer_id") if isinstance(ipfs, dict) else None
|
||||
if peer_id:
|
||||
meta["peer_id"] = str(peer_id)
|
||||
agent = ipfs.get("agent_version") if isinstance(ipfs, dict) else None
|
||||
if agent:
|
||||
meta["agent_version"] = str(agent)
|
||||
return meta
|
||||
|
||||
|
||||
async def _connect_ipfs_multiaddrs(multiaddrs: List[str]):
|
||||
for addr in multiaddrs or []:
|
||||
try:
|
||||
await swarm_connect(addr)
|
||||
except Exception as exc:
|
||||
make_log("Handshake", f"IPFS connect failed {addr}: {exc}", level='debug')
|
||||
|
||||
|
||||
async def _connect_static_peers():
|
||||
env_addrs: List[str] = []
|
||||
env_addrs.extend(IPFS_PRIVATE_BOOTSTRAP_ADDRESSES or [])
|
||||
for peer in IPFS_PEERING_PEERS or []:
|
||||
if isinstance(peer, dict):
|
||||
env_addrs.extend(peer.get("Addrs") or [])
|
||||
elif isinstance(peer, str):
|
||||
env_addrs.append(peer)
|
||||
await _connect_ipfs_multiaddrs(env_addrs)
|
||||
|
||||
|
||||
def _compatibility_for_meta(remote_version: str) -> str:
|
||||
if not remote_version or remote_version == "0.0.0":
|
||||
return "warning"
|
||||
@@ -90,6 +131,7 @@ async def list_known_public_nodes(session) -> List[Dict[str, Any]]:
|
||||
meta = r.meta or {}
|
||||
if not meta.get("is_public", True):
|
||||
continue
|
||||
ipfs_meta = meta.get("ipfs") or ((meta.get("last_response") or {}).get("node", {}) or {}).get("ipfs") or {}
|
||||
result.append({
|
||||
"host": r.ip,
|
||||
"port": r.port,
|
||||
@@ -99,6 +141,7 @@ async def list_known_public_nodes(session) -> List[Dict[str, Any]]:
|
||||
"last_seen": (r.last_sync.isoformat() + "Z") if r.last_sync else None,
|
||||
"public_host": meta.get("public_host"),
|
||||
"capabilities": meta.get("capabilities") or {},
|
||||
"ipfs": ipfs_meta,
|
||||
})
|
||||
return result
|
||||
|
||||
@@ -171,6 +214,7 @@ async def perform_handshake_round():
|
||||
resp = await _handshake_with(session, base_url)
|
||||
# Merge known nodes received
|
||||
for peer in (resp or {}).get("known_public_nodes", []):
|
||||
peer_ipfs_meta = _ipfs_meta_from_payload(peer)
|
||||
try:
|
||||
await upsert_known_node(
|
||||
session,
|
||||
@@ -181,13 +225,17 @@ async def perform_handshake_round():
|
||||
"is_public": True,
|
||||
"version": peer.get("version") or "0.0.0",
|
||||
"public_host": peer.get("public_host") or (f"http://{peer.get('host')}:{peer.get('port')}" if peer.get('host') else None),
|
||||
"ipfs": peer_ipfs_meta,
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
make_log("Handshake", f"Ignore bad peer from {base_url}: {e}", level='warning')
|
||||
await _connect_ipfs_multiaddrs(peer_ipfs_meta.get("multiaddrs"))
|
||||
# Update last_sync and meta for node
|
||||
node_ipfs_meta = _ipfs_meta_from_payload((resp or {}).get("node", {}))
|
||||
await _connect_ipfs_multiaddrs(node_ipfs_meta.get("multiaddrs"))
|
||||
node.last_sync = _now()
|
||||
node.meta = {**(node.meta or {}), "last_response": resp, "fail_count": 0}
|
||||
node.meta = {**(node.meta or {}), "last_response": resp, "fail_count": 0, "ipfs": node_ipfs_meta}
|
||||
await session.commit()
|
||||
make_log("Handshake", f"Handshake OK with {base_url}")
|
||||
except Exception as e:
|
||||
@@ -205,6 +253,10 @@ async def network_handshake_daemon(app):
|
||||
# Stagger start a bit to allow HTTP server to come up
|
||||
await asyncio.sleep(3)
|
||||
make_log("Handshake", f"Daemon started; interval={HANDSHAKE_INTERVAL_SEC}s")
|
||||
try:
|
||||
await _connect_static_peers()
|
||||
except Exception as exc:
|
||||
make_log("Handshake", f"Static IPFS peering failed: {exc}", level='warning')
|
||||
while True:
|
||||
try:
|
||||
await perform_handshake_round()
|
||||
@@ -218,6 +270,10 @@ async def bootstrap_once_and_exit_if_failed():
|
||||
seeds = BOOTSTRAP_SEEDS or []
|
||||
if not seeds:
|
||||
return # Nothing to do
|
||||
try:
|
||||
await _connect_static_peers()
|
||||
except Exception as exc:
|
||||
make_log("Bootstrap", f"Static IPFS peering failed: {exc}", level='warning')
|
||||
async with db_session(auto_commit=True) as session:
|
||||
# If we already know nodes, skip bootstrap
|
||||
have_any = (await session.execute(select(KnownNode))).scalars().first()
|
||||
@@ -235,6 +291,8 @@ async def bootstrap_once_and_exit_if_failed():
|
||||
ok = True
|
||||
# Seed itself gets inserted by handshake handling route; also insert it explicitly
|
||||
try:
|
||||
seed_ipfs_meta = _ipfs_meta_from_payload(resp.get("node", {}))
|
||||
await _connect_ipfs_multiaddrs(seed_ipfs_meta.get("multiaddrs"))
|
||||
await upsert_known_node(
|
||||
session,
|
||||
host=seed,
|
||||
@@ -244,6 +302,7 @@ async def bootstrap_once_and_exit_if_failed():
|
||||
"is_public": True,
|
||||
"version": resp.get("node", {}).get("version", "0.0.0"),
|
||||
"public_host": resp.get("node", {}).get("public_host") or seed,
|
||||
"ipfs": seed_ipfs_meta,
|
||||
}
|
||||
)
|
||||
except Exception:
|
||||
|
||||
Reference in new issue
Block a user