@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.
Files changed (183) hide show
  1. package/package.json +2 -2
  2. package/src/codec/index.ts +82 -157
  3. package/src/dispatch/dispatcher/index.ts +116 -33
  4. package/src/dispatch/session/index.ts +9 -3
  5. package/src/index.ts +2 -4
  6. package/src/photon/balancing/codec/index.ts +27 -53
  7. package/src/photon/balancing/format/authenticate/request/index.ts +52 -6
  8. package/src/photon/balancing/format/friends/request/index.ts +21 -5
  9. package/src/photon/balancing/format/game/create/request/index.ts +45 -8
  10. package/src/photon/balancing/format/game/debug/request/index.ts +19 -4
  11. package/src/photon/balancing/format/game/join/request/index.ts +65 -13
  12. package/src/photon/balancing/format/game/list/request/index.ts +29 -3
  13. package/src/photon/balancing/format/game/random/request/index.ts +58 -10
  14. package/src/photon/balancing/format/groups/request/index.ts +19 -3
  15. package/src/photon/balancing/format/index.ts +7 -0
  16. package/src/photon/balancing/format/leave/request/index.ts +19 -3
  17. package/src/photon/balancing/format/lobby/join/request/index.ts +23 -3
  18. package/src/photon/balancing/format/lobby/stats/request/index.ts +19 -3
  19. package/src/photon/balancing/format/once/request/index.ts +31 -8
  20. package/src/photon/balancing/format/properties/get/request/index.ts +31 -4
  21. package/src/photon/balancing/format/properties/set/request/index.ts +36 -7
  22. package/src/photon/balancing/format/raise/request/index.ts +76 -10
  23. package/src/photon/balancing/format/reader/index.ts +75 -6
  24. package/src/photon/balancing/format/regions/request/index.ts +16 -1
  25. package/src/photon/balancing/format/rpc/request/index.ts +26 -3
  26. package/src/photon/balancing/format/settings/request/index.ts +15 -2
  27. package/src/photon/balancing/format/shared/credentials/index.ts +77 -33
  28. package/src/photon/balancing/format/shared/lobby/index.ts +41 -5
  29. package/src/photon/balancing/format/shared/options/index.ts +56 -24
  30. package/src/photon/balancing/format/table/index.ts +399 -0
  31. package/src/photon/balancing/index.ts +11 -1
  32. package/src/photon/balancing/travel/index.ts +15 -12
  33. package/src/photon/binary/v18/index.ts +26 -1
  34. package/src/photon/binary/value/index.ts +52 -3
  35. package/src/photon/client/connection/index.ts +6 -6
  36. package/src/photon/cluster/back/index.ts +32 -45
  37. package/src/photon/cluster/dresser/index.ts +7 -8
  38. package/src/photon/cluster/front/index.ts +25 -49
  39. package/src/photon/cluster/grid/index.ts +1 -1
  40. package/src/photon/cluster/mover/index.ts +12 -12
  41. package/src/photon/cluster/press/index.ts +2 -18
  42. package/src/photon/cluster/route/game/index.ts +9 -11
  43. package/src/photon/cluster/route/master/index.ts +15 -34
  44. package/src/photon/cluster/route/name/index.ts +4 -21
  45. package/src/photon/cluster/seat/game/index.ts +6 -26
  46. package/src/photon/cluster/seat/master/index.ts +1 -1
  47. package/src/photon/cluster/sessions/index.ts +5 -6
  48. package/src/photon/cluster/spread/index.ts +8 -147
  49. package/src/photon/enet/client/index.ts +1 -8
  50. package/src/photon/enet/gateway/index.ts +5 -15
  51. package/src/photon/enet/index.ts +1 -1
  52. package/src/photon/enet/link/index.ts +34 -15
  53. package/src/photon/enet/outbound/index.ts +279 -0
  54. package/src/photon/enet/peer/index.ts +59 -0
  55. package/src/photon/enet/transport/index.ts +8 -24
  56. package/src/photon/game/admission/index.ts +353 -0
  57. package/src/photon/game/bag/index.ts +13 -0
  58. package/src/photon/game/broadcast/index.ts +79 -0
  59. package/src/photon/game/editor/index.ts +446 -0
  60. package/src/photon/game/gate/index.ts +9 -8
  61. package/src/photon/game/handler/index.ts +30 -3
  62. package/src/photon/game/history/index.ts +234 -0
  63. package/src/photon/game/hooked/index.ts +16 -15
  64. package/src/photon/game/index.ts +12 -1
  65. package/src/photon/game/lifecycle/index.ts +193 -0
  66. package/src/photon/game/peer/index.ts +1 -10
  67. package/src/photon/game/properties/index.ts +342 -0
  68. package/src/photon/game/room/index.ts +435 -1793
  69. package/src/photon/game/rooms/index.ts +2 -1
  70. package/src/photon/game/seats/index.ts +237 -0
  71. package/src/photon/game/server/index.ts +12 -2
  72. package/src/photon/game/services/index.ts +41 -0
  73. package/src/photon/game/settings/index.ts +22 -0
  74. package/src/photon/game/snapshot/index.ts +133 -0
  75. package/src/photon/game/throttle/index.ts +60 -0
  76. package/src/photon/game/touch/index.ts +2 -5
  77. package/src/photon/game/venue/index.ts +31 -0
  78. package/src/photon/master/application/index.ts +7 -0
  79. package/src/photon/master/gate/index.ts +12 -8
  80. package/src/photon/master/handler/index.ts +36 -748
  81. package/src/photon/master/index.ts +5 -0
  82. package/src/photon/master/list/sql/index.ts +37 -3
  83. package/src/photon/master/listing/index.ts +133 -0
  84. package/src/photon/master/lobby/index.ts +9 -0
  85. package/src/photon/master/login/index.ts +424 -0
  86. package/src/photon/master/matchmaking/index.ts +282 -0
  87. package/src/photon/master/query/index.ts +45 -0
  88. package/src/photon/master/redirect/index.ts +8 -7
  89. package/src/photon/master/services/index.ts +35 -0
  90. package/src/photon/master/settings/index.ts +44 -0
  91. package/src/photon/master/sql/cost/index.ts +263 -0
  92. package/src/photon/master/sql/index.ts +1 -0
  93. package/src/photon/message/binary/index.ts +35 -1
  94. package/src/photon/message/client/index.ts +14 -11
  95. package/src/photon/message/format/index.ts +9 -0
  96. package/src/photon/message/handler/index.ts +48 -63
  97. package/src/photon/message/pipeline/index.ts +12 -12
  98. package/src/photon/message/server/index.ts +87 -43
  99. package/src/photon/message/watch/index.ts +127 -15
  100. package/src/photon/server/config/index.ts +5 -0
  101. package/src/photon/server/deferral/index.ts +57 -0
  102. package/src/photon/server/host/index.ts +36 -10
  103. package/src/photon/server/index.ts +1 -0
  104. package/src/photon/server/listeners/index.ts +11 -0
  105. package/src/photon/tcp/client/index.ts +1 -1
  106. package/src/photon/tcp/codec/index.ts +41 -38
  107. package/src/photon/tcp/framing/index.ts +24 -4
  108. package/src/photon/tcp/server/index.ts +4 -4
  109. package/src/photon/websocket/bridge/index.ts +16 -20
  110. package/src/photon/websocket/message/index.ts +27 -39
  111. package/src/photon/websocket/path/index.ts +2 -5
  112. package/src/scale/batch/index.ts +14 -6
  113. package/src/scale/fanout/index.ts +6 -6
  114. package/src/scale/index.ts +2 -4
  115. package/src/scale/outbound/index.ts +192 -0
  116. package/src/scale/relay/index.ts +24 -16
  117. package/src/scale/sink/index.ts +6 -11
  118. package/src/scale/transport/index.ts +27 -44
  119. package/src/scale/tunnel/index.ts +56 -86
  120. package/src/smartfox/common/bluebox/gateway/index.ts +401 -0
  121. package/src/smartfox/common/bluebox/index.ts +4 -0
  122. package/src/smartfox/{x2 → common}/bluebox/transport/index.ts +36 -23
  123. package/src/smartfox/common/index.ts +5 -0
  124. package/src/smartfox/common/strikes/index.ts +74 -0
  125. package/src/smartfox/pro/bluebox/index.ts +176 -4
  126. package/src/smartfox/pro/codec/index.ts +8 -7
  127. package/src/smartfox/pro/escalation/index.ts +28 -20
  128. package/src/smartfox/pro/flood/index.ts +2 -2
  129. package/src/smartfox/pro/format/extension/raw/index.ts +12 -2
  130. package/src/smartfox/pro/handler/chat/index.ts +11 -8
  131. package/src/smartfox/pro/handler/login/index.ts +2 -25
  132. package/src/smartfox/pro/handler/shared/index.ts +4 -3
  133. package/src/smartfox/pro/index.ts +11 -5
  134. package/src/smartfox/pro/login/gate/index.ts +166 -0
  135. package/src/smartfox/pro/login/index.ts +1 -0
  136. package/src/smartfox/pro/moderation/index.ts +220 -0
  137. package/src/smartfox/pro/options/index.ts +5 -5
  138. package/src/smartfox/pro/protocol/index.ts +62 -8
  139. package/src/smartfox/pro/web/index.ts +33 -26
  140. package/src/smartfox/pro/zone/index.ts +59 -282
  141. package/src/smartfox/pro/zones/index.ts +3 -3
  142. package/src/smartfox/x2/bluebox/index.ts +149 -4
  143. package/src/smartfox/x2/chat/index.ts +9 -19
  144. package/src/smartfox/x2/codec/index.ts +34 -40
  145. package/src/smartfox/x2/config/index.ts +6 -1
  146. package/src/smartfox/x2/data/array/index.ts +96 -60
  147. package/src/smartfox/x2/data/object/index.ts +144 -82
  148. package/src/smartfox/x2/data/serializer/index.ts +41 -23
  149. package/src/smartfox/x2/flood/gate/index.ts +19 -13
  150. package/src/smartfox/x2/format/buddy/init/response/index.ts +4 -3
  151. package/src/smartfox/x2/format/find/rooms/response/index.ts +4 -3
  152. package/src/smartfox/x2/format/find/users/response/index.ts +4 -3
  153. package/src/smartfox/x2/format/group/subscribe/response/index.ts +4 -3
  154. package/src/smartfox/x2/format/login/response/index.ts +4 -3
  155. package/src/smartfox/x2/format/mmo/position/response/index.ts +8 -6
  156. package/src/smartfox/x2/format/room/join/response/index.ts +4 -3
  157. package/src/smartfox/x2/games/index.ts +35 -8
  158. package/src/smartfox/x2/hall/index.ts +0 -6
  159. package/src/smartfox/x2/handler/session/index.ts +1 -1
  160. package/src/smartfox/x2/index.ts +8 -3
  161. package/src/smartfox/x2/member/index.ts +10 -3
  162. package/src/smartfox/x2/packet/login/response/index.ts +12 -0
  163. package/src/smartfox/x2/protocol/index.ts +11 -1
  164. package/src/smartfox/x2/rooms/index.ts +1 -2
  165. package/src/smartfox/x2/sessions/index.ts +6 -0
  166. package/src/smartfox/x2/udp/index.ts +61 -24
  167. package/src/smartfox/x2/web/index.ts +35 -17
  168. package/src/smartfox/x2/websocket/index.ts +18 -36
  169. package/src/smartfox/x2/zone/index.ts +77 -28
  170. package/src/smartfox/x2/zones/index.ts +17 -5
  171. package/src/photon/enet/handler/index.ts +0 -65
  172. package/src/scale/hosted/index.ts +0 -68
  173. package/src/scale/outlet/index.ts +0 -121
  174. package/src/smartfox/pro/bluebox/gateway/index.ts +0 -310
  175. package/src/smartfox/pro/bluebox/session/index.ts +0 -63
  176. package/src/smartfox/pro/bluebox/transport/index.ts +0 -104
  177. package/src/smartfox/x2/bluebox/gateway/index.ts +0 -315
  178. /package/src/smartfox/{x2 → common}/bluebox/session/index.ts +0 -0
  179. /package/src/smartfox/{pro → common}/counters/index.ts +0 -0
  180. /package/src/smartfox/{pro → common}/counters/memory/index.ts +0 -0
  181. /package/src/smartfox/{pro → common}/counters/offender/index.ts +0 -0
  182. /package/src/smartfox/{pro → common}/counters/store/index.ts +0 -0
  183. /package/src/smartfox/{pro → common}/sequence/index.ts +0 -0
