@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
@@ -1,4 +1,176 @@
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
+ import { SmartFoxPro } from '../protocol/index.ts'
8
+
9
+ /**
10
+ * How the HTTP tunnel is set up
11
+ */
12
+ export type BlueBoxOptions = {
13
+ /**
14
+ * Builds the pipeline of each tunnelled connection
15
+ */
16
+ readonly setup: ChannelSetup
17
+
18
+ /**
19
+ * Quiet time before a session is dropped, 30000 ms by default
20
+ */
21
+ readonly timeout?: number
22
+
23
+ /**
24
+ * Most sessions open at once, 10000 by default
25
+ */
26
+ readonly maxSessions?: number
27
+
28
+ /**
29
+ * Most bytes one session holds between polls, 64 KiB by default
30
+ */
31
+ readonly maxQueued?: number
32
+
33
+ /**
34
+ * What crossdomain.xml answers, null to switch it off
35
+ */
36
+ readonly policy?: string | null
37
+
38
+ /**
39
+ * Connects the server's shards
40
+ */
41
+ readonly link?: ShardLink | null
42
+
43
+ /**
44
+ * Goes in front of the link topics the tunnel uses
45
+ */
46
+ readonly topic?: string
47
+ }
48
+
49
+ /**
50
+ * SmartFoxServer Pro's HTTP tunnel, with plain text messages one per line
51
+ * @class
52
+ */
53
+ export class BlueBox extends BlueBoxGateway {
54
+ /**
55
+ * Goes in front of the link topics the tunnel uses by default
56
+ */
57
+ public static readonly topic: string = 'netify:protocol:smartfox:bluebox'
58
+
59
+ /**
60
+ * Path the client posts to
61
+ */
62
+ public static readonly path: string = '/BlueBox/HttpBox.do'
63
+
64
+ /**
65
+ * Form field the client puts its request in
66
+ */
67
+ public static readonly field: string = 'sfsHttp'
68
+
69
+ /**
70
+ * What a client with no session is told
71
+ */
72
+ public static readonly lost: string = 'ERR#01'
73
+
74
+ /**
75
+ * What a poll with nothing waiting is told
76
+ */
77
+ public static readonly idle: string = 'ok'
78
+
79
+ /**
80
+ * Length of a session id
81
+ */
82
+ private static readonly idLength: number = 32
83
+
84
+ /**
85
+ * What crossdomain.xml answers, or null
86
+ */
87
+ public readonly policy: string | null
88
+
89
+ /**
90
+ * Turns client text into bytes
91
+ */
92
+ private readonly encoder: TextEncoder
93
+
94
+ /**
95
+ * Turns queued bytes into text
96
+ */
97
+ private readonly decoder: TextDecoder
98
+
99
+ /**
100
+ * @param options how the tunnel is set up
101
+ */
102
+ private constructor(options: BlueBoxOptions) {
103
+ super({
104
+ setup: options.setup,
105
+ timeout: options.timeout ?? 30_000,
106
+ hold: 0,
107
+ maxSessions: options.maxSessions ?? 10_000,
108
+ maxQueued: options.maxQueued ?? 64 * 1024,
109
+ link: options.link ?? null,
110
+ topic: options.topic ?? BlueBox.topic,
111
+ lost: BlueBox.lost,
112
+ idle: BlueBox.idle,
113
+ })
114
+
115
+ this.policy =
116
+ options.policy === undefined ? SmartFoxPro.defaultPolicy : options.policy
117
+ this.encoder = new TextEncoder()
118
+ this.decoder = new TextDecoder()
119
+ }
120
+
121
+ /**
122
+ * A tunnel with no sessions
123
+ * @param options how the tunnel is set up
124
+ */
125
+ public static of(options: BlueBoxOptions): BlueBox {
126
+ return new BlueBox(options)
127
+ }
128
+
129
+ /**
130
+ * Reads the session id up front and treats anything but a command as data
131
+ * @param request the form field's value
132
+ */
133
+ protected parse(request: string): BlueBoxRequest {
134
+ if (request === 'connect') return { id: '', command: request, data: '' }
135
+
136
+ const id = request.slice(0, BlueBox.idLength)
137
+ const message = request.slice(BlueBox.idLength)
138
+ if (message === 'poll' || message === 'disconnect') {
139
+ return { id, command: message, data: '' }
140
+ }
141
+
142
+ return { id, command: 'data', data: message }
143
+ }
144
+
145
+ /**
146
+ * One message with the terminator the decoder looks for
147
+ * @param data what came with the request
148
+ */
149
+ protected bytes(data: string): Uint8Array {
150
+ return this.encoder.encode(`${data}\0`)
151
+ }
152
+
153
+ /**
154
+ * Stops after the last whole message, leaving a cut one for later
155
+ * @param bytes everything queued for a poll
156
+ */
157
+ protected whole(bytes: Uint8Array): number {
158
+ return bytes.lastIndexOf(0) + 1
159
+ }
160
+
161
+ /**
162
+ * Tells the client its session id
163
+ * @param id the session's id
164
+ */
165
+ protected opened(id: string): string {
166
+ return `#${id}`
167
+ }
168
+
169
+ /**
170
+ * Puts each waiting message on its own line
171
+ * @param bytes whole messages waiting for the client
172
+ */
173
+ protected packed(bytes: Uint8Array): string {
174
+ return this.decoder.decode(bytes).replaceAll('\0', '\n')
175
+ }
176
+ }
@@ -1,4 +1,8 @@
1
- import { ByteBuf, DecoderError, type HandlerContext } from '@netifyjs/netify'
1
+ import {
2
+ type ByteBuf,
3
+ DecoderError,
4
+ type HandlerContext,
5
+ } from '@netifyjs/netify'
2
6
 
