Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Funlink v1

Status: draft. The name and port range are provisional.

Funlink lets an application tell other local applications that it is running and which services it offers. A hub such as RustyFunPlayer can then offer the user a one-click “Connect” prompt instead of manual endpoint setup.

Funlink only handles discovery. The actual connection still uses the application’s existing protocol, such as the OFS WebSocket API or Buttplug.

Quick start

To announce your application, send this JSON every 5 seconds:

POST http://127.0.0.1:<port>/funlink/v1/announce
Content-Type: application/json

to each port from 37420 to 37429. Ignore connection errors: no hub is listening on that port. That is all a peer must do.

{
  "id": "8d0c6a8e-3f0e-4c38-9a57-2f1d6b0c9e11",
  "session": "f3a91c0e5b7d",
  "name": "FapLand",
  "version": "1.4.0",
  "services": [
    { "protocol": "ofs-ws", "endpoint": "ws://127.0.0.1:8080/ofs" }
  ]
}

Optionally send POST /funlink/v1/bye on a clean exit so the hub removes the entry immediately.

Terms

  • Peer: an application that announces itself, for example FapLand.
  • Hub: an application that receives announcements and offers to connect, for example RustyFunPlayer.
  • Service: one endpoint that a peer offers, identified by a protocol.

The key words MUST, MUST NOT, SHOULD and MAY are used as in RFC 2119.

Transport

  • HTTP/1.1 over TCP on the loopback interface, without TLS.
  • Hubs listen on 127.0.0.1 and bind the first free port from 37420 to 37429.
  • Peers connect to 127.0.0.1. They SHOULD NOT use localhost, which may resolve to ::1 first.
  • Bodies are UTF-8 JSON with Content-Type: application/json.
  • If the environment variable FUNLINK_PORT is set, hubs bind and peers send to that single port only. This is meant for tests and unusual setups.

Announce

POST /funlink/v1/announce

A peer MUST send an announce when it starts and then every 5 seconds while it runs. Each announce carries the complete record; there are no partial updates. Peers SHOULD use a request timeout of about 1 second.

Record

FieldRequiredTypeRules
idyesstringStable for the peer. 1 to 64 characters from A-Z a-z 0-9 . _ -. A fixed name for the application is recommended, such as fapland or org.example.player; it also seeds the OFS port. A random UUID created on first run also works.
sessionyesstringRandom for each launch of the peer. 8 to 32 characters from A-Z a-z 0-9.
nameyesstringDisplay name, 1 to 64 characters.
versionnostringDisplay version, up to 32 characters.
iconnostringdata:image/png;base64,..., square, 128 px recommended, at most 512 px on each side. The whole URI is at most 64 KiB.
servicesyesarray1 to 8 service objects.
extnoobjectApplication-specific data. Keys MUST be reverse domain names such as org.fapland.

Service

FieldRequiredTypeRules
protocolyesstringA registered protocol name, or x-<vendor>-<name> for unregistered ones.
endpointyesstringAbsolute URL. The host MUST be a loopback address: 127.0.0.0/8 or [::1]. The scheme MUST match the protocol.
featuresnoarrayUp to 16 strings that describe optional capabilities.

Response

On success the hub answers 200 OK:

{
  "hub": { "id": "4b1e...", "name": "RustyFunPlayer", "version": "0.1.0" },
  "supported": ["ofs-ws", "buttplug-ws"],
  "state": "available"
}
stateMeaning
availableThe hub knows the peer and may offer it to the user.
connectedThe hub is connected to one of the peer’s services.
dismissedThe user declined for this session. The hub will not prompt again until the session changes.
unsupportedThe hub cannot use any of the announced services.

Peers MAY ignore the response. Peers that read it can, for example, show “Connected to RustyFunPlayer”.

Bye

POST /funlink/v1/bye
{ "id": "8d0c6a8e-3f0e-4c38-9a57-2f1d6b0c9e11", "session": "f3a91c0e5b7d" }

The hub answers 204 No Content and removes the entry. Sending bye is optional; without it the entry expires.

Errors

Errors use an HTTP status and this body:

{ "code": "invalid_announce", "message": "services[0].endpoint must use a loopback host" }
StatuscodeCause
400invalid_announceThe record does not follow this specification.
403forbiddenThe Host or Origin header was rejected.
404not_foundUnknown path, including unsupported versions.
413too_largeThe body exceeds 96 KiB.
429rate_limitedRequests for the same id less than 200 ms apart, or more than 64 peers.

