Expire tokens, use std::shared_ptr for I2PControlSession.

This commit is contained in:
EinMByte 2015-08-02 16:32:54 +02:00
parent 109127a39e
commit 69d93146f2
4 changed files with 69 additions and 14 deletions

View file

@ -4,14 +4,15 @@
#include <boost/property_tree/ptree.hpp>
#include <string>
#include <map>
#include <set>
#include <functional>
#include <mutex>
#include <boost/asio.hpp>
namespace i2p {
namespace client {
const char I2P_CONTROL_DEFAULT_PASSWORD[] = "itoopie";
const uint64_t I2P_CONTROL_TOKEN_LIFETIME = 600; // Token lifetime in seconds
const char I2P_CONTROL_PROPERTY_ID[] = "id";
const char I2P_CONTROL_PROPERTY_METHOD[] = "method";
@ -57,9 +58,10 @@ const char I2P_CONTROL_ROUTER_MANAGER_RESEED[] = "Reseed";
/**
* "Null" I2P control implementation, does not do actual networking.
* @note authentication tokens are per-session
* @note I2PControlSession must always be used as a std::shared_ptr
* @warning an I2PControlSession must be destroyed before its io_service
*/
class I2PControlSession {
class I2PControlSession : public std::enable_shared_from_this<I2PControlSession> {
public:
enum class ErrorCode {
@ -110,11 +112,16 @@ public:
*/
I2PControlSession(boost::asio::io_service& ios);
~I2PControlSession();
/**
* Starts the I2PControlSession.
* In essence, this starts the expireTokensTimer.
* @note should always be called after construction
*/
void start();
/**
* Cancels all operations that are waiting.
* @note must not be called before destruction, destructor handles this
* @note it's a good idea to call this before destruction (shared_ptr reset)
*/
void stop();
@ -143,6 +150,13 @@ private:
*/
std::string generateToken() const;
void startExpireTokensJob();
/**
* Expire tokens that are too old.
*/
void expireTokens(const boost::system::error_code& error);
// Method handlers
void handleAuthenticate(const PropertyTree& pt, Response& response);
void handleEcho(const PropertyTree& pt, Response& response);
@ -168,7 +182,8 @@ private:
void handleReseed(Response& response);
std::string password;
std::set<std::string> tokens;
std::map<std::string, uint64_t> tokens;
std::mutex tokensMutex;
std::map<std::string, MethodHandler> methodHandlers;
std::map<std::string, RequestHandler> routerInfoHandlers;
@ -177,6 +192,7 @@ private:
boost::asio::io_service& service;
boost::asio::deadline_timer shutdownTimer;
boost::asio::deadline_timer expireTokensTimer;
};
}