@netifyjs/protocol 1.0.0-alpha.2 → 1.0.0-beta.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/package.json +2 -2
- package/src/codec/index.ts +82 -157
- package/src/dispatch/dispatcher/index.ts +116 -33
- package/src/dispatch/session/index.ts +9 -3
- package/src/index.ts +2 -4
- package/src/photon/balancing/codec/index.ts +27 -53
- package/src/photon/balancing/format/authenticate/request/index.ts +52 -6
- package/src/photon/balancing/format/friends/request/index.ts +21 -5
- package/src/photon/balancing/format/game/create/request/index.ts +45 -8
- package/src/photon/balancing/format/game/debug/request/index.ts +19 -4
- package/src/photon/balancing/format/game/join/request/index.ts +65 -13
- package/src/photon/balancing/format/game/list/request/index.ts +29 -3
- package/src/photon/balancing/format/game/random/request/index.ts +58 -10
- package/src/photon/balancing/format/groups/request/index.ts +19 -3
- package/src/photon/balancing/format/index.ts +7 -0
- package/src/photon/balancing/format/leave/request/index.ts +19 -3
- package/src/photon/balancing/format/lobby/join/request/index.ts +23 -3
- package/src/photon/balancing/format/lobby/stats/request/index.ts +19 -3
- package/src/photon/balancing/format/once/request/index.ts +31 -8
- package/src/photon/balancing/format/properties/get/request/index.ts +31 -4
- package/src/photon/balancing/format/properties/set/request/index.ts +36 -7
- package/src/photon/balancing/format/raise/request/index.ts +76 -10
- package/src/photon/balancing/format/reader/index.ts +75 -6
- package/src/photon/balancing/format/regions/request/index.ts +16 -1
- package/src/photon/balancing/format/rpc/request/index.ts +26 -3
- package/src/photon/balancing/format/settings/request/index.ts +15 -2
- package/src/photon/balancing/format/shared/credentials/index.ts +77 -33
- package/src/photon/balancing/format/shared/lobby/index.ts +41 -5
- package/src/photon/balancing/format/shared/options/index.ts +56 -24
- package/src/photon/balancing/format/table/index.ts +399 -0
- package/src/photon/balancing/index.ts +11 -1
- package/src/photon/balancing/travel/index.ts +15 -12
- package/src/photon/binary/v18/index.ts +26 -1
- package/src/photon/binary/value/index.ts +52 -3
- package/src/photon/client/connection/index.ts +6 -6
- package/src/photon/cluster/back/index.ts +32 -45
- package/src/photon/cluster/dresser/index.ts +7 -8
- package/src/photon/cluster/front/index.ts +25 -49
- package/src/photon/cluster/grid/index.ts +1 -1
- package/src/photon/cluster/mover/index.ts +12 -12
- package/src/photon/cluster/press/index.ts +2 -18
- package/src/photon/cluster/route/game/index.ts +9 -11
- package/src/photon/cluster/route/master/index.ts +15 -34
- package/src/photon/cluster/route/name/index.ts +4 -21
- package/src/photon/cluster/seat/game/index.ts +6 -26
- package/src/photon/cluster/seat/master/index.ts +1 -1
- package/src/photon/cluster/sessions/index.ts +5 -6
- package/src/photon/cluster/spread/index.ts +8 -147
- package/src/photon/enet/client/index.ts +1 -8
- package/src/photon/enet/gateway/index.ts +5 -15
- package/src/photon/enet/index.ts +1 -1
- package/src/photon/enet/link/index.ts +34 -15
- package/src/photon/enet/outbound/index.ts +279 -0
- package/src/photon/enet/peer/index.ts +59 -0
- package/src/photon/enet/transport/index.ts +8 -24
- package/src/photon/game/admission/index.ts +353 -0
- package/src/photon/game/bag/index.ts +13 -0
- package/src/photon/game/broadcast/index.ts +79 -0
- package/src/photon/game/editor/index.ts +446 -0
- package/src/photon/game/gate/index.ts +9 -8
- package/src/photon/game/handler/index.ts +30 -3
- package/src/photon/game/history/index.ts +234 -0
- package/src/photon/game/hooked/index.ts +16 -15
- package/src/photon/game/index.ts +12 -1
- package/src/photon/game/lifecycle/index.ts +193 -0
- package/src/photon/game/peer/index.ts +1 -10
- package/src/photon/game/properties/index.ts +342 -0
- package/src/photon/game/room/index.ts +435 -1793
- package/src/photon/game/rooms/index.ts +2 -1
- package/src/photon/game/seats/index.ts +237 -0
- package/src/photon/game/server/index.ts +12 -2
- package/src/photon/game/services/index.ts +41 -0
- package/src/photon/game/settings/index.ts +22 -0
- package/src/photon/game/snapshot/index.ts +133 -0
- package/src/photon/game/throttle/index.ts +60 -0
- package/src/photon/game/touch/index.ts +2 -5
- package/src/photon/game/venue/index.ts +31 -0
- package/src/photon/master/application/index.ts +7 -0
- package/src/photon/master/gate/index.ts +12 -8
- package/src/photon/master/handler/index.ts +36 -748
- package/src/photon/master/index.ts +5 -0
- package/src/photon/master/list/sql/index.ts +37 -3
- package/src/photon/master/listing/index.ts +133 -0
- package/src/photon/master/lobby/index.ts +9 -0
- package/src/photon/master/login/index.ts +424 -0
- package/src/photon/master/matchmaking/index.ts +282 -0
- package/src/photon/master/query/index.ts +45 -0
- package/src/photon/master/redirect/index.ts +8 -7
- package/src/photon/master/services/index.ts +35 -0
- package/src/photon/master/settings/index.ts +44 -0
- package/src/photon/master/sql/cost/index.ts +263 -0
- package/src/photon/master/sql/index.ts +1 -0
- package/src/photon/message/binary/index.ts +35 -1
- package/src/photon/message/client/index.ts +14 -11
- package/src/photon/message/format/index.ts +9 -0
- package/src/photon/message/handler/index.ts +48 -63
- package/src/photon/message/pipeline/index.ts +12 -12
- package/src/photon/message/server/index.ts +87 -43
- package/src/photon/message/watch/index.ts +127 -15
- package/src/photon/server/config/index.ts +5 -0
- package/src/photon/server/deferral/index.ts +57 -0
- package/src/photon/server/host/index.ts +36 -10
- package/src/photon/server/index.ts +1 -0
- package/src/photon/server/listeners/index.ts +11 -0
- package/src/photon/tcp/client/index.ts +1 -1
- package/src/photon/tcp/codec/index.ts +41 -38
- package/src/photon/tcp/framing/index.ts +24 -4
- package/src/photon/tcp/server/index.ts +4 -4
- package/src/photon/websocket/bridge/index.ts +16 -20
- package/src/photon/websocket/message/index.ts +27 -39
- package/src/photon/websocket/path/index.ts +2 -5
- package/src/scale/batch/index.ts +14 -6
- package/src/scale/fanout/index.ts +6 -6
- package/src/scale/index.ts +2 -4
- package/src/scale/outbound/index.ts +192 -0
- package/src/scale/relay/index.ts +24 -16
- package/src/scale/sink/index.ts +6 -11
- package/src/scale/transport/index.ts +27 -44
- package/src/scale/tunnel/index.ts +56 -86
- package/src/smartfox/common/bluebox/gateway/index.ts +401 -0
- package/src/smartfox/common/bluebox/index.ts +4 -0
- package/src/smartfox/{x2 → common}/bluebox/transport/index.ts +36 -23
- package/src/smartfox/common/index.ts +5 -0
- package/src/smartfox/common/strikes/index.ts +74 -0
- package/src/smartfox/pro/bluebox/index.ts +176 -4
- package/src/smartfox/pro/codec/index.ts +8 -7
- package/src/smartfox/pro/escalation/index.ts +28 -20
- package/src/smartfox/pro/flood/index.ts +2 -2
- package/src/smartfox/pro/format/extension/raw/index.ts +12 -2
- package/src/smartfox/pro/handler/chat/index.ts +11 -8
- package/src/smartfox/pro/handler/login/index.ts +2 -25
- package/src/smartfox/pro/handler/shared/index.ts +4 -3
- package/src/smartfox/pro/index.ts +11 -5
- package/src/smartfox/pro/login/gate/index.ts +166 -0
- package/src/smartfox/pro/login/index.ts +1 -0
- package/src/smartfox/pro/moderation/index.ts +220 -0
- package/src/smartfox/pro/options/index.ts +5 -5
- package/src/smartfox/pro/protocol/index.ts +62 -8
- package/src/smartfox/pro/web/index.ts +33 -26
- package/src/smartfox/pro/zone/index.ts +59 -282
- package/src/smartfox/pro/zones/index.ts +3 -3
- package/src/smartfox/x2/bluebox/index.ts +149 -4
- package/src/smartfox/x2/chat/index.ts +9 -19
- package/src/smartfox/x2/codec/index.ts +34 -40
- package/src/smartfox/x2/config/index.ts +6 -1
- package/src/smartfox/x2/data/array/index.ts +96 -60
- package/src/smartfox/x2/data/object/index.ts +144 -82
- package/src/smartfox/x2/data/serializer/index.ts +41 -23
- package/src/smartfox/x2/flood/gate/index.ts +19 -13
- package/src/smartfox/x2/format/buddy/init/response/index.ts +4 -3
- package/src/smartfox/x2/format/find/rooms/response/index.ts +4 -3
- package/src/smartfox/x2/format/find/users/response/index.ts +4 -3
- package/src/smartfox/x2/format/group/subscribe/response/index.ts +4 -3
- package/src/smartfox/x2/format/login/response/index.ts +4 -3
- package/src/smartfox/x2/format/mmo/position/response/index.ts +8 -6
- package/src/smartfox/x2/format/room/join/response/index.ts +4 -3
- package/src/smartfox/x2/games/index.ts +35 -8
- package/src/smartfox/x2/hall/index.ts +0 -6
- package/src/smartfox/x2/handler/session/index.ts +1 -1
- package/src/smartfox/x2/index.ts +8 -3
- package/src/smartfox/x2/member/index.ts +10 -3
- package/src/smartfox/x2/packet/login/response/index.ts +12 -0
- package/src/smartfox/x2/protocol/index.ts +11 -1
- package/src/smartfox/x2/rooms/index.ts +1 -2
- package/src/smartfox/x2/sessions/index.ts +6 -0
- package/src/smartfox/x2/udp/index.ts +61 -24
- package/src/smartfox/x2/web/index.ts +35 -17
- package/src/smartfox/x2/websocket/index.ts +18 -36
- package/src/smartfox/x2/zone/index.ts +77 -28
- package/src/smartfox/x2/zones/index.ts +17 -5
- package/src/photon/enet/handler/index.ts +0 -65
- package/src/scale/hosted/index.ts +0 -68
- package/src/scale/outlet/index.ts +0 -121
- package/src/smartfox/pro/bluebox/gateway/index.ts +0 -310
- package/src/smartfox/pro/bluebox/session/index.ts +0 -63
- package/src/smartfox/pro/bluebox/transport/index.ts +0 -104
- package/src/smartfox/x2/bluebox/gateway/index.ts +0 -315
- /package/src/smartfox/{x2 → common}/bluebox/session/index.ts +0 -0
- /package/src/smartfox/{pro → common}/counters/index.ts +0 -0
- /package/src/smartfox/{pro → common}/counters/memory/index.ts +0 -0
- /package/src/smartfox/{pro → common}/counters/offender/index.ts +0 -0
- /package/src/smartfox/{pro → common}/counters/store/index.ts +0 -0
- /package/src/smartfox/{pro → common}/sequence/index.ts +0 -0
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AttributeKey,
|
|
3
|
+
Channel,
|
|
4
|
+
type ChannelSetup,
|
|
5
|
+
type ShardLink,
|
|
6
|
+
} from '@netifyjs/netify'
|
|
7
|
+
|
|
8
|
+
import { Courier } from '../../../../scale/courier/index.ts'
|
|
9
|
+
import { Switchboard } from '../../../../scale/switchboard/index.ts'
|
|
10
|
+
|
|
11
|
+
import { BlueBoxSession } from '../session/index.ts'
|
|
12
|
+
import { BlueBoxTransport } from '../transport/index.ts'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* How a tunnel runs once its protocol filled in the defaults
|
|
16
|
+
*/
|
|
17
|
+
export type BlueBoxSettings = {
|
|
18
|
+
/**
|
|
19
|
+
* Builds the pipeline of each tunnelled connection
|
|
20
|
+
*/
|
|
21
|
+
readonly setup: ChannelSetup
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Quiet time before a session is dropped, in ms
|
|
25
|
+
*/
|
|
26
|
+
readonly timeout: number
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Longest a poll is held open for data, in ms
|
|
30
|
+
*/
|
|
31
|
+
readonly hold: number
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Most sessions open at once
|
|
35
|
+
*/
|
|
36
|
+
readonly maxSessions: number
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Most bytes one session holds between polls
|
|
40
|
+
*/
|
|
41
|
+
readonly maxQueued: number
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Connects the server's shards, or null
|
|
45
|
+
*/
|
|
46
|
+
readonly link: ShardLink | null
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Goes in front of the link topics the tunnel uses
|
|
50
|
+
*/
|
|
51
|
+
readonly topic: string
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What a client with no session is told
|
|
55
|
+
*/
|
|
56
|
+
readonly lost: string
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* What a poll with nothing waiting is told
|
|
60
|
+
*/
|
|
61
|
+
readonly idle: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* One client request split into its parts
|
|
66
|
+
*/
|
|
67
|
+
export type BlueBoxRequest = {
|
|
68
|
+
/**
|
|
69
|
+
* Session it's for, empty if it has none yet
|
|
70
|
+
*/
|
|
71
|
+
readonly id: string
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* What the client wants, like connect, poll, data or disconnect
|
|
75
|
+
*/
|
|
76
|
+
readonly command: string
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* What came with it, still in its wire form
|
|
80
|
+
*/
|
|
81
|
+
readonly data: string
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A client's request as it travels to the shard that holds its session
|
|
86
|
+
*/
|
|
87
|
+
type Forwarded = {
|
|
88
|
+
/**
|
|
89
|
+
* The form field's value
|
|
90
|
+
*/
|
|
91
|
+
readonly request: string
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Where the request came from
|
|
95
|
+
*/
|
|
96
|
+
readonly address: string
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Port the request came from
|
|
100
|
+
*/
|
|
101
|
+
readonly port: number
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Lets clients that can't open a socket talk over HTTP requests
|
|
106
|
+
* @class
|
|
107
|
+
*/
|
|
108
|
+
export abstract class BlueBoxGateway {
|
|
109
|
+
/**
|
|
110
|
+
* Marks a connection that's tunnelled over HTTP
|
|
111
|
+
*/
|
|
112
|
+
private static readonly tunnelled: AttributeKey<boolean> =
|
|
113
|
+
new AttributeKey<boolean>('netify:protocol:smartfox:bluebox')
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Builds the pipeline of each tunnelled connection
|
|
117
|
+
*/
|
|
118
|
+
private readonly setup: ChannelSetup
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Quiet time before a session is dropped, in ms
|
|
122
|
+
*/
|
|
123
|
+
private readonly timeout: number
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Longest a poll is held open for data, in ms
|
|
127
|
+
*/
|
|
128
|
+
private readonly hold: number
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Most sessions open at once
|
|
132
|
+
*/
|
|
133
|
+
private readonly maxSessions: number
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Most bytes one session holds between polls
|
|
137
|
+
*/
|
|
138
|
+
private readonly maxQueued: number
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* What a client with no session is told
|
|
142
|
+
*/
|
|
143
|
+
private readonly lost: string
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* What a poll with nothing waiting is told
|
|
147
|
+
*/
|
|
148
|
+
private readonly idle: string
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Open sessions by id
|
|
152
|
+
*/
|
|
153
|
+
private readonly sessions: Map<string, BlueBoxSession>
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Looks for quiet sessions
|
|
157
|
+
*/
|
|
158
|
+
private readonly sweeper: Timer
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Finds the shard that holds a session
|
|
162
|
+
*/
|
|
163
|
+
private readonly switchboard: Switchboard<Forwarded, string>
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @param settings how the tunnel runs
|
|
167
|
+
*/
|
|
168
|
+
protected constructor(settings: BlueBoxSettings) {
|
|
169
|
+
const reason = BlueBoxGateway.refusal(settings)
|
|
170
|
+
if (reason !== null) throw new RangeError(reason)
|
|
171
|
+
|
|
172
|
+
this.setup = settings.setup
|
|
173
|
+
this.timeout = settings.timeout
|
|
174
|
+
this.hold = settings.hold
|
|
175
|
+
this.maxSessions = settings.maxSessions
|
|
176
|
+
this.maxQueued = settings.maxQueued
|
|
177
|
+
this.lost = settings.lost
|
|
178
|
+
this.idle = settings.idle
|
|
179
|
+
this.sessions = new Map<string, BlueBoxSession>()
|
|
180
|
+
this.switchboard = Switchboard.of<Forwarded, string>(
|
|
181
|
+
settings.link,
|
|
182
|
+
settings.topic,
|
|
183
|
+
(forwarded: Forwarded): string | Promise<string> =>
|
|
184
|
+
this.handle(forwarded.request, forwarded.address, forwarded.port),
|
|
185
|
+
)
|
|
186
|
+
this.sweeper = setInterval(
|
|
187
|
+
(): void => {
|
|
188
|
+
this.sweep()
|
|
189
|
+
},
|
|
190
|
+
Math.max(10, settings.timeout / 4),
|
|
191
|
+
)
|
|
192
|
+
this.sweeper.unref()
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* What's wrong with the settings, or null
|
|
197
|
+
* @param settings what the protocol passed in
|
|
198
|
+
*/
|
|
199
|
+
private static refusal(settings: BlueBoxSettings): string | null {
|
|
200
|
+
if (!(settings.timeout > 0)) return 'Timeout is invalid'
|
|
201
|
+
if (!(settings.hold >= 0) || settings.hold >= settings.timeout) {
|
|
202
|
+
return 'Hold is invalid'
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (!Number.isInteger(settings.maxSessions) || settings.maxSessions <= 0) {
|
|
206
|
+
return 'Session limit is invalid'
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return Number.isInteger(settings.maxQueued) && settings.maxQueued > 0
|
|
210
|
+
? null
|
|
211
|
+
: 'Queue limit is invalid'
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Whether a connection is tunnelled over HTTP
|
|
216
|
+
* @param channel the connection handlers see
|
|
217
|
+
*/
|
|
218
|
+
public static carries(channel: Channel): boolean {
|
|
219
|
+
return channel.attributes.get<boolean>(BlueBoxGateway.tunnelled) === true
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* How many sessions are open
|
|
224
|
+
*/
|
|
225
|
+
public get size(): number {
|
|
226
|
+
return this.sessions.size
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Answers one request from a client
|
|
231
|
+
* @param request the form field's value
|
|
232
|
+
* @param address where the request came from
|
|
233
|
+
* @param port port the request came from
|
|
234
|
+
* @param now current time in ms
|
|
235
|
+
*/
|
|
236
|
+
public handle(
|
|
237
|
+
request: string,
|
|
238
|
+
address: string,
|
|
239
|
+
port: number,
|
|
240
|
+
now: number = performance.now(),
|
|
241
|
+
): string | Promise<string> {
|
|
242
|
+
const parsed = this.parse(request)
|
|
243
|
+
if (parsed.command === 'connect') return this.connect(address, port, now)
|
|
244
|
+
|
|
245
|
+
const elsewhere = this.switchboard.route(parsed.id, {
|
|
246
|
+
request,
|
|
247
|
+
address,
|
|
248
|
+
port,
|
|
249
|
+
})
|
|
250
|
+
if (elsewhere !== null) return this.relayed(elsewhere)
|
|
251
|
+
|
|
252
|
+
const session = this.sessions.get(parsed.id)
|
|
253
|
+
if (session === undefined) return this.lost
|
|
254
|
+
|
|
255
|
+
session.seen = now
|
|
256
|
+
|
|
257
|
+
switch (parsed.command) {
|
|
258
|
+
case 'poll':
|
|
259
|
+
return this.poll(session)
|
|
260
|
+
case 'data':
|
|
261
|
+
this.receive(session, parsed.data)
|
|
262
|
+
return ''
|
|
263
|
+
case 'disconnect':
|
|
264
|
+
this.drop(session)
|
|
265
|
+
return ''
|
|
266
|
+
default:
|
|
267
|
+
return ''
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Drops every session and stops looking for quiet ones
|
|
273
|
+
*/
|
|
274
|
+
public shutdown(): void {
|
|
275
|
+
clearInterval(this.sweeper)
|
|
276
|
+
this.switchboard.close()
|
|
277
|
+
|
|
278
|
+
for (const session of [...this.sessions.values()]) {
|
|
279
|
+
this.drop(session)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Splits a client request into its parts
|
|
285
|
+
* @param request the form field's value
|
|
286
|
+
*/
|
|
287
|
+
protected abstract parse(request: string): BlueBoxRequest
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* The bytes a data request carries, or null
|
|
291
|
+
* @param data what came with the request
|
|
292
|
+
*/
|
|
293
|
+
protected abstract bytes(data: string): Uint8Array | null
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* How many leading bytes of the queue make up whole packets
|
|
297
|
+
* @param bytes everything queued for a poll
|
|
298
|
+
*/
|
|
299
|
+
protected abstract whole(bytes: Uint8Array): number
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* What a client is told about its new session
|
|
303
|
+
* @param id the session's id
|
|
304
|
+
*/
|
|
305
|
+
protected abstract opened(id: string): string
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* What a poll hands over
|
|
309
|
+
* @param bytes whole packets waiting for the client
|
|
310
|
+
*/
|
|
311
|
+
protected abstract packed(bytes: Uint8Array): string
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Passes what a client sent to its connection
|
|
315
|
+
* @param session who sent it
|
|
316
|
+
* @param data what came with the request
|
|
317
|
+
*/
|
|
318
|
+
private receive(session: BlueBoxSession, data: string): void {
|
|
319
|
+
const bytes = this.bytes(data)
|
|
320
|
+
if (bytes !== null) session.channel.receiveBytes(bytes)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Opens a session and tells the client its id
|
|
325
|
+
* @param address where the request came from
|
|
326
|
+
* @param port port the request came from
|
|
327
|
+
* @param now current time in ms
|
|
328
|
+
*/
|
|
329
|
+
private connect(address: string, port: number, now: number): string {
|
|
330
|
+
if (this.sessions.size >= this.maxSessions) return this.lost
|
|
331
|
+
|
|
332
|
+
const id = this.switchboard.mint()
|
|
333
|
+
const transport = new BlueBoxTransport(address, port, this.maxQueued)
|
|
334
|
+
const channel = new Channel(transport)
|
|
335
|
+
channel.attributes.set<boolean>(BlueBoxGateway.tunnelled, true)
|
|
336
|
+
|
|
337
|
+
this.sessions.set(id, BlueBoxSession.of(id, channel, transport, now))
|
|
338
|
+
channel.activate(this.setup)
|
|
339
|
+
|
|
340
|
+
return this.opened(id)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* The answer of the shard that holds a session
|
|
345
|
+
* @param answer what that shard is asked
|
|
346
|
+
*/
|
|
347
|
+
private async relayed(answer: Promise<string>): Promise<string> {
|
|
348
|
+
try {
|
|
349
|
+
return await answer
|
|
350
|
+
} catch (cause) {
|
|
351
|
+
if (cause instanceof Error && cause.message === Courier.down) {
|
|
352
|
+
return this.lost
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
throw new Error('Tunnel request failed', { cause })
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Hands a client everything that's waiting for it
|
|
361
|
+
* @param session who's polling
|
|
362
|
+
*/
|
|
363
|
+
private async poll(session: BlueBoxSession): Promise<string> {
|
|
364
|
+
const waiting = await session.transport.take(
|
|
365
|
+
this.hold,
|
|
366
|
+
(bytes: Uint8Array): number => this.whole(bytes),
|
|
367
|
+
)
|
|
368
|
+
session.seen = performance.now()
|
|
369
|
+
|
|
370
|
+
if (waiting.length > 0) {
|
|
371
|
+
session.channel.drained()
|
|
372
|
+
return this.packed(waiting)
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (!session.transport.ended) return this.idle
|
|
376
|
+
|
|
377
|
+
this.drop(session)
|
|
378
|
+
return this.lost
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Closes a session's connection and forgets it
|
|
383
|
+
* @param session what to drop
|
|
384
|
+
*/
|
|
385
|
+
private drop(session: BlueBoxSession): void {
|
|
386
|
+
if (!this.sessions.delete(session.id)) return
|
|
387
|
+
|
|
388
|
+
session.transport.end()
|
|
389
|
+
session.channel.ended()
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Drops sessions that stopped polling
|
|
394
|
+
* @param now current time in ms
|
|
395
|
+
*/
|
|
396
|
+
private sweep(now: number = performance.now()): void {
|
|
397
|
+
for (const session of [...this.sessions.values()]) {
|
|
398
|
+
if (now - session.seen >= this.timeout) this.drop(session)
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
@@ -5,11 +5,6 @@ import type { Transport } from '@netifyjs/netify'
|
|
|
5
5
|
* @class
|
|
6
6
|
*/
|
|
7
7
|
export class BlueBoxTransport implements Transport {
|
|
8
|
-
/**
|
|
9
|
-
* Never a datagram socket
|
|
10
|
-
*/
|
|
11
|
-
public readonly datagram: boolean = false
|
|
12
|
-
|
|
13
8
|
/**
|
|
14
9
|
* Where the client's first request came from
|
|
15
10
|
*/
|
|
@@ -26,15 +21,20 @@ export class BlueBoxTransport implements Transport {
|
|
|
26
21
|
public ended: boolean
|
|
27
22
|
|
|
28
23
|
/**
|
|
29
|
-
*
|
|
24
|
+
* Most bytes held between polls
|
|
30
25
|
*/
|
|
31
|
-
|
|
26
|
+
private readonly limit: number
|
|
32
27
|
|
|
33
28
|
/**
|
|
34
|
-
*
|
|
29
|
+
* Bytes written since the last poll
|
|
35
30
|
*/
|
|
36
31
|
private pending: Uint8Array[]
|
|
37
32
|
|
|
33
|
+
/**
|
|
34
|
+
* How many bytes are waiting for a poll
|
|
35
|
+
*/
|
|
36
|
+
private held: number
|
|
37
|
+
|
|
38
38
|
/**
|
|
39
39
|
* Wakes the poll that's being held open, or null
|
|
40
40
|
*/
|
|
@@ -43,27 +43,33 @@ export class BlueBoxTransport implements Transport {
|
|
|
43
43
|
/**
|
|
44
44
|
* @param remoteAddress where the client's first request came from
|
|
45
45
|
* @param remotePort port the client's first request came from
|
|
46
|
+
* @param limit most bytes held between polls
|
|
46
47
|
*/
|
|
47
|
-
public constructor(remoteAddress: string, remotePort: number) {
|
|
48
|
+
public constructor(remoteAddress: string, remotePort: number, limit: number) {
|
|
48
49
|
this.remoteAddress = remoteAddress
|
|
49
50
|
this.remotePort = remotePort
|
|
50
51
|
this.ended = false
|
|
51
|
-
this.
|
|
52
|
+
this.limit = limit
|
|
52
53
|
this.pending = []
|
|
54
|
+
this.held = 0
|
|
53
55
|
this.waiter = null
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
/**
|
|
57
|
-
* Queues bytes
|
|
58
|
-
* @param bytes
|
|
59
|
+
* Queues as many bytes as fit before the next poll
|
|
60
|
+
* @param bytes encoded packets
|
|
59
61
|
*/
|
|
60
62
|
public write(bytes: Uint8Array): number {
|
|
61
63
|
if (this.ended) return -1
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
const taken = Math.min(bytes.length, this.limit - this.held)
|
|
66
|
+
if (taken <= 0) return 0
|
|
67
|
+
|
|
68
|
+
this.pending.push(bytes.slice(0, taken))
|
|
69
|
+
this.held += taken
|
|
64
70
|
this.wake()
|
|
65
71
|
|
|
66
|
-
return
|
|
72
|
+
return taken
|
|
67
73
|
}
|
|
68
74
|
|
|
69
75
|
/**
|
|
@@ -75,24 +81,28 @@ export class BlueBoxTransport implements Transport {
|
|
|
75
81
|
}
|
|
76
82
|
|
|
77
83
|
/**
|
|
78
|
-
*
|
|
84
|
+
* Can't hold a polling client back
|
|
79
85
|
*/
|
|
80
|
-
public pause():
|
|
81
|
-
|
|
86
|
+
public pause(): boolean {
|
|
87
|
+
return false
|
|
82
88
|
}
|
|
83
89
|
|
|
84
90
|
/**
|
|
85
|
-
*
|
|
91
|
+
* Nothing to start again
|
|
86
92
|
*/
|
|
87
93
|
public resume(): void {
|
|
88
|
-
|
|
94
|
+
return
|
|
89
95
|
}
|
|
90
96
|
|
|
91
97
|
/**
|
|
92
|
-
*
|
|
98
|
+
* Every whole packet queued, waiting a while if there's nothing yet
|
|
93
99
|
* @param hold longest wait in ms
|
|
100
|
+
* @param whole how many leading bytes make up whole packets
|
|
94
101
|
*/
|
|
95
|
-
public async take(
|
|
102
|
+
public async take(
|
|
103
|
+
hold: number,
|
|
104
|
+
whole: (bytes: Uint8Array) => number,
|
|
105
|
+
): Promise<Uint8Array> {
|
|
96
106
|
this.wake()
|
|
97
107
|
|
|
98
108
|
if (this.pending.length === 0 && !this.ended && hold > 0) {
|
|
@@ -108,9 +118,12 @@ export class BlueBoxTransport implements Transport {
|
|
|
108
118
|
}
|
|
109
119
|
|
|
110
120
|
const bytes = Buffer.concat(this.pending)
|
|
111
|
-
|
|
121
|
+
const end = whole(bytes)
|
|
122
|
+
const rest = bytes.subarray(end)
|
|
123
|
+
this.pending = rest.length > 0 ? [rest] : []
|
|
124
|
+
this.held = rest.length
|
|
112
125
|
|
|
113
|
-
return bytes
|
|
126
|
+
return bytes.subarray(0, end)
|
|
114
127
|
}
|
|
115
128
|
|
|
116
129
|
/**
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Offender } from '../counters/offender/index.ts'
|
|
2
|
+
import type { CounterStore } from '../counters/store/index.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What one more offence earns
|
|
6
|
+
*/
|
|
7
|
+
export type Sentence = 'warn' | 'kick' | 'ban'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Counts warnings and kicks for one rule in a counter store
|
|
11
|
+
* @class
|
|
12
|
+
*/
|
|
13
|
+
export class Strikes {
|
|
14
|
+
/**
|
|
15
|
+
* Rule name inside counter keys
|
|
16
|
+
*/
|
|
17
|
+
private readonly rule: string
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Warnings before a kick
|
|
21
|
+
*/
|
|
22
|
+
private readonly warnings: number
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Kicks let off before a ban, or null for never
|
|
26
|
+
*/
|
|
27
|
+
private readonly kicks: number | null
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* How long a count lasts from its first hit in ms
|
|
31
|
+
*/
|
|
32
|
+
private readonly window: number
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param rule rule name inside counter keys
|
|
36
|
+
* @param warnings warnings before a kick
|
|
37
|
+
* @param kicks kicks let off before a ban, or null for never
|
|
38
|
+
* @param window how long a count lasts from its first hit in ms
|
|
39
|
+
*/
|
|
40
|
+
public constructor(
|
|
41
|
+
rule: string,
|
|
42
|
+
warnings: number,
|
|
43
|
+
kicks: number | null,
|
|
44
|
+
window: number,
|
|
45
|
+
) {
|
|
46
|
+
this.rule = rule
|
|
47
|
+
this.warnings = warnings
|
|
48
|
+
this.kicks = kicks
|
|
49
|
+
this.window = window
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Counts one offence and says what it earns
|
|
54
|
+
* @param store where warnings and kicks are counted
|
|
55
|
+
* @param offender who did it
|
|
56
|
+
*/
|
|
57
|
+
public async count(
|
|
58
|
+
store: CounterStore,
|
|
59
|
+
offender: Offender,
|
|
60
|
+
): Promise<Sentence> {
|
|
61
|
+
const warnings = offender.counter(`${this.rule}:warnings`)
|
|
62
|
+
const warned = await store.hit(warnings, this.window)
|
|
63
|
+
if (warned <= this.warnings) return 'warn'
|
|
64
|
+
|
|
65
|
+
await store.clear(warnings)
|
|
66
|
+
|
|
67
|
+
const kicks = offender.counter(`${this.rule}:kicks`)
|
|
68
|
+
const kicked = await store.hit(kicks, this.window)
|
|
69
|
+
if (this.kicks === null || kicked <= this.kicks) return 'kick'
|
|
70
|
+
|
|
71
|
+
await store.clear(kicks)
|
|
72
|
+
return 'ban'
|
|
73
|
+
}
|
|
74
|
+
}
|