3
7
  import { PacketCodec } from '../../../codec/index.ts'
4
8
  import type { SmartFoxPro } from '../protocol/index.ts'
@@ -11,7 +15,7 @@ export class SmartFoxProCodec extends PacketCodec<object> {
11
15
  /**
12
16
  * The protocol that knows the messages
13
17
  */
14
- private readonly protocol: SmartFoxPro
18
+ declare protected readonly protocol: SmartFoxPro
15
19
 
16
20
  /**
17
21
  * Whether the bytes arriving belong to a message that was too long
@@ -24,7 +28,6 @@ export class SmartFoxProCodec extends PacketCodec<object> {
24
28
  public constructor(protocol: SmartFoxPro) {
25
29
  super(protocol)
26
30
 
27
- this.protocol = protocol
28
31
  this.dropping = false
29
32
  }
30
33
 
@@ -50,9 +53,7 @@ export class SmartFoxProCodec extends PacketCodec<object> {
50
53
 
51
54
  input.skipBytes(end === -1 ? input.readableBytes : size + 1)
52
55
  if (!this.dropping) {
53
- void context.fireExceptionCaught(
54
- new DecoderError('Frame is too large'),
55
- )
56
+ context.fireExceptionCaught(new DecoderError('Frame is too large'))
56
57
  }
57
58
 
58
59
  this.dropping = end === -1
@@ -81,7 +82,7 @@ export class SmartFoxProCodec extends PacketCodec<object> {
81
82
  }
82
83
 
83
84
  const bytes = protocol.encode(answer)
84
- if (bytes !== null) context.writeAndFlushSync(ByteBuf.wrap(bytes))
85
+ if (bytes !== null) context.writeAndFlush(bytes)
85
86
  }
86
87
  }
87
88
  }
@@ -1,5 +1,6 @@
1
- import type { Offender } from '../counters/offender/index.ts'
2
- import type { CounterStore } from '../counters/store/index.ts'
1
+ import type { Offender } from '../../common/counters/offender/index.ts'
2
+ import type { CounterStore } from '../../common/counters/store/index.ts'
3
+ import { type Sentence, Strikes } from '../../common/strikes/index.ts'
3
4
  import { Penalty } from '../penalty/index.ts'
4
5
 
5
6
  /**
@@ -47,11 +48,6 @@ export type EscalationOptions = {
47
48
  * @class
48
49
  */
49
50
  export class Escalation {
50
- /**
51
- * Rule name inside counter keys
52
- */
53
- private readonly rule: string
54
-
55
51
  /**
56
52
  * Warnings before a kick
57
53
  */
@@ -97,6 +93,11 @@ export class Escalation {
97
93
  */
98
94
  private readonly kicks: Map<string, number[]>
99
95
 
96
+ /**
97
+ * Counts the same ladder in a counter store
98
+ */
99
+ private readonly strikes: Strikes
100
+
100
101
  /**
101
102
  * @param options how it climbs
102
103
  * @param rule what was broken, for the default texts
@@ -109,7 +110,6 @@ export class Escalation {
109
110
  ) {
110
111
  const lower = rule.toLowerCase()
111
112
 
112
- this.rule = lower
113
113
  this.warnings = options.warnings ?? defaults.warnings ?? 2
114
114
  this.warningMessage =
115
115
  options.warningMessage ?? defaults.warningMessage ?? `${rule} warning`
@@ -127,6 +127,12 @@ export class Escalation {
127
127
  this.banFor = options.banFor ?? defaults.banFor ?? null
128
128
  this.warned = new Map<number, number>()
129
129
  this.kicks = new Map<string, number[]>()
130
+ this.strikes = new Strikes(
131
+ lower,
132
+ this.warnings,
133
+ this.banAfter,
134
+ this.banWithin,
135
+ )
130
136
  }
131
137
 
132
138
  /**
@@ -168,20 +174,22 @@ export class Escalation {
168
174
  store: CounterStore,
169
175
  offender: Offender,
170
176
  ): Promise<Penalty> {
171
- const warnings = offender.counter(`${this.rule}:warnings`)
172
- const warned = await store.hit(warnings, this.banWithin)
173
- if (warned <= this.warnings) return Penalty.warn(this.warningMessage)
174
-
175
- await store.clear(warnings)
177
+ return this.penalty(await this.strikes.count(store, offender))
178
+ }
176
179
 
177
- const kicks = offender.counter(`${this.rule}:kicks`)
178
- const kicked = await store.hit(kicks, this.banWithin)
179
- if (this.banAfter === null || kicked <= this.banAfter) {
180
- return Penalty.kick(this.kickMessage)
180
+ /**
181
+ * The penalty that goes with a sentence
182
+ * @param sentence what the offence earned
183
+ */
184
+ private penalty(sentence: Sentence): Penalty {
185
+ switch (sentence) {
186
+ case 'warn':
187
+ return Penalty.warn(this.warningMessage)
188
+ case 'kick':
189
+ return Penalty.kick(this.kickMessage)
190
+ case 'ban':
191
+ return Penalty.ban(this.banMessage, this.banFor)
181
192
  }
182
-
183
- await store.clear(kicks)
184
- return Penalty.ban(this.banMessage, this.banFor)
185
193
  }
186
194
 
187
195
  /**
@@ -1,5 +1,5 @@
1
- import type { Offender } from '../counters/offender/index.ts'
2
- import type { CounterStore } from '../counters/store/index.ts'
1
+ import type { Offender } from '../../common/counters/offender/index.ts'
2
+ import type { CounterStore } from '../../common/counters/store/index.ts'
3
3
  import { Escalation, type EscalationOptions } from '../escalation/index.ts'
4
4
 
5
5
  /**
@@ -132,8 +132,18 @@ export class RawFormat {
132
132
  * @param text the whole message
133
133
  */
134
134
  private split(text: string): string[] {
135
- const end = text.endsWith(this.separator) ? text.length - 1 : text.length
135
+ const separator = this.separator
136
+ const end = text.endsWith(separator) ? text.length - 1 : text.length
137
+ const fields: string[] = []
138
+
139
+ let from = 1
140
+ for (let at = text.indexOf(separator, from); at >= 0 && at < end;) {
141
+ fields.push(text.substring(from, at))
142
+ from = at + separator.length
143
+ at = text.indexOf(separator, from)
144
+ }
136
145
 
137
- return text.substring(1, end).split(this.separator)
146
+ fields.push(text.substring(from, end))
147
+ return fields
138
148
  }
139
149
  }
@@ -122,10 +122,10 @@ export class ChatHandler {
122
122
  if (zone === null || user === null) return
123
123
  if (!zone.inside(user, packet.room)) return
124
124
 
125
- const message = DataObject.of(packet.room, user.id, packet.data)
126
- for (const recipient of packet.recipients) {
127
- zone.send(recipient, message)
128
- }
125
+ zone.deliver(
126
+ packet.recipients,
127
+ DataObject.of(packet.room, user.id, packet.data),
128
+ )
129
129
  }
130
130
 
131
131
  /**
@@ -139,19 +139,22 @@ export class ChatHandler {
139
139
  user: User,
140
140
  text: string,
141
141
  ): string | null | Promise<string | null> {
142
- const flood = zone.flood
142
+ const moderation = zone.moderation
143
+ const flood = moderation.flood
143
144
  if (flood?.floods(user.id, text) === true) {
144
- return zone.discipline(user, flood.escalation).then((): null => null)
145
+ return moderation
146
+ .discipline(user, flood.escalation)
147
+ .then((): null => null)
145
148
  }
146
149
 
147
150
  if (text.length > zone.limits.messageLength) return null
148
151
 
149
- const filter = zone.filter
152
+ const filter = moderation.filter
150
153
  if (filter === null) return text
151
154
 
152
155
  const escalation = filter.escalation
153
156
  if (escalation !== null && filter.has(text)) {
154
- return zone
157
+ return moderation
155
158
  .discipline(user, escalation)
156
159
  .then((): string | null => filter.clean(text))
157
160
  }
@@ -98,7 +98,8 @@ export class LoginHandler {
98
98
  session.channel.remoteAddress,
99
99
  )
100
100
 
101
- const reason = await this.refusal(session, zone, attempt)
101
+ const reason = await zone.gate.admit(session, attempt)
102
+ if (!session.channel.isActive) return
102
103
  if (reason !== null) {
103
104
  session.send(LoginRefused.of(reason))
104
105
  return
@@ -148,28 +149,4 @@ export class LoginHandler {
148
149
  public async idle(session: Session<User>): Promise<void> {
149
150
  await session.close()
150
151
  }
151
-
152
- /**
153
- * Logs the connection in, or says why it can't
154
- * @param session the connection
155
- * @param zone where the client wants in
156
- * @param attempt what the client sent
157
- */
158
- private async refusal(
159
- session: Session<User>,
160
- zone: Zone,
161
- attempt: LoginAttempt,
162
- ): Promise<string | null> {
163
- if (session.user !== null) return zone.messages.alreadyLoggedIn
164
-
165
- const banned = await zone.bans.has(attempt.name, attempt.address)
166
- if (banned === 'unreachable') return zone.messages.unavailable
167
- if (banned) return zone.messages.banned
168
-
169
- const verdict = await zone.authenticator(attempt)
170
-
171
- return (
172
- verdict.reason ?? zone.login(session, verdict.name, verdict.moderator)
173
- )
174
- }
175
152
  }
@@ -18,9 +18,10 @@ export class SharedChatHandler extends ChatHandler {
18
18
  user: User,
19
19
  text: string,
20
20
  ): Promise<string | null> {
21
- const flood = zone.flood
22
- if (flood?.shared === true && (await zone.floods(user, text))) {
23
- await zone.discipline(user, flood.escalation)
21
+ const moderation = zone.moderation
22
+ const flood = moderation.flood
23
+ if (flood?.shared === true && (await moderation.floods(user, text))) {
24
+ await moderation.discipline(user, flood.escalation)
24
25
  return null
25
26
  }
26
27
 
@@ -1,14 +1,20 @@
1
1
  export { Bans, MemoryBanStore } from './bans/index.ts'
2
- export { BlueBox, BlueBoxSession, BlueBoxTransport } from './bluebox/index.ts'
2
+ export { BlueBox } from './bluebox/index.ts'
3
3
  export type { BlueBoxOptions } from './bluebox/index.ts'
4
+ export {
5
+ BlueBoxSession,
6
+ BlueBoxTransport,
7
+ MemoryCounterStore,
8
+ Offender,
9
+ Sequence,
10
+ } from '../common/index.ts'
11
+ export type { CounterStore } from '../common/index.ts'
4
12
  export type { BanRecord, BanStore } from './bans/index.ts'
5
13
  export { BuddyRoster, MemoryBuddyStore } from './buddies/index.ts'
6
14
  export type { BuddyOptions, BuddyRecord, BuddyStore } from './buddies/index.ts'
7
15
  export { Buddy } from './buddy/index.ts'
8
16
  export { Catalogue } from './catalogue/index.ts'
9
17
  export { Claims } from './claims/index.ts'
10
- export { MemoryCounterStore, Offender } from './counters/index.ts'
11
- export type { CounterStore } from './counters/index.ts'
12
18
  export { SmartFoxProCodec } from './codec/index.ts'
13
19
  export { Entities } from './entity/index.ts'
14
20
  export { Escalation } from './escalation/index.ts'
@@ -27,10 +33,11 @@ export {
27
33
  SharedChatHandler,
28
34
  VariableHandler,
29
35
  } from './handler/index.ts'
30
- export { LoginAttempt, LoginVerdict } from './login/index.ts'
36
+ export { LoginAttempt, LoginGate, LoginVerdict } from './login/index.ts'
31
37
  export type { Authenticator } from './login/index.ts'
32
38
  export { defaultMessages } from './messages/index.ts'
33
39
  export type { ZoneMessages } from './messages/index.ts'
40
+ export { Moderation } from './moderation/index.ts'
34
41
  export type {
35
42
  BuddyPlan,
36
43
  RoomPlan,
@@ -45,7 +52,6 @@ export { SmartFoxPro } from './protocol/index.ts'
45
52
  export type { SmartFoxProOptions } from './protocol/index.ts'
46
53
  export { Room } from './room/index.ts'
47
54
  export { json, object, raw } from './route/index.ts'
48
- export { Sequence } from './sequence/index.ts'
49
55
  export { User } from './user/index.ts'
50
56
  export type {
51
57
  RoomVariable,
@@ -0,0 +1,166 @@
1
+ import type { Session } from '../../../../dispatch/session/index.ts'
2
+ import type { Sequence } from '../../../common/sequence/index.ts'
3
+ import type { ZoneOptions } from '../../options/index.ts'
4
+ import { User } from '../../user/index.ts'
5
+ import type { Zone } from '../../zone/index.ts'
6
+ import type { LoginAttempt } from '../attempt/index.ts'
7
+ import type { Authenticator } from '../authenticator/index.ts'
8
+ import { LoginVerdict } from '../verdict/index.ts'
9
+
10
+ /**
11
+ * Decides who gets into a zone and under what name
12
+ * @class
13
+ */
14
+ export class LoginGate {
15
+ /**
16
+ * Decides who gets in
17
+ */
18
+ public readonly authenticator: Authenticator
19
+
20
+ /**
21
+ * The zone being guarded
22
+ */
23
+ private readonly zone: Zone
24
+
25
+ /**
26
+ * Where user ids come from
27
+ */
28
+ private readonly userIds: Sequence
29
+
30
+ /**
31
+ * Most users online at once
32
+ */
33
+ private readonly maxUsers: number
34
+
35
+ /**
36
+ * Whether a login with no name gets a guest name
37
+ */
38
+ private readonly guests: boolean
39
+
40
+ /**
41
+ * What guest names start with
42
+ */
43
+ private readonly guestPrefix: string
44
+
45
+ /**
46
+ * Number for the next guest name
47
+ */
48
+ private nextGuest: number
49
+
50
+ /**
51
+ * Ids of everyone online under a guest name
52
+ */
53
+ private readonly guestIds: Set<number>
54
+
55
+ /**
56
+ * @param zone the zone being guarded
57
+ * @param options how the zone is set up
58
+ * @param userIds where user ids come from
59
+ */
60
+ public constructor(zone: Zone, options: ZoneOptions, userIds: Sequence) {
61
+ this.zone = zone
62
+ this.userIds = userIds
63
+ this.authenticator =
64
+ options.authenticator ??
65
+ ((attempt: LoginAttempt): LoginVerdict =>
66
+ LoginVerdict.accept(attempt.name))
67
+ this.maxUsers = options.maxUsers ?? Infinity
68
+ this.guests = options.guests ?? true
69
+ this.guestPrefix = options.guestPrefix ?? 'guest_'
70
+ this.nextGuest = 1
71
+ this.guestIds = new Set<number>()
72
+ }
73
+
74
+ /**
75
+ * Runs a login past bans and the authenticator, or says why not
76
+ * @param session the connection
77
+ * @param attempt what the client sent
78
+ */
79
+ public async admit(
80
+ session: Session<User>,
81
+ attempt: LoginAttempt,
82
+ ): Promise<string | null> {
83
+ const messages = this.zone.messages
84
+ if (session.user !== null) return messages.alreadyLoggedIn
85
+
86
+ const banned = await this.zone.moderation.bans.has(
87
+ attempt.name,
88
+ attempt.address,
89
+ )
90
+ if (banned === 'unreachable') return messages.unavailable
91
+ if (banned) return messages.banned
92
+
93
+ const verdict = await this.authenticator(attempt)
94
+
95
+ return (
96
+ verdict.reason ?? this.login(session, verdict.name, verdict.moderator)
97
+ )
98
+ }
99
+
100
+ /**
101
+ * Logs a connection in, or says why it can't
102
+ * @param session the connection
103
+ * @param name name the user plays under, empty for a guest
104
+ * @param moderator whether they can kick and ban
105
+ */
106
+ public login(
107
+ session: Session<User>,
108
+ name: string,
109
+ moderator = false,
110
+ ): string | null {
111
+ const reason = this.refusal(session, name)
112
+ if (reason !== null) return reason
113
+
114
+ const user = User.of(
115
+ this.userIds.next(),
116
+ name.length > 0 ? name : this.guest(),
117
+ moderator,
118
+ )
119
+ if (name.length === 0) this.guestIds.add(user.id)
120
+
121
+ this.zone.enter(session, user)
122
+ return null
123
+ }
124
+
125
+ /**
126
+ * Whether a user is online under a guest name
127
+ * @param user who to look up
128
+ */
129
+ public guested(user: User): boolean {
130
+ return this.guestIds.has(user.id)
131
+ }
132
+
133
+ /**
134
+ * Forgets a user who left
135
+ * @param user who left
136
+ */
137
+ public forget(user: User): void {
138
+ this.guestIds.delete(user.id)
139
+ }
140
+
141
+ /**
142
+ * A guest name nobody online has
143
+ */
144
+ private guest(): string {
145
+ for (;;) {
146
+ const name = `${this.guestPrefix}${String(this.nextGuest++)}`
147
+ if (this.zone.find(name) === null) return name
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Why a connection can't log in, or null
153
+ * @param session the connection
154
+ * @param name name the user plays under, empty for a guest
155
+ */
156
+ private refusal(session: Session<User>, name: string): string | null {
157
+ const messages = this.zone.messages
158
+ if (!session.channel.isActive) return messages.unavailable
159
+ if (session.user !== null) return messages.alreadyLoggedIn
160
+ if (name.length === 0 && !this.guests) return messages.nameMissing
161
+ if (this.zone.moderation.unfit(name)) return messages.nameRefused
162
+ if (this.zone.find(name) !== null) return messages.nameTaken
163
+
164
+ return this.zone.users.size >= this.maxUsers ? messages.zoneFull : null
165
+ }
166
+ }
@@ -1,3 +1,4 @@
1
1
  export { LoginAttempt } from './attempt/index.ts'
2
2
  export type { Authenticator } from './authenticator/index.ts'
3
+ export { LoginGate } from './gate/index.ts'
3
4
  export { LoginVerdict } from './verdict/index.ts'