Clean-ups, bump copyright date

This commit is contained in:
Alex Hultman
2020-06-08 13:31:06 +02:00
parent 5d5f9edc63
commit 025415d1a0
20 changed files with 183 additions and 160 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ prefix ?= /usr/local
# WITH_PROXY enables PROXY Protocol v2 support # WITH_PROXY enables PROXY Protocol v2 support
ifeq ($(WITH_PROXY),1) ifeq ($(WITH_PROXY),1)
override CXXFLAGS += -DWITH_PROXY override CXXFLAGS += -DUWS_WITH_PROXY
endif endif
# WITH_OPENSSL=1 enables OpenSSL 1.1+ support # WITH_OPENSSL=1 enables OpenSSL 1.1+ support
-3
View File
@@ -1,5 +1,4 @@
#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 */
@@ -10,8 +9,6 @@ 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) {
+1 -1
View File
@@ -127,7 +127,7 @@ private:
// how far did we read then? we need to know to continue with websocket parsing data? or? // how far did we read then? we need to know to continue with websocket parsing data? or?
void *proxyParser = nullptr; void *proxyParser = nullptr;
#ifdef WITH_PROXY #ifdef UWS_WITH_PROXY
proxyParser = &httpResponseData->proxyParser; proxyParser = &httpResponseData->proxyParser;
#endif #endif
+1 -1
View File
@@ -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");
+1 -1
View File
@@ -183,7 +183,7 @@ private:
/* How much data we CONSUMED (to throw away) */ /* How much data we CONSUMED (to throw away) */
int consumedTotal = 0; int consumedTotal = 0;
#ifdef WITH_PROXY #ifdef UWS_WITH_PROXY
/* ProxyParser is passed as reserved parameter */ /* ProxyParser is passed as reserved parameter */
ProxyParser *pp = (ProxyParser *) reserved; ProxyParser *pp = (ProxyParser *) reserved;
+1 -1
View File
@@ -168,7 +168,7 @@ private:
public: public:
/* If we have proxy support; returns the proxed source address as reported by the proxy. */ /* If we have proxy support; returns the proxed source address as reported by the proxy. */
#ifdef WITH_PROXY #ifdef UWS_WITH_PROXY
std::string_view getProxiedRemoteAddress() { std::string_view getProxiedRemoteAddress() {
return getHttpResponseData()->proxyParser.getSourceAddress(); return getHttpResponseData()->proxyParser.getSourceAddress();
} }
+2 -4
View File
@@ -22,11 +22,10 @@
#include "HttpParser.h" #include "HttpParser.h"
#include "AsyncSocketData.h" #include "AsyncSocketData.h"
#include "ProxyParser.h"
#include "f2/function2.hpp" #include "f2/function2.hpp"
#include "ProxyParser.h"
namespace uWS { namespace uWS {
template <bool SSL> template <bool SSL>
@@ -53,8 +52,7 @@ private:
/* Current state (content-length sent, status sent, write called, etc */ /* Current state (content-length sent, status sent, write called, etc */
int state = 0; int state = 0;
#ifdef WITH_PROXY #ifdef UWS_WITH_PROXY
// proxy protocol
ProxyParser proxyParser; ProxyParser proxyParser;
#endif #endif
}; };
+1 -1
View File
@@ -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");
+2 -2
View File
@@ -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");
@@ -80,7 +80,7 @@ private:
Loop *loop = nullptr; Loop *loop = nullptr;
bool cleanMe = false; bool cleanMe = false;
}; };
public: public:
/* Lazily initializes a per-thread loop and returns it. /* Lazily initializes a per-thread loop and returns it.
* Will automatically free all initialized loops at exit. */ * Will automatically free all initialized loops at exit. */
+1 -1
View File
@@ -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");
+1 -1
View File
@@ -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");
+163 -135
View File
@@ -1,135 +1,163 @@
// /*
* Authored by Alex Hultman, 2018-2020.
// implements PROXY v2 protocol * Intellectual property of third-party.
#ifndef PROXY_PARSER * Licensed under the Apache License, Version 2.0 (the "License");
#define PROXY_PARSER * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
#ifdef WITH_PROXY
* http://www.apache.org/licenses/LICENSE-2.0
struct proxy_hdr_v2 {
uint8_t sig[12]; /* hex 0D 0A 0D 0A 00 0D 0A 51 55 49 54 0A */ * Unless required by applicable law or agreed to in writing, software
uint8_t ver_cmd; /* protocol version and command */ * distributed under the License is distributed on an "AS IS" BASIS,
uint8_t fam; /* protocol family and address */ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
uint16_t len; /* number of following bytes part of the header */ * See the License for the specific language governing permissions and
}; * limitations under the License.
*/
union proxy_addr {
struct { /* for TCP/UDP over IPv4, len = 12 */ /* This module implements The PROXY Protocol v2 */
uint32_t src_addr;
uint32_t dst_addr; #ifndef UWS_PROXY_PARSER_H
uint16_t src_port; #define UWS_PROXY_PARSER_H
uint16_t dst_port;
} ipv4_addr; #ifdef UWS_WITH_PROXY
struct { /* for TCP/UDP over IPv6, len = 36 */
uint8_t src_addr[16]; namespace uWS {
uint8_t dst_addr[16];
uint16_t src_port; struct proxy_hdr_v2 {
uint16_t dst_port; uint8_t sig[12]; /* hex 0D 0A 0D 0A 00 0D 0A 51 55 49 54 0A */
} ipv6_addr; uint8_t ver_cmd; /* protocol version and command */
}; uint8_t fam; /* protocol family and address */
uint16_t len; /* number of following bytes part of the header */
/* Byte swap for little-endian systems */ };
template <typename T>
T _cond_byte_swap(T value) { union proxy_addr {
uint32_t endian_test = 1; struct { /* for TCP/UDP over IPv4, len = 12 */
if (*((char *)&endian_test)) { uint32_t src_addr;
union { uint32_t dst_addr;
T i; uint16_t src_port;
uint8_t b[sizeof(T)]; uint16_t dst_port;
} src = { value }, dst; } ipv4_addr;
struct { /* for TCP/UDP over IPv6, len = 36 */
for (unsigned int i = 0; i < sizeof(value); i++) { uint8_t src_addr[16];
dst.b[i] = src.b[sizeof(value) - 1 - i]; uint8_t dst_addr[16];
} uint16_t src_port;
uint16_t dst_port;
return dst.i; } ipv6_addr;
} };
return value;
} /* Byte swap for little-endian systems */
/* Todo: This functions should be shared with the one in WebSocketProtocol.h! */
struct ProxyParser { template <typename T>
private: T _cond_byte_swap(T value) {
union proxy_addr addr; uint32_t endian_test = 1;
if (*((char *)&endian_test)) {
// we should have "unset" where the return value is "" from proxied ip! union {
uint8_t family = 0; T i;
uint8_t b[sizeof(T)];
public: } src = { value }, dst;
/* Returns 4 or 16 bytes source address */
std::string_view getSourceAddress() { for (unsigned int i = 0; i < sizeof(value); i++) {
dst.b[i] = src.b[sizeof(value) - 1 - i];
// UNSPEC family and protocol }
if (family == 0) {
return {}; return dst.i;
} }
return value;
if ((family & 0xf0) >> 4 == 1) { }
/* Family 1 is INET4 */
return {(char *) &addr.ipv4_addr.src_addr, 4}; struct ProxyParser {
} else { private:
/* Family 2 is INET6 */ union proxy_addr addr;
return {(char *) &addr.ipv6_addr.src_addr, 16};
} /* Default family of 0 signals no proxy address */
} uint8_t family = 0;
/* Returns [done, consumed] where done = false on failure */ public:
std::pair<bool, unsigned int> parse(std::string_view data) { /* Returns 4 or 16 bytes source address */
std::string_view getSourceAddress() {
/* We require at least one byte to be done */
if (!data.length()) { // UNSPEC family and protocol
return {false, 0}; if (family == 0) {
} return {};
}
/* HTTP does not start with \r, but PROXY always does */
if (data[0] != '\r') { if ((family & 0xf0) >> 4 == 1) {
/* This is HTTP, so be done */ /* Family 1 is INET4 */
return {true, 0}; return {(char *) &addr.ipv4_addr.src_addr, 4};
} } else {
/* Family 2 is INET6 */
/* We assume we are parsing PROXY V2 here */ return {(char *) &addr.ipv6_addr.src_addr, 16};
}
/* We require 16 bytes here */ }
if (data.length() < 16) {
return {false, 0}; /* Returns [done, consumed] where done = false on failure */
} std::pair<bool, unsigned int> parse(std::string_view data) {
struct proxy_hdr_v2 header; /* We require at least one byte to be done */
memcpy(&header, data.data(), 16); if (!data.length()) {
return {false, 0};
if (memcmp(header.sig, "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", 12)) { }
/* This is not PROXY protocol at all */
return {false, 0}; /* HTTP does not start with \r, but PROXY always does */
} if (data[0] != '\r') {
/* This is HTTP, so be done */
/* If we have version 2 */ return {true, 0};
}
printf("Version: %d\n", (header.ver_cmd & 0xf0) >> 4);
printf("Command: %d\n", (header.ver_cmd & 0x0f)); /* We assume we are parsing PROXY V2 here */
uint16_t hostLength = _cond_byte_swap<uint16_t>(header.len); /* We require 16 bytes here */
if (data.length() < 16) {
if (data.length() < 16 + hostLength) { return {false, 0};
return {false, 0}; }
}
/* Header is 16 bytes */
printf("Length: %d\n", hostLength); struct proxy_hdr_v2 header;
memcpy(&header, data.data(), 16);
printf("Family: %d\n", (header.fam & 0xf0) >> 4);
printf("Transport: %d\n", (header.fam & 0x0f)); if (memcmp(header.sig, "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", 12)) {
/* This is not PROXY protocol at all */
family = header.fam; return {false, 0};
}
// copy source ip
memcpy(&addr, data.data() + 16, hostLength); /* We only support version 2 */
if ((header.ver_cmd & 0xf0) >> 4 != 2) {
return {false, 0};
}
return {true, 16 + hostLength};
} //printf("Version: %d\n", (header.ver_cmd & 0xf0) >> 4);
//printf("Command: %d\n", (header.ver_cmd & 0x0f));
};
/* We get length in network byte order (todo: share this function with the rest) */
#endif uint16_t hostLength = _cond_byte_swap<uint16_t>(header.len);
#endif /* We must have all the data available */
if (data.length() < 16 + hostLength) {
return {false, 0};
}
/* Payload cannot be more than sizeof proxy_addr */
if (sizeof(proxy_addr) < hostLength) {
return {false, 0};
}
//printf("Family: %d\n", (header.fam & 0xf0) >> 4);
//printf("Transport: %d\n", (header.fam & 0x0f));
/* We have 0 family by default, and UNSPEC is 0 as well */
family = header.fam;
/* Copy payload */
memcpy(&addr, data.data() + 16, hostLength);
/* We consumed everything */
return {true, 16 + hostLength};
}
};
}
#endif
#endif // UWS_PROXY_PARSER_H
+1 -1
View File
@@ -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");
+1 -1
View File
@@ -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");
+1 -1
View File
@@ -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");
+1 -1
View File
@@ -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");
+1 -1
View File
@@ -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");
+1 -1
View File
@@ -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");
+1 -1
View File
@@ -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");
+1 -1
View File
@@ -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");