Skip to content

Commit

Permalink
refactor(routes): extract IP address logic to utility function
Browse files Browse the repository at this point in the history
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
mauvehed committed Dec 7, 2024
1 parent fb4c3b4 commit e261466
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
18 changes: 4 additions & 14 deletions server/routes/json.ts
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 };
});
19 changes: 5 additions & 14 deletions server/routes/raw.ts
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;
});
13 changes: 13 additions & 0 deletions server/utils/get-client-ip.ts
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';
}

0 comments on commit e261466

Please sign in to comment.