"""Protocol suite and host multiplexer configuration blocks."""
from __future__ import annotations
import dataclasses
from typing import Any, Mapping, Optional
from .base import Block, Problems, is_http_url, is_relay_url, opt, positive_int
__all__ = [
'HostMultiplexerConfig',
'NostrProtocolConfig',
'ATProtoProtocolConfig',
'ActivityPubProtocolConfig',
'SSBProtocolConfig',
'P2PandaProtocolConfig',
'RhioProtocolConfig',
'ProtocolSuitesConfig',
]
[docs]@dataclasses.dataclass(frozen=True)
class HostMultiplexerConfig(Block):
listen: str = '127.0.0.1'
port: int = 0
public_url: Optional[str] = None
bind_wait: float = 10.0
[docs] @classmethod
def check(cls, raw: Any, p: Problems) -> None:
if not isinstance(raw, Mapping):
p.add('protocols.host must be an object'); return
opt(raw, 'listen', lambda v: isinstance(v, str) and len(v) > 0, p, 'protocols.host.listen must be a non-empty string')
opt(raw, 'port', lambda v: isinstance(v, int) and 0 <= v <= 65535, p, 'protocols.host.port must be 0..65535')
opt(raw, 'public_url', is_http_url, p, 'protocols.host.public_url must be an http or https URL')
opt(raw, 'bind_wait', lambda v: isinstance(v, (int, float)) and v >= 0, p, 'protocols.host.bind_wait must be non-negative')
[docs] @classmethod
def from_dict(cls, raw: Any) -> 'HostMultiplexerConfig':
if not isinstance(raw, Mapping):
return cls()
return cls(
listen=str(raw.get('listen', '127.0.0.1')),
port=int(raw.get('port', 0)),
public_url=str(raw['public_url']) if raw.get('public_url') else None,
bind_wait=float(raw.get('bind_wait', 10.0)),
)
[docs]@dataclasses.dataclass(frozen=True)
class NostrProtocolConfig(Block):
enabled: bool = True
relays: tuple[str, ...] = ()
require_auth: bool = False
future_seconds: int = 900
[docs] @classmethod
def check(cls, raw: Any, p: Problems) -> None:
if not isinstance(raw, Mapping):
p.add('protocols.nostr must be an object'); return
opt(raw, 'enabled', lambda v: isinstance(v, bool), p, 'protocols.nostr.enabled must be a boolean')
opt(raw, 'relays', lambda v: isinstance(v, (list, tuple)) and all(is_relay_url(u) for u in v), p,
'protocols.nostr.relays must be a list of relay URLs (wss:// or ws://)')
opt(raw, 'require_auth', lambda v: isinstance(v, bool), p, 'protocols.nostr.require_auth must be a boolean')
opt(raw, 'future_seconds', positive_int, p, 'protocols.nostr.future_seconds must be a positive integer')
[docs] @classmethod
def from_dict(cls, raw: Any) -> 'NostrProtocolConfig':
if not isinstance(raw, Mapping):
return cls()
return cls(
enabled=bool(raw.get('enabled', True)),
relays=tuple(str(u) for u in (raw.get('relays') or ())),
require_auth=bool(raw.get('require_auth', False)),
future_seconds=int(raw.get('future_seconds', 900)),
)
[docs]@dataclasses.dataclass(frozen=True)
class ATProtoProtocolConfig(Block):
enabled: bool = False
handle: Optional[str] = None
service_did: Optional[str] = None
pds_enabled: bool = True
firehose_subscribe: bool = False
firehose_publish: bool = True
relay_upstream: Optional[str] = None
metered_peering: bool = True
[docs] @classmethod
def check(cls, raw: Any, p: Problems) -> None:
if not isinstance(raw, Mapping):
p.add('protocols.atproto must be an object'); return
opt(raw, 'enabled', lambda v: isinstance(v, bool), p, 'protocols.atproto.enabled must be a boolean')
opt(raw, 'handle', lambda v: isinstance(v, str) and len(v) > 0, p, 'protocols.atproto.handle must be a string')
opt(raw, 'service_did', lambda v: isinstance(v, str) and v.startswith('did:'), p, 'protocols.atproto.service_did must be a did: URI')
opt(raw, 'pds_enabled', lambda v: isinstance(v, bool), p, 'protocols.atproto.pds_enabled must be a boolean')
opt(raw, 'firehose_subscribe', lambda v: isinstance(v, bool), p, 'protocols.atproto.firehose_subscribe must be a boolean')
opt(raw, 'firehose_publish', lambda v: isinstance(v, bool), p, 'protocols.atproto.firehose_publish must be a boolean')
opt(raw, 'relay_upstream', is_relay_url, p, 'protocols.atproto.relay_upstream must be a websocket URL')
opt(raw, 'metered_peering', lambda v: isinstance(v, bool), p, 'protocols.atproto.metered_peering must be a boolean')
[docs] @classmethod
def from_dict(cls, raw: Any) -> 'ATProtoProtocolConfig':
if not isinstance(raw, Mapping):
return cls()
return cls(
enabled=bool(raw.get('enabled', False)),
handle=str(raw['handle']) if raw.get('handle') else None,
service_did=str(raw['service_did']) if raw.get('service_did') else None,
pds_enabled=bool(raw.get('pds_enabled', True)),
firehose_subscribe=bool(raw.get('firehose_subscribe', False)),
firehose_publish=bool(raw.get('firehose_publish', True)),
relay_upstream=str(raw['relay_upstream']) if raw.get('relay_upstream') else None,
metered_peering=bool(raw.get('metered_peering', True)),
)
[docs]@dataclasses.dataclass(frozen=True)
class ActivityPubProtocolConfig(Block):
enabled: bool = False
domain: Optional[str] = None
summary: str = 'Fleetmesh Sovereign Edge Node'
rate_limit_per_inbox: int = 60
# Dev/LAN only. By default outbound federation refuses a peer whose actor
# or inbox URL is loopback, LAN, link-local or metadata, because that URL
# comes from a remote signature. Setting this true is the operator saying
# "this node federates with a local dev fleet", and it is the only thing
# that turns the address policy off. Never enabled by a remote peer.
allow_local_federation: bool = False
[docs] @classmethod
def check(cls, raw: Any, p: Problems) -> None:
if not isinstance(raw, Mapping):
p.add('protocols.activitypub must be an object'); return
opt(raw, 'enabled', lambda v: isinstance(v, bool), p, 'protocols.activitypub.enabled must be a boolean')
opt(raw, 'domain', lambda v: isinstance(v, str) and len(v) > 0, p, 'protocols.activitypub.domain must be a string')
opt(raw, 'summary', lambda v: isinstance(v, str), p, 'protocols.activitypub.summary must be a string')
opt(raw, 'rate_limit_per_inbox', positive_int, p, 'protocols.activitypub.rate_limit_per_inbox must be a positive integer')
opt(raw, 'allow_local_federation', lambda v: isinstance(v, bool), p,
'protocols.activitypub.allow_local_federation must be a boolean')
[docs] @classmethod
def from_dict(cls, raw: Any) -> 'ActivityPubProtocolConfig':
if not isinstance(raw, Mapping):
return cls()
return cls(
enabled=bool(raw.get('enabled', False)),
domain=str(raw['domain']) if raw.get('domain') else None,
summary=str(raw.get('summary', 'Fleetmesh Sovereign Edge Node')),
rate_limit_per_inbox=int(raw.get('rate_limit_per_inbox', 60)),
allow_local_federation=bool(raw.get('allow_local_federation', False)),
)
[docs]@dataclasses.dataclass(frozen=True)
class SSBProtocolConfig(Block):
enabled: bool = False
port: int = 8008
caps: str = 'd4a1da283c1e443e022da12bc6139952b324e4001f40fa38ba5754035a49807f'
[docs] @classmethod
def check(cls, raw: Any, p: Problems) -> None:
if not isinstance(raw, Mapping):
p.add('protocols.ssb must be an object'); return
opt(raw, 'enabled', lambda v: isinstance(v, bool), p, 'protocols.ssb.enabled must be a boolean')
opt(raw, 'port', lambda v: isinstance(v, int) and 0 <= v <= 65535, p, 'protocols.ssb.port must be 0..65535')
opt(raw, 'caps', lambda v: isinstance(v, str) and len(v) == 64, p, 'protocols.ssb.caps must be a 64-char hex string')
[docs] @classmethod
def from_dict(cls, raw: Any) -> 'SSBProtocolConfig':
if not isinstance(raw, Mapping):
return cls()
return cls(
enabled=bool(raw.get('enabled', False)),
port=int(raw.get('port', 8008)),
caps=str(raw.get('caps', 'd4a1da283c1e443e022da12bc6139952b324e4001f40fa38ba5754035a49807f')),
)
[docs]@dataclasses.dataclass(frozen=True)
class P2PandaProtocolConfig(Block):
enabled: bool = False
port: int = 9102
network_id: str = 'fleetmesh-default'
discovery: bool = True
# Dev/LAN only. A p2panda push posts to the endpoint the peer advertised
# in its descriptor; netfetch refuses loopback/LAN/link-local/metadata
# targets by default. This is the operator's explicit "my peers are on
# this LAN" switch and the only thing that relaxes that policy for the
# push.
allow_local_peers: bool = False
[docs] @classmethod
def check(cls, raw: Any, p: Problems) -> None:
if not isinstance(raw, Mapping):
p.add('protocols.p2panda must be an object'); return
opt(raw, 'enabled', lambda v: isinstance(v, bool), p, 'protocols.p2panda.enabled must be a boolean')
opt(raw, 'port', lambda v: isinstance(v, int) and 0 <= v <= 65535, p, 'protocols.p2panda.port must be 0..65535')
opt(raw, 'network_id', lambda v: isinstance(v, str) and len(v) > 0, p, 'protocols.p2panda.network_id must be a string')
opt(raw, 'discovery', lambda v: isinstance(v, bool), p, 'protocols.p2panda.discovery must be a boolean')
opt(raw, 'allow_local_peers', lambda v: isinstance(v, bool), p,
'protocols.p2panda.allow_local_peers must be a boolean')
[docs] @classmethod
def from_dict(cls, raw: Any) -> 'P2PandaProtocolConfig':
if not isinstance(raw, Mapping):
return cls()
return cls(
enabled=bool(raw.get('enabled', False)),
port=int(raw.get('port', 9102)),
network_id=str(raw.get('network_id', 'fleetmesh-default')),
discovery=bool(raw.get('discovery', True)),
allow_local_peers=bool(raw.get('allow_local_peers', False)),
)
[docs]@dataclasses.dataclass(frozen=True)
class RhioProtocolConfig(Block):
enabled: bool = False
nats_endpoint: str = 'localhost:4222'
s3_endpoint: str = 'http://localhost:9000'
network_id: str = 'rhio-default-network-1'
bind_port: int = 9102
http_bind_port: int = 3000
[docs] @classmethod
def check(cls, raw: Any, p: Problems) -> None:
if not isinstance(raw, Mapping):
p.add('protocols.rhio must be an object'); return
opt(raw, 'enabled', lambda v: isinstance(v, bool), p, 'protocols.rhio.enabled must be a boolean')
opt(raw, 'nats_endpoint', lambda v: isinstance(v, str) and len(v) > 0, p, 'protocols.rhio.nats_endpoint must be a string')
opt(raw, 's3_endpoint', lambda v: isinstance(v, str) and len(v) > 0, p, 'protocols.rhio.s3_endpoint must be a string')
opt(raw, 'network_id', lambda v: isinstance(v, str) and len(v) > 0, p, 'protocols.rhio.network_id must be a string')
opt(raw, 'bind_port', lambda v: isinstance(v, int) and 0 <= v <= 65535, p, 'protocols.rhio.bind_port must be 0..65535')
opt(raw, 'http_bind_port', lambda v: isinstance(v, int) and 0 <= v <= 65535, p, 'protocols.rhio.http_bind_port must be 0..65535')
[docs] @classmethod
def from_dict(cls, raw: Any) -> 'RhioProtocolConfig':
if not isinstance(raw, Mapping):
return cls()
return cls(
enabled=bool(raw.get('enabled', False)),
nats_endpoint=str(raw.get('nats_endpoint', 'localhost:4222')),
s3_endpoint=str(raw.get('s3_endpoint', 'http://localhost:9000')),
network_id=str(raw.get('network_id', 'rhio-default-network-1')),
bind_port=int(raw.get('bind_port', 9102)),
http_bind_port=int(raw.get('http_bind_port', 3000)),
)
[docs]@dataclasses.dataclass(frozen=True)
class ProtocolSuitesConfig(Block):
host: HostMultiplexerConfig = HostMultiplexerConfig()
nostr: NostrProtocolConfig = NostrProtocolConfig()
atproto: ATProtoProtocolConfig = ATProtoProtocolConfig()
activitypub: ActivityPubProtocolConfig = ActivityPubProtocolConfig()
ssb: SSBProtocolConfig = SSBProtocolConfig()
p2panda: P2PandaProtocolConfig = P2PandaProtocolConfig()
rhio: RhioProtocolConfig = RhioProtocolConfig()
[docs] @classmethod
def check(cls, raw: Any, p: Problems) -> None:
if not isinstance(raw, Mapping):
p.add('protocols must be an object'); return
if 'host' in raw:
HostMultiplexerConfig.check(raw['host'], p)
if 'nostr' in raw:
NostrProtocolConfig.check(raw['nostr'], p)
if 'atproto' in raw:
ATProtoProtocolConfig.check(raw['atproto'], p)
if 'activitypub' in raw:
ActivityPubProtocolConfig.check(raw['activitypub'], p)
if 'ssb' in raw:
SSBProtocolConfig.check(raw['ssb'], p)
if 'p2panda' in raw:
P2PandaProtocolConfig.check(raw['p2panda'], p)
if 'rhio' in raw:
RhioProtocolConfig.check(raw['rhio'], p)
[docs] @classmethod
def from_dict(cls, raw: Any) -> 'ProtocolSuitesConfig':
if not isinstance(raw, Mapping):
return cls()
return cls(
host=HostMultiplexerConfig.from_dict(raw.get('host')),
nostr=NostrProtocolConfig.from_dict(raw.get('nostr')),
atproto=ATProtoProtocolConfig.from_dict(raw.get('atproto')),
activitypub=ActivityPubProtocolConfig.from_dict(raw.get('activitypub')),
ssb=SSBProtocolConfig.from_dict(raw.get('ssb')),
p2panda=P2PandaProtocolConfig.from_dict(raw.get('p2panda')),
rhio=RhioProtocolConfig.from_dict(raw.get('rhio')),
)