From e70bbb4fb69f24e1b1a3941d94763e7d288e3732 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Fri, 5 Jun 2020 19:21:36 +0200 Subject: [PATCH] Add basic proxy parser --- src/HttpParser.h | 19 +++++++++++++++++-- src/HttpResponse.h | 7 +++++++ src/ProxyParser.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ uSockets | 2 +- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/ProxyParser.h diff --git a/src/HttpParser.h b/src/HttpParser.h index 36ba3a3..97fbc6c 100644 --- a/src/HttpParser.h +++ b/src/HttpParser.h @@ -28,6 +28,7 @@ #include "f2/function2.hpp" #include "BloomFilter.h" +#include "ProxyParser.h" namespace uWS { @@ -237,9 +238,9 @@ private: public: /* We do this to prolong the validity of parsed headers by keeping only the fallback buffer alive */ - std::string &&salvageFallbackBuffer() { + /*std::string &&salvageFallbackBuffer() { return std::move(fallback); - } + }*/ void *consumePostPadded(char *data, int length, void *user, fu2::unique_function &&requestHandler, fu2::unique_function &&dataHandler, fu2::unique_function &&errorHandler) { @@ -275,6 +276,9 @@ public: fallback.reserve(fallback.length() + maxCopyDistance + std::max(MINIMUM_HTTP_POST_PADDING, sizeof(std::string))); fallback.append(data, maxCopyDistance); + // parse proxy here + + // break here on break std::pair consumed = fenceAndConsumePostPadded(fallback.data(), (int) fallback.length(), user, &req, requestHandler, dataHandler); if (consumed.second != user) { @@ -318,6 +322,17 @@ public: } } + // parse proxy here + /* Parse proxy header */ + ProxyParser pp; + auto [done, offset] = pp.parse({data, length}); + + if (!done) { + + } else { + printf("Proxy parser is done\n"); + } + std::pair consumed = fenceAndConsumePostPadded(data, length, user, &req, requestHandler, dataHandler); if (consumed.second != user) { return consumed.second; diff --git a/src/HttpResponse.h b/src/HttpResponse.h index d617f59..98bba55 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -52,6 +52,13 @@ private: return (HttpResponseData *) Super::getAsyncSocketData(); } + /* If we have proxy support */ +#ifdef WITH_PROXY + void getProxiedRemoteAddress() { + + } +#endif + /* Write an unsigned 32-bit integer in hex */ void writeUnsignedHex(unsigned int value) { char buf[10]; diff --git a/src/ProxyParser.h b/src/ProxyParser.h new file mode 100644 index 0000000..38f0ba8 --- /dev/null +++ b/src/ProxyParser.h @@ -0,0 +1,44 @@ +// + +// implements PROXY v2 protocol + + + +struct ProxyParser { + int done = false; + + // 16 byte IP, 2 byte port + // 16 byte our IP, 2 byte our port + + // return true when done, always return next offset for http parsing + std::pair parse(std::string_view data) { + + /* If already parsed, we're done */ + if (done) { + return {true, 0}; + } + + // we require 4 bytes to determine if this is http or not + if (data.length() < 4) { + // we are not done, buffer everything + return {false, 0}; + } else { + + + // is this proxy protocol? + if (memcmp("\r\n\r\n", data.data(), 4) == 0) { + + + + } else { + // it cannot be proxy protocol here, so we are done now + done = true; + return {true, 0}; + } + + + } + + } + +}; \ No newline at end of file diff --git a/uSockets b/uSockets index 2ed0584..fa16479 160000 --- a/uSockets +++ b/uSockets @@ -1 +1 @@ -Subproject commit 2ed0584e0b7fac11a80886d36903517e3a5ef9c3 +Subproject commit fa16479609d7f5c9575499b059657819c5ab92fd