add ip func to request

This commit is contained in:
HorizonCode 2023-05-11 12:33:57 +02:00
parent a9017c7550
commit b974abff1b
2 changed files with 20 additions and 3 deletions

View File

@ -4,7 +4,7 @@ import { HTTPServer } from "../mod.ts";
const httpServer = new HTTPServer(); const httpServer = new HTTPServer();
httpServer.middleware((req) => { httpServer.middleware((req) => {
console.log(`User Requested ${req.path}`); console.log(`${req.method} - ${req.ip()} - ${req.path}`);
}) })
httpServer.error((req, _rep) => { httpServer.error((req, _rep) => {

21
mod.ts
View File

@ -97,7 +97,7 @@ export class HTTPServer {
private async handleHttp(conn: Deno.Conn) { private async handleHttp(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn); const httpConn = Deno.serveHttp(conn);
for await (const requestEvent of httpConn) { for await (const requestEvent of httpConn) {
const routeRequest = new RouteRequest(requestEvent.request); const routeRequest = new RouteRequest(requestEvent.request, conn);
const routeReply: RouteReply = new RouteReply(); const routeReply: RouteReply = new RouteReply();
const url = new URL(requestEvent.request.url); const url = new URL(requestEvent.request.url);
const filepath = decodeURIComponent(url.pathname); const filepath = decodeURIComponent(url.pathname);
@ -276,8 +276,9 @@ export class RouteRequest {
method: HTTPMethod; method: HTTPMethod;
queryParams: { [key: string]: string }; queryParams: { [key: string]: string };
pathParams: { [key: string]: string }; pathParams: { [key: string]: string };
private remoteIpAddr: string;
constructor(request: Request) { constructor(request: Request, conn: Deno.Conn) {
this.url = request.url; this.url = request.url;
const urlObj = new URL(request.url); const urlObj = new URL(request.url);
this.path = decodeURIComponent(urlObj.pathname); this.path = decodeURIComponent(urlObj.pathname);
@ -285,6 +286,9 @@ export class RouteRequest {
this.method = request.method as HTTPMethod; this.method = request.method as HTTPMethod;
this.pathParams = {}; this.pathParams = {};
this.queryParams = this.paramsToObject(urlObj.searchParams.entries()); this.queryParams = this.paramsToObject(urlObj.searchParams.entries());
this.remoteIpAddr = "hostname" in conn.remoteAddr
? conn.remoteAddr["hostname"]
: "127.0.0.1";
} }
private paramsToObject(entries: IterableIterator<[string, string]>) { private paramsToObject(entries: IterableIterator<[string, string]>) {
@ -295,6 +299,19 @@ export class RouteRequest {
return result; return result;
} }
ip() {
const cfConnectingIp: string = this.header("cf-connecting-ip") as string;
if (cfConnectingIp && cfConnectingIp.length > 0) return cfConnectingIp;
const xRealIp: string = this.header("x-real-ip") as string;
if (xRealIp && xRealIp.length > 0) xRealIp;
const xForwardedFor: string = this.header("x-forwarded-For") as string;
if (xForwardedFor && xForwardedFor.length > 0) return xForwardedFor;
return this.remoteIpAddr;
}
header(name: string): unknown { header(name: string): unknown {
const matchingHeader = Array.from(this.headers.keys()).find((headerName) => const matchingHeader = Array.from(this.headers.keys()).find((headerName) =>
headerName === name headerName === name