-
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.
- Loading branch information
1 parent
fa99657
commit ed30823
Showing
8 changed files
with
117 additions
and
1 deletion.
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
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 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,10 @@ | ||
import { createGetAdmissionCheckController } from '../../factories/controllers/get-admission-check-controller-factory' | ||
import { Router } from 'express' | ||
import { withController } from '../../handlers/request-handlers/with-controller-request-handler' | ||
|
||
const admissionRouter = Router() | ||
|
||
admissionRouter | ||
.get('/check/:pubkey', withController(createGetAdmissionCheckController)) | ||
|
||
export default admissionRouter |
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