deepkit-framework
deepkit-framework copied to clipboard
[Feature Request] Deepkit RPC: Getting connection info e.g., client IP & http request header
IP information is useful for various purposes like rate-limiting, logging, etc, ... I believe that popular RPC libraries usually have a way to access that info. (e.g., we're using gRPC and it's included in the message metadata)
update: I was able to get the IP with the below approach. Though my server is behind Cloudflare & Kubernetes so need to check http request headers("CF-Connecting-IP" or "x-forwarded-for") to get the public client IP :/, oh well...
@rpc.controller("/test")
export class TestService {
constructor(private connection: RpcKernelBaseConnection) {}
@rpc.action()
getClientIp() {
return this.connection.clientAddress() || "It's undefined";
}
}
you can just inject HttpRequest, it is available in your rpc controller, there you can read all your headers
@rpc.controller("/test")
export class TestService {
constructor(private request: HttpRequest) {}
@rpc.action()
getClientIp() {
const cf = this.request.headers['cf-connecting-ip'];
if (cf) return String(cf);
return this.request.socket?.remoteAddress || undefined;
}
}