@netifyjs/netify 1.0.0-alpha.4 → 1.0.0-alpha.6
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/README.md +6 -6
- package/package.json +1 -1
- package/src/bootstrap/client/index.ts +1 -1
- package/src/bootstrap/dispatcher/index.ts +7 -0
- package/src/bootstrap/reconnect/index.ts +14 -4
- package/src/bootstrap/server/index.ts +10 -4
- package/src/buffer/index.ts +230 -89
- package/src/buffer/quantizer/index.ts +16 -3
- package/src/channel/context/index.ts +18 -11
- package/src/channel/handler/index.ts +1 -1
- package/src/channel/index.ts +30 -7
- package/src/channel/outbound/index.ts +28 -12
- package/src/channel/pipeline/index.ts +45 -5
- package/src/channel/queue/index.ts +3 -2
- package/src/channel/resolved/index.ts +13 -3
- package/src/channel/server/index.ts +19 -10
- package/src/cluster/index.ts +73 -32
- package/src/codec/binary/index.ts +19 -0
- package/src/codec/decoder/index.ts +223 -83
- package/src/codec/delimiter/index.ts +95 -37
- package/src/codec/encoder/index.ts +30 -5
- package/src/codec/fixed/index.ts +5 -1
- package/src/codec/http/aggregator/index.ts +45 -16
- package/src/codec/http/decoder/object/index.ts +156 -40
- package/src/codec/http/decoder/request/index.ts +6 -2
- package/src/codec/http/encoder/object/index.ts +2 -7
- package/src/codec/http/encoder/request/index.ts +5 -0
- package/src/codec/http/headers/index.ts +45 -2
- package/src/codec/http/message/full/request/index.ts +1 -1
- package/src/codec/http/message/full/response/index.ts +1 -1
- package/src/codec/http/message/request/index.ts +14 -1
- package/src/codec/http/message/response/index.ts +1 -1
- package/src/codec/http/version/index.ts +3 -0
- package/src/codec/length/index.ts +66 -44
- package/src/codec/message/decoder/index.ts +18 -1
- package/src/codec/message/encoder/index.ts +85 -8
- package/src/codec/prepender/index.ts +4 -1
- package/src/codec/string/decoder/index.ts +6 -1
- package/src/codec/websocket/decoder/index.ts +78 -3
- package/src/codec/websocket/encoder/index.ts +90 -6
- package/src/codec/websocket/frame/base/index.ts +19 -10
- package/src/codec/websocket/frame/text/index.ts +2 -2
- package/src/codec/websocket/protocol/index.ts +12 -0
- package/src/datagram/bootstrap/index.ts +1 -1
- package/src/datagram/transport/index.ts +30 -1
- package/src/handler/flush/index.ts +25 -5
- package/src/handler/idle/index.ts +120 -26
- package/src/handler/logging/index.ts +13 -1
- package/src/handler/metrics/index.ts +49 -16
- package/src/handler/offload/index.ts +7 -0
- package/src/handler/timeout/read/index.ts +1 -1
- package/src/handler/timeout/write/index.ts +104 -23
- package/src/handler/traffic/index.ts +251 -38
- package/src/index.ts +1 -0
- package/src/reliable/cipher/index.ts +49 -5
- package/src/reliable/cookie/index.ts +64 -2
- package/src/reliable/handler/index.ts +11 -15
- package/src/reliable/index.ts +7 -1
- package/src/reliable/lane/index.ts +277 -58
- package/src/reliable/peer/index.ts +125 -38
- package/src/serve/bootstrap/index.ts +82 -135
- package/src/serve/channel/index.ts +84 -2
- package/src/serve/gate/index.ts +215 -0
- package/src/serve/index.ts +6 -2
- package/src/serve/outbox/index.ts +63 -2
- package/src/serve/transport/index.ts +33 -3
- package/src/serve/writer/index.ts +4 -3
- package/src/shard/gateway/index.ts +102 -29
- package/src/shard/link/index.ts +15 -0
- package/src/worker/group/index.ts +231 -13
- package/src/worker/pool/index.ts +100 -19
- package/src/worker/service/index.ts +9 -1
|
@@ -51,6 +51,11 @@ const tagSize = 16
|
|
|
51
51
|
*/
|
|
52
52
|
const counterSize = 8
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* How far behind the newest a counter can land
|
|
56
|
+
*/
|
|
57
|
+
const replayWindow = 2048
|
|
58
|
+
|
|
54
59
|
/**
|
|
55
60
|
* AES-256-GCM for data packets, the header stays readable but is authenticated
|
|
56
61
|
* @class
|
|
@@ -81,6 +86,16 @@ export class PacketCipher {
|
|
|
81
86
|
*/
|
|
82
87
|
private readonly nonce: Buffer
|
|
83
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Highest counter opened, or -1
|
|
91
|
+
*/
|
|
92
|
+
private newest: number
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Counters opened behind the newest
|
|
96
|
+
*/
|
|
97
|
+
private readonly opened: Uint8Array
|
|
98
|
+
|
|
84
99
|
/**
|
|
85
100
|
* Usually built by PacketCipher.derive
|
|
86
101
|
* @param keys send and receive keys
|
|
@@ -90,6 +105,8 @@ export class PacketCipher {
|
|
|
90
105
|
this.receiveKey = keys.receive
|
|
91
106
|
this.counter = 0
|
|
92
107
|
this.nonce = Buffer.alloc(12)
|
|
108
|
+
this.newest = -1
|
|
109
|
+
this.opened = new Uint8Array(replayWindow)
|
|
93
110
|
}
|
|
94
111
|
|
|
95
112
|
/**
|
|
@@ -197,7 +214,7 @@ export class PacketCipher {
|
|
|
197
214
|
}
|
|
198
215
|
|
|
199
216
|
/**
|
|
200
|
-
*
|
|
217
|
+
* Opens a sealed packet's body, or null
|
|
201
218
|
* @param packet a sealed data packet
|
|
202
219
|
*/
|
|
203
220
|
public open(packet: Uint8Array): Uint8Array | null {
|
|
@@ -219,9 +236,36 @@ export class PacketCipher {
|
|
|
219
236
|
return null
|
|
220
237
|
}
|
|
221
238
|
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
239
|
+
const counter =
|
|
240
|
+
nonce.readUInt32BE(4) * 0x1_0000_0000 + nonce.readUInt32BE(8)
|
|
241
|
+
if (!this.admit(counter)) return null
|
|
242
|
+
|
|
243
|
+
return body
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Marks a counter used, false for a repeat
|
|
248
|
+
* @param counter the sender's packet counter
|
|
249
|
+
*/
|
|
250
|
+
private admit(counter: number): boolean {
|
|
251
|
+
const opened = this.opened
|
|
252
|
+
const newest = this.newest
|
|
253
|
+
const slot = counter & (replayWindow - 1)
|
|
254
|
+
if (counter <= newest) {
|
|
255
|
+
if (newest - counter >= replayWindow || opened[slot] === 1) return false
|
|
256
|
+
|
|
257
|
+
opened[slot] = 1
|
|
258
|
+
return true
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const first =
|
|
262
|
+
counter - newest > replayWindow ? counter - replayWindow : newest
|
|
263
|
+
for (let skipped = first + 1; skipped < counter; skipped++) {
|
|
264
|
+
opened[skipped & (replayWindow - 1)] = 0
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
opened[slot] = 1
|
|
268
|
+
this.newest = counter
|
|
269
|
+
return true
|
|
226
270
|
}
|
|
227
271
|
}
|
|
@@ -13,6 +13,11 @@ export class CookieJar {
|
|
|
13
13
|
*/
|
|
14
14
|
public static readonly lifetime = 30
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Used cookies remembered per stretch
|
|
18
|
+
*/
|
|
19
|
+
public static readonly capacity = 65_536
|
|
20
|
+
|
|
16
21
|
/**
|
|
17
22
|
* Random per server, so cookies can't be forged
|
|
18
23
|
*/
|
|
@@ -23,12 +28,30 @@ export class CookieJar {
|
|
|
23
28
|
*/
|
|
24
29
|
private readonly input: Buffer
|
|
25
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Cookies already used in this stretch
|
|
33
|
+
*/
|
|
34
|
+
private spent: Set<string>
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Cookies used in the stretch before
|
|
38
|
+
*/
|
|
39
|
+
private older: Set<string>
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Second the current stretch started at
|
|
43
|
+
*/
|
|
44
|
+
private turnedAt: number
|
|
45
|
+
|
|
26
46
|
/**
|
|
27
47
|
* Picks a fresh secret
|
|
28
48
|
*/
|
|
29
49
|
public constructor() {
|
|
30
50
|
this.secret = randomBytes(32)
|
|
31
51
|
this.input = Buffer.alloc(10)
|
|
52
|
+
this.spent = new Set<string>()
|
|
53
|
+
this.older = new Set<string>()
|
|
54
|
+
this.turnedAt = CookieJar.now()
|
|
32
55
|
}
|
|
33
56
|
|
|
34
57
|
/**
|
|
@@ -58,7 +81,43 @@ export class CookieJar {
|
|
|
58
81
|
}
|
|
59
82
|
|
|
60
83
|
/**
|
|
61
|
-
*
|
|
84
|
+
* Spends a cookie, false if full
|
|
85
|
+
* @param cookie the bytes a client sent back
|
|
86
|
+
*/
|
|
87
|
+
public spend(cookie: Uint8Array): boolean {
|
|
88
|
+
this.turn()
|
|
89
|
+
if (this.spent.size >= CookieJar.capacity) return false
|
|
90
|
+
|
|
91
|
+
this.spent.add(CookieJar.keyOf(cookie))
|
|
92
|
+
return true
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Forgets expired cookies
|
|
97
|
+
*/
|
|
98
|
+
private turn(): void {
|
|
99
|
+
const age = CookieJar.now() - this.turnedAt
|
|
100
|
+
if (age <= CookieJar.lifetime) return
|
|
101
|
+
|
|
102
|
+
this.older = age > 2 * CookieJar.lifetime ? new Set<string>() : this.spent
|
|
103
|
+
this.spent = new Set<string>()
|
|
104
|
+
this.turnedAt = CookieJar.now()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Cookie key for the spent sets
|
|
109
|
+
* @param cookie raw bytes
|
|
110
|
+
*/
|
|
111
|
+
private static keyOf(cookie: Uint8Array): string {
|
|
112
|
+
return Buffer.from(
|
|
113
|
+
cookie.buffer,
|
|
114
|
+
cookie.byteOffset,
|
|
115
|
+
cookie.byteLength,
|
|
116
|
+
).toString('latin1')
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Whether a returned cookie checks out
|
|
62
121
|
* @param address where the client is
|
|
63
122
|
* @param salt the client's handshake number
|
|
64
123
|
* @param time seconds the cookie claims it was issued at
|
|
@@ -73,6 +132,9 @@ export class CookieJar {
|
|
|
73
132
|
const age = CookieJar.now() - time
|
|
74
133
|
if (age < 0 || age > CookieJar.lifetime) return false
|
|
75
134
|
|
|
76
|
-
|
|
135
|
+
if (!timingSafeEqual(this.issue(address, salt, time), cookie)) return false
|
|
136
|
+
|
|
137
|
+
const key = CookieJar.keyOf(cookie)
|
|
138
|
+
return !this.spent.has(key) && !this.older.has(key)
|
|
77
139
|
}
|
|
78
140
|
}
|
|
@@ -73,7 +73,7 @@ export type SimulateOptions = {
|
|
|
73
73
|
*/
|
|
74
74
|
export type EncryptionOptions = {
|
|
75
75
|
/**
|
|
76
|
-
* The server's fixed keys, a fresh pair
|
|
76
|
+
* The server's fixed keys, or a fresh pair
|
|
77
77
|
*/
|
|
78
78
|
readonly keyPair?: KeyPair
|
|
79
79
|
|
|
@@ -632,8 +632,12 @@ export class ReliableHandler extends DuplexHandlerAdapter {
|
|
|
632
632
|
* @param context our place in the pipeline
|
|
633
633
|
*/
|
|
634
634
|
public override async close(context: HandlerContext): Promise<void> {
|
|
635
|
+
const now = performance.now()
|
|
636
|
+
|
|
635
637
|
for (const peer of this.registry.peers) {
|
|
636
|
-
if (peer.state
|
|
638
|
+
if (peer.state !== 'connected') continue
|
|
639
|
+
this.send(context, peer, now, true)
|
|
640
|
+
this.sendDisconnect(context, peer)
|
|
637
641
|
}
|
|
638
642
|
|
|
639
643
|
await context.flush()
|
|
@@ -729,10 +733,8 @@ export class ReliableHandler extends DuplexHandlerAdapter {
|
|
|
729
733
|
if (peer?.state !== 'connected') return
|
|
730
734
|
if (peer.session !== readUint32(bytes, 1)) return
|
|
731
735
|
|
|
732
|
-
const plain = peer.cipher === null ? bytes : peer.cipher.open(bytes)
|
|
733
|
-
if (plain === null) return
|
|
734
736
|
const messages: ReliableMessage[] = []
|
|
735
|
-
peer.receive(
|
|
737
|
+
peer.receive(bytes, now, messages)
|
|
736
738
|
if (peer.hasWork) this.schedule(peer)
|
|
737
739
|
else if (peer.owesAck) this.delayAck(context, peer)
|
|
738
740
|
|
|
@@ -828,6 +830,7 @@ export class ReliableHandler extends DuplexHandlerAdapter {
|
|
|
828
830
|
}
|
|
829
831
|
}
|
|
830
832
|
|
|
833
|
+
if (!this.cookies.spend(cookie)) return
|
|
831
834
|
return this.admit(context, peer, existing, now)
|
|
832
835
|
}
|
|
833
836
|
|
|
@@ -1026,7 +1029,6 @@ export class ReliableHandler extends DuplexHandlerAdapter {
|
|
|
1026
1029
|
const now = performance.now()
|
|
1027
1030
|
|
|
1028
1031
|
for (const peer of this.dirty) {
|
|
1029
|
-
if (peer.closing) continue
|
|
1030
1032
|
this.send(context, peer, now, false)
|
|
1031
1033
|
if (peer.owesAck) this.delayAck(context, peer)
|
|
1032
1034
|
}
|
|
@@ -1060,6 +1062,7 @@ export class ReliableHandler extends DuplexHandlerAdapter {
|
|
|
1060
1062
|
if (now - peer.lastReceivedAt > settings.timeout) {
|
|
1061
1063
|
gone.push([peer, 'timeout'])
|
|
1062
1064
|
} else if (peer.failed || peer.closing) {
|
|
1065
|
+
this.send(context, peer, now, true)
|
|
1063
1066
|
this.sendDisconnect(context, peer)
|
|
1064
1067
|
gone.push([peer, peer.failed ? 'resends' : 'local'])
|
|
1065
1068
|
} else if (peer.isDue(now)) {
|
|
@@ -1073,7 +1076,7 @@ export class ReliableHandler extends DuplexHandlerAdapter {
|
|
|
1073
1076
|
}
|
|
1074
1077
|
|
|
1075
1078
|
/**
|
|
1076
|
-
* Packs a peer's packets
|
|
1079
|
+
* Packs a peer's packets and writes them
|
|
1077
1080
|
* @param context our place in the pipeline
|
|
1078
1081
|
* @param peer the peer
|
|
1079
1082
|
* @param now current time in ms
|
|
@@ -1088,14 +1091,7 @@ export class ReliableHandler extends DuplexHandlerAdapter {
|
|
|
1088
1091
|
const outbox = this.outbox
|
|
1089
1092
|
peer.pack(now, outbox, force)
|
|
1090
1093
|
|
|
1091
|
-
const
|
|
1092
|
-
for (const bytes of outbox) {
|
|
1093
|
-
this.emit(
|
|
1094
|
-
context,
|
|
1095
|
-
cipher === null ? bytes : cipher.seal(bytes),
|
|
1096
|
-
peer.address,
|
|
1097
|
-
)
|
|
1098
|
-
}
|
|
1094
|
+
for (const bytes of outbox) this.emit(context, bytes, peer.address)
|
|
1099
1095
|
|
|
1100
1096
|
outbox.length = 0
|
|
1101
1097
|
}
|
package/src/reliable/index.ts
CHANGED
|
@@ -25,7 +25,13 @@ export type {
|
|
|
25
25
|
SimulateOptions,
|
|
26
26
|
} from './handler/index.ts'
|
|
27
27
|
export { Lane } from './lane/index.ts'
|
|
28
|
-
export type {
|
|
28
|
+
export type {
|
|
29
|
+
Deliver,
|
|
30
|
+
Delivery,
|
|
31
|
+
LaneLimits,
|
|
32
|
+
LaneOptions,
|
|
33
|
+
Outgoing,
|
|
34
|
+
} from './lane/index.ts'
|
|
29
35
|
export {
|
|
30
36
|
PeerConnected,
|
|
31
37
|
PeerDisconnected,
|