Hub rules

A hub:

  • MUST forget an entry 15 seconds after its last announce, or immediately on bye.
  • MUST treat an announce with the same id and a new session as a restart of that peer.
  • MUST reject requests whose Host header is not 127.0.0.1:<port>, localhost:<port> or [::1]:<port>. This blocks DNS rebinding.
  • MUST NOT connect to a service because of an announce alone. It connects only after the user confirms, or after the user earlier chose to always connect to that id.
  • SHOULD prompt at most once per session and SHOULD NOT prompt while it is already connected to a source.
  • MUST reject bodies larger than 96 KiB.
  • SHOULD refuse connections from other users of the machine where the operating system names the owner of a loopback socket, as Linux does.

Browsers

Browser-based peers can announce with fetch. Because the body is JSON, the browser sends a CORS preflight. A hub that accepts browser peers answers OPTIONS with Access-Control-Allow-Origin set to the request origin, Access-Control-Allow-Methods: POST and Access-Control-Allow-Headers: Content-Type. Browsers may additionally ask the user for local network access.

When an Origin header is present, the hub MUST show that origin in the prompt and MUST store “always connect” decisions per origin and id, never per id alone. A hub MAY reject all browser origins.

Local network

Everything above happens on one machine. A peer on another machine of the local network, such as a TCode device with Wi-Fi or a player on a Steam Deck, announces the same record through two transports instead:

  • DNS-SD over multicast DNS (RFC 6762 and RFC 6763), the standard that Bonjour and Avahi implement, under the service type _funlink._udp.local.
  • The Funlink beacon: the record as JSON in a UDP datagram on port 37420, sent to a multicast group and as a broadcast.

Hubs that support the local network listen on both and merge what they hear. A peer MAY use either transport or both; small firmware may prefer the beacon, which needs no DNS stack, and DNS-SD makes the peer visible in standard browsers such as avahi-browse or Bonjour Browser.

Discovery on the local network is a separate choice on both sides. A hub MUST NOT listen on the local network unless its user turned that on, and a peer announces there only when the application asks for it.

Endpoints

A peer does not know which of its addresses a hub reaches it by, so it writes the host of each endpoint as 0.0.0.0, such as ws://0.0.0.0:8080/ofs. A hub MUST replace the host of every endpoint with the source address of the packet that carried the record, whatever host the record names. A record therefore cannot point a hub at another machine. An IPv6 source address is written in brackets, with a zone as in RFC 6874, such as ws://[fe80::1%25eth0]:8080/.

Services whose scheme does not name a network socket, such as unix or serial, MUST be dropped; a record without other services is ignored. The services of a local network record listen on a network interface, so anyone on the network can connect to them, and peers SHOULD protect them accordingly.

Record limits

A local network record is the record of Announce with these differences:

  • It has no icon.
  • Over DNS-SD it has no ext either.
  • The whole record, encoded as described below, fits in 1200 bytes.

DNS-SD

A peer registers one instance of _funlink._udp.local:

  • Instance name: the display name, cut to at most 54 bytes at a character boundary, a space and the first 8 characters of the session, such as Stroker f3a91c0e. Hubs ignore instance names.
  • SRV: target funlink-<session>.local, with an A record of each of the peer’s IPv4 addresses. The port is the port of the first service. Hubs ignore the target and the port.
  • TXT, with v first. Hubs accept the other keys in any order.
KeyRequiredValue
vyes1
idyesThe record’s id.
syesThe record’s session.
nyesThe record’s name.
vernoThe record’s version.
p0 to p7p0Service N as its protocol, a space and its endpoint, such as ofs-ws ws://0.0.0.0:8080/ofs.
f0 to f7noThe features of service N, separated by commas.

Services are numbered from 0 without gaps; a hub stops at the first missing p<N>. Each key=value string is at most 255 bytes, as DNS requires, and the TXT data is at most 1200 bytes. Features MUST NOT contain commas in a record announced over DNS-SD.

Timing:

  • PTR, SRV and TXT records SHOULD have a TTL of 15 seconds and A records one of 120 seconds. Hubs do not rely on other TTLs than 0, so the defaults of an existing mDNS stack also work.
  • A peer announces its records unsolicited when it starts, again 1 second later, and whenever the record changes. It answers every query for _funlink._udp.local by multicast after a random delay of 20 to 120 milliseconds, with the PTR record as the answer and the SRV, TXT and A records as additional records. On a clean exit it sends the same records with a TTL of 0.
  • A hub sends a query for the PTR records of _funlink._udp.local to 224.0.0.251:5353 when it starts, 1 second later and then every 5 seconds. It MUST NOT list known answers, so that every peer answers each query.
  • A hub reads every response that carries a PTR record of _funlink._udp.local with the instance’s SRV and TXT records in the same packet, and MAY query SRV and TXT on their own when a response lacks them. A record with a TTL of 0 counts as a bye.

