BBS2chProxyKeyManager.h 886 B

123456789101112131415161718192021222324252627282930313233
  1. #pragma once
  2. #include <string>
  3. #include <map>
  4. #include <set>
  5. #include <pthread.h>
  6. class BBS2chProxyKeyManager {
  7. private:
  8. std::map<std::string, std::string> _keys;
  9. std::map<std::string, double> _keyIssueTimes;
  10. std::set<std::string> _expiredKeys;
  11. pthread_mutex_t _mutex;
  12. std::string _lastKey;
  13. std::string _storagePath;
  14. static const std::string _emptyKey;
  15. public:
  16. BBS2chProxyKeyManager() {
  17. pthread_mutex_init(&_mutex, NULL);
  18. };
  19. ~BBS2chProxyKeyManager() {
  20. pthread_mutex_destroy(&_mutex);
  21. };
  22. const std::string& getKey();
  23. const std::string& getKey(const std::string &userAgent);
  24. void setKey(const std::string &key, const std::string &oldKey, const std::string &userAgent, int reason);
  25. bool isExpired(const std::string &key);
  26. double secondsToWaitBeforePosting(const std::string &key);
  27. void setStorage(const char *jsonPath);
  28. int loadKeys();
  29. private:
  30. bool saveKeys();
  31. };