Skip to content
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

fix(server): fix get_client_ip method, use x-forwarded-fro first #1717

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion runtimes/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { parseToken, splitBearerToken } from './support/token'
import Config from './config'
import { router } from './handler/router'
import { logger } from './support/logger'
import { generateUUID } from './support/utils'
import { GetClientIPFromRequest, generateUUID } from './support/utils'
import { WebSocketAgent } from './support/ws'
import { DatabaseAgent } from './db'
import xmlparser from 'express-xml-bodyparser'
Expand Down Expand Up @@ -42,6 +42,14 @@ app.use(
}),
)

// fix x-real-ip while gateway not set
app.use((req, _res, next) => {
if (!req.headers['x-real-ip']) {
req.headers['x-real-ip'] = GetClientIPFromRequest(req)
}
next()
})

app.use(express.json({ limit: Config.REQUEST_LIMIT_SIZE }) as any)
app.use(
express.urlencoded({
Expand Down
18 changes: 18 additions & 0 deletions runtimes/nodejs/src/support/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as crypto from 'crypto'
import { IRequest } from './types'

/**
* Generate UUID v4
Expand Down Expand Up @@ -115,3 +116,20 @@ export function base64ToUint8Array(base64: string) {
const buffer = Buffer.from(base64, 'base64')
return new Uint8Array(buffer)
}

export function GetClientIPFromRequest(req: IRequest) {
// try to get ip from x-forwarded-for
const ips_str = req.headers['x-forwarded-for'] as string
if (ips_str) {
const ips = ips_str.split(',')
return ips[0]
}

// try to get ip from x-real-ip
const ip = req.headers['x-real-ip'] as string
if (ip) {
return ip
}

return null
}
3 changes: 2 additions & 1 deletion server/src/authentication/email/email.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'
import { ResponseUtil } from 'src/utils/response'
import { SendEmailCodeDto } from '../dto/send-email-code.dto'
import { EmailService } from './email.service'
import { GetClientIPFromRequest } from 'src/utils/getter'

@ApiTags('Authentication')
@Controller('auth')
Expand All @@ -21,7 +22,7 @@ export class EmailController {
@Post('email/code')
async sendCode(@Req() req: IRequest, @Body() dto: SendEmailCodeDto) {
const { email, type } = dto
const ip = req.headers['x-real-ip'] as string
const ip = GetClientIPFromRequest(req)

const err = await this.emailService.sendCode(email, type, ip)
if (err) {
Expand Down
3 changes: 2 additions & 1 deletion server/src/authentication/phone/phone.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { UserService } from 'src/user/user.service'
import { AuthBindingType, AuthProviderBinding } from '../entities/types'
import { SmsVerifyCodeType } from '../entities/sms-verify-code'
import { SmsService } from './sms.service'
import { GetClientIPFromRequest } from 'src/utils/getter'

@ApiTags('Authentication')
@Controller('auth')
Expand All @@ -31,7 +32,7 @@ export class PhoneController {
@Post('phone/sms/code')
async sendCode(@Req() req: IRequest, @Body() dto: SendPhoneCodeDto) {
const { phone, type } = dto
const ip = req.headers['x-real-ip'] as string
const ip = GetClientIPFromRequest(req)

const err = await this.phoneService.sendCode(phone, type, ip)
if (err) {
Expand Down
18 changes: 18 additions & 0 deletions server/src/utils/getter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Condition } from 'src/region/cluster/types'
import { ApplicationNamespaceMode, Region } from 'src/region/entities/region'
import { IRequest } from './interface'

/**
* Get application namespace name by appid (in kubernetes)
Expand Down Expand Up @@ -30,3 +31,20 @@ export function isConditionTrue(type: string, conditions: Condition[] | any[]) {
}
return false
}

export function GetClientIPFromRequest(req: IRequest) {
// try to get ip from x-forwarded-for
const ips_str = req.headers['x-forwarded-for'] as string
if (ips_str) {
const ips = ips_str.split(',')
return ips[0]
}

// try to get ip from x-real-ip
const ip = req.headers['x-real-ip'] as string
if (ip) {
return ip
}

return null
}