IPv4 is required. Peers and hubs MAY also use [ff02::fb]:5353.

Beacon

Every beacon datagram is one UTF-8 JSON object of at most 1200 bytes with the fields funlink, which is 1, and type. Receivers MUST ignore datagrams that are larger, are not such an object, have another funlink value, or have an unknown type, without answering them.

The beacon uses UDP port 37420, the multicast group 239.255.37.20 and the limited broadcast address 255.255.255.255. If the environment variable FUNLINK_LAN_PORT is set, hubs and peers use that port instead; this is meant for tests.

typeSenderOther fields
announcePeerThe record: id, session, name, version, services, ext.
byePeerid, session.
queryHubNone.
statusHubid and session of the peer, then hub, supported and state as in the response to an announce.
{"funlink":1,"type":"announce","id":"sr6-a41c","session":"f3a91c0e5b7d","name":"SR6","services":[{"protocol":"tcode-udp","endpoint":"udp://0.0.0.0:8000"}]}

A peer:

  • binds port 37420 with address reuse (SO_REUSEADDR, and SO_REUSEPORT where it exists), so that several peers and hubs share it, and joins the multicast group on each IPv4 interface; a peer that cannot set address reuse, such as one written in a game engine without that option, MUST NOT bind the port, so that it never keeps a hub on the same machine from listening; it then answers no queries, and hubs find it by its next announce;
  • sends its datagrams from a second socket on a random port, so that answers reach it and not another program on port 37420;
  • sends announce when it starts and then every 5 seconds, each time to the multicast group and to the broadcast address;
  • answers a query with an announce sent only to the query’s source address and port, after a random delay of 20 to 120 milliseconds, and at most once per second for each source address;
  • MAY send bye to the group and the broadcast address on a clean exit.

A hub:

  • binds and joins like a peer, and sends from a second socket on a random port;
  • sends query to the group and the broadcast address when it starts, 1 second later and whenever the user asks to search again;
  • MAY send query to addresses that the user entered, which reaches peers on other subnets. It repeats these queries every 5 seconds, because the announces of such peers do not cross the router;
  • treats an announce that answers its query like any other;
  • answers each announce with a status sent only to its source address and port, at most once per second for each peer. Peers MAY ignore it.

Hub rules for the local network

On top of the hub rules, a hub:

  • MUST ignore packets whose source address is not a loopback address, a private IPv4 address (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16), a link-local address (169.254.0.0/16, fe80::/10) or a unique local IPv6 address (fc00::/7).
  • MUST validate the record as an announce, except that endpoint hosts are replaced as described in Endpoints and not checked.
  • MUST key entries by source address and id. The same key heard over DNS-SD and the beacon is one entry; its record is the latest one heard, but ext from the beacon stays until the beacon record changes or expires.
  • MUST ignore a local network record with the id and session of a peer that announces over loopback HTTP, and drops such an entry when the loopback announce arrives later.
  • MUST treat a record with the id and session of an existing entry but from another source address, such as from a peer with Wi-Fi and Ethernet, as a sign of life of that entry only. It neither creates a second entry nor changes the record or address of the first.
  • MUST forget an entry 15 seconds after it last heard the record by either transport, or at once on bye or a DNS-SD TTL of 0.
  • MUST NOT keep more than 64 entries from the local network, nor more than 8 from one source address, and ignores records for one key and transport that arrive less than 200 milliseconds apart. A peer announces over both transports at once, so the gap does not apply across them.
  • MUST NOT act on ext data of a local network record that names files, programs or processes, because it describes another machine.
  • MUST show the source address in the prompt and MUST store “always connect” and “never ask” decisions per address and id, never per id alone.

Compatibility rules

  • Receivers MUST ignore unknown fields, unknown state values, unknown protocols and unknown features.
  • Fields keep their meaning and type in all v1 revisions.
  • Only incompatible changes create a new path, such as /funlink/v2/. A peer MAY announce to several versions.

Protocol registry

