addressAsText, getRemoteProxiedAddress
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include "App.h"
|
#include "App.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
/* Note that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support */
|
/* Note that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support */
|
||||||
|
|
||||||
@@ -9,6 +10,8 @@ int main() {
|
|||||||
.cert_file_name = "../misc/cert.pem",
|
.cert_file_name = "../misc/cert.pem",
|
||||||
.passphrase = "1234"
|
.passphrase = "1234"
|
||||||
}).get("/*", [](auto *res, auto *req) {
|
}).get("/*", [](auto *res, auto *req) {
|
||||||
|
std::cout << res->getRemoteAddressAsText() << std::endl;
|
||||||
|
std::cout << res->getProxiedRemoteAddressAsText() << std::endl;
|
||||||
res->end("Hello world!");
|
res->end("Hello world!");
|
||||||
}).listen(3000, [](auto *token) {
|
}).listen(3000, [](auto *token) {
|
||||||
if (token) {
|
if (token) {
|
||||||
|
|||||||
+24
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Authored by Alex Hultman, 2018-2019.
|
* Authored by Alex Hultman, 2018-2020.
|
||||||
* Intellectual property of third-party.
|
* Intellectual property of third-party.
|
||||||
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@@ -95,6 +95,24 @@ protected:
|
|||||||
return (int) getAsyncSocketData()->buffer.size();
|
return (int) getAsyncSocketData()->buffer.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns the text representation of an IPv4 or IPv6 address */
|
||||||
|
std::string_view addressAsText(std::string_view binary) {
|
||||||
|
static thread_local char buf[64];
|
||||||
|
int ipLength = 0;
|
||||||
|
|
||||||
|
unsigned char *b = (unsigned char *) binary.data();
|
||||||
|
|
||||||
|
if (binary.length() == 4) {
|
||||||
|
ipLength = sprintf(buf, "%u.%u.%u.%u", b[0], b[1], b[2], b[3]);
|
||||||
|
} else {
|
||||||
|
ipLength = sprintf(buf, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
|
||||||
|
b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11],
|
||||||
|
b[12], b[13], b[14], b[15]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {buf, ipLength};
|
||||||
|
}
|
||||||
|
|
||||||
/* Returns the remote IP address or empty string on failure */
|
/* Returns the remote IP address or empty string on failure */
|
||||||
std::string_view getRemoteAddress() {
|
std::string_view getRemoteAddress() {
|
||||||
static thread_local char buf[16];
|
static thread_local char buf[16];
|
||||||
@@ -103,6 +121,11 @@ protected:
|
|||||||
return std::string_view(buf, ipLength);
|
return std::string_view(buf, ipLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns the text representation of IP */
|
||||||
|
std::string_view getRemoteAddressAsText() {
|
||||||
|
return addressAsText(getRemoteAddress());
|
||||||
|
}
|
||||||
|
|
||||||
/* Write in three levels of prioritization: cork-buffer, syscall, socket-buffer. Always drain if possible.
|
/* Write in three levels of prioritization: cork-buffer, syscall, socket-buffer. Always drain if possible.
|
||||||
* Returns pair of bytes written (anywhere) and wheter or not this call resulted in the polling for
|
* Returns pair of bytes written (anywhere) and wheter or not this call resulted in the polling for
|
||||||
* writable (or we are in a state that implies polling for writable). */
|
* writable (or we are in a state that implies polling for writable). */
|
||||||
|
|||||||
@@ -28,13 +28,8 @@
|
|||||||
#include "f2/function2.hpp"
|
#include "f2/function2.hpp"
|
||||||
|
|
||||||
#include "BloomFilter.h"
|
#include "BloomFilter.h"
|
||||||
|
|
||||||
|
|
||||||
// if using proxy parser, depend on the layout of HttpResponesData
|
|
||||||
#include "ProxyParser.h"
|
#include "ProxyParser.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace uWS {
|
namespace uWS {
|
||||||
|
|
||||||
/* We require at least this much post padding */
|
/* We require at least this much post padding */
|
||||||
|
|||||||
+12
-7
@@ -52,13 +52,6 @@ private:
|
|||||||
return (HttpResponseData<SSL> *) Super::getAsyncSocketData();
|
return (HttpResponseData<SSL> *) Super::getAsyncSocketData();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we have proxy support */
|
|
||||||
#ifdef WITH_PROXY
|
|
||||||
void getProxiedRemoteAddress() {
|
|
||||||
getHttpResponseData()->proxyParser.getSourceIp();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Write an unsigned 32-bit integer in hex */
|
/* Write an unsigned 32-bit integer in hex */
|
||||||
void writeUnsignedHex(unsigned int value) {
|
void writeUnsignedHex(unsigned int value) {
|
||||||
char buf[10];
|
char buf[10];
|
||||||
@@ -174,6 +167,17 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/* If we have proxy support */
|
||||||
|
#ifdef WITH_PROXY
|
||||||
|
std::string_view getProxiedRemoteAddress() {
|
||||||
|
return getHttpResponseData()->proxyParser.getSourceIp();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view getProxiedRemoteAddressAsText() {
|
||||||
|
return Super::addressAsText(getProxiedRemoteAddress());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Manually upgrade to WebSocket. Typically called in upgrade handler. Immediately calls open handler.
|
/* Manually upgrade to WebSocket. Typically called in upgrade handler. Immediately calls open handler.
|
||||||
* NOTE: Will invalidate 'this' as socket might change location in memory. Throw away aftert use. */
|
* NOTE: Will invalidate 'this' as socket might change location in memory. Throw away aftert use. */
|
||||||
template <typename UserData>
|
template <typename UserData>
|
||||||
@@ -284,6 +288,7 @@ public:
|
|||||||
using Super::close;
|
using Super::close;
|
||||||
|
|
||||||
using Super::getRemoteAddress;
|
using Super::getRemoteAddress;
|
||||||
|
using Super::getRemoteAddressAsText;
|
||||||
|
|
||||||
/* Note: Headers are not checked in regards to timeout.
|
/* Note: Headers are not checked in regards to timeout.
|
||||||
* We only check when you actively push data or end the request */
|
* We only check when you actively push data or end the request */
|
||||||
|
|||||||
+6
-2
@@ -41,8 +41,8 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
/* Returns 4 or 16 bytes */
|
/* Returns 4 or 16 bytes */
|
||||||
std::string_view getSourceIP() {
|
std::string_view getSourceIp() {
|
||||||
return {"hello", 5};
|
return {(char *) sourceIp, 16};
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns [done, consumed] where done = false on failure */
|
/* Returns [done, consumed] where done = false on failure */
|
||||||
@@ -86,6 +86,10 @@ public:
|
|||||||
printf("Family: %d\n", (header.fam & 0xf0) >> 4);
|
printf("Family: %d\n", (header.fam & 0xf0) >> 4);
|
||||||
printf("Transport: %d\n", (header.fam & 0x0f));
|
printf("Transport: %d\n", (header.fam & 0x0f));
|
||||||
|
|
||||||
|
//memcpy(sourceIp, )
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return {true, 16 + hostLength};
|
return {true, 16 + hostLength};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ public:
|
|||||||
/* See AsyncSocket */
|
/* See AsyncSocket */
|
||||||
using Super::getBufferedAmount;
|
using Super::getBufferedAmount;
|
||||||
using Super::getRemoteAddress;
|
using Super::getRemoteAddress;
|
||||||
|
using Super::getRemoteAddressAsText;
|
||||||
|
|
||||||
/* Simple, immediate close of the socket. Emits close event */
|
/* Simple, immediate close of the socket. Emits close event */
|
||||||
using Super::close;
|
using Super::close;
|
||||||
|
|||||||
Reference in New Issue
Block a user