-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/Cameri/nostream
- Loading branch information
Showing
34 changed files
with
1,465 additions
and
1,251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
migrations/20240111204900_remove_delegator_from_events_table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
exports.up = async function (knex) { | ||
await knex.schema | ||
.raw('DROP INDEX IF EXISTS pubkey_delegator_kind_idx;') | ||
await knex.schema.alterTable('events', function (table) { | ||
table.dropColumn('event_delegator') | ||
}) | ||
} | ||
|
||
exports.down = async function (knex) { | ||
await knex.schema.alterTable('events', function (table) { | ||
table.binary('event_delegator').nullable().index() | ||
}) | ||
await knex.schema | ||
.raw( | ||
`CREATE UNIQUE INDEX pubkey_delegator_kind_idx | ||
ON events ( event_pubkey, event_delegator, event_kind );`, | ||
) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/controllers/admission/get-admission-check-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Request, Response } from 'express' | ||
import { createLogger } from '../../factories/logger-factory' | ||
import { getRemoteAddress } from '../../utils/http' | ||
import { IController } from '../../@types/controllers' | ||
import { IRateLimiter } from '../../@types/utils' | ||
import { IUserRepository } from '../../@types/repositories' | ||
import { path } from 'ramda' | ||
import { Settings } from '../../@types/settings' | ||
|
||
const debug = createLogger('get-admission-check-controller') | ||
|
||
export class GetSubmissionCheckController implements IController { | ||
public constructor( | ||
private readonly userRepository: IUserRepository, | ||
private readonly settings: () => Settings, | ||
private readonly rateLimiter: () => IRateLimiter, | ||
){} | ||
|
||
public async handleRequest(request: Request, response: Response): Promise<void> { | ||
const currentSettings = this.settings() | ||
|
||
const limited = await this.isRateLimited(request, currentSettings) | ||
if (limited) { | ||
response | ||
.status(429) | ||
.setHeader('content-type', 'text/plain; charset=utf8') | ||
.send('Too many requests') | ||
return | ||
} | ||
|
||
const pubkey = request.params.pubkey | ||
const user = await this.userRepository.findByPubkey(pubkey) | ||
|
||
let userAdmitted = false | ||
|
||
const minBalance = currentSettings.limits?.event?.pubkey?.minBalance | ||
if (user && user.isAdmitted && (!minBalance || user.balance >= minBalance)) { | ||
userAdmitted = true | ||
} | ||
|
||
response | ||
.status(200) | ||
.setHeader('content-type', 'application/json; charset=utf8') | ||
.send({ userAdmitted }) | ||
|
||
return | ||
} | ||
|
||
public async isRateLimited(request: Request, settings: Settings) { | ||
const rateLimits = path(['limits', 'admissionCheck', 'rateLimits'], settings) | ||
if (!Array.isArray(rateLimits) || !rateLimits.length) { | ||
return false | ||
} | ||
|
||
const ipWhitelist = path(['limits', 'admissionCheck', 'ipWhitelist'], settings) | ||
const remoteAddress = getRemoteAddress(request, settings) | ||
|
||
let limited = false | ||
if (Array.isArray(ipWhitelist) && !ipWhitelist.includes(remoteAddress)) { | ||
const rateLimiter = this.rateLimiter() | ||
for (const { rate, period } of rateLimits) { | ||
if (await rateLimiter.hit(`${remoteAddress}:admission-check:${period}`, 1, { period, rate })) { | ||
debug('rate limited %s: %d in %d milliseconds', remoteAddress, rate, period) | ||
limited = true | ||
} | ||
} | ||
} | ||
return limited | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/factories/controllers/get-admission-check-controller-factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createSettings } from '../settings-factory' | ||
import { getMasterDbClient } from '../../database/client' | ||
import { GetSubmissionCheckController } from '../../controllers/admission/get-admission-check-controller' | ||
import { slidingWindowRateLimiterFactory } from '../rate-limiter-factory' | ||
import { UserRepository } from '../../repositories/user-repository' | ||
|
||
export const createGetAdmissionCheckController = () => { | ||
const dbClient = getMasterDbClient() | ||
const userRepository = new UserRepository(dbClient) | ||
|
||
return new GetSubmissionCheckController( | ||
userRepository, | ||
createSettings, | ||
slidingWindowRateLimiterFactory | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.