@@ -9,23 +9,13 @@ import {
9
9
  import type { Session } from '../../../dispatch/session/index.ts'
10
10
  import type { Fanout } from '../../../scale/fanout/index.ts'
11
11
  import type { Protocol } from '../../../protocol/index.ts'
12
- import { Bans } from '../bans/list/index.ts'
13
- import { MemoryBanStore } from '../bans/memory/index.ts'
14
12
  import { MemoryBuddyStore } from '../buddies/memory/index.ts'
15
13
  import { BuddyRoster } from '../buddies/roster/index.ts'
16
14
  import { Claims } from '../claims/index.ts'
17
- import { Offender } from '../counters/offender/index.ts'
18
- import type { CounterStore } from '../counters/store/index.ts'
19
- import type { Escalation } from '../escalation/index.ts'
20
- import { WordFilter } from '../filter/index.ts'
21
- import { FloodGuard } from '../flood/index.ts'
22
- import type { LoginAttempt } from '../login/attempt/index.ts'
23
- import type { Authenticator } from '../login/authenticator/index.ts'
24
- import { LoginVerdict } from '../login/verdict/index.ts'
15
+ import { LoginGate } from '../login/gate/index.ts'
25
16
  import { defaultMessages, type ZoneMessages } from '../messages/index.ts'
26
- import type { Penalty } from '../penalty/index.ts'
17
+ import { Moderation } from '../moderation/index.ts'
27
18
  import type { ZoneLimits, ZoneOptions } from '../options/index.ts'
28
- import { AdminMessage } from '../packet/chat/admin/index.ts'
29
19
  import { RoomAdded } from '../packet/create/added/index.ts'
30
20
  import { RoomDeleted } from '../packet/create/deleted/index.ts'
31
21
  import type { RoomSettings } from '../packet/create/request/index.ts'
@@ -35,8 +25,8 @@ import { UserEntered } from '../packet/presence/entered/index.ts'
35
25
  import { UserLeft } from '../packet/presence/left/index.ts'
36
26
  import { RoomVariablesUpdate } from '../packet/variables/room/update/index.ts'
37
27
  import { Room } from '../room/index.ts'
38
- import { Sequence } from '../sequence/index.ts'
39
- import { User } from '../user/index.ts'
28
+ import { Sequence } from '../../common/sequence/index.ts'
29
+ import type { User } from '../user/index.ts'
40
30
  import type { VariableValue } from '../variable/index.ts'
41
31
 
42
32
  /**
@@ -76,11 +66,6 @@ export class Zone {
76
66
  */
77
67
  public readonly users: Map<number, User>
78
68
 
79
- /**
80
- * Names and addresses that can't log in
81
- */
82
- public readonly bans: Bans
83
-
84
69
  /**
85
70
  * Buddy lists of everyone online
86
71
  */
@@ -92,24 +77,19 @@ export class Zone {
92
77
  public readonly claims: Claims
93
78
 
94
79
  /**
95
- * Decides who gets in
96
- */
97
- public readonly authenticator: Authenticator
98
-
99
- /**
100
- * Hides unwanted words in chat, or null
80
+ * Hears about stores that fail
101
81
  */
102
- public readonly filter: WordFilter | null
82
+ public readonly logger: Logger
103
83
 
104
84
  /**
105
- * Stops users from spamming chat, or null
85
+ * Bans, chat filters and the penalties that go with them
106
86
  */
107
- public readonly flood: FloodGuard | null
87
+ public readonly moderation: Moderation
108
88
 
109
89
  /**
110
- * Hears about stores that fail
90
+ * Decides who logs in and under what name
111
91
  */
112
- public readonly logger: Logger
92
+ public readonly gate: LoginGate
113
93
 
114
94
  /**
115
95
  * Turns packets into bytes for broadcasts
@@ -141,11 +121,6 @@ export class Zone {
141
121
  */
142
122
  private readonly memberships: Map<number, Set<number>>
143
123
 
144
- /**
145
- * Most users online at once
146
- */
147
- private readonly maxUsers: number
148
-
149
124
  /**
150
125
  * Most rooms open at once
151
126
  */
@@ -156,46 +131,6 @@ export class Zone {
156
131
  */
157
132
  private readonly counts: boolean
158
133
 
159
- /**
160
- * How long a kicked user gets to read why, in ms
161
- */
162
- private readonly kickDelay: number
163
-
164
- /**
165
- * Whether a login with no name gets a guest name
166
- */
167
- private readonly guests: boolean
168
-
169
- /**
170
- * What guest names start with
171
- */
172
- private readonly guestPrefix: string
173
-
174
- /**
175
- * Number for the next guest name
176
- */
177
- private nextGuest: number
178
-
179
- /**
180
- * Ids of everyone online under a guest name
181
- */
182
- private readonly guestIds: Set<number>
183
-
184
- /**
185
- * Whether flooding and swearing ban the address too
186
- */
187
- private readonly banAddresses: boolean
188
-
189
- /**
190
- * Where warnings and kicks are counted, or null
191
- */
192
- private readonly counters: CounterStore | null
193
-
194
- /**
195
- * Where user ids come from
196
- */
197
- private readonly userIds: Sequence
198
-
199
134
  /**
200
135
  * Where room ids come from
201
136
  */
@@ -228,18 +163,13 @@ export class Zone {
228
163
  this.limits = {
229
164
  messageLength: options.limits?.messageLength ?? 4096,
230
165
  nameLength: options.limits?.nameLength ?? Infinity,
231
- roomsPerUser: options.limits?.roomsPerUser ?? Infinity,
232
- roomVariables: options.limits?.roomVariables ?? Infinity,
233
- userVariables: options.limits?.userVariables ?? Infinity,
166
+ roomsPerUser: options.limits?.roomsPerUser ?? 5,
167
+ roomVariables: options.limits?.roomVariables ?? 100,
168
+ userVariables: options.limits?.userVariables ?? 100,
234
169
  }
235
170
  this.logger = options.logger ?? new ConsoleLogger()
236
171
  this.rooms = new Map<number, Room>()
237
172
  this.users = new Map<number, User>()
238
- this.bans = new Bans(
239
- options.bans ?? new MemoryBanStore(),
240
- this.logger,
241
- options.banOutage === 'refuse',
242
- )
243
173
  this.buddies = new BuddyRoster(
244
174
  this,
245
175
  options.buddies?.store ?? new MemoryBuddyStore(),
@@ -255,34 +185,16 @@ export class Zone {
255
185
  },
256
186
  )
257
187
  this.claims = new Claims()
258
- this.authenticator =
259
- options.authenticator ??
260
- ((attempt: LoginAttempt): LoginVerdict =>
261
- LoginVerdict.accept(attempt.name))
262
- this.filter =
263
- options.filter === undefined ? null : new WordFilter(options.filter)
264
- this.flood =
265
- options.flood === undefined ? null : new FloodGuard(options.flood)
188
+ this.moderation = new Moderation(this, options)
189
+ this.gate = new LoginGate(this, options, userIds)
266
190
  this.protocol = protocol
267
191
  this.sessions = new Map<number, Session<User>>()
268
192
  this.names = new Map<string, User>()
269
193
  this.crowds = new Map<number, ChannelGroup>()
270
194
  this.everyone = new ChannelGroup()
271
195
  this.memberships = new Map<number, Set<number>>()
272
- this.maxUsers = options.maxUsers ?? Infinity
273
- this.maxRooms = options.maxRooms ?? Infinity
196
+ this.maxRooms = options.maxRooms ?? 10_000
274
197
  this.counts = options.counts ?? true
275
- this.kickDelay = options.kickDelay ?? 1000
276
- this.guests = options.guests ?? true
277
- this.guestPrefix = options.guestPrefix ?? 'guest_'
278
- this.nextGuest = 1
279
- this.guestIds = new Set<number>()
280
- this.banAddresses = options.banMode !== 'name'
281
- this.counters = options.counters ?? null
282
- if (this.flood?.shared === true && this.counters === null) {
283
- throw new RangeError('Counter store is missing')
284
- }
285
- this.userIds = userIds
286
198
  this.roomIds = roomIds
287
199
 
288
200
  for (const plan of options.rooms ?? []) {
@@ -423,32 +335,19 @@ export class Zone {
423
335
  }
424
336
 
425
337
  /**
426
- * Logs a connection in, or says why it can't
338
+ * Seats a user the gate let in
427
339
  * @param session the connection
428
- * @param name name the user plays under, empty for a guest
429
- * @param moderator whether they can kick and ban
340
+ * @param user who logged in
430
341
  */
431
- public login(
432
- session: Session<User>,
433
- name: string,
434
- moderator = false,
435
- ): string | null {
436
- const reason = this.loginRefusal(session, name)
437
- if (reason !== null) return reason
438
-
439
- const known = name.length > 0 ? name : this.guest()
440
- const user = User.of(this.userIds.next(), known, moderator)
441
- if (name.length === 0) this.guestIds.add(user.id)
342
+ public enter(session: Session<User>, user: User): void {
442
343
  this.users.set(user.id, user)
443
- this.names.set(known.toLowerCase(), user)
344
+ this.names.set(user.name.toLowerCase(), user)
444
345
  this.sessions.set(user.id, session)
445
346
  this.memberships.set(user.id, new Set<number>())
446
347
  this.everyone.add(session.channel)
447
348
  this.fanout?.join(session.channel, `${this.name}:all`)
448
349
  session.channel.attributes.set<Zone>(Zone.key, this)
449
350
  session.user = user
450
-
451
- return null
452
351
  }
453
352
 
454
353
  /**
@@ -464,15 +363,20 @@ export class Zone {
464
363
  if (room !== undefined) this.depart(user, session.channel, room, true)
465
364
  }
466
365
 
366
+ for (const room of [...this.rooms.values()]) {
367
+ if (room.temporary && room.owner === user.id && room.users.size === 0) {
368
+ this.shut(room)
369
+ }
370
+ }
371
+
467
372
  this.users.delete(user.id)
468
373
  this.names.delete(user.name.toLowerCase())
469
374
  this.sessions.delete(user.id)
470
375
  this.memberships.delete(user.id)
471
- this.guestIds.delete(user.id)
472
376
  this.everyone.remove(session.channel)
473
377
  this.fanout?.leave(session.channel, `${this.name}:all`)
474
- this.flood?.forget(user.id)
475
- this.filter?.escalation?.forget(user.id)
378
+ this.gate.forget(user)
379
+ this.moderation.forget(user)
476
380
  session.channel.attributes.delete<Zone>(Zone.key)
477
381
  session.user = null
478
382
 
@@ -558,7 +462,7 @@ export class Zone {
558
462
  return
559
463
  }
560
464
 
561
- crowd.broadcastSync(
465
+ crowd.broadcast(
562
466
  this.bytes(packet),
563
467
  except === null ? null : ChannelGroup.except(except),
564
468
  )
@@ -576,7 +480,15 @@ export class Zone {
576
480
  return
577
481
  }
578
482
 
579
- this.everyone.broadcastSync(this.bytes(packet))
483
+ this.everyone.broadcast(this.bytes(packet))
484
+ }
485
+
486
+ /**
487
+ * The connection of someone online, or null
488
+ * @param user id of who to look up
489
+ */
490
+ public session(user: number): Session<User> | null {
491
+ return this.sessions.get(user) ?? null
580
492
  }
581
493
 
582
494
  /**
@@ -593,172 +505,37 @@ export class Zone {
593
505
  }
594
506
 
595
507
  /**
596
- * Tells everyone how many are in a room
597
- * @param room the room that changed
598
- */
599
- public recount(room: Room): void {
600
- if (!this.counts) return
601
-
602
- this.announce(UserCount.of(room.id, room.userCount, room.spectatorCount))
603
- }
604
-
605
- /**
606
- * Tells a user why, then hangs up on them
607
- * @param user who to put out
608
- * @param text what they read first
609
- */
610
- public kick(user: User, text: string): void {
611
- const session = this.sessions.get(user.id)
612
- if (session === undefined) return
613
-
614
- session.send(AdminMessage.of(-1, 0, text))
615
- setTimeout((): void => {
616
- void session.close()
617
- }, this.kickDelay)
618
- }
619
-
620
- /**
621
- * Kicks a user and keeps their name and address out
622
- * @param user who to ban
623
- * @param text what they read first
624
- * @param duration how long it lasts in ms, null for good
625
- */
626
- public async ban(
627
- user: User,
628
- text: string,
629
- duration: number | null = null,
630
- ): Promise<void> {
631
- const session = this.sessions.get(user.id)
632
-
633
- this.kick(user, text)
634
- await this.bans.add(
635
- user.name,
636
- session?.channel.remoteAddress ?? '',
637
- duration,
638
- )
639
- }
640
-
641
- /**
642
- * Warns, kicks or bans a user for breaking a rule
643
- * @param user who broke it
644
- * @param escalation what that rule earns
645
- */
646
- public discipline(user: User, escalation: Escalation): Promise<void> {
647
- return this.counters === null
648
- ? this.punish(user, escalation.strike(user.id, user.name))
649
- : this.counted(user, escalation, this.counters)
650
- }
651
-
652
- /**
653
- * Counts a chat line in the counter store and says whether it's flooding
654
- * @param user who wrote it
655
- * @param text what they wrote
508
+ * Sends a packet to a list of users, encoded once
509
+ * @param users ids of who gets it
510
+ * @param packet what to send
656
511
  */
657
- public async floods(user: User, text: string): Promise<boolean> {
658
- const flood = this.flood
659
- const counters = this.counters
660
- if (flood?.shared !== true || counters === null) return false
661
-
662
- try {
663
- return await flood.counts(counters, this.offender(user), text)
664
- } catch (err) {
665
- this.logger.warn({ err }, 'Counter store failed')
666
-
667
- return false
512
+ public deliver(users: Iterable<number>, packet: object): void {
513
+ const channels = new Set<Channel>()
514
+ for (const user of users) {
515
+ const session = this.sessions.get(user)
516
+ if (session !== undefined) channels.add(session.channel)
668
517
  }
669
- }
670
518
 
671
- /**
672
- * Carries out one step up the ladder
673
- * @param user who broke a rule
674
- * @param penalty what they earned
675
- */
676
- private async punish(user: User, penalty: Penalty): Promise<void> {
677
- switch (penalty.action) {
678
- case 'warn':
679
- this.send(user.id, AdminMessage.of(-1, 0, penalty.text))
680
- break
681
- case 'kick':
682
- this.kick(user, penalty.text)
683
- break
684
- case 'ban':
685
- if (this.banAddresses) {
686
- await this.ban(user, penalty.text, penalty.duration)
687
- break
688
- }
689
-
690
- this.kick(user, penalty.text)
691
- await this.bans.add(user.name, '', penalty.duration)
692
- break
693
- }
694
- }
695
-
696
- /**
697
- * Punishes by the counts in a store
698
- * @param user who broke a rule
699
- * @param escalation what that rule earns
700
- * @param counters where warnings and kicks are counted
701
- */
702
- private async counted(
703
- user: User,
704
- escalation: Escalation,
705
- counters: CounterStore,
706
- ): Promise<void> {
707
- let penalty: Penalty
708
- try {
709
- penalty = await escalation.count(counters, this.offender(user))
710
- } catch (err) {
711
- this.logger.warn({ err }, 'Counter store failed')
712
- penalty = escalation.strike(user.id, user.name)
519
+ if (channels.size === 0) return
520
+ if (this.fanout !== null) {
521
+ this.fanout.spread(channels, packet)
522
+ return
713
523
  }
714
524
 
715
- await this.punish(user, penalty)
716
- }
717
-
718
- /**
719
- * Who a user's offences are counted against
720
- * @param user who broke a rule
721
- */
722
- private offender(user: User): Offender {
723
- const address = this.sessions.get(user.id)?.channel.remoteAddress ?? ''
724
-
725
- return this.guestIds.has(user.id) && address !== ''
726
- ? Offender.addressed(this.name, address)
727
- : Offender.named(this.name, user.name.toLowerCase())
728
- }
729
-
730
- /**
731
- * Whether a name is too long or has a listed word
732
- * @param name a user or room name
733
- */
734
- private unfit(name: string): boolean {
735
- if (name.length > this.limits.nameLength) return true
736
-
737
- return this.filter !== null && this.filter.names && this.filter.has(name)
738
- }
739
-
740
- /**
741
- * A guest name nobody online has
742
- */
743
- private guest(): string {
744
- for (;;) {
745
- const name = `${this.guestPrefix}${String(this.nextGuest++)}`
746
- if (!this.names.has(name.toLowerCase())) return name
525
+ const bytes = this.bytes(packet)
526
+ for (const channel of channels) {
527
+ channel.send(bytes)
747
528
  }
748
529
  }
749
530
 
750
531
  /**
751
- * Why a connection can't log in, or null
752
- * @param session the connection
753
- * @param name name the user plays under, empty for a guest
532
+ * Tells everyone how many are in a room
533
+ * @param room the room that changed
754
534
  */
755
- private loginRefusal(session: Session<User>, name: string): string | null {
756
- if (session.user !== null) return this.messages.alreadyLoggedIn
757
- if (name.length === 0 && !this.guests) return this.messages.nameMissing
758
- if (this.unfit(name)) return this.messages.nameRefused
759
- if (this.names.has(name.toLowerCase())) return this.messages.nameTaken
535
+ public recount(room: Room): void {
536
+ if (!this.counts) return
760
537
 
761
- return this.users.size >= this.maxUsers ? this.messages.zoneFull : null
538
+ this.announce(UserCount.of(room.id, room.userCount, room.spectatorCount))
762
539
  }
763
540
 
764
541
  /**
@@ -768,7 +545,7 @@ export class Zone {
768
545
  */
769
546
  private roomRefusal(user: User, name: string): string | null {
770
547
  if (name.length === 0) return this.messages.roomNameMissing
771
- if (this.unfit(name)) return this.messages.roomNameRefused
548
+ if (this.moderation.unfit(name)) return this.messages.roomNameRefused
772
549
  if (this.rooms.size >= this.maxRooms) return this.messages.zoneFull
773
550
 
774
551
  let owned = 0
@@ -13,7 +13,7 @@ import { Layout } from '../../../scale/layout/index.ts'
13
13
  import { Mesh } from '../../../scale/mesh/index.ts'
14
14
  import type { PlacementRule } from '../../../scale/placement/index.ts'
15
15
  import type { Portable, Reach } from '../../../scale/portable/index.ts'
16
- import { BlueBox } from '../bluebox/gateway/index.ts'
16
+ import { BlueBox } from '../bluebox/index.ts'
17
17
  import { SmartFoxProCodec } from '../codec/index.ts'
18
18
  import { BuddyHandler } from '../handler/buddy/index.ts'
19
19
  import { ChatHandler } from '../handler/chat/index.ts'
@@ -26,7 +26,7 @@ import { defaultMessages } from '../messages/index.ts'
26
26
  import type { ZoneOptions } from '../options/index.ts'
27
27
  import type { LoginRequest } from '../packet/login/request/index.ts'
28
28
  import { SmartFoxPro } from '../protocol/index.ts'
29
- import { Sequence } from '../sequence/index.ts'
29
+ import { Sequence } from '../../common/sequence/index.ts'
30
30
  import type { User } from '../user/index.ts'
31
31
  import { Zone } from '../zone/index.ts'
32
32
 
@@ -310,7 +310,7 @@ export class Zones implements Portable<SessionSeed> {
310
310
  */
311
311
  public serve(dispatcher: PacketDispatcher<User>): PacketDispatcher<User> {
312
312
  const shared = [...this.zones.values()].some(
313
- (zone: Zone): boolean => zone.flood?.shared === true,
313
+ (zone: Zone): boolean => zone.moderation.flood?.shared === true,
314
314
  )
315
315
 
316
316
  return dispatcher
@@ -1,4 +1,149 @@
1
- export { BlueBox } from './gateway/index.ts'
2
- export type { BlueBoxOptions } from './gateway/index.ts'
3
- export { BlueBoxSession } from './session/index.ts'
4
- export { BlueBoxTransport } from './transport/index.ts'
1
+ import type { ChannelSetup, ShardLink } from '@netifyjs/netify'
2
+
3
+ import {
4
+ BlueBoxGateway,
5
+ type BlueBoxRequest,
6
+ } from '../../common/bluebox/gateway/index.ts'
7
+
8
+ /**
9
+ * How the HTTP tunnel is set up
10
+ */
11
+ export type BlueBoxOptions = {
12
+ /**
13
+ * Builds the pipeline of each tunnelled connection
14
+ */
15
+ readonly setup: ChannelSetup
16
+
17
+ /**
18
+ * Quiet time before a session is dropped, 60000 ms by default
19
+ */
20
+ readonly timeout?: number
21
+
22
+ /**
23
+ * Longest a poll is held open for data, 25000 ms by default
24
+ */
25
+ readonly hold?: number
26
+
27
+ /**
28
+ * Most sessions open at once, 10000 by default
29
+ */
30
+ readonly maxSessions?: number
31
+
32
+ /**
33
+ * Most bytes one session holds between polls, 256 KiB by default
34
+ */
35
+ readonly maxQueued?: number
36
+
37
+ /**
38
+ * Connects the server's shards
39
+ */
40
+ readonly link?: ShardLink | null
41
+
42
+ /**
43
+ * Goes in front of the link topics the tunnel uses
44
+ */
45
+ readonly topic?: string
46
+ }
47
+
48
+ /**
49
+ * SmartFoxServer 2X's HTTP tunnel, with base64 packets behind pipes
50
+ * @class
51
+ */
52
+ export class BlueBox extends BlueBoxGateway {
53
+ /**
54
+ * Goes in front of the link topics the tunnel uses by default
55
+ */
56
+ public static readonly topic: string = 'netify:protocol:smartfox2x:bluebox'
57
+
58
+ /**
59
+ * Path the client posts to
60
+ */
61
+ public static readonly path: string = '/BlueBox/BlueBox.do'
62
+
63
+ /**
64
+ * Form field the client puts its request in
65
+ */
66
+ public static readonly field: string = 'sfsHttp'
67
+
68
+ /**
69
+ * What a client with no session is told
70
+ */
71
+ public static readonly lost: string = 'err01|Invalid http session !'
72
+
73
+ /**
74
+ * What a poll with nothing waiting is told
75
+ */
76
+ public static readonly idle: string = 'poll|null'
77
+
78
+ /**
79
+ * What stands in for a missing field
80
+ */
81
+ private static readonly nothing: string = 'null'
82
+
83
+ /**
84
+ * @param options how the tunnel is set up
85
+ */
86
+ private constructor(options: BlueBoxOptions) {
87
+ super({
88
+ setup: options.setup,
89
+ timeout: options.timeout ?? 60_000,
90
+ hold: options.hold ?? 25_000,
91
+ maxSessions: options.maxSessions ?? 10_000,
92
+ maxQueued: options.maxQueued ?? 256 * 1024,
93
+ link: options.link ?? null,
94
+ topic: options.topic ?? BlueBox.topic,
95
+ lost: BlueBox.lost,
96
+ idle: BlueBox.idle,
97
+ })
98
+ }
99
+
100
+ /**
101
+ * A tunnel with no sessions
102
+ * @param options how the tunnel is set up
103
+ */
104
+ public static of(options: BlueBoxOptions): BlueBox {
105
+ return new BlueBox(options)
106
+ }
107
+
108
+ /**
109
+ * Reads the id, command and data a request holds between pipes
110
+ * @param request the form field's value
111
+ */
112
+ protected parse(request: string): BlueBoxRequest {
113
+ const [id = '', command = '', data = BlueBox.nothing] = request.split('|')
114
+
115
+ return { id, command, data }
116
+ }
117
+
118
+ /**
119
+ * Decodes the base64 a client sent, or null for none
120
+ * @param data what came with the request
121
+ */
122
+ protected bytes(data: string): Uint8Array | null {
123
+ return data === BlueBox.nothing ? null : Buffer.from(data, 'base64')
124
+ }
125
+
126
+ /**
127
+ * Hands over the whole queue, since packets are framed
128
+ * @param bytes everything queued for a poll
129
+ */
130
+ protected whole(bytes: Uint8Array): number {
131
+ return bytes.length
132
+ }
133
+
134
+ /**
135
+ * Tells the client its session id
136
+ * @param id the session's id
137
+ */
138
+ protected opened(id: string): string {
139
+ return `connect|${id}`
140
+ }
141
+
142
+ /**
143
+ * Sends the waiting packets as base64
144
+ * @param bytes whole packets waiting for the client
145
+ */
146
+ protected packed(bytes: Uint8Array): string {
147
+ return `poll|${Buffer.from(bytes).toString('base64')}`
148
+ }
149
+ }