This is carry-over from conversation on the Swift@IBM Slack instance. Thought I'd bring it up here.
So how do you find the IP address of the client on an incoming request?
The RouterRequest
object has a remoteAddress
property. That seems promising. But here's how it's defined:
/// IP address string of server.
public var remoteAddress: String { return serverRequest.remoteAddress }
So… wait. Is it the IP address of the server, despite being named remoteAddress
? Or is it the IP address of the client?
Spoiler alert: After some testing, I confirmed it's the IP address of the server. So the property is misleadingly named, in my opinion. But just for fun, let's go down the rabbit hole.
The serverRequest
property of the Kitura RouterRequest
object is an instance of the HTTPServerRequest
object, provided by Kitura-net. There, it is defined as:
/// Server IP address pulled from socket.
public var remoteAddress: String {
return socket.remoteHostname
}
Okay. But HTTPServerRequest
is an implementation of the ServerRequest
protocol, which is actually where you're taken if you do the "Jump to Definition" thingie in Xcode when Command-Clicking on serverRequest.remoteAddress
. And here's the definition there:
/// The IP address of the client
var remoteAddress: String { get }
Hey! That comment says it's the client IP address, not the server one. Is that a typo, or is HTTPServerRequest
implementing the protocol incorrectly?
Well, at any rate, here's where the remoteHostname
property of Socket
is defined:
///
/// The remote host name this socket is connected to. (Readonly)
///
public var remoteHostname: String {
guard let sig = signature,
let host = sig.hostname else {
return Socket.NO_HOSTNAME
}
return host
}
Hmm. So I think I get it now. Socket
, or at least this part of it, may have been written from the perspective of making outgoing connections, not incoming ones, and that remoteSomething
naming just persisted all the way up to RouterRequest
. (Also, Socket
uses tabs instead of spaces for indentation. tsk tsk tsk.)
So, tl:dr;
- From inside a route handler, how can I get the IP address of the client on the incoming request?
-
RouterRequest.remoteAddress
implies it has the client's IP address, but it actually has the server's, despite its naming and despite the comments on the corresponding property in theServerRequest
protocol. So either that comment is incorrect orHTTPServerRequest
's implementation of it is incorrect. Can someone identify what the intention was here?