-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(routes): extract IP address logic to utility function
Moves the logic for extracting and validating the client IP address from the 'json.ts' and 'raw.ts' route handlers to a new utility function 'getClientIp' in 'utils/get-client-ip.ts'. This change improves code reusability and maintainability by centralizing the IP extraction logic, reducing code duplication, and making it easier to update or modify the IP handling logic in the future.
- Loading branch information
Showing
3 changed files
with
22 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,7 @@ | ||
import { defineEventHandler, getRequestHeaders } from 'h3'; | ||
import { defineEventHandler } from 'h3'; | ||
import { getClientIp } from '../utils/get-client-ip'; | ||
|
||
export default defineEventHandler((event) => { | ||
const headers = getRequestHeaders(event); | ||
|
||
// Extract the X-Forwarded-For header | ||
const forwardedFor = headers['x-forwarded-for'] || ''; | ||
|
||
// Take the leftmost (original client) IP and sanitize it | ||
const userIp = forwardedFor.split(',')[0]?.trim() || event.node.req.socket.remoteAddress; | ||
|
||
// Validate the IP address (simple regex for IPv4/IPv6) | ||
const isValidIp = /^(\d{1,3}\.){3}\d{1,3}$|^[0-9a-fA-F:]+$/.test(userIp); | ||
const safeIp = isValidIp ? userIp : 'Invalid IP'; | ||
|
||
return { ip: safeIp }; | ||
const userIp = getClientIp(event); | ||
return { ip: userIp }; | ||
}); |
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 |
---|---|---|
@@ -1,17 +1,8 @@ | ||
import { defineEventHandler, getRequestHeaders } from 'h3'; | ||
import { defineEventHandler } from 'h3'; | ||
import { getClientIp } from '../utils/get-client-ip'; | ||
|
||
export default defineEventHandler((event) => { | ||
const headers = getRequestHeaders(event); | ||
|
||
// Extract the X-Forwarded-For header | ||
const forwardedFor = headers['x-forwarded-for'] || ''; | ||
|
||
// Take the leftmost (original client) IP and sanitize it | ||
const userIp = forwardedFor.split(',')[0]?.trim() || event.node.req.socket.remoteAddress; | ||
|
||
// Validate the IP address (simple regex for IPv4/IPv6) | ||
const isValidIp = /^(\d{1,3}\.){3}\d{1,3}$|^[0-9a-fA-F:]+$/.test(userIp); | ||
const safeIp = isValidIp ? userIp : 'Invalid IP'; | ||
|
||
return safeIp; | ||
const userIp = getClientIp(event); | ||
event.res.setHeader('Content-Type', 'text/plain'); | ||
return userIp; | ||
}); |
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,13 @@ | ||
export function getClientIp(event) { | ||
const headers = event.node.req.headers; | ||
|
||
// Extract the X-Forwarded-For header | ||
const forwardedFor = headers['x-forwarded-for'] || ''; | ||
|
||
// Take the leftmost (original client) IP and sanitize it | ||
const userIp = forwardedFor.split(',')[0]?.trim() || event.node.req.socket.remoteAddress; | ||
|
||
// Validate the IP address (simple regex for IPv4/IPv6) | ||
const isValidIp = /^(\d{1,3}\.){3}\d{1,3}$|^[0-9a-fA-F:]+$/.test(userIp); | ||
return isValidIp ? userIp : 'Invalid IP'; | ||
} |