aboutsummaryrefslogtreecommitdiff
path: root/src/server/bnetserver/REST/LoginRESTService.h
blob: 9309f71f68f220969ff14aa9d9b1084a208d1717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LoginRESTService_h__
#define LoginRESTService_h__

#include "Define.h"
#include "IoContext.h"
#include "IpAddress.h"
#include "Login.pb.h"
#include "Session.h"
#include <boost/asio/ip/tcp.hpp>
#include <atomic>
#include <thread>

class AsyncRequest;
struct soap;
struct soap_plugin;

enum class BanMode
{
    BAN_IP = 0,
    BAN_ACCOUNT = 1
};

class LoginRESTService
{
public:
    LoginRESTService() : _ioContext(nullptr), _stopped(false), _port(0), _loginTicketDuration(0) { }

    static LoginRESTService& Instance();

    bool Start(Trinity::Asio::IoContext* ioContext);
    void Stop();

    boost::asio::ip::tcp::endpoint const& GetAddressForClient(boost::asio::ip::address const& address) const;

private:
    void Run();

    friend int32 handle_get_plugin(soap* soapClient);
    friend int32 handle_post_plugin(soap* soapClient);

    using HttpMethodHandlerMap = std::unordered_map<std::string, int32(LoginRESTService::*)(std::shared_ptr<AsyncRequest>)>;
    int32 HandleHttpRequest(soap* soapClient, char const* method, HttpMethodHandlerMap const& handlers);

    int32 HandleGetForm(std::shared_ptr<AsyncRequest> request);
    int32 HandleGetGameAccounts(std::shared_ptr<AsyncRequest> request);
    int32 HandleGetPortal(std::shared_ptr<AsyncRequest> request);

    int32 HandlePostLogin(std::shared_ptr<AsyncRequest> request);
    int32 HandlePostRefreshLoginTicket(std::shared_ptr<AsyncRequest> request);

    int32 SendResponse(soap* soapClient, google::protobuf::Message const& response);

    void HandleAsyncRequest(std::shared_ptr<AsyncRequest> request);

    std::string CalculateShaPassHash(std::string const& name, std::string const& password);

    struct ResponseCodePlugin
    {
        static char const* const PluginId;
        static int32 Init(soap* s, soap_plugin*, void*);
        static int32 Copy(soap* s, soap_plugin* dst, soap_plugin* src);
        static void Destroy(soap* s, soap_plugin* p);
        static int32 ChangeResponse(soap* s, int32 originalResponse, size_t contentLength);

        static ResponseCodePlugin* GetForClient(soap* s);

        int32(*fresponse)(soap* s, int32 status, size_t length);
        int32 ErrorCode;
    };

    struct ContentTypePlugin
    {
        static char const* const PluginId;
        static int32 Init(soap* s, soap_plugin* p, void*);
        static void Destroy(soap* s, soap_plugin* p);
        static int32 OnSetHeader(soap* s, char const* key, char const* value);

        int32(*fposthdr)(soap* s, char const* key, char const* value);
        char const* ContentType;
    };

    Trinity::Asio::IoContext* _ioContext;
    std::thread _thread;
    std::atomic<bool> _stopped;
    Battlenet::JSON::Login::FormInputs _formInputs;
    std::string _bindIP;
    int32 _port;
    boost::asio::ip::tcp::endpoint _externalAddress;
    boost::asio::ip::tcp::endpoint _localAddress;
    boost::asio::ip::address_v4 _localNetmask;
    uint32 _loginTicketDuration;

    HttpMethodHandlerMap _getHandlers;
    HttpMethodHandlerMap _postHandlers;
};

#define sLoginService LoginRESTService::Instance()

#endif // LoginRESTService_h__