Skip to content

Commit

Permalink
feat: add msg/event rate limit to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
cameri committed Nov 16, 2022
1 parent 59c6f80 commit a46fcc6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/@types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export interface PubkeyLimits {

export type EventKindsRange = [EventKinds, EventKinds]

export interface EventRateLimit {
kinds?: (EventKinds | [EventKinds, EventKinds])[]
rate: number
period: number
}

export interface KindLimits {
whitelist?: (EventKinds | EventKindsRange)[]
blacklist?: (EventKinds | EventKindsRange)[]
Expand All @@ -42,6 +48,7 @@ export interface EventLimits {
pubkey?: PubkeyLimits
kind?: KindLimits
createdAt?: CreatedAtLimits
rateLimits?: EventRateLimit[]
}

export interface ClientSubscriptionLimits {
Expand All @@ -53,9 +60,20 @@ export interface ClientLimits {
subscription?: ClientSubscriptionLimits
}

export interface MessageRateLimit {
rate: number
period: number
}

export interface MessageLimits {
rateLimits?: MessageRateLimit[]
ipWhitelist?: string[]
}

export interface Limits {
client?: ClientLimits
event?: EventLimits
message?: MessageLimits
}

export interface Worker {
Expand Down
42 changes: 42 additions & 0 deletions src/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { join } from 'path'
import { mergeDeepRight } from 'ramda'

import { createLogger } from '../factories/logger-factory'
import { EventKinds } from '../constants/base'
import { ISettings } from '../@types/settings'
import packageJson from '../../package.json'

const debug = createLogger('settings')

export class SettingsStatic {
static _settings: ISettings

Expand Down Expand Up @@ -48,13 +50,53 @@ export class SettingsStatic {
maxPositiveDelta: 900,
maxNegativeDelta: 0, // disabled
},
rateLimits: [
{
kinds: [EventKinds.TEXT_NOTE],
period: 60000,
rate: 60,
},
{
kinds: [[EventKinds.EPHEMERAL_FIRST, EventKinds.EPHEMERAL_LAST]],
period: 60000,
rate: 240,
},
{
period: 3600000,
rate: 3600,
},
{
period: 86400000,
rate: 86400,
},
],
},
client: {
subscription: {
maxSubscriptions: 10,
maxFilters: 10,
},
},
message: {
rateLimits: [
{
period: 60000, // minute
rate: 240,
},
{
period: 3600000, // hour
rate: 3600,
},
{
period: 86400000, // day
rate: 86400,
},
],
ipWhitelist: [
'::1', // local host
'::ffff:10.10.10.1', // host running docker
],
},
},
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit/utils/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ describe('SettingsStatic', () => {
maxFilters: 10,
},
},
message: {
dailyRate: 86400,
hourlyRate: 3600,
minutelyRate: 240,
ipWhitelist: [
'::1',
'::ffff:10.10.10.1',
],
},
})
})
})
Expand Down

0 comments on commit a46fcc6

Please sign in to comment.