mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.0 KiB
107 lines
3.0 KiB
5 years ago
|
/*
|
||
|
* Copyright (c) 2013-2020, The PurpleI2P Project
|
||
|
*
|
||
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||
|
*
|
||
|
* See full license text in LICENSE file at top of project tree
|
||
|
*/
|
||
|
|
||
11 years ago
|
#ifndef HTTP_SERVER_H__
|
||
|
#define HTTP_SERVER_H__
|
||
|
|
||
8 years ago
|
#include <inttypes.h>
|
||
|
#include <string>
|
||
|
#include <memory>
|
||
|
#include <map>
|
||
|
#include <thread>
|
||
|
#include <boost/asio.hpp>
|
||
7 years ago
|
#include <sstream>
|
||
8 years ago
|
#include "HTTP.h"
|
||
|
|
||
7 years ago
|
namespace i2p
|
||
8 years ago
|
{
|
||
7 years ago
|
namespace http
|
||
8 years ago
|
{
|
||
7 years ago
|
const size_t HTTP_CONNECTION_BUFFER_SIZE = 8192;
|
||
|
const int TOKEN_EXPIRATION_TIMEOUT = 30; // in seconds
|
||
9 years ago
|
|
||
10 years ago
|
class HTTPConnection: public std::enable_shared_from_this<HTTPConnection>
|
||
11 years ago
|
{
|
||
|
public:
|
||
|
|
||
7 years ago
|
HTTPConnection (std::string serverhost, std::shared_ptr<boost::asio::ip::tcp::socket> socket);
|
||
10 years ago
|
void Receive ();
|
||
7 years ago
|
|
||
11 years ago
|
private:
|
||
|
|
||
11 years ago
|
void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||
9 years ago
|
void Terminate (const boost::system::error_code& ecode);
|
||
9 years ago
|
|
||
9 years ago
|
void RunRequest ();
|
||
9 years ago
|
bool CheckAuth (const HTTPReq & req);
|
||
9 years ago
|
void HandleRequest (const HTTPReq & req);
|
||
|
void HandlePage (const HTTPReq & req, HTTPRes & res, std::stringstream& data);
|
||
|
void HandleCommand (const HTTPReq & req, HTTPRes & res, std::stringstream& data);
|
||
|
void SendReply (HTTPRes & res, std::string & content);
|
||
5 years ago
|
uint32_t CreateToken ();
|
||
9 years ago
|
|
||
9 years ago
|
private:
|
||
11 years ago
|
|
||
9 years ago
|
std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
|
||
9 years ago
|
char m_Buffer[HTTP_CONNECTION_BUFFER_SIZE + 1];
|
||
10 years ago
|
size_t m_BufferLen;
|
||
8 years ago
|
std::string m_SendBuffer;
|
||
9 years ago
|
bool needAuth;
|
||
|
std::string user;
|
||
|
std::string pass;
|
||
7 years ago
|
std::string expected_host;
|
||
8 years ago
|
|
||
|
static std::map<uint32_t, uint32_t> m_Tokens; // token->timestamp in seconds
|
||
11 years ago
|
};
|
||
11 years ago
|
|
||
|
class HTTPServer
|
||
|
{
|
||
|
public:
|
||
|
|
||
9 years ago
|
HTTPServer (const std::string& address, int port);
|
||
9 years ago
|
~HTTPServer ();
|
||
11 years ago
|
|
||
|
void Start ();
|
||
|
void Stop ();
|
||
|
|
||
|
private:
|
||
|
|
||
11 years ago
|
void Run ();
|
||
7 years ago
|
void Accept ();
|
||
9 years ago
|
void HandleAccept(const boost::system::error_code& ecode,
|
||
|
std::shared_ptr<boost::asio::ip::tcp::socket> newSocket);
|
||
9 years ago
|
void CreateConnection(std::shared_ptr<boost::asio::ip::tcp::socket> newSocket);
|
||
7 years ago
|
|
||
11 years ago
|
private:
|
||
|
|
||
8 years ago
|
bool m_IsRunning;
|
||
9 years ago
|
std::unique_ptr<std::thread> m_Thread;
|
||
11 years ago
|
boost::asio::io_service m_Service;
|
||
|
boost::asio::io_service::work m_Work;
|
||
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
||
7 years ago
|
std::string m_Hostname;
|
||
11 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
//all the below functions are also used by Qt GUI, see mainwindow.cpp -> getStatusPageHtml
|
||
|
enum OutputFormatEnum { forWebConsole, forQtUi };
|
||
|
void ShowStatus (std::stringstream& s, bool includeHiddenContent, OutputFormatEnum outputFormat);
|
||
|
void ShowLocalDestinations (std::stringstream& s);
|
||
|
void ShowLeasesSets(std::stringstream& s);
|
||
|
void ShowTunnels (std::stringstream& s);
|
||
|
void ShowTransitTunnels (std::stringstream& s);
|
||
|
void ShowTransports (std::stringstream& s);
|
||
|
void ShowSAMSessions (std::stringstream& s);
|
||
|
void ShowI2PTunnels (std::stringstream& s);
|
||
|
void ShowLocalDestination (std::stringstream& s, const std::string& b32, uint32_t token);
|
||
3 years ago
|
void ShowSAMSession (std::stringstream& s, const std::string& id);
|
||
|
void ShowI2CPLocalDestination (std::stringstream& s, const std::string& id);
|
||
9 years ago
|
} // http
|
||
|
} // i2p
|
||
11 years ago
|
|
||
9 years ago
|
#endif /* HTTP_SERVER_H__ */
|