main.cpp 15 KB

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