protocolEndpoint schemesDescription
ofs-wsws, wssOpenFunscripter WebSocket API.
ofs-ws-extendedws, wssOpenFunscripter WebSocket API with device state and range edits. See ofs-ws-extended. Announced beside an ofs-ws service with the same endpoint.
ofs-ws-faplandws, wssAlias of ofs-ws-extended that FapLand announces. Hubs that support one of the two MUST support both.
buttplug-wsws, wssButtplug protocol server, such as Intiface.
vlc-httphttpVLC HTTP interface.
mpc-httphttpMPC-HC web interface.
whirligig-tcptcpWhirligig timecode server.
tcode-wsws, wssTCode device over WebSocket.
tcode-tcptcpTCode device over TCP.
tcode-udpudpTCode device over UDP.
edihttpA game that expects an EDI server at the endpoint. The hub serves it instead of connecting to it.
mpv-ipcunixAn mpv player’s JSON IPC socket. The endpoint’s path is the socket on this machine, such as unix://127.0.0.1/run/user/1000/mpv.sock.
tcode-serialserialA TCode device on a serial port of this machine. The endpoint’s path is the port, such as serial://127.0.0.1/dev/serial/by-id/usb-1a86_USB_Serial-if00-port0. The hub connects an output to it, beside any source.

New entries are added by request with a short description and a link to the protocol’s documentation. Names starting with x- need no registration.

Examples

An EDI game

A game with an EDI mod announces the edi service on the port it calls, and tells RustyFunPlayer where it is installed. The paths are absolute paths of the host system, so a game under Wine translates its Z:\ paths first. The program and the configuration lie inside root, and the configuration is the game’s EdiConfig.json. Use your own id; RustyFunPlayer recognizes the game by its config, so its card replaces the one for the same game found by rustyfunplayer-detect.

{
  "id": "castle-of-temptation",
  "session": "b81c4e0a92d3",
  "name": "Castle of Temptation",
  "services": [{ "protocol": "edi", "endpoint": "http://127.0.0.1:5000/" }],
  "ext": {
    "org.rustyfunplayer.edi": {
      "root": "/games/Castle",
      "executable": "/games/Castle/Castle.exe",
      "config": "/games/Castle/Edi/EdiConfig.json"
    }
  }
}

Shell

curl -sS -X POST http://127.0.0.1:37420/funlink/v1/announce \
  -H 'Content-Type: application/json' \
  -d '{"id":"test","session":"abcdefgh","name":"Test","services":[{"protocol":"ofs-ws","endpoint":"ws://127.0.0.1:8080/ofs"}]}'

JavaScript (Node.js 18+ and Electron)

The same code runs in a browser, but RustyFunPlayer refuses browser requests (see Browsers).

const record = {
  id: installId, // stored on first run
  session: crypto.randomUUID().replaceAll("-", ""),
  name: "FapLand",
  version: "1.4.0",
  services: [{ protocol: "ofs-ws", endpoint: "ws://127.0.0.1:8080/ofs" }],
};

function announce() {
  for (let port = 37420; port <= 37429; port++) {
    fetch(`http://127.0.0.1:${port}/funlink/v1/announce`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(record),
      signal: AbortSignal.timeout(1000),
    }).catch(() => {});
  }
}

announce();
setInterval(announce, 5000);

Python

import json, secrets, time, urllib.request

record = {
    "id": install_id,  # stored on first run
    "session": secrets.token_hex(8),
    "name": "Example",
    "services": [{"protocol": "ofs-ws", "endpoint": "ws://127.0.0.1:8080/ofs"}],
}
body = json.dumps(record).encode()

def announce():
    for port in range(37420, 37430):
        request = urllib.request.Request(
            f"http://127.0.0.1:{port}/funlink/v1/announce",
            data=body,
            headers={"Content-Type": "application/json"},
        )
        try:
            urllib.request.urlopen(request, timeout=1).close()
        except OSError:
            pass

# Run this loop on a background thread in a real application.
while True:
    announce()
    time.sleep(5)

Implementations

  • RustyFunPlayer is a hub.
  • FapLand announces its OFS bridge as ofs-ws.
  • The libraries in this repository implement both roles.

Planned extensions

These are intentionally left out of v1:

  • Coordination between several hubs, so that a service used by one hub is not offered by another.
  • A peer request such as “connect me now” for a button in the peer’s UI.
  • Pairing with keys, so that a hub can trust a peer on the local network across addresses and connect to it without asking.
  • A hub that announces itself on the local network.