BBS2chProxyConnection.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include <string>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <pthread.h>
  6. #include <sys/types.h>
  7. #include <regex.h>
  8. #include "DataStorage.h"
  9. #include "BBS2chProxyThreadInfo.h"
  10. #include "BBS2chProxyAuth.h"
  11. #include "IBBS2chProxySocket.h"
  12. #include "BBS2chProxyHttpHeaders.h"
  13. class BBS2chProxyConnection {
  14. private:
  15. int sock_c, sock_s;
  16. BBS2chProxyThreadCache *threadCache;
  17. std::string threadKey;
  18. pthread_mutex_t *mutex;
  19. regex_t regex;
  20. regex_t regex_kako;
  21. regex_t regex_offlaw;
  22. BBS2chProxyAuth *auth;
  23. std::string monaKeyForRequest;
  24. public:
  25. int content_length;
  26. int status;
  27. bool chunked;
  28. bool force5ch;
  29. bool bbscgi;
  30. std::string responseHeaders;
  31. IBBS2chProxySocket *socketToClient;
  32. public:
  33. BBS2chProxyConnection(int socket, BBS2chProxyThreadCache *cache, BBS2chProxyAuth *a, pthread_mutex_t *m) :
  34. sock_c(socket), threadCache(cache), auth(a), mutex(m),
  35. content_length(0), status(0), chunked(false), force5ch(false), bbscgi(false), socketToClient(NULL)
  36. {
  37. regcomp(&regex, "^https?://([^:/.]+)\\.(2ch\\.net|5ch\\.net|bbspink\\.com)(:[0-9]+)?/([^/]+)/dat/([0-9]+)\\.dat", REG_EXTENDED|REG_ICASE);
  38. regcomp(&regex_kako, "^https?://([^:/.]+)\\.(2ch\\.net|5ch\\.net|bbspink\\.com)(:[0-9]+)?/([^/]+)/kako/[0-9]+/([0-9]+/)?([0-9]+)\\.dat", REG_EXTENDED|REG_ICASE);
  39. regcomp(&regex_offlaw, "^https?://([^:/.]+)\\.(2ch\\.net|5ch\\.net|bbspink\\.com)(:[0-9]+)?/test/offlaw2.so\\?.*bbs=([^&]+)", REG_EXTENDED|REG_ICASE);
  40. };
  41. ~BBS2chProxyConnection()
  42. {
  43. regfree(&regex);
  44. regfree(&regex_kako);
  45. regfree(&regex_offlaw);
  46. if (socketToClient) delete socketToClient;
  47. };
  48. void run(void);
  49. void setMonaKey(const std::string &key, int reason);
  50. private:
  51. void connect(void);
  52. int datProxy(const char *url, const char *method);
  53. int datProxyAPI(const char *url, const char *method);
  54. int bbsmenuProxy(const char *url, const char *method);
  55. int bbsCgiProxy(const char *url, const char *protocol, BBS2chProxyHttpHeaders *requestHeaders, const char *requestBody);
  56. DataStorage *html2dat_old(DataStorage *html, int startResNum, time_t *lastModified, bool useCache);
  57. DataStorage *html2dat(DataStorage *html, int startResNum, time_t *lastModified, bool useCache);
  58. static void *launch(void *param);
  59. int tunnel(const char *addr, int port);
  60. static void *tunnel_c2s(void *param);
  61. static void *tunnel_s2c(void *param);
  62. std::string getMonaKey();
  63. };