BBS2chProxyKeyManager.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include <string>
  3. #include <map>
  4. #include <set>
  5. #include <vector>
  6. #include <pthread.h>
  7. class BBS2chProxyKeyManager {
  8. public:
  9. struct Cookie {
  10. std::string name;
  11. std::string value;
  12. std::string domain;
  13. bool includeSubdomains;
  14. std::string path;
  15. bool secure;
  16. std::string expires;
  17. Cookie() : includeSubdomains(true), path("/"), secure(), expires("0") {};
  18. Cookie(const std::string &value);
  19. std::string valueInNetscapeFormat();
  20. bool isExpired();
  21. bool isSameAs(Cookie &cookie);
  22. void *jsonValue();
  23. };
  24. class CookieJar {
  25. private:
  26. std::vector<Cookie> _cookies;
  27. pthread_mutex_t _mutex;
  28. public:
  29. time_t timeAcornSynced;
  30. time_t timeBeSynced;
  31. time_t timeUpliftSynced;
  32. CookieJar() : timeAcornSynced(), timeBeSynced(), timeUpliftSynced() {
  33. pthread_mutex_init(&_mutex, NULL);
  34. };
  35. ~CookieJar() {
  36. pthread_mutex_destroy(&_mutex);
  37. };
  38. std::vector<Cookie>& getList();
  39. void set(Cookie &cookie);
  40. void clear();
  41. void lock();
  42. void unlock();
  43. void *jsonValue();
  44. };
  45. private:
  46. std::map<std::string, std::string> _keys;
  47. std::map<std::string, double> _keyIssueTimes;
  48. std::set<std::string> _expiredKeys;
  49. pthread_mutex_t _mutex;
  50. std::string _lastKey;
  51. std::string _storagePath;
  52. std::map<std::string, CookieJar> _cookies;
  53. static const std::string _emptyKey;
  54. public:
  55. BBS2chProxyKeyManager() {
  56. pthread_mutex_init(&_mutex, NULL);
  57. };
  58. ~BBS2chProxyKeyManager() {
  59. pthread_mutex_destroy(&_mutex);
  60. };
  61. const std::string& getKey();
  62. const std::string& getKey(const std::string &userAgent);
  63. void setKey(const std::string &key, const std::string &oldKey, const std::string &userAgent, int reason);
  64. bool isExpired(const std::string &key);
  65. double secondsToWaitBeforePosting(const std::string &key);
  66. void setStorage(const char *jsonPath);
  67. int loadKeys();
  68. CookieJar& getCookieJar(const std::string &userAgent, bool syncCookie=false);
  69. bool flushCookies();
  70. std::string cookieManagerHTML();
  71. private:
  72. bool saveKeys();
  73. };