SNode.C
Loading...
Searching...
No Matches
servers.h
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#ifndef APPS_HTTP_MODEL_SERVERS_H
43#define APPS_HTTP_MODEL_SERVERS_H
44
45#define QUOTE_INCLUDE(a) STR_INCLUDE(a)
46#define STR_INCLUDE(a) #a
47
48// clang-format off
49#define WEBAPP_INCLUDE QUOTE_INCLUDE(express/STREAM/NET/WebApp.h)
50// clang-format on
51
52#include WEBAPP_INCLUDE // IWYU pragma: export
53
54#include "express/middleware/VerboseRequest.h"
55
56#ifndef DOXYGEN_SHOULD_SKIP_THIS
57
58#include "log/Logger.h"
59
60#if (STREAM_TYPE == TLS) // tls
61#include <cstddef>
62#include <openssl/ssl.h>
63#include <openssl/x509v3.h>
64#endif
65
66#endif /* DOXYGEN_SHOULD_SKIP_THIS */
67
68static express::Router getRouter() {
69 return express::middleware::VerboseRequest();
70}
71
72#if (STREAM_TYPE == LEGACY) // legacy
73
74namespace apps::http::legacy {
75
78
79 static WebApp getWebApp(const std::string& name) {
80 WebApp webApp(name, getRouter());
81
82 return webApp;
83 }
84
85} // namespace apps::http::legacy
86
87#endif // (STREAM_TYPE == LEGACY) // legacy
88
89#if (STREAM_TYPE == TLS) // tls
90
91namespace apps::http::tls {
92
95
96 static WebApp getWebApp(const std::string& name) {
97 WebApp webApp(name, getRouter());
98
99 const std::string& instanceName = webApp.getConfig().getInstanceName();
100
101 webApp.setOnConnect([instanceName](SocketConnection* socketConnection) { // onConnect
102 VLOG(1) << "OnConnect " << instanceName;
103
104 VLOG(1) << " Local: " << socketConnection->getLocalAddress().toString();
105 VLOG(1) << " Peer: " << socketConnection->getRemoteAddress().toString();
106
107 /* Enable automatic hostname checks */
108 // X509_VERIFY_PARAM* param = SSL_get0_param(socketConnection->getSSL());
109
110 // X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
111 // if (!X509_VERIFY_PARAM_set1_host(param, "localhost", sizeof("localhost") - 1)) {
112 // // handle error
113 // socketConnection->close();
114 // }
115 });
116
117 webApp.setOnConnected([instanceName](SocketConnection* socketConnection) { // onConnected
118 VLOG(1) << "OnConnected " << instanceName;
119
120 X509* server_cert = SSL_get_peer_certificate(socketConnection->getSSL());
121 if (server_cert != nullptr) {
122 long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
123
124 VLOG(1) << "\tPeer certificate verifyErr = " + std::to_string(verifyErr) + ": " +
125 std::string(X509_verify_cert_error_string(verifyErr));
126
127 char* str = X509_NAME_oneline(X509_get_subject_name(server_cert), nullptr, 0);
128 VLOG(1) << "\t Subject: " + std::string(str);
129 OPENSSL_free(str);
130
131 str = X509_NAME_oneline(X509_get_issuer_name(server_cert), nullptr, 0);
132 VLOG(1) << "\t Issuer: " + std::string(str);
133 OPENSSL_free(str);
134
135 // We could do all sorts of certificate verification stuff here before deallocating the certificate.
136
137 GENERAL_NAMES* subjectAltNames =
138 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr));
139
140 int32_t altNameCount = OPENSSL_sk_num(reinterpret_cast<const OPENSSL_STACK*>(subjectAltNames));
141
142 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
143 for (int32_t i = 0; i < altNameCount; ++i) {
144 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
145
146 if (generalName->type == GEN_URI) {
147 std::string subjectAltName =
148 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
149 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
150 VLOG(1) << "\t SAN (URI): '" + subjectAltName;
151 } else if (generalName->type == GEN_DNS) {
152 std::string subjectAltName =
153 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
154 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
155 VLOG(1) << "\t SAN (DNS): '" + subjectAltName;
156 } else {
157 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
158 }
159 }
160
161 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
162
163 X509_free(server_cert);
164 } else {
165 LOG(WARNING) << "\tPeer certificate: no certificate";
166 }
167 });
168
169 webApp.setOnDisconnect([instanceName](SocketConnection* socketConnection) { // onDisconnect
170 VLOG(1) << "OnDisconnect " << instanceName;
171
172 VLOG(2) << " Local: " << socketConnection->getLocalAddress().toString(false);
173 VLOG(2) << " Peer: " << socketConnection->getRemoteAddress().toString(false);
174
175 VLOG(2) << " Online Since: " << socketConnection->getOnlineSince();
176 VLOG(2) << " Online Duration: " << socketConnection->getOnlineDuration();
177
178 VLOG(2) << " Total Queued: " << socketConnection->getTotalQueued();
179 VLOG(2) << " Total Sent: " << socketConnection->getTotalSent();
180 VLOG(2) << " Write Delta: " << socketConnection->getTotalQueued() - socketConnection->getTotalSent();
181 VLOG(2) << " Total Read: " << socketConnection->getTotalRead();
182 VLOG(2) << " Total Processed: " << socketConnection->getTotalProcessed();
183 VLOG(2) << " Read Delta: " << socketConnection->getTotalRead() - socketConnection->getTotalProcessed();
184 });
185
186 return webApp;
187 }
188
189} // namespace apps::http::tls
190
191#endif // (STREAM_TYPE == TLS) // tls
192
193#endif // APPS_HTTP_MODEL_SERVERS_H
void required(CLI::Option *opt, bool req=true)
int main(int argc, char *argv[])
#define QUOTE_INCLUDE(a)
Definition clients.h:47
#define STR_INCLUDE(a)
Definition servers.h:48
static express::Router getRouter()
Definition servers.h:68
static WebApp getWebApp(const std::string &name)
Definition servers.h:79
static WebApp getWebApp(const std::string &name)
Definition servers.h:96