getIP method

String getIP()

Retrieves the client's IP address from the request headers or connection information.

This method checks the X-Real-IP and X-Forwarded-For headers, which are set by Nginx. If these headers are not present, it falls back to the remote address of the connection.

Returns a String containing the client's IP address.

Implementation

String getIP() {
  String? ip = headers.value('X-Real-IP');

  if (ip != null) {
    return ip;
  }

  ip = headers.value('X-Forwarded-For');

  if (ip != null) {
    return ip;
  }

  return response.connectionInfo?.remoteAddress.address ?? '127.0.0.1';
}