TrinityCore
LoginHttpSession.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "LoginHttpSession.h"
19#include "DatabaseEnv.h"
20#include "LoginRESTService.h"
21#include "SslContext.h"
22#include "Util.h"
23
24namespace Battlenet
25{
26LoginHttpSession::LoginHttpSession(boost::asio::ip::tcp::socket&& socket)
27 : SslSocket(std::move(socket), SslContext::instance())
28{
29}
30
32
34{
35 std::string ip_address = GetRemoteIpAddress().to_string();
36 TC_LOG_TRACE("server.http.session", "{} Accepted connection", GetClientInfo());
37
38 // Verify that this IP is not in the ip_banned table
39 LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_DEL_EXPIRED_IP_BANS));
40
42 stmt->setString(0, ip_address);
43
45 .WithPreparedCallback([sess = shared_from_this()](PreparedQueryResult result) { sess->CheckIpCallback(std::move(result)); }));
46}
47
49{
50 if (result)
51 {
52 bool banned = false;
53 do
54 {
55 Field* fields = result->Fetch();
56 if (fields[0].GetUInt64() != 0)
57 banned = true;
58
59 } while (result->NextRow());
60
61 if (banned)
62 {
63 TC_LOG_DEBUG("server.http.session", "{} tries to log in using banned IP!", GetClientInfo());
65 return;
66 }
67 }
68
70}
71
73{
74 return sLoginService.HandleRequest(shared_from_this(), context);
75}
76
77std::shared_ptr<Trinity::Net::Http::SessionState> LoginHttpSession::ObtainSessionState(Trinity::Net::Http::RequestContext& context) const
78{
79 using namespace std::string_literals;
80
81 std::shared_ptr<Trinity::Net::Http::SessionState> state;
82
83 auto cookieItr = context.request.find(boost::beast::http::field::cookie);
84 if (cookieItr != context.request.end())
85 {
86 std::vector<std::string_view> cookies = Trinity::Tokenize(Trinity::Net::Http::ToStdStringView(cookieItr->value()), ';', false);
87 std::size_t eq = 0;
88 auto sessionIdItr = std::find_if(cookies.begin(), cookies.end(), [&](std::string_view cookie)
89 {
90 std::string_view name = cookie;
91 eq = cookie.find('=');
92 if (eq != std::string_view::npos)
93 name = cookie.substr(0, eq);
94
95 return name == SESSION_ID_COOKIE;
96 });
97 if (sessionIdItr != cookies.end())
98 {
99 std::string_view value = sessionIdItr->substr(eq + 1);
100 state = sLoginService.FindAndRefreshSessionState(value, GetRemoteIpAddress());
101 }
102 }
103
104 if (!state)
105 {
106 state = sLoginService.CreateNewSessionState(GetRemoteIpAddress());
107
108 std::string_view host = Trinity::Net::Http::ToStdStringView(context.request[boost::beast::http::field::host]);
109 if (std::size_t port = host.find(':'); port != std::string_view::npos)
110 host.remove_suffix(host.length() - port);
111
112 context.response.insert(boost::beast::http::field::set_cookie, Trinity::StringFormat("{}={}; Path=/bnetserver; Domain={}; Secure; HttpOnly; SameSite=None",
113 SESSION_ID_COOKIE, boost::uuids::to_string(state->Id), host));
114 }
115
116 return state;
117}
118}
std::shared_ptr< PreparedResultSet > PreparedQueryResult
DatabaseWorkerPool< LoginDatabaseConnection > LoginDatabase
Accessor to the realm/login database.
Definition: DatabaseEnv.cpp:22
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:156
#define TC_LOG_TRACE(filterType__,...)
Definition: Log.h:153
@ LOGIN_DEL_EXPIRED_IP_BANS
Definition: LoginDatabase.h:32
@ LOGIN_SEL_IP_INFO
Definition: LoginDatabase.h:34
#define sLoginService
Trinity::Net::Http::RequestHandlerResult RequestHandler(Trinity::Net::Http::RequestContext &context) override
LoginHttpSession(boost::asio::ip::tcp::socket &&socket)
static constexpr std::string_view SESSION_ID_COOKIE
void CheckIpCallback(PreparedQueryResult result)
std::shared_ptr< Trinity::Net::Http::SessionState > ObtainSessionState(Trinity::Net::Http::RequestContext &context) const override
Class used to access individual fields of database query result.
Definition: Field.h:90
void setString(const uint8 index, const std::string &value)
boost::asio::ip::address GetRemoteIpAddress() const
Definition: Socket.h:103
std::string_view ToStdStringView(boost::beast::string_view bsw)
Definition: HttpCommon.h:43
TC_COMMON_API std::vector< std::string_view > Tokenize(std::string_view str, char sep, bool keepEmpty)
Definition: Util.cpp:56
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
STL namespace.