@bowl42/keywire 0.0.0-stage → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +118 -2
- package/dist/app.d.ts +2 -0
- package/dist/app.js +13 -0
- package/dist/app.js.map +1 -0
- package/dist/bytes.d.ts +1 -0
- package/dist/bytes.js +7 -0
- package/dist/bytes.js.map +1 -0
- package/dist/client.d.ts +66 -0
- package/dist/client.js +846 -0
- package/dist/client.js.map +1 -0
- package/dist/compress/block.d.ts +51 -0
- package/dist/compress/block.js +667 -0
- package/dist/compress/block.js.map +1 -0
- package/dist/compress/deflate.d.ts +7 -0
- package/dist/compress/deflate.js +603 -0
- package/dist/compress/deflate.js.map +1 -0
- package/dist/compress/index.d.ts +6 -0
- package/dist/compress/index.js +13 -0
- package/dist/compress/index.js.map +1 -0
- package/dist/compress/inflate.d.ts +21 -0
- package/dist/compress/inflate.js +380 -0
- package/dist/compress/inflate.js.map +1 -0
- package/dist/compress/wasm.d.ts +50 -0
- package/dist/compress/wasm.js +192 -0
- package/dist/compress/wasm.js.map +1 -0
- package/dist/compress/zlib-wasm.d.ts +15 -0
- package/dist/compress/zlib-wasm.js +18 -0
- package/dist/compress/zlib-wasm.js.map +1 -0
- package/dist/conn.d.ts +126 -0
- package/dist/conn.js +685 -0
- package/dist/conn.js.map +1 -0
- package/dist/dial/index.d.ts +3 -0
- package/dist/dial/index.js +10 -0
- package/dist/dial/index.js.map +1 -0
- package/dist/discovery/index.d.ts +16 -0
- package/dist/discovery/index.js +173 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/reach.d.ts +4 -0
- package/dist/discovery/reach.js +36 -0
- package/dist/discovery/reach.js.map +1 -0
- package/dist/domain.d.ts +3 -0
- package/dist/domain.js +12 -0
- package/dist/domain.js.map +1 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +49 -0
- package/dist/errors.js.map +1 -0
- package/dist/extension.d.ts +53 -0
- package/dist/extension.js +158 -0
- package/dist/extension.js.map +1 -0
- package/dist/forward.d.ts +11 -0
- package/dist/forward.js +125 -0
- package/dist/forward.js.map +1 -0
- package/dist/framed-stream.d.ts +14 -0
- package/dist/framed-stream.js +113 -0
- package/dist/framed-stream.js.map +1 -0
- package/dist/gen/keywire/discovery/v1/discovery_pb.d.ts +105 -0
- package/dist/gen/keywire/discovery/v1/discovery_pb.js +57 -0
- package/dist/gen/keywire/discovery/v1/discovery_pb.js.map +1 -0
- package/dist/gen/keywire/state/v1/state_pb.d.ts +988 -0
- package/dist/gen/keywire/state/v1/state_pb.js +184 -0
- package/dist/gen/keywire/state/v1/state_pb.js.map +1 -0
- package/dist/gen/keywire/v1/forward_pb.d.ts +53 -0
- package/dist/gen/keywire/v1/forward_pb.js +58 -0
- package/dist/gen/keywire/v1/forward_pb.js.map +1 -0
- package/dist/gen/keywire/v1/guest_pb.d.ts +322 -0
- package/dist/gen/keywire/v1/guest_pb.js +100 -0
- package/dist/gen/keywire/v1/guest_pb.js.map +1 -0
- package/dist/gen/keywire/v1/pair_pb.d.ts +106 -0
- package/dist/gen/keywire/v1/pair_pb.js +45 -0
- package/dist/gen/keywire/v1/pair_pb.js.map +1 -0
- package/dist/gen/keywire/v1/relay_pb.d.ts +116 -0
- package/dist/gen/keywire/v1/relay_pb.js +20 -0
- package/dist/gen/keywire/v1/relay_pb.js.map +1 -0
- package/dist/gen/keywire/v1/roster_pb.d.ts +213 -0
- package/dist/gen/keywire/v1/roster_pb.js +77 -0
- package/dist/gen/keywire/v1/roster_pb.js.map +1 -0
- package/dist/gen/keywire/v1/sync_pb.d.ts +68 -0
- package/dist/gen/keywire/v1/sync_pb.js +24 -0
- package/dist/gen/keywire/v1/sync_pb.js.map +1 -0
- package/dist/guest/equal-bytes.d.ts +1 -0
- package/dist/guest/equal-bytes.js +10 -0
- package/dist/guest/equal-bytes.js.map +1 -0
- package/dist/guest/hello.d.ts +4 -0
- package/dist/guest/hello.js +42 -0
- package/dist/guest/hello.js.map +1 -0
- package/dist/guest/invite-frame.d.ts +8 -0
- package/dist/guest/invite-frame.js +44 -0
- package/dist/guest/invite-frame.js.map +1 -0
- package/dist/guest/invite.d.ts +17 -0
- package/dist/guest/invite.js +86 -0
- package/dist/guest/invite.js.map +1 -0
- package/dist/guest/relay-hello.d.ts +2 -0
- package/dist/guest/relay-hello.js +38 -0
- package/dist/guest/relay-hello.js.map +1 -0
- package/dist/guest/revocations.d.ts +12 -0
- package/dist/guest/revocations.js +134 -0
- package/dist/guest/revocations.js.map +1 -0
- package/dist/guest/roster.d.ts +2 -0
- package/dist/guest/roster.js +14 -0
- package/dist/guest/roster.js.map +1 -0
- package/dist/guest/token-payload.d.ts +2 -0
- package/dist/guest/token-payload.js +71 -0
- package/dist/guest/token-payload.js.map +1 -0
- package/dist/guest-conn.d.ts +20 -0
- package/dist/guest-conn.js +99 -0
- package/dist/guest-conn.js.map +1 -0
- package/dist/guest-relay.d.ts +7 -0
- package/dist/guest-relay.js +45 -0
- package/dist/guest-relay.js.map +1 -0
- package/dist/guest.d.ts +20 -0
- package/dist/guest.js +99 -0
- package/dist/guest.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/msg-path.d.ts +9 -0
- package/dist/msg-path.js +2 -0
- package/dist/msg-path.js.map +1 -0
- package/dist/mux/byte-queue.d.ts +9 -0
- package/dist/mux/byte-queue.js +62 -0
- package/dist/mux/byte-queue.js.map +1 -0
- package/dist/mux/codec.d.ts +30 -0
- package/dist/mux/codec.js +23 -0
- package/dist/mux/codec.js.map +1 -0
- package/dist/mux/errors.d.ts +23 -0
- package/dist/mux/errors.js +47 -0
- package/dist/mux/errors.js.map +1 -0
- package/dist/mux/frame.d.ts +44 -0
- package/dist/mux/frame.js +161 -0
- package/dist/mux/frame.js.map +1 -0
- package/dist/mux/nexttask.d.ts +1 -0
- package/dist/mux/nexttask.js +19 -0
- package/dist/mux/nexttask.js.map +1 -0
- package/dist/mux/session.d.ts +69 -0
- package/dist/mux/session.js +586 -0
- package/dist/mux/session.js.map +1 -0
- package/dist/mux/signal.d.ts +5 -0
- package/dist/mux/signal.js +14 -0
- package/dist/mux/signal.js.map +1 -0
- package/dist/mux/stream.d.ts +34 -0
- package/dist/mux/stream.js +620 -0
- package/dist/mux/stream.js.map +1 -0
- package/dist/nodekit/device.d.ts +40 -0
- package/dist/nodekit/device.js +188 -0
- package/dist/nodekit/device.js.map +1 -0
- package/dist/nodekit/index.d.ts +5 -0
- package/dist/nodekit/index.js +8 -0
- package/dist/nodekit/index.js.map +1 -0
- package/dist/nodekit/indexeddb.d.ts +2 -0
- package/dist/nodekit/indexeddb.js +114 -0
- package/dist/nodekit/indexeddb.js.map +1 -0
- package/dist/nodekit/pair-device.d.ts +27 -0
- package/dist/nodekit/pair-device.js +44 -0
- package/dist/nodekit/pair-device.js.map +1 -0
- package/dist/nodekit/sealed-signer.d.ts +22 -0
- package/dist/nodekit/sealed-signer.js +74 -0
- package/dist/nodekit/sealed-signer.js.map +1 -0
- package/dist/nodekit/store.d.ts +22 -0
- package/dist/nodekit/store.js +21 -0
- package/dist/nodekit/store.js.map +1 -0
- package/dist/noise/backend.d.ts +25 -0
- package/dist/noise/backend.js +2 -0
- package/dist/noise/backend.js.map +1 -0
- package/dist/noise/backends.d.ts +4 -0
- package/dist/noise/backends.js +19 -0
- package/dist/noise/backends.js.map +1 -0
- package/dist/noise/cipher.d.ts +18 -0
- package/dist/noise/cipher.js +69 -0
- package/dist/noise/cipher.js.map +1 -0
- package/dist/noise/crypto.d.ts +11 -0
- package/dist/noise/crypto.js +47 -0
- package/dist/noise/crypto.js.map +1 -0
- package/dist/noise/handshake.d.ts +28 -0
- package/dist/noise/handshake.js +183 -0
- package/dist/noise/handshake.js.map +1 -0
- package/dist/noise/import.d.ts +2 -0
- package/dist/noise/import.js +16 -0
- package/dist/noise/import.js.map +1 -0
- package/dist/noise/noble.d.ts +2 -0
- package/dist/noise/noble.js +51 -0
- package/dist/noise/noble.js.map +1 -0
- package/dist/noise/pair-code.d.ts +2 -0
- package/dist/noise/pair-code.js +11 -0
- package/dist/noise/pair-code.js.map +1 -0
- package/dist/noise/pkcs8.d.ts +3 -0
- package/dist/noise/pkcs8.js +18 -0
- package/dist/noise/pkcs8.js.map +1 -0
- package/dist/noise/sealed.d.ts +21 -0
- package/dist/noise/sealed.js +40 -0
- package/dist/noise/sealed.js.map +1 -0
- package/dist/noise/symmetric.d.ts +17 -0
- package/dist/noise/symmetric.js +56 -0
- package/dist/noise/symmetric.js.map +1 -0
- package/dist/noise/webcrypto.d.ts +3 -0
- package/dist/noise/webcrypto.js +98 -0
- package/dist/noise/webcrypto.js.map +1 -0
- package/dist/pair-relay.d.ts +4 -0
- package/dist/pair-relay.js +46 -0
- package/dist/pair-relay.js.map +1 -0
- package/dist/pair.d.ts +7 -0
- package/dist/pair.js +50 -0
- package/dist/pair.js.map +1 -0
- package/dist/paths/link.d.ts +33 -0
- package/dist/paths/link.js +132 -0
- package/dist/paths/link.js.map +1 -0
- package/dist/paths/node-paths.d.ts +10 -0
- package/dist/paths/node-paths.js +41 -0
- package/dist/paths/node-paths.js.map +1 -0
- package/dist/paths/path.d.ts +11 -0
- package/dist/paths/path.js +25 -0
- package/dist/paths/path.js.map +1 -0
- package/dist/paths/race.d.ts +18 -0
- package/dist/paths/race.js +157 -0
- package/dist/paths/race.js.map +1 -0
- package/dist/paths/relay-sessions.d.ts +11 -0
- package/dist/paths/relay-sessions.js +93 -0
- package/dist/paths/relay-sessions.js.map +1 -0
- package/dist/paths/upgrades.d.ts +12 -0
- package/dist/paths/upgrades.js +58 -0
- package/dist/paths/upgrades.js.map +1 -0
- package/dist/relay-server/admission.d.ts +24 -0
- package/dist/relay-server/admission.js +140 -0
- package/dist/relay-server/admission.js.map +1 -0
- package/dist/relay-server/config.d.ts +28 -0
- package/dist/relay-server/config.js +47 -0
- package/dist/relay-server/config.js.map +1 -0
- package/dist/relay-server/discovery/addr.d.ts +18 -0
- package/dist/relay-server/discovery/addr.js +211 -0
- package/dist/relay-server/discovery/addr.js.map +1 -0
- package/dist/relay-server/discovery/directory.d.ts +42 -0
- package/dist/relay-server/discovery/directory.js +101 -0
- package/dist/relay-server/discovery/directory.js.map +1 -0
- package/dist/relay-server/discovery/http.d.ts +10 -0
- package/dist/relay-server/discovery/http.js +172 -0
- package/dist/relay-server/discovery/http.js.map +1 -0
- package/dist/relay-server/discovery/index.d.ts +6 -0
- package/dist/relay-server/discovery/index.js +15 -0
- package/dist/relay-server/discovery/index.js.map +1 -0
- package/dist/relay-server/discovery/listing.d.ts +24 -0
- package/dist/relay-server/discovery/listing.js +115 -0
- package/dist/relay-server/discovery/listing.js.map +1 -0
- package/dist/relay-server/discovery/options.d.ts +39 -0
- package/dist/relay-server/discovery/options.js +97 -0
- package/dist/relay-server/discovery/options.js.map +1 -0
- package/dist/relay-server/discovery/service.d.ts +21 -0
- package/dist/relay-server/discovery/service.js +409 -0
- package/dist/relay-server/discovery/service.js.map +1 -0
- package/dist/relay-server/forward.d.ts +3 -0
- package/dist/relay-server/forward.js +97 -0
- package/dist/relay-server/forward.js.map +1 -0
- package/dist/relay-server/handshake.d.ts +10 -0
- package/dist/relay-server/handshake.js +36 -0
- package/dist/relay-server/handshake.js.map +1 -0
- package/dist/relay-server/index.d.ts +6 -0
- package/dist/relay-server/index.js +10 -0
- package/dist/relay-server/index.js.map +1 -0
- package/dist/relay-server/relay.d.ts +25 -0
- package/dist/relay-server/relay.js +392 -0
- package/dist/relay-server/relay.js.map +1 -0
- package/dist/relay-server/service-read.d.ts +4 -0
- package/dist/relay-server/service-read.js +16 -0
- package/dist/relay-server/service-read.js.map +1 -0
- package/dist/relay-server/service.d.ts +25 -0
- package/dist/relay-server/service.js +8 -0
- package/dist/relay-server/service.js.map +1 -0
- package/dist/relay-server/session.d.ts +60 -0
- package/dist/relay-server/session.js +35 -0
- package/dist/relay-server/session.js.map +1 -0
- package/dist/relay-server/url.d.ts +1 -0
- package/dist/relay-server/url.js +8 -0
- package/dist/relay-server/url.js.map +1 -0
- package/dist/relay-url.d.ts +2 -0
- package/dist/relay-url.js +11 -0
- package/dist/relay-url.js.map +1 -0
- package/dist/relay.d.ts +5 -0
- package/dist/relay.js +60 -0
- package/dist/relay.js.map +1 -0
- package/dist/roster/admission.d.ts +17 -0
- package/dist/roster/admission.js +85 -0
- package/dist/roster/admission.js.map +1 -0
- package/dist/roster/credentials.d.ts +5 -0
- package/dist/roster/credentials.js +72 -0
- package/dist/roster/credentials.js.map +1 -0
- package/dist/roster/digest.d.ts +3 -0
- package/dist/roster/digest.js +23 -0
- package/dist/roster/digest.js.map +1 -0
- package/dist/roster/envelope.d.ts +11 -0
- package/dist/roster/envelope.js +35 -0
- package/dist/roster/envelope.js.map +1 -0
- package/dist/roster/errors.d.ts +6 -0
- package/dist/roster/errors.js +11 -0
- package/dist/roster/errors.js.map +1 -0
- package/dist/roster/limits.d.ts +25 -0
- package/dist/roster/limits.js +26 -0
- package/dist/roster/limits.js.map +1 -0
- package/dist/roster/merge.d.ts +10 -0
- package/dist/roster/merge.js +101 -0
- package/dist/roster/merge.js.map +1 -0
- package/dist/roster/name.d.ts +1 -0
- package/dist/roster/name.js +7 -0
- package/dist/roster/name.js.map +1 -0
- package/dist/roster/order.d.ts +5 -0
- package/dist/roster/order.js +22 -0
- package/dist/roster/order.js.map +1 -0
- package/dist/roster/ownership.d.ts +9 -0
- package/dist/roster/ownership.js +42 -0
- package/dist/roster/ownership.js.map +1 -0
- package/dist/roster/retain.d.ts +2 -0
- package/dist/roster/retain.js +55 -0
- package/dist/roster/retain.js.map +1 -0
- package/dist/roster/revocations.d.ts +5 -0
- package/dist/roster/revocations.js +37 -0
- package/dist/roster/revocations.js.map +1 -0
- package/dist/roster/scan.d.ts +16 -0
- package/dist/roster/scan.js +194 -0
- package/dist/roster/scan.js.map +1 -0
- package/dist/roster/state.d.ts +34 -0
- package/dist/roster/state.js +204 -0
- package/dist/roster/state.js.map +1 -0
- package/dist/roster/statement.d.ts +38 -0
- package/dist/roster/statement.js +176 -0
- package/dist/roster/statement.js.map +1 -0
- package/dist/roster/types.d.ts +35 -0
- package/dist/roster/types.js +2 -0
- package/dist/roster/types.js.map +1 -0
- package/dist/roster/verify.d.ts +3 -0
- package/dist/roster/verify.js +63 -0
- package/dist/roster/verify.js.map +1 -0
- package/dist/roster-sync/apply.d.ts +2 -0
- package/dist/roster-sync/apply.js +23 -0
- package/dist/roster-sync/apply.js.map +1 -0
- package/dist/roster-sync/exchange.d.ts +3 -0
- package/dist/roster-sync/exchange.js +25 -0
- package/dist/roster-sync/exchange.js.map +1 -0
- package/dist/roster-sync/frame.d.ts +17 -0
- package/dist/roster-sync/frame.js +66 -0
- package/dist/roster-sync/frame.js.map +1 -0
- package/dist/roster-sync/records.d.ts +8 -0
- package/dist/roster-sync/records.js +78 -0
- package/dist/roster-sync/records.js.map +1 -0
- package/dist/roster-sync/sync.d.ts +12 -0
- package/dist/roster-sync/sync.js +69 -0
- package/dist/roster-sync/sync.js.map +1 -0
- package/dist/rtc.d.ts +15 -0
- package/dist/rtc.js +275 -0
- package/dist/rtc.js.map +1 -0
- package/dist/state/errors.d.ts +30 -0
- package/dist/state/errors.js +72 -0
- package/dist/state/errors.js.map +1 -0
- package/dist/state/index.d.ts +6 -0
- package/dist/state/index.js +11 -0
- package/dist/state/index.js.map +1 -0
- package/dist/state/limits.d.ts +34 -0
- package/dist/state/limits.js +115 -0
- package/dist/state/limits.js.map +1 -0
- package/dist/state/remote.d.ts +217 -0
- package/dist/state/remote.js +1934 -0
- package/dist/state/remote.js.map +1 -0
- package/dist/state/shared.d.ts +56 -0
- package/dist/state/shared.js +300 -0
- package/dist/state/shared.js.map +1 -0
- package/dist/state/state.d.ts +118 -0
- package/dist/state/state.js +1048 -0
- package/dist/state/state.js.map +1 -0
- package/dist/state/store.d.ts +31 -0
- package/dist/state/store.js +375 -0
- package/dist/state/store.js.map +1 -0
- package/dist/state/wire.d.ts +43 -0
- package/dist/state/wire.js +179 -0
- package/dist/state/wire.js.map +1 -0
- package/dist/strict-proto.d.ts +10 -0
- package/dist/strict-proto.js +167 -0
- package/dist/strict-proto.js.map +1 -0
- package/dist/token.d.ts +18 -0
- package/dist/token.js +60 -0
- package/dist/token.js.map +1 -0
- package/dist/urls.d.ts +7 -0
- package/dist/urls.js +117 -0
- package/dist/urls.js.map +1 -0
- package/dist/ws.d.ts +19 -0
- package/dist/ws.js +143 -0
- package/dist/ws.js.map +1 -0
- package/package.json +72 -3
package/dist/conn.js
ADDED
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
import { create, toBinary } from "@bufbuild/protobuf";
|
|
2
|
+
import { validApp } from "./app.js";
|
|
3
|
+
import { copyBytes } from "./bytes.js";
|
|
4
|
+
import { resolveDomain } from "./domain.js";
|
|
5
|
+
import { failedWith, KeywireError } from "./errors.js";
|
|
6
|
+
import { PairAcceptedSchema, PairKind, PairRequestSchema } from "./gen/keywire/v1/pair_pb.js";
|
|
7
|
+
import { CredentialsSchema, HandshakeReplySchema } from "./gen/keywire/v1/roster_pb.js";
|
|
8
|
+
import { freshRoster } from "./guest/roster.js";
|
|
9
|
+
import { SessionClosed } from "./mux/errors.js";
|
|
10
|
+
import { CONN_WINDOW, MAX_CONN_WINDOW, MAX_STREAM_WINDOW, STREAM_WINDOW } from "./mux/frame.js";
|
|
11
|
+
import { COMPRESSION_IDLE_MS, COMPRESSION_MEMORY, Session, WRITE_QUEUE, } from "./mux/session.js";
|
|
12
|
+
import { CipherState, MAX_MESSAGE } from "./noise/cipher.js";
|
|
13
|
+
import { concat } from "./noise/crypto.js";
|
|
14
|
+
import { Handshake } from "./noise/handshake.js";
|
|
15
|
+
import { pairingCode } from "./noise/pair-code.js";
|
|
16
|
+
import { maxCredentials } from "./roster/limits.js";
|
|
17
|
+
import { validName } from "./roster/scan.js";
|
|
18
|
+
import { validSigningKey } from "./roster/verify.js";
|
|
19
|
+
import { decode } from "./strict-proto.js";
|
|
20
|
+
import { checkUrl } from "./urls.js";
|
|
21
|
+
import { WsPath } from "./ws.js";
|
|
22
|
+
// The dialer's first message is: version | pattern | Noise message. Both
|
|
23
|
+
// header bytes and the network id are bound into the handshake via the
|
|
24
|
+
// prologue, so a peer of another Network or version cannot complete it.
|
|
25
|
+
const WIRE_VERSION = 1;
|
|
26
|
+
const PATTERN_IK = 1;
|
|
27
|
+
// An invite pairs with the invite's secret (Noise XXpsk3).
|
|
28
|
+
const PATTERN_PAIR = 2;
|
|
29
|
+
// A knock pairs without an invite (plain Noise XX): only the pairing code,
|
|
30
|
+
// which the Node's application compares, tells the two ends that they
|
|
31
|
+
// reached each other.
|
|
32
|
+
const PATTERN_KNOCK = 3;
|
|
33
|
+
// While waiting for its pairing's answer the dialer sends empty messages,
|
|
34
|
+
// and this one once answered, after which the path carries records.
|
|
35
|
+
const PAIR_DONE = Uint8Array.of(1);
|
|
36
|
+
// FEATURE_STREAM_ENCODINGS is feature bit 0: streams of services 0 and 16
|
|
37
|
+
// to 255 carry an encoding byte, and may be compressed
|
|
38
|
+
// (spec/compression.md).
|
|
39
|
+
export const FEATURE_STREAM_ENCODINGS = 1n;
|
|
40
|
+
// localFeatures are the features a side states in its handshakes: the
|
|
41
|
+
// stream encodings feature only with a codec registered.
|
|
42
|
+
export function localFeatures(opts) {
|
|
43
|
+
return opts.compression ? FEATURE_STREAM_ENCODINGS : 0n;
|
|
44
|
+
}
|
|
45
|
+
// compressionModeOf is opts' mode, "auto" if it names none; any other
|
|
46
|
+
// value is refused.
|
|
47
|
+
export function compressionModeOf(opts) {
|
|
48
|
+
const m = opts.compressionMode ?? "auto";
|
|
49
|
+
if (m !== "auto" && m !== "off" && m !== "force")
|
|
50
|
+
throw new Error('keywire: compressionMode must be "auto", "off" or "force"');
|
|
51
|
+
return m;
|
|
52
|
+
}
|
|
53
|
+
// checkLoopbackView refuses a dialer's loopback view and proof
|
|
54
|
+
// (spec/compression.md, Loopback) that are not ones: a view stated without
|
|
55
|
+
// features, of another size than 36 bytes, or of an address that is not a
|
|
56
|
+
// loopback address; a proof of another size than 32 bytes, or without a
|
|
57
|
+
// view. This side states none: it does not know its socket, so it never
|
|
58
|
+
// counts a connection as loopback.
|
|
59
|
+
export function checkLoopbackView(view, features, proof = new Uint8Array(0)) {
|
|
60
|
+
if (proof.length !== 0 && (proof.length !== 32 || view.length === 0))
|
|
61
|
+
throw new Error("keywire: handshake failed: a malformed loopback proof");
|
|
62
|
+
if (view.length === 0)
|
|
63
|
+
return;
|
|
64
|
+
if (!features || view.length !== 36)
|
|
65
|
+
throw new Error("keywire: handshake failed: a malformed loopback view");
|
|
66
|
+
for (const at of [0, 18]) {
|
|
67
|
+
const ip = view.subarray(at, at + 16);
|
|
68
|
+
const v4 = ip.subarray(0, 10).every((b) => b === 0) && ip[10] === 0xff && ip[11] === 0xff;
|
|
69
|
+
const loopback = v4 ? ip[12] === 127 : ip.subarray(0, 15).every((b) => b === 0) && ip[15] === 1;
|
|
70
|
+
if (!loopback)
|
|
71
|
+
throw new Error("keywire: handshake failed: a loopback view not of loopback");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// limitsOf is opts's limits, else the defaults. A zero field takes its
|
|
75
|
+
// default; one that is negative or not an integer is refused, as are
|
|
76
|
+
// keepalives no more often than the idle timeout, a pairing keepalive no
|
|
77
|
+
// more often than its whole wait, or windows out of their range.
|
|
78
|
+
export function limitsOf(opts) {
|
|
79
|
+
resolveDomain(opts.domain);
|
|
80
|
+
if (!opts.limits)
|
|
81
|
+
return defaultLimits;
|
|
82
|
+
const l = { ...opts.limits };
|
|
83
|
+
for (const key of Object.keys(defaultLimits)) {
|
|
84
|
+
const v = l[key];
|
|
85
|
+
if (!Number.isSafeInteger(v) || v < 0)
|
|
86
|
+
throw new RangeError(`keywire: limits.${key} must be an integer of 0 or more`);
|
|
87
|
+
if (v === 0)
|
|
88
|
+
l[key] = defaultLimits[key];
|
|
89
|
+
}
|
|
90
|
+
if (l.keepAliveMs >= l.idleTimeoutMs || l.pairKeepAliveMs >= l.pairTimeoutMs)
|
|
91
|
+
throw new RangeError("keywire: keepAliveMs must be shorter than idleTimeoutMs, and pairKeepAliveMs than pairTimeoutMs");
|
|
92
|
+
if (l.streamWindow < STREAM_WINDOW ||
|
|
93
|
+
l.streamWindow > MAX_STREAM_WINDOW ||
|
|
94
|
+
l.connWindow < CONN_WINDOW ||
|
|
95
|
+
l.connWindow > MAX_CONN_WINDOW)
|
|
96
|
+
throw new RangeError("keywire: streamWindow must be from 1 MiB to 16 MiB, and connWindow from 4 MiB to 64 MiB");
|
|
97
|
+
return l;
|
|
98
|
+
}
|
|
99
|
+
// defaultLimits are the limits a zero Limits field takes.
|
|
100
|
+
export const defaultLimits = {
|
|
101
|
+
handshakeTimeoutMs: 10_000,
|
|
102
|
+
pairTimeoutMs: 120_000,
|
|
103
|
+
pairKeepAliveMs: 5_000,
|
|
104
|
+
maxStreams: 256,
|
|
105
|
+
acceptBacklog: 32,
|
|
106
|
+
// A path that goes silent is given up within seconds, so the client
|
|
107
|
+
// reconnects over another before the person notices.
|
|
108
|
+
keepAliveMs: 3_000,
|
|
109
|
+
idleTimeoutMs: 10_000,
|
|
110
|
+
stallTimeoutMs: 30_000,
|
|
111
|
+
streamWindow: STREAM_WINDOW,
|
|
112
|
+
connWindow: CONN_WINDOW,
|
|
113
|
+
writeQueue: WRITE_QUEUE,
|
|
114
|
+
compressionMemory: COMPRESSION_MEMORY,
|
|
115
|
+
compressionIdleMs: COMPRESSION_IDLE_MS,
|
|
116
|
+
extensionStreams: 64,
|
|
117
|
+
};
|
|
118
|
+
// MAX_PAIR_DATA bounds PairOptions.data, in bytes.
|
|
119
|
+
export const MAX_PAIR_DATA = 1024;
|
|
120
|
+
// prologue binds a handshake between devices: "keywire" 0x00 | version |
|
|
121
|
+
// pattern | network id | app length | app.
|
|
122
|
+
export function prologue(app, network, pattern = PATTERN_IK) {
|
|
123
|
+
if (!validApp(app))
|
|
124
|
+
throw new Error("keywire: app must be 1 to 64 bytes of a-z, 0-9, ., - or _");
|
|
125
|
+
const id = new TextEncoder().encode(app);
|
|
126
|
+
return concat(relayPrologue(network, pattern), Uint8Array.of(id.length), id);
|
|
127
|
+
}
|
|
128
|
+
// relayPrologue binds a handshake with a relay, which serves every
|
|
129
|
+
// application: "keywire" 0x00 | version | pattern | network id.
|
|
130
|
+
export function relayPrologue(network, pattern) {
|
|
131
|
+
return concat(new TextEncoder().encode("keywire"), Uint8Array.of(0, WIRE_VERSION, pattern), network);
|
|
132
|
+
}
|
|
133
|
+
// Data in flight is bounded by the connection window, at most
|
|
134
|
+
// MAX_CONN_WINDOW; beyond that, control frames and record overhead get a
|
|
135
|
+
// fixed allowance. More queued in either direction means the connection is
|
|
136
|
+
// stuck or abused.
|
|
137
|
+
export const MAX_BUFFERED = MAX_CONN_WINDOW + (1 << 20);
|
|
138
|
+
export function checkCredentials(credentials = []) {
|
|
139
|
+
if (credentials.length > maxCredentials)
|
|
140
|
+
throw new Error("keywire: too many credentials");
|
|
141
|
+
}
|
|
142
|
+
function credentialPayload(credentials = [], features = 0n) {
|
|
143
|
+
checkCredentials(credentials);
|
|
144
|
+
return credentials.length || features
|
|
145
|
+
? toBinary(CredentialsSchema, create(CredentialsSchema, { records: credentials, features }))
|
|
146
|
+
: EMPTY;
|
|
147
|
+
}
|
|
148
|
+
export async function dial(url, opts) {
|
|
149
|
+
checkUrl(url);
|
|
150
|
+
if (opts.network.length !== 32)
|
|
151
|
+
throw new Error("keywire: network id must be 32 bytes");
|
|
152
|
+
checkCredentials(opts.credentials);
|
|
153
|
+
const limits = limitsOf(opts);
|
|
154
|
+
const path = await WsPath.open(url, MAX_BUFFERED, limits.handshakeTimeoutMs, opts.signal);
|
|
155
|
+
return dialPath(path, opts, url);
|
|
156
|
+
}
|
|
157
|
+
// dialPath runs IK over any bounded message path; where names it in errors.
|
|
158
|
+
export async function dialPath(path, opts, where = "peer") {
|
|
159
|
+
if (opts.network.length !== 32)
|
|
160
|
+
throw new Error("keywire: network id must be 32 bytes");
|
|
161
|
+
const limits = limitsOf(opts);
|
|
162
|
+
const timer = setTimeout(() => path.close(), limits.handshakeTimeoutMs);
|
|
163
|
+
const abort = () => path.close();
|
|
164
|
+
opts.signal?.addEventListener("abort", abort, { once: true });
|
|
165
|
+
try {
|
|
166
|
+
opts.signal?.throwIfAborted();
|
|
167
|
+
const mode = compressionModeOf(opts);
|
|
168
|
+
const features = localFeatures(opts);
|
|
169
|
+
const payload = credentialPayload(opts.credentials, features);
|
|
170
|
+
const hs = await Handshake.create({
|
|
171
|
+
pattern: "IK",
|
|
172
|
+
initiator: true,
|
|
173
|
+
prologue: prologue(opts.app, opts.network),
|
|
174
|
+
static: opts.keypair,
|
|
175
|
+
remoteStatic: opts.remote,
|
|
176
|
+
});
|
|
177
|
+
path.send(concat(Uint8Array.of(WIRE_VERSION, PATTERN_IK), await hs.writeMessage(payload)));
|
|
178
|
+
// The responder's second message carries its features, if we stated
|
|
179
|
+
// ours and it has any; otherwise nothing.
|
|
180
|
+
const reply = await hs.readMessage(await path.read());
|
|
181
|
+
let remote = 0n;
|
|
182
|
+
if (reply.length !== 0) {
|
|
183
|
+
if (!features)
|
|
184
|
+
throw new Error("keywire: handshake failed: payload in message 2");
|
|
185
|
+
const r = decode(HandshakeReplySchema, reply);
|
|
186
|
+
// This side stated no loopback view: a reply's view, or proof,
|
|
187
|
+
// answers none.
|
|
188
|
+
if (r.loopback.length !== 0 || r.loopbackProof.length !== 0)
|
|
189
|
+
throw new Error("keywire: handshake failed: a loopback view not asked for");
|
|
190
|
+
remote = r.features;
|
|
191
|
+
}
|
|
192
|
+
clearTimeout(timer);
|
|
193
|
+
opts.signal?.throwIfAborted();
|
|
194
|
+
return new Conn(path, hs, opts.remote, limits, true, false, undefined, features & remote, opts.compression, mode);
|
|
195
|
+
}
|
|
196
|
+
catch (e) {
|
|
197
|
+
clearTimeout(timer);
|
|
198
|
+
path.close();
|
|
199
|
+
throw failedWith(`keywire: handshake with ${where} failed: ${e.message}`, e);
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
opts.signal?.removeEventListener("abort", abort);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// acceptPath answers IK (pattern 0x01) over path from a device the roster
|
|
206
|
+
// admits, owner or member, as Go's acceptIK; anything else gets no reply
|
|
207
|
+
// and closes the path. The connection is the dialer's: this side accepted
|
|
208
|
+
// it.
|
|
209
|
+
export async function acceptPath(path, opts) {
|
|
210
|
+
if (opts.network.length !== 32)
|
|
211
|
+
throw new Error("keywire: network id must be 32 bytes");
|
|
212
|
+
if (resolveDomain(opts.domain) !== opts.roster.domain)
|
|
213
|
+
throw new Error("keywire: roster domain mismatch");
|
|
214
|
+
const limits = limitsOf(opts);
|
|
215
|
+
const timer = setTimeout(() => path.close(), limits.handshakeTimeoutMs);
|
|
216
|
+
const abort = () => path.close();
|
|
217
|
+
opts.signal?.addEventListener("abort", abort, { once: true });
|
|
218
|
+
try {
|
|
219
|
+
opts.signal?.throwIfAborted();
|
|
220
|
+
const mode = compressionModeOf(opts);
|
|
221
|
+
const first = await path.read();
|
|
222
|
+
if (first.length < 2 || first[0] !== WIRE_VERSION || first[1] !== PATTERN_IK)
|
|
223
|
+
throw new Error("unsupported version or pattern");
|
|
224
|
+
const hs = await Handshake.create({
|
|
225
|
+
pattern: "IK",
|
|
226
|
+
initiator: false,
|
|
227
|
+
prologue: prologue(opts.app, opts.network),
|
|
228
|
+
static: opts.keypair,
|
|
229
|
+
});
|
|
230
|
+
const payload = await hs.readMessage(first.subarray(2));
|
|
231
|
+
const remote = hs.remoteStatic;
|
|
232
|
+
if (opts.remote && !equalBytes(remote, opts.remote))
|
|
233
|
+
throw new Error("unexpected static key");
|
|
234
|
+
const now = Date.now();
|
|
235
|
+
// A payload is decoded whether or not the dialer is admitted: it
|
|
236
|
+
// carries the dialer's features too.
|
|
237
|
+
const credentials = payload.length !== 0 ? decode(CredentialsSchema, payload) : undefined;
|
|
238
|
+
if (credentials) {
|
|
239
|
+
checkCredentials(credentials.records);
|
|
240
|
+
checkLoopbackView(credentials.loopback, credentials.features, credentials.loopbackProof);
|
|
241
|
+
}
|
|
242
|
+
if (!opts.roster.admitted(remote, now) && credentials)
|
|
243
|
+
opts.roster.apply(credentials.records, now);
|
|
244
|
+
if (!opts.roster.admitted(remote, Date.now()))
|
|
245
|
+
throw new Error("dialer not admitted");
|
|
246
|
+
// The reply states this side's features only to a dialer that stated
|
|
247
|
+
// its own: one that did not may refuse any payload.
|
|
248
|
+
const theirs = credentials?.features ?? 0n;
|
|
249
|
+
const ours = localFeatures(opts);
|
|
250
|
+
const reply = theirs && ours
|
|
251
|
+
? toBinary(HandshakeReplySchema, create(HandshakeReplySchema, { features: ours }))
|
|
252
|
+
: EMPTY;
|
|
253
|
+
path.send(await hs.writeMessage(reply));
|
|
254
|
+
clearTimeout(timer);
|
|
255
|
+
opts.signal?.throwIfAborted();
|
|
256
|
+
return new Conn(path, hs, copyBytes(remote), limits, false, false, undefined, theirs & ours, opts.compression, mode);
|
|
257
|
+
}
|
|
258
|
+
catch (e) {
|
|
259
|
+
clearTimeout(timer);
|
|
260
|
+
path.close();
|
|
261
|
+
throw new Error(`keywire: accepting a connection failed: ${e.message}`);
|
|
262
|
+
}
|
|
263
|
+
finally {
|
|
264
|
+
opts.signal?.removeEventListener("abort", abort);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const equalBytes = (a, b) => a.length === b.length && a.every((x, i) => x === b[i]);
|
|
268
|
+
export function checkPairOptions(opts) {
|
|
269
|
+
if (opts.network.length !== 32)
|
|
270
|
+
throw new Error("keywire: network id must be 32 bytes");
|
|
271
|
+
if (!opts.name.isWellFormed() || !validName(new TextEncoder().encode(opts.name)))
|
|
272
|
+
throw new Error("keywire: invalid device name (1-64 UTF-8 bytes, no control or format characters, no white space at either end)");
|
|
273
|
+
if (opts.data !== undefined && opts.data.length > MAX_PAIR_DATA)
|
|
274
|
+
throw new Error(`keywire: pairing data must be at most ${MAX_PAIR_DATA} bytes`);
|
|
275
|
+
if (opts.signer) {
|
|
276
|
+
const key = opts.signer.publicKey;
|
|
277
|
+
if (key.length !== 32 || !validSigningKey(key))
|
|
278
|
+
throw new Error("keywire: the signer's public key is not a valid signing key");
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// signingKeyMessage is what a pairing dialer signs to prove it holds the
|
|
282
|
+
// signing key it states: "<domain> signing key v1" 0x00 | the handshake
|
|
283
|
+
// hash after message 2 | its static key.
|
|
284
|
+
export function signingKeyMessage(binding, local, domain) {
|
|
285
|
+
return concat(new TextEncoder().encode(`${resolveDomain(domain)} signing key v1\x00`), binding, local);
|
|
286
|
+
}
|
|
287
|
+
// pairPath pairs over any bounded message path; where names it in errors.
|
|
288
|
+
export async function pairPath(path, opts, where) {
|
|
289
|
+
const signing = opts.signer?.publicKey;
|
|
290
|
+
const limits = limitsOf(opts);
|
|
291
|
+
const pattern = opts.secret ? PATTERN_PAIR : PATTERN_KNOCK;
|
|
292
|
+
let timer = setTimeout(() => path.close(), limits.handshakeTimeoutMs);
|
|
293
|
+
let keepAlive;
|
|
294
|
+
let timedOut = false;
|
|
295
|
+
const { signal } = opts;
|
|
296
|
+
const abort = () => path.close();
|
|
297
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
298
|
+
try {
|
|
299
|
+
signal?.throwIfAborted();
|
|
300
|
+
const hs = await Handshake.create({
|
|
301
|
+
pattern: opts.secret ? "XXpsk3" : "XX",
|
|
302
|
+
initiator: true,
|
|
303
|
+
prologue: prologue(opts.app, opts.network, pattern),
|
|
304
|
+
static: opts.keypair,
|
|
305
|
+
...(opts.secret && { psk: opts.secret }),
|
|
306
|
+
});
|
|
307
|
+
const empty = new Uint8Array(0);
|
|
308
|
+
path.send(concat(Uint8Array.of(WIRE_VERSION, pattern), await hs.writeMessage(empty)));
|
|
309
|
+
if ((await hs.readMessage(await path.read())).length !== 0)
|
|
310
|
+
throw new Error("keywire: handshake failed: payload in message 2");
|
|
311
|
+
const rs = hs.remoteStatic;
|
|
312
|
+
if (!rs || rs.length !== opts.remote.length || !rs.every((b, i) => b === opts.remote[i])) {
|
|
313
|
+
throw new Error("responder is not the invited device");
|
|
314
|
+
}
|
|
315
|
+
const features = localFeatures(opts);
|
|
316
|
+
const request = toBinary(PairRequestSchema, create(PairRequestSchema, {
|
|
317
|
+
features,
|
|
318
|
+
kind: PairKind.DEVICE,
|
|
319
|
+
name: opts.name,
|
|
320
|
+
data: opts.data ?? EMPTY,
|
|
321
|
+
...(opts.signer &&
|
|
322
|
+
signing && {
|
|
323
|
+
signingKey: signing,
|
|
324
|
+
signingProof: await opts.signer.sign(signingKeyMessage(hs.binding, opts.keypair.publicKey, opts.domain)),
|
|
325
|
+
}),
|
|
326
|
+
}));
|
|
327
|
+
path.send(await hs.writeMessage(request));
|
|
328
|
+
clearTimeout(timer);
|
|
329
|
+
timer = setTimeout(() => {
|
|
330
|
+
timedOut = true;
|
|
331
|
+
path.close();
|
|
332
|
+
}, limits.pairTimeoutMs);
|
|
333
|
+
keepAlive = setInterval(() => {
|
|
334
|
+
try {
|
|
335
|
+
path.send(empty);
|
|
336
|
+
}
|
|
337
|
+
catch {
|
|
338
|
+
// The pending read reports the failed path.
|
|
339
|
+
}
|
|
340
|
+
}, limits.pairKeepAliveMs);
|
|
341
|
+
const code = await pairingCode(hs.hash);
|
|
342
|
+
opts.onCode?.(code);
|
|
343
|
+
const ack = await path.read();
|
|
344
|
+
// The Node accepted: an abort no longer undoes the pairing.
|
|
345
|
+
signal?.removeEventListener("abort", abort);
|
|
346
|
+
clearInterval(keepAlive);
|
|
347
|
+
const payload = await hs.ciphers().recv.decryptWithAd(empty, ack);
|
|
348
|
+
const accepted = decode(PairAcceptedSchema, payload);
|
|
349
|
+
const { credentials } = accepted;
|
|
350
|
+
checkPairCredentials(opts.network, credentials, opts.keypair.publicKey, signing, opts.domain);
|
|
351
|
+
// A responder states its features only to a dialer that stated some.
|
|
352
|
+
if (accepted.features && !features) {
|
|
353
|
+
throw new Error("keywire: handshake failed: features to a dialer that stated none");
|
|
354
|
+
}
|
|
355
|
+
clearTimeout(timer);
|
|
356
|
+
path.send(PAIR_DONE);
|
|
357
|
+
const conn = new Conn(path, hs, opts.remote, limits, true, false, undefined, features & accepted.features, opts.compression, compressionModeOf(opts));
|
|
358
|
+
return { conn, credentials };
|
|
359
|
+
}
|
|
360
|
+
catch (e) {
|
|
361
|
+
clearTimeout(timer);
|
|
362
|
+
clearInterval(keepAlive);
|
|
363
|
+
signal?.removeEventListener("abort", abort);
|
|
364
|
+
path.close();
|
|
365
|
+
if (signal?.aborted)
|
|
366
|
+
throw signal.reason;
|
|
367
|
+
const reason = timedOut ? "pairing confirmation timed out" : e.message;
|
|
368
|
+
throw failedWith(`keywire: pairing with ${where} failed: ${reason}`, e);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
// Conn is an authenticated, encrypted connection to one peer.
|
|
372
|
+
export class Conn {
|
|
373
|
+
peer;
|
|
374
|
+
// features are the bits both sides stated in the handshake
|
|
375
|
+
// (FEATURE_STREAM_ENCODINGS).
|
|
376
|
+
features;
|
|
377
|
+
// loopback says the handshake established the connection as end-to-end
|
|
378
|
+
// loopback (spec/compression.md, Loopback): never, in TypeScript, which
|
|
379
|
+
// does not know its socket.
|
|
380
|
+
loopback = false;
|
|
381
|
+
#session;
|
|
382
|
+
#records;
|
|
383
|
+
#ciphers;
|
|
384
|
+
#receiving = { busy: true };
|
|
385
|
+
#away = Promise.withResolvers();
|
|
386
|
+
#onSettle;
|
|
387
|
+
#settling = false;
|
|
388
|
+
// saved, if given, is the session state the connection goes on from.
|
|
389
|
+
constructor(path, hs, peer, limits, dialer = true, refuseApplication = false, saved, features = 0n, compression, compressionMode = "auto") {
|
|
390
|
+
this.peer = peer;
|
|
391
|
+
this.features = features;
|
|
392
|
+
this.#ciphers = hs.ciphers();
|
|
393
|
+
const { send, recv } = this.#ciphers;
|
|
394
|
+
const records = new SecureRecords(path, send, () => this.#settle());
|
|
395
|
+
this.#records = records;
|
|
396
|
+
this.#session = new Session(records, {
|
|
397
|
+
dialer,
|
|
398
|
+
maxStreams: limits.maxStreams,
|
|
399
|
+
acceptBacklog: limits.acceptBacklog,
|
|
400
|
+
keepAliveMs: limits.keepAliveMs,
|
|
401
|
+
timeoutMs: limits.idleTimeoutMs,
|
|
402
|
+
stallTimeoutMs: limits.stallTimeoutMs,
|
|
403
|
+
serviceTimeoutMs: SERVICE_TIMEOUT_MS,
|
|
404
|
+
refuseApplication,
|
|
405
|
+
streamWindow: limits.streamWindow,
|
|
406
|
+
connWindow: limits.connWindow,
|
|
407
|
+
writeQueue: limits.writeQueue,
|
|
408
|
+
...(compression && (features & FEATURE_STREAM_ENCODINGS) !== 0n && { compression }),
|
|
409
|
+
// Never loopback here: only "off" compresses nothing.
|
|
410
|
+
plainWrites: compressionMode === "off",
|
|
411
|
+
compressionMemory: limits.compressionMemory,
|
|
412
|
+
compressionIdleMs: limits.compressionIdleMs,
|
|
413
|
+
onGoAway: () => this.#away.resolve(),
|
|
414
|
+
}, saved);
|
|
415
|
+
sessions.set(this, this.#session);
|
|
416
|
+
records.onError = (err) => this.#session.fail(err);
|
|
417
|
+
path.onError = (err) => this.#session.fail(new SessionClosed(err.message));
|
|
418
|
+
void receive(path, recv, this.#session, this.#receiving, () => this.#settle());
|
|
419
|
+
}
|
|
420
|
+
// restore goes on with a connection saved over path, whose peer has
|
|
421
|
+
// carried on with it all along.
|
|
422
|
+
static async restore(path, saved, peer, limits, dialer, refuseApplication = false) {
|
|
423
|
+
const send = await CipherState.restore(saved.send);
|
|
424
|
+
const recv = await CipherState.restore(saved.recv);
|
|
425
|
+
const ciphers = () => ({ send, recv });
|
|
426
|
+
return new Conn(path, { ciphers }, peer, limits, dialer, refuseApplication, saved.session);
|
|
427
|
+
}
|
|
428
|
+
// save returns the connection's state while it is idle: no streams,
|
|
429
|
+
// nothing received unread or to send. Its ciphers must have been made to
|
|
430
|
+
// be kept.
|
|
431
|
+
save() {
|
|
432
|
+
if (this.#receiving.busy || !this.#records.idle)
|
|
433
|
+
return undefined;
|
|
434
|
+
const session = this.#session.save();
|
|
435
|
+
if (!session)
|
|
436
|
+
return undefined;
|
|
437
|
+
return {
|
|
438
|
+
send: this.#ciphers.send.save(),
|
|
439
|
+
recv: this.#ciphers.recv.save(),
|
|
440
|
+
session,
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
// onSettle hears, soon after, that the connection received or sent: the
|
|
444
|
+
// moment to save it, if it is idle.
|
|
445
|
+
set onSettle(fn) {
|
|
446
|
+
this.#onSettle = fn;
|
|
447
|
+
}
|
|
448
|
+
#settle() {
|
|
449
|
+
if (!this.#onSettle || this.#settling)
|
|
450
|
+
return;
|
|
451
|
+
this.#settling = true;
|
|
452
|
+
queueMicrotask(() => {
|
|
453
|
+
this.#settling = false;
|
|
454
|
+
this.#onSettle?.();
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
// silence is how long the peer has sent nothing, in ms.
|
|
458
|
+
silence() {
|
|
459
|
+
return this.#session.silence();
|
|
460
|
+
}
|
|
461
|
+
// openStream opens an application stream; options.compress asks for it
|
|
462
|
+
// to be compressed.
|
|
463
|
+
openStream(options) {
|
|
464
|
+
const st = this.#session.openServiceStream(0, options?.compress === true);
|
|
465
|
+
st.peer = this.peer;
|
|
466
|
+
return st;
|
|
467
|
+
}
|
|
468
|
+
async acceptStream() {
|
|
469
|
+
const st = await this.#session.acceptStream();
|
|
470
|
+
st.peer = this.peer;
|
|
471
|
+
return st;
|
|
472
|
+
}
|
|
473
|
+
// goAway stops opening streams on the connection and asks the peer to
|
|
474
|
+
// stop too; open streams carry on, and the connection ends once they do.
|
|
475
|
+
goAway() {
|
|
476
|
+
this.#session.goAway();
|
|
477
|
+
this.#away.resolve();
|
|
478
|
+
}
|
|
479
|
+
get goingAway() {
|
|
480
|
+
return this.#session.goingAway;
|
|
481
|
+
}
|
|
482
|
+
// wentAway resolves once either side has gone away.
|
|
483
|
+
get wentAway() {
|
|
484
|
+
return this.#away.promise;
|
|
485
|
+
}
|
|
486
|
+
close() {
|
|
487
|
+
this.#session.close();
|
|
488
|
+
}
|
|
489
|
+
rtt() {
|
|
490
|
+
return this.#session.rtt();
|
|
491
|
+
}
|
|
492
|
+
// streams counts the connection's open streams.
|
|
493
|
+
streams() {
|
|
494
|
+
return this.#session.streams;
|
|
495
|
+
}
|
|
496
|
+
probe(timeoutMs) {
|
|
497
|
+
return this.#session.probe(timeoutMs);
|
|
498
|
+
}
|
|
499
|
+
// closed resolves with the reason once the connection ends.
|
|
500
|
+
get closed() {
|
|
501
|
+
return this.#session.closed;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
// receive decrypts messages that have already arrived concurrently (nonces
|
|
505
|
+
// are taken in arrival order) and hands them to the session in order. It is
|
|
506
|
+
// busy but while it waits with nothing unread.
|
|
507
|
+
async function receive(path, recv, session, receiving, settle) {
|
|
508
|
+
const decrypt = (msg) => {
|
|
509
|
+
if (msg.length > MAX_MESSAGE)
|
|
510
|
+
throw new Error("keywire: message exceeds limit");
|
|
511
|
+
return recv.decryptWithAd(EMPTY, msg);
|
|
512
|
+
};
|
|
513
|
+
try {
|
|
514
|
+
for (;;) {
|
|
515
|
+
let first = path.tryRead();
|
|
516
|
+
if (!first) {
|
|
517
|
+
receiving.busy = false;
|
|
518
|
+
settle();
|
|
519
|
+
first = await path.read();
|
|
520
|
+
receiving.busy = true;
|
|
521
|
+
}
|
|
522
|
+
const batch = [decrypt(first)];
|
|
523
|
+
while (batch.length < 16) {
|
|
524
|
+
const m = path.tryRead();
|
|
525
|
+
if (!m)
|
|
526
|
+
break;
|
|
527
|
+
batch.push(decrypt(m));
|
|
528
|
+
}
|
|
529
|
+
for (const p of batch)
|
|
530
|
+
session.receive(await p);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
catch (e) {
|
|
534
|
+
session.fail(e);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
// SecureRecords encrypts each record as soon as it is written: WebCrypto
|
|
538
|
+
// copies the input synchronously, so the caller may reuse the buffer, and
|
|
539
|
+
// nonces follow call order. Sends wait for encryptions in that same order.
|
|
540
|
+
class SecureRecords {
|
|
541
|
+
#path;
|
|
542
|
+
#send;
|
|
543
|
+
#sent;
|
|
544
|
+
#queue = [];
|
|
545
|
+
#queued = 0;
|
|
546
|
+
#running = false;
|
|
547
|
+
#closed = false;
|
|
548
|
+
onError = () => { };
|
|
549
|
+
constructor(path, send, sent) {
|
|
550
|
+
this.#path = path;
|
|
551
|
+
this.#send = send;
|
|
552
|
+
this.#sent = sent;
|
|
553
|
+
}
|
|
554
|
+
// idle says every record written has been sent.
|
|
555
|
+
get idle() {
|
|
556
|
+
return this.#queue.length === 0 && !this.#running;
|
|
557
|
+
}
|
|
558
|
+
write(record) {
|
|
559
|
+
if (this.#closed)
|
|
560
|
+
return;
|
|
561
|
+
this.#queued += record.length;
|
|
562
|
+
if (this.#queued + this.#path.pending > MAX_BUFFERED) {
|
|
563
|
+
this.onError(new Error("keywire: send queue limit exceeded"));
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
this.#queue.push(this.#send.encryptWithAd(EMPTY, record));
|
|
567
|
+
if (!this.#running)
|
|
568
|
+
void this.#drain();
|
|
569
|
+
}
|
|
570
|
+
close() {
|
|
571
|
+
this.#closed = true;
|
|
572
|
+
this.#queue = [];
|
|
573
|
+
this.#path.close();
|
|
574
|
+
}
|
|
575
|
+
async #drain() {
|
|
576
|
+
this.#running = true;
|
|
577
|
+
try {
|
|
578
|
+
for (let p = this.#queue.shift(); p && !this.#closed; p = this.#queue.shift()) {
|
|
579
|
+
const ct = await p;
|
|
580
|
+
this.#queued -= ct.length - 16;
|
|
581
|
+
if (!this.#closed)
|
|
582
|
+
this.#path.send(ct);
|
|
583
|
+
this.#sent();
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
catch (e) {
|
|
587
|
+
this.onError(e);
|
|
588
|
+
}
|
|
589
|
+
finally {
|
|
590
|
+
this.#running = false;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
const EMPTY = new Uint8Array(0);
|
|
595
|
+
const SERVICE_TIMEOUT_MS = 5_000;
|
|
596
|
+
const sessions = new WeakMap();
|
|
597
|
+
const guests = new WeakSet();
|
|
598
|
+
export function markGuest(conn) {
|
|
599
|
+
guests.add(conn);
|
|
600
|
+
}
|
|
601
|
+
export function openServiceStream(conn, service) {
|
|
602
|
+
if (!Number.isInteger(service) || service < 0 || service > 255)
|
|
603
|
+
throw new Error("keywire: invalid service byte");
|
|
604
|
+
if (guests.has(conn) && service !== 4 && service !== 8)
|
|
605
|
+
throw new Error("keywire: guest connection cannot open Network services");
|
|
606
|
+
return sessions.get(conn).openServiceStream(service);
|
|
607
|
+
}
|
|
608
|
+
export function registerService(conn, service, handler) {
|
|
609
|
+
if (guests.has(conn))
|
|
610
|
+
throw new Error("keywire: guest connection cannot register services");
|
|
611
|
+
sessions.get(conn).registerService(service, handler);
|
|
612
|
+
}
|
|
613
|
+
// Services FIRST_EXTENSION_SERVICE to LAST_EXTENSION_SERVICE are kept for
|
|
614
|
+
// extensions: protocols a module other than keywire's core serves on the
|
|
615
|
+
// connections between a Network's devices. Services below
|
|
616
|
+
// FIRST_EXTENSION_SERVICE are the core's (spec/mux.md).
|
|
617
|
+
export const FIRST_EXTENSION_SERVICE = 16;
|
|
618
|
+
export const LAST_EXTENSION_SERVICE = 255;
|
|
619
|
+
const extensions = new WeakMap();
|
|
620
|
+
function checkExtensionService(service) {
|
|
621
|
+
if (!Number.isInteger(service) ||
|
|
622
|
+
service < FIRST_EXTENSION_SERVICE ||
|
|
623
|
+
service > LAST_EXTENSION_SERVICE)
|
|
624
|
+
throw new RangeError(`keywire: an extension service is ${FIRST_EXTENSION_SERVICE} to ${LAST_EXTENSION_SERVICE}`);
|
|
625
|
+
}
|
|
626
|
+
// serveExtension has conn hand every stream of service its peer opens to
|
|
627
|
+
// handler, which owns it. service is an extension service, claimed once a
|
|
628
|
+
// connection; guest connections take none. The extension ends with the
|
|
629
|
+
// connection (conn.closed).
|
|
630
|
+
export function serveExtension(conn, service, handler) {
|
|
631
|
+
checkExtensionService(service);
|
|
632
|
+
if (guests.has(conn))
|
|
633
|
+
throw new Error("keywire: guest connections take no extension");
|
|
634
|
+
const claimed = extensions.get(conn) ?? new Set();
|
|
635
|
+
extensions.set(conn, claimed);
|
|
636
|
+
if (claimed.has(service))
|
|
637
|
+
throw new Error(`keywire: extension service ${service} is claimed twice`);
|
|
638
|
+
claimed.add(service);
|
|
639
|
+
sessions.get(conn).registerService(service, handler);
|
|
640
|
+
}
|
|
641
|
+
// openExtensionStream opens a stream of an extension service conn serves.
|
|
642
|
+
export function openExtensionStream(conn, service, options) {
|
|
643
|
+
checkExtensionService(service);
|
|
644
|
+
if (!extensions.get(conn)?.has(service))
|
|
645
|
+
throw new KeywireError("not-extension", `service ${service}`);
|
|
646
|
+
return sessions.get(conn).openServiceStream(service, options?.compress === true);
|
|
647
|
+
}
|
|
648
|
+
// serveGuestExtensions has conn, a guest's connection to a Node made with
|
|
649
|
+
// a share, hand every extension stream the Node opens to handler, with its
|
|
650
|
+
// service. It is called once, as the connection is made.
|
|
651
|
+
export function serveGuestExtensions(conn, handler) {
|
|
652
|
+
if (!guests.has(conn))
|
|
653
|
+
throw new Error("keywire: not a guest connection");
|
|
654
|
+
const session = sessions.get(conn);
|
|
655
|
+
for (let s = FIRST_EXTENSION_SERVICE; s <= LAST_EXTENSION_SERVICE; s++)
|
|
656
|
+
session.registerService(s, (stream) => handler(s, stream));
|
|
657
|
+
}
|
|
658
|
+
// openGuestExtensionStream opens a stream of an extension service on
|
|
659
|
+
// conn, a guest's connection to a Node.
|
|
660
|
+
export function openGuestExtensionStream(conn, service, options) {
|
|
661
|
+
checkExtensionService(service);
|
|
662
|
+
if (!guests.has(conn))
|
|
663
|
+
throw new Error("keywire: not a guest connection");
|
|
664
|
+
return sessions.get(conn).openServiceStream(service, options?.compress === true);
|
|
665
|
+
}
|
|
666
|
+
// checkPairCredentials refuses credentials from a pairing unless every
|
|
667
|
+
// record holds and they admit this device as a member, with the signing
|
|
668
|
+
// key it stated (none if undefined).
|
|
669
|
+
export function checkPairCredentials(network, credentials, local, signing, domain) {
|
|
670
|
+
let roster;
|
|
671
|
+
try {
|
|
672
|
+
roster = freshRoster(network, credentials, Date.now(), domain);
|
|
673
|
+
}
|
|
674
|
+
catch {
|
|
675
|
+
throw new Error("invalid pairing credentials");
|
|
676
|
+
}
|
|
677
|
+
if (roster.admitted(local, Date.now())?.role !== "member")
|
|
678
|
+
throw new Error("pairing credentials do not admit this device");
|
|
679
|
+
const granted = roster.members(Date.now()).find((m) => equalBytes(m.noiseKey, local))?.signingKey;
|
|
680
|
+
if (!(granted === undefined
|
|
681
|
+
? signing === undefined
|
|
682
|
+
: signing !== undefined && equalBytes(granted, signing)))
|
|
683
|
+
throw new Error("pairing credentials do not carry this device's signing key");
|
|
684
|
+
}
|
|
685
|
+
//# sourceMappingURL=conn.js.map
|