x-real-ip
, x-forwarded-for
로 client IP 획득
- 단,
x-real-ip
, x-forwarded-for
는 클라이언트와 proxy 에서 임의로 조작 가능하므로 맹신할 수 없다.
getClientIp(request) {
try {
let clientIp = request.headers['x-real-ip']
if (!clientIp) {
const forwardedIpList = request.headers['x-forwarded-for']
if (forwardedIpList) {
clientIp = forwardedIpList.split(',')[0]
}
}
return clientIp
} catch (error) {
logger.error(`failed to get client ip: `, error)
return null
}
}
- Redis incr, expire 를 이용하여 스로틀링 로직 구현
async isRequestAllowed(originKey, redisClient) {
const expiredSec = 600
const throttleCount = 50
try {
if (!originKey) {
return true
}
let redisKey = `throttle:${originKey}`
const count = await redisClient.incr(redisKey)
if (count === 1) {
await redisClient.expire(key, expiredSec)
}
if (count > throttleCount) {
return false
}
return true
} catch (error) {
logger.error(`failed to check request allowed: `, error)
return true
}
}