diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index 5b6c64747..3c991f93e 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -230,6 +230,12 @@ struct AsyncSocket { return addressAsText(getRemoteAddress()); } + /* Returns the remote port number or -1 on failure */ + unsigned int getRemotePort() { + int port = us_socket_remote_port(SSL, (us_socket_t *) this); + return (unsigned int) port; + } + /* Write in three levels of prioritization: cork-buffer, syscall, socket-buffer. Always drain if possible. * Returns pair of bytes written (anywhere) and whether or not this call resulted in the polling for * writable (or we are in a state that implies polling for writable). */ diff --git a/src/HttpResponse.h b/src/HttpResponse.h index e73da46dd..c1faffb11 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -229,6 +229,10 @@ struct HttpResponse : public AsyncSocket { std::string_view getProxiedRemoteAddressAsText() { return Super::addressAsText(getProxiedRemoteAddress()); } + + unsigned int getProxiedRemotePort() { + return getHttpResponseData()->proxyParser.getSourcePort(); + } #endif /* Manually upgrade to WebSocket. Typically called in upgrade handler. Immediately calls open handler. @@ -355,6 +359,7 @@ struct HttpResponse : public AsyncSocket { /* See AsyncSocket */ using Super::getRemoteAddress; using Super::getRemoteAddressAsText; + using Super::getRemotePort; using Super::getNativeHandle; /* Throttle reads and writes */ diff --git a/src/ProxyParser.h b/src/ProxyParser.h index 95ee3d119..dee280980 100644 --- a/src/ProxyParser.h +++ b/src/ProxyParser.h @@ -91,6 +91,22 @@ struct ProxyParser { } } + unsigned int getSourcePort() { + + // UNSPEC family and protocol + if (family == 0) { + return {}; + } + + if ((family & 0xf0) >> 4 == 1) { + /* Family 1 is INET4 */ + return addr.ipv4_addr.src_port; + } else { + /* Family 2 is INET6 */ + return addr.ipv6_addr.src_port; + } + } + /* Returns [done, consumed] where done = false on failure */ std::pair parse(std::string_view data) { diff --git a/src/WebSocket.h b/src/WebSocket.h index 199e2f447..00da7812d 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -58,6 +58,7 @@ struct WebSocket : AsyncSocket { using Super::getBufferedAmount; using Super::getRemoteAddress; using Super::getRemoteAddressAsText; + using Super::getRemotePort; using Super::getNativeHandle; /* WebSocket close cannot be an alias to AsyncSocket::close since