-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Admission check endpoint #338
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0711065
feat: admission check
medjedovicm 3f585aa
chore: test
medjedovicm 0d3b182
chore: status 200
medjedovicm 97945f6
chore: rate limit enable
medjedovicm 0c2ea0e
feat: added admission controller
medjedovicm 428262c
chore: admission check as a separate limits configuration
medjedovicm 0c30c20
chore(git): Merge branch 'main' into admission-check
medjedovicm f2301e4
chore: style
medjedovicm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/controllers/invoices/get-invoice-pubkey-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,72 @@ | ||
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-invoice-pubkey-check-controller') | ||
|
||
export class GetInvoicePubkeyCheckController 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) | ||
|
||
console.log('user', user) | ||
|
||
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', 'invoice', 'rateLimits'], settings) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a different rate limit for this endpoint |
||
if (!Array.isArray(rateLimits) || !rateLimits.length) { | ||
return false | ||
} | ||
|
||
const ipWhitelist = path(['limits', 'invoice', '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}:invoice:${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-invoice-pubkey-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 { GetInvoicePubkeyCheckController } from '../../controllers/invoices/get-invoice-pubkey-check-controller' | ||
import { getMasterDbClient } from '../../database/client' | ||
import { slidingWindowRateLimiterFactory } from '../rate-limiter-factory' | ||
import { UserRepository } from '../../repositories/user-repository' | ||
|
||
export const createGetInvoicePubkeyCheckController = () => { | ||
const dbClient = getMasterDbClient() | ||
const userRepository = new UserRepository(dbClient) | ||
|
||
return new GetInvoicePubkeyCheckController( | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove