deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

[Feature Request] Deepkit RPC: Getting connection info e.g., client IP & http request header

Open kkimdev opened this issue 1 year ago • 1 comments

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)

kkimdev avatar Jul 08 '24 23:07 kkimdev

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";
  }
}

kkimdev avatar Jul 09 '24 01:07 kkimdev

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;
  }
}

marcj avatar Apr 02 '25 20:04 marcj