@netifyjs/netify 0.0.0-stage → 1.0.0-alpha.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 +105 -2
- package/package.json +65 -4
- package/src/bootstrap/client/index.ts +191 -0
- package/src/bootstrap/dispatcher/index.ts +204 -0
- package/src/bootstrap/index.ts +22 -0
- package/src/bootstrap/reconnect/index.ts +334 -0
- package/src/bootstrap/server/index.ts +284 -0
- package/src/buffer/bits/index.ts +2 -0
- package/src/buffer/bits/reader/index.ts +64 -0
- package/src/buffer/bits/writer/index.ts +79 -0
- package/src/buffer/index.ts +1375 -0
- package/src/buffer/pool/index.ts +46 -0
- package/src/buffer/quantizer/index.ts +101 -0
- package/src/channel/attribute/index.ts +89 -0
- package/src/channel/context/index.ts +621 -0
- package/src/channel/group/index.ts +256 -0
- package/src/channel/handler/duplex/index.ts +105 -0
- package/src/channel/handler/inbound/index.ts +78 -0
- package/src/channel/handler/index.ts +135 -0
- package/src/channel/handler/outbound/index.ts +33 -0
- package/src/channel/index.ts +619 -0
- package/src/channel/initializer/index.ts +77 -0
- package/src/channel/outbound/index.ts +611 -0
- package/src/channel/pipeline/head/index.ts +100 -0
- package/src/channel/pipeline/index.ts +414 -0
- package/src/channel/pipeline/tail/index.ts +45 -0
- package/src/channel/pipeline/tracker/index.ts +120 -0
- package/src/channel/queue/index.ts +203 -0
- package/src/channel/resolved/index.ts +34 -0
- package/src/channel/server/index.ts +164 -0
- package/src/channel/shutdown/index.ts +25 -0
- package/src/channel/simple/index.ts +38 -0
- package/src/channel/transport/index.ts +74 -0
- package/src/channel/transport/socket/index.ts +80 -0
- package/src/cluster/index.ts +360 -0
- package/src/codec/binary/index.ts +102 -0
- package/src/codec/decoder/index.ts +479 -0
- package/src/codec/delimiter/index.ts +189 -0
- package/src/codec/encoder/index.ts +48 -0
- package/src/codec/error/index.ts +33 -0
- package/src/codec/fixed/index.ts +45 -0
- package/src/codec/http/aggregator/index.ts +236 -0
- package/src/codec/http/client/index.ts +120 -0
- package/src/codec/http/decoder/index.ts +4 -0
- package/src/codec/http/decoder/object/index.ts +644 -0
- package/src/codec/http/decoder/request/index.ts +49 -0
- package/src/codec/http/decoder/response/index.ts +59 -0
- package/src/codec/http/encoder/index.ts +3 -0
- package/src/codec/http/encoder/object/index.ts +196 -0
- package/src/codec/http/encoder/request/index.ts +24 -0
- package/src/codec/http/encoder/response/index.ts +26 -0
- package/src/codec/http/headers/index.ts +287 -0
- package/src/codec/http/index.ts +33 -0
- package/src/codec/http/message/content/index.ts +36 -0
- package/src/codec/http/message/full/index.ts +10 -0
- package/src/codec/http/message/full/request/index.ts +45 -0
- package/src/codec/http/message/full/response/index.ts +43 -0
- package/src/codec/http/message/head/index.ts +79 -0
- package/src/codec/http/message/index.ts +16 -0
- package/src/codec/http/message/last/index.ts +27 -0
- package/src/codec/http/message/request/index.ts +39 -0
- package/src/codec/http/message/response/index.ts +31 -0
- package/src/codec/http/method/index.ts +101 -0
- package/src/codec/http/server/index.ts +81 -0
- package/src/codec/http/status/index.ts +170 -0
- package/src/codec/http/version/index.ts +70 -0
- package/src/codec/index.ts +83 -0
- package/src/codec/length/index.ts +330 -0
- package/src/codec/line/index.ts +45 -0
- package/src/codec/message/decoder/index.ts +84 -0
- package/src/codec/message/encoder/index.ts +56 -0
- package/src/codec/message/index.ts +2 -0
- package/src/codec/msgpack/index.ts +51 -0
- package/src/codec/prepender/index.ts +296 -0
- package/src/codec/protobuf/index.ts +57 -0
- package/src/codec/string/decoder/index.ts +47 -0
- package/src/codec/string/encoder/index.ts +26 -0
- package/src/codec/string/index.ts +2 -0
- package/src/codec/websocket/client/index.ts +235 -0
- package/src/codec/websocket/decoder/index.ts +371 -0
- package/src/codec/websocket/encoder/index.ts +170 -0
- package/src/codec/websocket/error/index.ts +42 -0
- package/src/codec/websocket/frame/base/index.ts +55 -0
- package/src/codec/websocket/frame/binary/index.ts +24 -0
- package/src/codec/websocket/frame/close/index.ts +82 -0
- package/src/codec/websocket/frame/continuation/index.ts +31 -0
- package/src/codec/websocket/frame/index.ts +10 -0
- package/src/codec/websocket/frame/opcode/index.ts +35 -0
- package/src/codec/websocket/frame/ping/index.ts +24 -0
- package/src/codec/websocket/frame/pong/index.ts +24 -0
- package/src/codec/websocket/frame/text/index.ts +31 -0
- package/src/codec/websocket/handshake/index.ts +38 -0
- package/src/codec/websocket/index.ts +25 -0
- package/src/codec/websocket/key/index.ts +41 -0
- package/src/codec/websocket/protocol/index.ts +147 -0
- package/src/codec/websocket/server/index.ts +233 -0
- package/src/codec/websocket/status/index.ts +97 -0
- package/src/codec/websocket/upgrade/index.ts +116 -0
- package/src/datagram/bootstrap/index.ts +269 -0
- package/src/datagram/channel/index.ts +103 -0
- package/src/datagram/index.ts +10 -0
- package/src/datagram/packet/index.ts +72 -0
- package/src/datagram/transport/index.ts +239 -0
- package/src/handler/flush/index.ts +187 -0
- package/src/handler/idle/index.ts +295 -0
- package/src/handler/index.ts +22 -0
- package/src/handler/logging/index.ts +195 -0
- package/src/handler/metrics/index.ts +300 -0
- package/src/handler/offload/index.ts +141 -0
- package/src/handler/timeout/error/index.ts +29 -0
- package/src/handler/timeout/index.ts +3 -0
- package/src/handler/timeout/read/index.ts +44 -0
- package/src/handler/timeout/write/index.ts +89 -0
- package/src/handler/traffic/index.ts +259 -0
- package/src/index.ts +258 -0
- package/src/logger/index.ts +47 -0
- package/src/reliable/bootstrap/index.ts +154 -0
- package/src/reliable/cipher/index.ts +227 -0
- package/src/reliable/cookie/index.ts +78 -0
- package/src/reliable/handler/index.ts +1202 -0
- package/src/reliable/handshake/index.ts +144 -0
- package/src/reliable/index.ts +38 -0
- package/src/reliable/lane/index.ts +420 -0
- package/src/reliable/message/index.ts +102 -0
- package/src/reliable/packet/index.ts +117 -0
- package/src/reliable/peer/index.ts +929 -0
- package/src/reliable/registry/index.ts +107 -0
- package/src/serve/bootstrap/index.ts +606 -0
- package/src/serve/channel/index.ts +246 -0
- package/src/serve/index.ts +17 -0
- package/src/serve/outbox/index.ts +292 -0
- package/src/serve/transport/index.ts +96 -0
- package/src/serve/writer/index.ts +119 -0
- package/src/shard/envelope/index.ts +44 -0
- package/src/shard/gateway/index.ts +119 -0
- package/src/shard/index.ts +11 -0
- package/src/shard/link/index.ts +743 -0
- package/src/shard/map/index.ts +91 -0
- package/src/worker/group/index.ts +175 -0
- package/src/worker/index.ts +10 -0
- package/src/worker/pool/index.ts +235 -0
- package/src/worker/service/index.ts +55 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type KeyObject,
|
|
3
|
+
createCipheriv,
|
|
4
|
+
createDecipheriv,
|
|
5
|
+
createPrivateKey,
|
|
6
|
+
createPublicKey,
|
|
7
|
+
diffieHellman,
|
|
8
|
+
generateKeyPairSync,
|
|
9
|
+
hkdfSync,
|
|
10
|
+
} from 'node:crypto'
|
|
11
|
+
|
|
12
|
+
import { dataHeaderSize } from '../packet/index.ts'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Raw X25519 keys
|
|
16
|
+
*/
|
|
17
|
+
export type KeyPair = {
|
|
18
|
+
/**
|
|
19
|
+
* 32 byte public key, safe to hand out
|
|
20
|
+
*/
|
|
21
|
+
readonly publicKey: Uint8Array
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 32 byte private key, keep it secret
|
|
25
|
+
*/
|
|
26
|
+
readonly privateKey: Uint8Array
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Keys for one end of a session
|
|
31
|
+
*/
|
|
32
|
+
type SessionKeys = {
|
|
33
|
+
/**
|
|
34
|
+
* Encrypts what we send
|
|
35
|
+
*/
|
|
36
|
+
readonly send: Buffer
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Decrypts what we get
|
|
40
|
+
*/
|
|
41
|
+
readonly receive: Buffer
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Counter bytes plus the auth tag
|
|
46
|
+
*/
|
|
47
|
+
const tagSize = 16
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Bytes for the nonce counter
|
|
51
|
+
*/
|
|
52
|
+
const counterSize = 8
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* AES-256-GCM for data packets, the header stays readable but is authenticated
|
|
56
|
+
* @class
|
|
57
|
+
*/
|
|
58
|
+
export class PacketCipher {
|
|
59
|
+
/**
|
|
60
|
+
* Bytes each sealed packet grows by
|
|
61
|
+
*/
|
|
62
|
+
public static readonly overhead = counterSize + tagSize
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Encrypts what we send
|
|
66
|
+
*/
|
|
67
|
+
private readonly sendKey: Buffer
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Decrypts what we get
|
|
71
|
+
*/
|
|
72
|
+
private readonly receiveKey: Buffer
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Next nonce counter, never reused under one key
|
|
76
|
+
*/
|
|
77
|
+
private counter: number
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Reused nonce buffer
|
|
81
|
+
*/
|
|
82
|
+
private readonly nonce: Buffer
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Usually built by PacketCipher.derive
|
|
86
|
+
* @param keys send and receive keys
|
|
87
|
+
*/
|
|
88
|
+
public constructor(keys: SessionKeys) {
|
|
89
|
+
this.sendKey = keys.send
|
|
90
|
+
this.receiveKey = keys.receive
|
|
91
|
+
this.counter = 0
|
|
92
|
+
this.nonce = Buffer.alloc(12)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* A fresh key pair, for a server with a fixed identity
|
|
97
|
+
*/
|
|
98
|
+
public static generateKeyPair(): KeyPair {
|
|
99
|
+
const { privateKey } = generateKeyPairSync('x25519')
|
|
100
|
+
const jwk = privateKey.export({ format: 'jwk' })
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
publicKey: Buffer.from(jwk.x ?? '', 'base64url'),
|
|
104
|
+
privateKey: Buffer.from(jwk.d ?? '', 'base64url'),
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Agrees on session keys from our private key and their public key
|
|
110
|
+
* @param own our key pair
|
|
111
|
+
* @param theirs their 32 byte public key
|
|
112
|
+
* @param salt binds the keys to this session
|
|
113
|
+
* @param client whether we're the client end
|
|
114
|
+
*/
|
|
115
|
+
public static derive(
|
|
116
|
+
own: KeyPair,
|
|
117
|
+
theirs: Uint8Array,
|
|
118
|
+
salt: Uint8Array,
|
|
119
|
+
client: boolean,
|
|
120
|
+
): PacketCipher {
|
|
121
|
+
let shared: Buffer
|
|
122
|
+
try {
|
|
123
|
+
shared = diffieHellman({
|
|
124
|
+
privateKey: PacketCipher.privateKeyOf(own),
|
|
125
|
+
publicKey: PacketCipher.publicKeyOf(theirs),
|
|
126
|
+
})
|
|
127
|
+
} catch (cause) {
|
|
128
|
+
throw new Error('Key exchange failed', { cause })
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const material = Buffer.from(
|
|
132
|
+
hkdfSync('sha256', shared, salt, 'netify reliable', 64),
|
|
133
|
+
)
|
|
134
|
+
const toServer = material.subarray(0, 32)
|
|
135
|
+
const toClient = material.subarray(32, 64)
|
|
136
|
+
return new PacketCipher(
|
|
137
|
+
client
|
|
138
|
+
? { send: toServer, receive: toClient }
|
|
139
|
+
: { send: toClient, receive: toServer },
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Imports a raw public key
|
|
145
|
+
* @param raw 32 bytes
|
|
146
|
+
*/
|
|
147
|
+
private static publicKeyOf(raw: Uint8Array): KeyObject {
|
|
148
|
+
return createPublicKey({
|
|
149
|
+
key: {
|
|
150
|
+
kty: 'OKP',
|
|
151
|
+
crv: 'X25519',
|
|
152
|
+
x: Buffer.from(raw).toString('base64url'),
|
|
153
|
+
},
|
|
154
|
+
format: 'jwk',
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Imports a raw key pair as a private key
|
|
160
|
+
* @param pair the raw keys
|
|
161
|
+
*/
|
|
162
|
+
private static privateKeyOf(pair: KeyPair): KeyObject {
|
|
163
|
+
return createPrivateKey({
|
|
164
|
+
key: {
|
|
165
|
+
kty: 'OKP',
|
|
166
|
+
crv: 'X25519',
|
|
167
|
+
x: Buffer.from(pair.publicKey).toString('base64url'),
|
|
168
|
+
d: Buffer.from(pair.privateKey).toString('base64url'),
|
|
169
|
+
},
|
|
170
|
+
format: 'jwk',
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Encrypts everything after the header
|
|
176
|
+
* @param packet a plain data packet
|
|
177
|
+
*/
|
|
178
|
+
public seal(packet: Uint8Array): Uint8Array {
|
|
179
|
+
const counter = this.counter
|
|
180
|
+
this.counter += 1
|
|
181
|
+
const nonce = this.nonce
|
|
182
|
+
nonce.writeUInt32BE(Math.floor(counter / 0x1_0000_0000), 4)
|
|
183
|
+
nonce.writeUInt32BE(counter >>> 0, 8)
|
|
184
|
+
|
|
185
|
+
const header = packet.subarray(0, dataHeaderSize)
|
|
186
|
+
const cipher = createCipheriv('aes-256-gcm', this.sendKey, nonce)
|
|
187
|
+
cipher.setAAD(header)
|
|
188
|
+
const body = cipher.update(packet.subarray(dataHeaderSize))
|
|
189
|
+
cipher.final()
|
|
190
|
+
|
|
191
|
+
const sealed = new Uint8Array(packet.length + PacketCipher.overhead)
|
|
192
|
+
sealed.set(header, 0)
|
|
193
|
+
sealed.set(nonce.subarray(4), dataHeaderSize)
|
|
194
|
+
sealed.set(body, dataHeaderSize + counterSize)
|
|
195
|
+
sealed.set(cipher.getAuthTag(), sealed.length - tagSize)
|
|
196
|
+
return sealed
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Checks and decrypts a sealed packet, or null when it's been tampered with
|
|
201
|
+
* @param packet a sealed data packet
|
|
202
|
+
*/
|
|
203
|
+
public open(packet: Uint8Array): Uint8Array | null {
|
|
204
|
+
if (packet.length < dataHeaderSize + PacketCipher.overhead) return null
|
|
205
|
+
|
|
206
|
+
const header = packet.subarray(0, dataHeaderSize)
|
|
207
|
+
const nonce = this.nonce
|
|
208
|
+
nonce.set(packet.subarray(dataHeaderSize, dataHeaderSize + counterSize), 4)
|
|
209
|
+
const decipher = createDecipheriv('aes-256-gcm', this.receiveKey, nonce)
|
|
210
|
+
decipher.setAAD(header)
|
|
211
|
+
decipher.setAuthTag(packet.subarray(packet.length - tagSize))
|
|
212
|
+
|
|
213
|
+
const body = decipher.update(
|
|
214
|
+
packet.subarray(dataHeaderSize + counterSize, packet.length - tagSize),
|
|
215
|
+
)
|
|
216
|
+
try {
|
|
217
|
+
decipher.final()
|
|
218
|
+
} catch {
|
|
219
|
+
return null
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const plain = new Uint8Array(dataHeaderSize + body.length)
|
|
223
|
+
plain.set(header, 0)
|
|
224
|
+
plain.set(body, dataHeaderSize)
|
|
225
|
+
return plain
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
import type { PacketAddress } from '../../datagram/packet/index.ts'
|
|
4
|
+
import { cookieSize } from '../packet/index.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hands out and checks connect cookies without keeping any state per client
|
|
8
|
+
* @class
|
|
9
|
+
*/
|
|
10
|
+
export class CookieJar {
|
|
11
|
+
/**
|
|
12
|
+
* Seconds a cookie stays good for
|
|
13
|
+
*/
|
|
14
|
+
public static readonly lifetime = 30
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Random per server, so cookies can't be forged
|
|
18
|
+
*/
|
|
19
|
+
private readonly secret: Buffer
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Reused input buffer
|
|
23
|
+
*/
|
|
24
|
+
private readonly input: Buffer
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Picks a fresh secret
|
|
28
|
+
*/
|
|
29
|
+
public constructor() {
|
|
30
|
+
this.secret = randomBytes(32)
|
|
31
|
+
this.input = Buffer.alloc(10)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Current time in whole seconds
|
|
36
|
+
*/
|
|
37
|
+
public static now(): number {
|
|
38
|
+
return Math.floor(Date.now() / 1000) >>> 0
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The cookie for this client at this time
|
|
43
|
+
* @param address where the client is
|
|
44
|
+
* @param salt the client's handshake number
|
|
45
|
+
* @param time seconds the cookie was issued at
|
|
46
|
+
*/
|
|
47
|
+
public issue(address: PacketAddress, salt: number, time: number): Buffer {
|
|
48
|
+
const input = this.input
|
|
49
|
+
input.writeUInt32BE(salt >>> 0, 0)
|
|
50
|
+
input.writeUInt32BE(time >>> 0, 4)
|
|
51
|
+
input.writeUInt16BE(address.port & 0xffff, 8)
|
|
52
|
+
|
|
53
|
+
return createHmac('sha256', this.secret)
|
|
54
|
+
.update(input)
|
|
55
|
+
.update(address.hostname)
|
|
56
|
+
.digest()
|
|
57
|
+
.subarray(0, cookieSize)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Whether a cookie the client sent back is ours, fresh and for this address
|
|
62
|
+
* @param address where the client is
|
|
63
|
+
* @param salt the client's handshake number
|
|
64
|
+
* @param time seconds the cookie claims it was issued at
|
|
65
|
+
* @param cookie the bytes it sent back
|
|
66
|
+
*/
|
|
67
|
+
public verify(
|
|
68
|
+
address: PacketAddress,
|
|
69
|
+
salt: number,
|
|
70
|
+
time: number,
|
|
71
|
+
cookie: Uint8Array,
|
|
72
|
+
): boolean {
|
|
73
|
+
const age = CookieJar.now() - time
|
|
74
|
+
if (age < 0 || age > CookieJar.lifetime) return false
|
|
75
|
+
|
|
76
|
+
return timingSafeEqual(this.issue(address, salt, time), cookie)
|
|
77
|
+
}
|
|
78
|
+
}
|