BBS2chProxyKeyManager.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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() {};
  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. CookieJar() {
  30. pthread_mutex_init(&_mutex, NULL);
  31. };
  32. ~CookieJar() {
  33. pthread_mutex_destroy(&_mutex);
  34. };
  35. std::vector<Cookie>& getList();
  36. void set(Cookie &cookie);
  37. void clear();
  38. void lock();
  39. void unlock();
  40. void *jsonValue();
  41. };
  42. private:
  43. std::map<std::string, std::string> _keys;
  44. std::map<std::string, double> _keyIssueTimes;
  45. std::set<std::string> _expiredKeys;
  46. pthread_mutex_t _mutex;
  47. std::string _lastKey;
  48. std::string _storagePath;
  49. std::map<std::string, CookieJar> _cookies;
  50. static const std::string _emptyKey;
  51. public:
  52. BBS2chProxyKeyManager() {
  53. pthread_mutex_init(&_mutex, NULL);
  54. };
  55. ~BBS2chProxyKeyManager() {
  56. pthread_mutex_destroy(&_mutex);
  57. };
  58. const std::string& getKey();
  59. const std::string& getKey(const std::string &userAgent);
  60. void setKey(const std::string &key, const std::string &oldKey, const std::string &userAgent, int reason);
  61. bool isExpired(const std::string &key);
  62. double secondsToWaitBeforePosting(const std::string &key);
  63. void setStorage(const char *jsonPath);
  64. int loadKeys();
  65. CookieJar& getCookieJar(const std::string &userAgent);
  66. bool flushCookies();
  67. private:
  68. bool saveKeys();
  69. };