main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #ifdef _WIN32
  6. #include <winsock2.h>
  7. #include <ws2tcpip.h>
  8. #include <mswsock.h>
  9. #else
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <netdb.h>
  13. #endif
  14. #include <pthread.h>
  15. #include <signal.h>
  16. #include <getopt.h>
  17. #include <curl/curl.h>
  18. #ifdef USE_LUA
  19. #include <lua.hpp>
  20. #endif
  21. #include "BBS2chProxyConnection.h"
  22. #include "BBS2chProxyThreadInfo.h"
  23. #include "BBS2chProxyAuth.h"
  24. #define PORT 9080
  25. #define VERSION "20210528"
  26. #define BACKLOG 32
  27. #define NUM_LOCKS 7
  28. char *proxy_server;
  29. long proxy_port;
  30. long proxy_type;
  31. long timeout = 30;
  32. char *user_agent;
  33. char *appKey;
  34. char *hmacKey;
  35. char *api_ua_auth;
  36. char *api_ua_dat;
  37. char *x_2ch_ua_auth;
  38. char *x_2ch_ua_dat;
  39. int allow_chunked;
  40. int verbosity;
  41. int curl_features;
  42. unsigned int curl_version_number;
  43. bool accept_https;
  44. int force_5chnet = 1;
  45. int force_5chnet_https;
  46. int force_ipv4;
  47. char *bbsmenu_url;
  48. char *api_server;
  49. std::map<std::string, std::string> bbscgi_headers;
  50. int gikofix;
  51. CURLSH *curl_share;
  52. char *lua_script;
  53. static pthread_mutex_t lockarray[NUM_LOCKS];
  54. void log_printf(int level, const char *format ...)
  55. {
  56. if(level > verbosity) return;
  57. va_list argp;
  58. va_start(argp, format);
  59. vfprintf(stderr, format, argp);
  60. va_end(argp);
  61. fflush(stderr);
  62. }
  63. struct listener {
  64. int port;
  65. int sock_listener;
  66. struct sockaddr_in addr_listener;
  67. };
  68. static void usage(void)
  69. {
  70. fprintf(stderr,"usage: proxy2ch [OPTIONS]\n");
  71. fprintf(stderr,"available options:\n");
  72. fprintf(stderr," -p <port> : Listen on port <port> (default: %d)\n",PORT);
  73. fprintf(stderr," -t <timeout> : Set connection timeout to <timeout> seconds (default: %ld)\n",timeout);
  74. fprintf(stderr," -a <user-agent> : Overwrite user-agent for connection\n");
  75. fprintf(stderr," -g : Accept all incoming connections (default: localhost only)\n");
  76. fprintf(stderr," -c : Accept HTTP CONNECT method (act as an HTTPS proxy)\n");
  77. fprintf(stderr," -4 : Force IPv4 DNS resolution\n");
  78. fprintf(stderr," -b <backlog> : Set backlog value to <backlog> for listen() (default: %d)\n",BACKLOG);
  79. fprintf(stderr," -s : Force https connection for 5ch.net/bbspink.com URLs\n");
  80. fprintf(stderr," --proxy <server:port> : Use proxy <server:port> for connection\n");
  81. fprintf(stderr," --api <AppKey:HmacKey> : Use API instead of read.cgi for dat retrieving\n");
  82. fprintf(stderr," --api-auth-ua <user-agent> : Specify user-agent for API authentication\n");
  83. fprintf(stderr," --api-dat-ua <user-agent> : Specify user-agent for dat retrieving via API\n");
  84. fprintf(stderr," --api-auth-xua <X-2ch-UA> : Specify X-2ch-UA for API authentication\n");
  85. fprintf(stderr," --api-dat-xua <X-2ch-UA> : Specify X-2ch-UA for dat retrieving via API\n");
  86. fprintf(stderr," --api-server <server> : Specify gateway server for API\n");
  87. fprintf(stderr," --bbsmenu <URL> : Replace \"5ch.net\" occurrences in links for URL\n");
  88. fprintf(stderr," --chunked : Preserve \"chunked\" transfer encoding\n");
  89. fprintf(stderr," --bbscgi-header <header: value> : Force replace header when requesting bbs.cgi\n");
  90. #ifdef USE_LUA
  91. fprintf(stderr," --bbscgi-lua <path> : Process request header/body sent to bbs.cgi with a Lua script at <path>\n");
  92. #endif
  93. fprintf(stderr," --verbose : Print logs in detail\n");
  94. fprintf(stderr," --gikofix : Fix invalid HTTP POST body (for gikoNavi)\n");
  95. }
  96. static void *listen(void *param)
  97. {
  98. struct listener *listener = (struct listener *)param;
  99. log_printf(0,"Listening on port %d...\n",listener->port);
  100. if(listener->addr_listener.sin_addr.s_addr == INADDR_ANY) {
  101. log_printf(0,"WARNING: proxy accepts all incoming connections!\n");
  102. }
  103. fflush(stderr);
  104. int sock_c;
  105. pthread_mutex_t mutex, mutex2;
  106. BBS2chProxyThreadCache *cache = new BBS2chProxyThreadCache();
  107. socklen_t addrlen = sizeof(listener->addr_listener);
  108. pthread_mutex_init(&mutex, NULL);
  109. pthread_mutex_init(&mutex2, NULL);
  110. BBS2chProxyAuth *auth = new BBS2chProxyAuth(&mutex2);
  111. while(1) {
  112. if (-1 == (sock_c = accept(listener->sock_listener, (struct sockaddr *)&listener->addr_listener, &addrlen))) {
  113. perror("accept");
  114. continue;
  115. }
  116. //fprintf(stderr,"accepted\n");
  117. BBS2chProxyConnection *connection = new BBS2chProxyConnection(sock_c, cache, auth, &mutex);
  118. connection->run();
  119. }
  120. pthread_mutex_destroy(&mutex);
  121. pthread_mutex_destroy(&mutex2);
  122. delete cache;
  123. }
  124. static void lock_cb(CURL *handle, curl_lock_data data, curl_lock_access access, void *userptr)
  125. {
  126. pthread_mutex_lock(&lockarray[data]);
  127. }
  128. static void unlock_cb(CURL *handle, curl_lock_data data, void *userptr)
  129. {
  130. pthread_mutex_unlock(&lockarray[data]);
  131. }
  132. static void init_locks(void)
  133. {
  134. int i;
  135. for(i = 0; i< NUM_LOCKS; i++)
  136. pthread_mutex_init(&lockarray[i], NULL);
  137. }
  138. int main(int argc, char *argv[])
  139. {
  140. struct listener listener;
  141. int ch;
  142. extern char *optarg;
  143. extern int optind, opterr;
  144. int option_index;
  145. bool global = false;
  146. int backlog = BACKLOG;
  147. struct option options[] = {
  148. {"proxy", 1, NULL, 0},
  149. {"api", 1, NULL, 0},
  150. {"api-auth-ua", 1, NULL, 0},
  151. {"api-dat-ua", 1, NULL, 0},
  152. {"api-auth-xua", 1, NULL, 0},
  153. {"api-dat-xua", 1, NULL, 0},
  154. {"api-server", 1, NULL, 0},
  155. {"bbsmenu", 1, NULL, 0},
  156. {"chunked", 0, NULL, 0},
  157. {"verbose", 0, NULL, 0},
  158. {"debug", 0, NULL, 0},
  159. {"bbscgi-header", 1, NULL, 0},
  160. #ifdef USE_LUA
  161. {"bbscgi-lua", 1, NULL, 0},
  162. #endif
  163. {"gikofix", 0, NULL, 0},
  164. {0, 0, 0, 0}
  165. };
  166. curl_global_init(CURL_GLOBAL_DEFAULT);
  167. curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
  168. curl_features = data->features;
  169. curl_version_number = data->version_num;
  170. if(data->version_num >= 0x074400) { /* version 7.68.0 or later */
  171. init_locks();
  172. curl_share = curl_share_init();
  173. curl_share_setopt(curl_share, CURLSHOPT_LOCKFUNC, lock_cb);
  174. curl_share_setopt(curl_share, CURLSHOPT_UNLOCKFUNC, unlock_cb);
  175. curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  176. #if LIBCURL_VERSION_NUM >= 0x070a03
  177. curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
  178. #endif
  179. /* Shared connection cache is still buggy at the moment!
  180. See https://github.com/curl/curl/issues/4915 */
  181. #if 0 && LIBCURL_VERSION_NUM >= 0x073900
  182. curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
  183. #endif
  184. }
  185. log_printf(0,"proxy2ch version %s with curl %s (TLS/SSL backend: %s)\n",VERSION,data->version,data->ssl_version);
  186. #ifdef USE_LUA
  187. log_printf(0,"Scripting enabled with " LUA_RELEASE "\n");
  188. #endif
  189. memset(&listener, 0, sizeof(listener));
  190. listener.port = PORT;
  191. api_server = strdup("api.5ch.net");
  192. while ((ch = getopt_long(argc, argv, "p:t:ha:gc4b:s", options, &option_index)) != -1) {
  193. switch (ch) {
  194. case 0:
  195. if(!strcmp(options[option_index].name, "proxy")) {
  196. char *ptr = strchr(optarg, '@');
  197. if(!ptr) {
  198. ptr = strstr(optarg, "://");
  199. if(ptr) ptr = strchr(ptr+3,':');
  200. else ptr = strchr(optarg,':');
  201. }
  202. else ptr = strchr(ptr+1,':');
  203. if(!ptr) {
  204. fprintf(stderr,"Proxy port is not specified, as --proxy=server:port\n");
  205. return -1;
  206. }
  207. proxy_server = (char *)malloc(ptr-optarg+1);
  208. proxy_port = atoi(ptr+1);
  209. memcpy(proxy_server,optarg,ptr-optarg);
  210. proxy_server[ptr-optarg] = 0;
  211. if(!strncasecmp(optarg,"socks4://",9)) proxy_type = CURLPROXY_SOCKS4;
  212. else if(!strncasecmp(optarg,"socks5://",9)) proxy_type = CURLPROXY_SOCKS5;
  213. #if LIBCURL_VERSION_NUM >= 0x071200
  214. else if(!strncasecmp(optarg,"socks4a://",10)) proxy_type = CURLPROXY_SOCKS4A;
  215. else if(!strncasecmp(optarg,"socks5h://",10)) proxy_type = CURLPROXY_SOCKS5_HOSTNAME;
  216. #endif
  217. }
  218. else if(!strcmp(options[option_index].name, "api")) {
  219. if((curl_features & CURL_VERSION_SSL) == 0) {
  220. fprintf(stderr,"Your libcurl doesn't support HTTPS; API mode cannot be enabled.\n");
  221. return -1;
  222. }
  223. char *ptr = strchr(optarg, ':');
  224. if(!ptr) {
  225. fprintf(stderr,"API keys should be provided as AppKey:HmacKey\n");
  226. return -1;
  227. }
  228. appKey = (char *)malloc(ptr-optarg+1);
  229. memcpy(appKey,optarg,ptr-optarg);
  230. appKey[ptr-optarg] = 0;
  231. char *start = ptr+1;
  232. ptr = strchr(start, ':');
  233. if(!ptr) ptr = strchr(optarg, 0);
  234. hmacKey = (char *)malloc(ptr-start+1);
  235. memcpy(hmacKey,start,ptr-start);
  236. hmacKey[ptr-start] = 0;
  237. /*if(*ptr) {
  238. x_2ch_ua = (char *)malloc(strlen(ptr+1)+11);
  239. sprintf(x_2ch_ua,"X-2ch-UA: %s",ptr+1);
  240. }*/
  241. //fprintf(stderr,"%s,%s,%s\n",appKey,hmacKey,x_2ch_ua);
  242. //return 0;
  243. }
  244. else if(!strcmp(options[option_index].name, "api-auth-ua")) {
  245. api_ua_auth = (char *)malloc(strlen(optarg)+1);
  246. strcpy(api_ua_auth,optarg);
  247. }
  248. else if(!strcmp(options[option_index].name, "api-dat-ua")) {
  249. api_ua_dat = (char *)malloc(strlen(optarg)+1);
  250. strcpy(api_ua_dat,optarg);
  251. }
  252. else if(!strcmp(options[option_index].name, "api-auth-xua")) {
  253. x_2ch_ua_auth = (char *)malloc(strlen(optarg)+11);
  254. sprintf(x_2ch_ua_auth,"X-2ch-UA: %s",optarg);
  255. }
  256. else if(!strcmp(options[option_index].name, "api-dat-xua")) {
  257. x_2ch_ua_dat = (char *)malloc(strlen(optarg)+11);
  258. sprintf(x_2ch_ua_dat,"X-2ch-UA: %s",optarg);
  259. }
  260. else if(!strcmp(options[option_index].name, "chunked")) {
  261. allow_chunked = 1;
  262. }
  263. else if(!strcmp(options[option_index].name, "verbose")) {
  264. verbosity = 1;
  265. }
  266. else if(!strcmp(options[option_index].name, "debug")) {
  267. verbosity = 5;
  268. }
  269. else if(!strcmp(options[option_index].name, "bbsmenu")) {
  270. bbsmenu_url = (char *)malloc(strlen(optarg)+1);
  271. strcpy(bbsmenu_url, optarg);
  272. }
  273. else if(!strcmp(options[option_index].name, "api-server")) {
  274. if(api_server) free(api_server);
  275. api_server = (char *)malloc(strlen(optarg)+1);
  276. strcpy(api_server, optarg);
  277. }
  278. else if(!strcmp(options[option_index].name, "bbscgi-header")) {
  279. char *ptr = strchr(optarg, ':');
  280. if(!ptr) break;
  281. char *header = (char *)malloc(ptr-optarg+1);
  282. memcpy(header,optarg,ptr-optarg);
  283. header[ptr-optarg] = 0;
  284. char *value = ptr+1;
  285. ptr = header+(ptr-optarg-1);
  286. while(*ptr == ' ') *ptr-- = 0;
  287. while(*value == ' ') value++;
  288. bbscgi_headers[header] = value;
  289. free(header);
  290. }
  291. #ifdef USE_LUA
  292. else if(!strcmp(options[option_index].name, "bbscgi-lua")) {
  293. lua_script = (char *)malloc(strlen(optarg)+1);
  294. strcpy(lua_script, optarg);
  295. }
  296. #endif
  297. else if(!strcmp(options[option_index].name, "gikofix")) {
  298. gikofix = 1;
  299. }
  300. break;
  301. case 'p':
  302. listener.port = atoi(optarg);
  303. break;
  304. case 't':
  305. timeout = atoi(optarg);
  306. break;
  307. case 'a':
  308. user_agent = (char *)malloc(strlen(optarg)+1);
  309. strcpy(user_agent, optarg);
  310. break;
  311. case 'g':
  312. global = true;
  313. break;
  314. case 'c':
  315. accept_https = true;
  316. break;
  317. case '4':
  318. force_ipv4 = 1;
  319. break;
  320. case 'b':
  321. backlog = atoi(optarg);
  322. break;
  323. case 's':
  324. if((curl_features & CURL_VERSION_SSL) == 0) {
  325. fprintf(stderr,"Your libcurl doesn't support HTTPS; it does not work with -s option.\n");
  326. return -1;
  327. }
  328. if(strstr(data->ssl_version, "OpenSSL/0") || strstr(data->ssl_version, "OpenSSL/1.0") ||
  329. (strstr(data->ssl_version, "LibreSSL/2") && !strstr(data->ssl_version, "LibreSSL/2.9"))) {
  330. fprintf(stderr,
  331. "WARNING: OpenSSL < 1.1.0 and LibreSSL < 2.9.0 aren't thread-safe without setting callbacks for mutex. "
  332. "It may cause unintended crashes when many requests are incoming at the same time.\n");
  333. }
  334. force_5chnet_https = 1;
  335. break;
  336. default:
  337. usage();
  338. return 0;
  339. }
  340. }
  341. log_printf(0, "Global User-Agent: %s\n",user_agent?user_agent:"n/a");
  342. if(appKey) {
  343. log_printf(0, "API gateway server: %s\n",api_server);
  344. log_printf(0, "User-Agent (for API authentication): %s\n",api_ua_auth?api_ua_auth:"");
  345. log_printf(0, "User-Agent (for API dat retrieving): %s\n",api_ua_dat?api_ua_dat:"");
  346. log_printf(0, "X-2ch-UA (for API authentication): %s\n",x_2ch_ua_auth?x_2ch_ua_auth+10:"");
  347. log_printf(0, "X-2ch-UA (for API dat retrieving): %s\n",x_2ch_ua_dat?x_2ch_ua_dat+10:"");
  348. }
  349. if(!bbscgi_headers.empty()) {
  350. log_printf(0, "Custom headers for bbs.cgi:\n");
  351. for(std::map<std::string, std::string>::iterator it = bbscgi_headers.begin(); it!=bbscgi_headers.end(); it++) {
  352. log_printf(0, " %s: %s\n", it->first.c_str(), it->second.c_str());
  353. }
  354. }
  355. if(lua_script) {
  356. log_printf(0, "Use Lua script %s for bbs.cgi request modification\n", lua_script);
  357. }
  358. if(proxy_server) {
  359. log_printf(0,"Use proxy %s:%ld for connection\n",proxy_server,proxy_port);
  360. }
  361. #ifdef _WIN32
  362. WSADATA wsaData;
  363. if (WSAStartup(MAKEWORD(2, 0), &wsaData) == SOCKET_ERROR) {
  364. fprintf(stderr, "WSAStartup: error initializing WSA.\n");
  365. return -1;
  366. }
  367. #endif
  368. listener.addr_listener.sin_family = AF_INET;
  369. if(global) listener.addr_listener.sin_addr.s_addr = INADDR_ANY;
  370. else listener.addr_listener.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  371. listener.addr_listener.sin_port = htons(listener.port);
  372. #ifdef _WIN32
  373. if ((listener.sock_listener = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0)) == INVALID_SOCKET) {
  374. fprintf(stderr,"WSASocket: socket initialize error\n");
  375. return -1;
  376. }
  377. #else
  378. if (-1 == (listener.sock_listener = socket(AF_INET, SOCK_STREAM, 0))) {
  379. perror("socket");
  380. return -1;
  381. }
  382. #endif
  383. int optval=1;
  384. setsockopt(listener.sock_listener, SOL_SOCKET, SO_REUSEADDR, (char *)&optval, sizeof(optval));
  385. #ifdef _WIN32
  386. optval = SO_SYNCHRONOUS_NONALERT;
  387. setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&optval, sizeof(optval));
  388. #endif
  389. socklen_t addrlen = sizeof(listener.addr_listener);
  390. if (-1 == bind(listener.sock_listener, (struct sockaddr *)&listener.addr_listener, addrlen)) {
  391. perror("bind");
  392. return -1;
  393. }
  394. if (-1 == listen(listener.sock_listener, backlog)) {
  395. perror("listen");
  396. return -1;
  397. }
  398. if (-1 == getsockname(listener.sock_listener, (struct sockaddr *)&listener.addr_listener, &addrlen)) {
  399. perror("getsockname");
  400. return -1;
  401. }
  402. #ifndef _WIN32
  403. signal( SIGPIPE , SIG_IGN );
  404. #endif
  405. pthread_t thread_listener;
  406. if(0 != pthread_create(&thread_listener , NULL , listen , &listener))
  407. perror("pthread_create");
  408. pthread_join(thread_listener, NULL);
  409. return 0;
  410. }