test_gns_proxy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2007, 2009, 2011, 2012 Christian Grothoff
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file test_gns_proxy.c
  18. * @brief testcase for accessing SOCKS5 GNS proxy
  19. * @author Martin Schanzenbach
  20. */
  21. #include "platform.h"
  22. /* Just included for the right curl.h */
  23. #include "gnunet_curl_lib.h"
  24. #include <microhttpd.h>
  25. #include "gnunet_util_lib.h"
  26. #include "gnutls/x509.h"
  27. /**
  28. * Largest allowed size for a PEM certificate.
  29. */
  30. #define MAX_PEM_SIZE (10 * 1024)
  31. #define TEST_DOMAIN "www.test"
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
  33. /**
  34. * Return value for 'main'.
  35. */
  36. static int global_ret;
  37. static struct MHD_Daemon *mhd;
  38. static struct GNUNET_SCHEDULER_Task *mhd_task_id;
  39. static struct GNUNET_SCHEDULER_Task *curl_task_id;
  40. static CURL *curl;
  41. static CURLM *multi;
  42. static char *url;
  43. static struct GNUNET_OS_Process *proxy_proc;
  44. static char*cafile_opt;
  45. static char*cafile_srv;
  46. static uint16_t port;
  47. static gnutls_x509_crt_t proxy_cert;
  48. static gnutls_x509_privkey_t proxy_key;
  49. struct CBC
  50. {
  51. char buf[1024];
  52. size_t pos;
  53. };
  54. static struct CBC cbc;
  55. /**
  56. * Read file in filename
  57. *
  58. * @param filename file to read
  59. * @param size pointer where filesize is stored
  60. * @return NULL on error
  61. */
  62. static void*
  63. load_file (const char*filename,
  64. unsigned int*size)
  65. {
  66. void *buffer;
  67. uint64_t fsize;
  68. if (GNUNET_OK !=
  69. GNUNET_DISK_file_size (filename,
  70. &fsize,
  71. GNUNET_YES,
  72. GNUNET_YES))
  73. return NULL;
  74. if (fsize > MAX_PEM_SIZE)
  75. return NULL;
  76. *size = (unsigned int) fsize;
  77. buffer = GNUNET_malloc (*size);
  78. if (fsize !=
  79. GNUNET_DISK_fn_read (filename,
  80. buffer,
  81. (size_t) fsize))
  82. {
  83. GNUNET_free (buffer);
  84. return NULL;
  85. }
  86. return buffer;
  87. }
  88. /**
  89. * Load PEM key from file
  90. *
  91. * @param key where to store the data
  92. * @param keyfile path to the PEM file
  93. * @return #GNUNET_OK on success
  94. */
  95. static int
  96. load_key_from_file (gnutls_x509_privkey_t key,
  97. const char*keyfile)
  98. {
  99. gnutls_datum_t key_data;
  100. int ret;
  101. key_data.data = load_file (keyfile,
  102. &key_data.size);
  103. if (NULL == key_data.data)
  104. return GNUNET_SYSERR;
  105. ret = gnutls_x509_privkey_import (key, &key_data,
  106. GNUTLS_X509_FMT_PEM);
  107. if (GNUTLS_E_SUCCESS != ret)
  108. {
  109. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  110. _ ("Unable to import private key from file `%s'\n"),
  111. keyfile);
  112. }
  113. GNUNET_free_non_null (key_data.data);
  114. return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
  115. }
  116. /**
  117. * Load cert from file
  118. *
  119. * @param crt struct to store data in
  120. * @param certfile path to pem file
  121. * @return #GNUNET_OK on success
  122. */
  123. static int
  124. load_cert_from_file (gnutls_x509_crt_t crt,
  125. const char*certfile)
  126. {
  127. gnutls_datum_t cert_data;
  128. int ret;
  129. cert_data.data = load_file (certfile,
  130. &cert_data.size);
  131. if (NULL == cert_data.data)
  132. return GNUNET_SYSERR;
  133. ret = gnutls_x509_crt_import (crt,
  134. &cert_data,
  135. GNUTLS_X509_FMT_PEM);
  136. if (GNUTLS_E_SUCCESS != ret)
  137. {
  138. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  139. _ ("Unable to import certificate from `%s'\n"),
  140. certfile);
  141. }
  142. GNUNET_free_non_null (cert_data.data);
  143. return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
  144. }
  145. static size_t
  146. copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
  147. {
  148. struct CBC *cbc = ctx;
  149. if (cbc->pos + size * nmemb > sizeof(cbc->buf))
  150. return 0; /* overflow */
  151. GNUNET_memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
  152. cbc->pos += size * nmemb;
  153. return size * nmemb;
  154. }
  155. static int
  156. mhd_ahc (void *cls,
  157. struct MHD_Connection *connection,
  158. const char *url,
  159. const char *method,
  160. const char *version,
  161. const char *upload_data, size_t *upload_data_size,
  162. void **unused)
  163. {
  164. static int ptr;
  165. struct MHD_Response *response;
  166. int ret;
  167. if (0 != strcmp ("GET", method))
  168. return MHD_NO; /* unexpected method */
  169. if (&ptr != *unused)
  170. {
  171. *unused = &ptr;
  172. return MHD_YES;
  173. }
  174. *unused = NULL;
  175. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  176. "MHD sends respose for request to URL `%s'\n", url);
  177. response = MHD_create_response_from_buffer (strlen (url),
  178. (void *) url,
  179. MHD_RESPMEM_MUST_COPY);
  180. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  181. MHD_destroy_response (response);
  182. if (ret == MHD_NO)
  183. {
  184. global_ret = 1;
  185. abort ();
  186. }
  187. global_ret = 0;
  188. return ret;
  189. }
  190. static void
  191. do_shutdown ()
  192. {
  193. if (mhd_task_id != NULL)
  194. {
  195. GNUNET_SCHEDULER_cancel (mhd_task_id);
  196. mhd_task_id = NULL;
  197. }
  198. if (curl_task_id != NULL)
  199. {
  200. GNUNET_SCHEDULER_cancel (curl_task_id);
  201. curl_task_id = NULL;
  202. }
  203. if (NULL != mhd)
  204. {
  205. MHD_stop_daemon (mhd);
  206. mhd = NULL;
  207. }
  208. GNUNET_free_non_null (url);
  209. if (NULL != proxy_proc)
  210. {
  211. (void) GNUNET_OS_process_kill (proxy_proc, SIGKILL);
  212. GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proxy_proc));
  213. GNUNET_OS_process_destroy (proxy_proc);
  214. proxy_proc = NULL;
  215. }
  216. url = NULL;
  217. GNUNET_SCHEDULER_shutdown ();
  218. }
  219. /**
  220. * Function to run the HTTP client.
  221. */
  222. static void
  223. curl_main (void);
  224. static void
  225. curl_task (void *cls)
  226. {
  227. curl_task_id = NULL;
  228. curl_main ();
  229. }
  230. static void
  231. curl_main ()
  232. {
  233. fd_set rs;
  234. fd_set ws;
  235. fd_set es;
  236. int max;
  237. struct GNUNET_NETWORK_FDSet nrs;
  238. struct GNUNET_NETWORK_FDSet nws;
  239. struct GNUNET_TIME_Relative delay;
  240. long timeout;
  241. int running;
  242. struct CURLMsg *msg;
  243. max = 0;
  244. FD_ZERO (&rs);
  245. FD_ZERO (&ws);
  246. FD_ZERO (&es);
  247. curl_multi_perform (multi, &running);
  248. if (running == 0)
  249. {
  250. GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
  251. if (msg->msg == CURLMSG_DONE)
  252. {
  253. if (msg->data.result != CURLE_OK)
  254. {
  255. fprintf (stderr,
  256. "%s failed at %s:%d: `%s'\n",
  257. "curl_multi_perform",
  258. __FILE__,
  259. __LINE__, curl_easy_strerror (msg->data.result));
  260. global_ret = 1;
  261. }
  262. }
  263. curl_multi_remove_handle (multi, curl);
  264. curl_multi_cleanup (multi);
  265. curl_easy_cleanup (curl);
  266. curl = NULL;
  267. multi = NULL;
  268. if (cbc.pos != strlen ("/hello_world"))
  269. {
  270. GNUNET_break (0);
  271. global_ret = 2;
  272. }
  273. if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
  274. {
  275. GNUNET_break (0);
  276. global_ret = 3;
  277. }
  278. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
  279. do_shutdown ();
  280. return;
  281. }
  282. GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
  283. if ((CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
  284. (-1 == timeout))
  285. delay = GNUNET_TIME_UNIT_SECONDS;
  286. else
  287. delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
  288. (unsigned int) timeout);
  289. GNUNET_NETWORK_fdset_copy_native (&nrs,
  290. &rs,
  291. max + 1);
  292. GNUNET_NETWORK_fdset_copy_native (&nws,
  293. &ws,
  294. max + 1);
  295. curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
  296. delay,
  297. &nrs,
  298. &nws,
  299. &curl_task,
  300. NULL);
  301. }
  302. static void
  303. start_curl (void *cls)
  304. {
  305. curl_task_id = NULL;
  306. GNUNET_asprintf (&url,
  307. "https://%s:%d/hello_world",
  308. TEST_DOMAIN, port);
  309. curl = curl_easy_init ();
  310. curl_easy_setopt (curl, CURLOPT_URL, url);
  311. // curl_easy_setopt (curl, CURLOPT_URL, "https://127.0.0.1:8443/hello_world");
  312. curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, &copy_buffer);
  313. curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
  314. curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
  315. curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
  316. curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
  317. curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
  318. curl_easy_setopt (curl, CURLOPT_CAINFO, cafile_opt);
  319. // curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
  320. // curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
  321. curl_easy_setopt (curl, CURLOPT_PROXY, "socks5h://127.0.0.1:7777");
  322. multi = curl_multi_init ();
  323. GNUNET_assert (multi != NULL);
  324. GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
  325. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  326. "Beginning HTTP download from `%s'\n",
  327. url);
  328. curl_main ();
  329. }
  330. /**
  331. * Callback invoked from the namestore service once record is
  332. * created.
  333. *
  334. * @param cls closure
  335. * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
  336. * will match 'result_af' from the request
  337. * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
  338. * that the VPN allocated for the redirection;
  339. * traffic to this IP will now be redirected to the
  340. * specified target peer; NULL on error
  341. */
  342. static void
  343. commence_testing (void *cls)
  344. {
  345. curl_task_id =
  346. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  347. &start_curl,
  348. NULL);
  349. }
  350. /**
  351. * Function to keep the HTTP server running.
  352. */
  353. static void
  354. mhd_main (void);
  355. static void
  356. mhd_task (void *cls)
  357. {
  358. mhd_task_id = NULL;
  359. MHD_run (mhd);
  360. mhd_main ();
  361. }
  362. static void
  363. mhd_main ()
  364. {
  365. struct GNUNET_NETWORK_FDSet nrs;
  366. struct GNUNET_NETWORK_FDSet nws;
  367. fd_set rs;
  368. fd_set ws;
  369. fd_set es;
  370. int max_fd;
  371. unsigned MHD_LONG_LONG timeout;
  372. struct GNUNET_TIME_Relative delay;
  373. GNUNET_assert (NULL == mhd_task_id);
  374. FD_ZERO (&rs);
  375. FD_ZERO (&ws);
  376. FD_ZERO (&es);
  377. max_fd = -1;
  378. GNUNET_assert (MHD_YES ==
  379. MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
  380. if (MHD_YES == MHD_get_timeout (mhd, &timeout))
  381. delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
  382. (unsigned int) timeout);
  383. else
  384. delay = GNUNET_TIME_UNIT_FOREVER_REL;
  385. GNUNET_NETWORK_fdset_copy_native (&nrs,
  386. &rs,
  387. max_fd + 1);
  388. GNUNET_NETWORK_fdset_copy_native (&nws,
  389. &ws,
  390. max_fd + 1);
  391. mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
  392. delay,
  393. &nrs,
  394. &nws,
  395. &mhd_task,
  396. NULL);
  397. }
  398. /**
  399. * Main function that will be run
  400. *
  401. * @param cls closure
  402. * @param args remaining command-line arguments
  403. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  404. * @param c configuration
  405. */
  406. static void
  407. run (void *cls,
  408. char *const *args,
  409. const char *cfgfile,
  410. const struct GNUNET_CONFIGURATION_Handle *c)
  411. {
  412. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  413. "Using `%s' as CA\n",
  414. cafile_srv);
  415. char cert[MAX_PEM_SIZE];
  416. char key[MAX_PEM_SIZE];
  417. size_t key_buf_size;
  418. size_t cert_buf_size;
  419. gnutls_global_init ();
  420. gnutls_x509_crt_init (&proxy_cert);
  421. gnutls_x509_privkey_init (&proxy_key);
  422. if ((GNUNET_OK !=
  423. load_cert_from_file (proxy_cert,
  424. cafile_srv)) ||
  425. (GNUNET_OK !=
  426. load_key_from_file (proxy_key,
  427. cafile_srv)))
  428. {
  429. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  430. _ ("Failed to load X.509 key and certificate from `%s'\n"),
  431. cafile_srv);
  432. gnutls_x509_crt_deinit (proxy_cert);
  433. gnutls_x509_privkey_deinit (proxy_key);
  434. gnutls_global_deinit ();
  435. return;
  436. }
  437. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  438. NULL);
  439. key_buf_size = sizeof(key);
  440. cert_buf_size = sizeof(cert);
  441. gnutls_x509_crt_export (proxy_cert,
  442. GNUTLS_X509_FMT_PEM,
  443. cert,
  444. &cert_buf_size);
  445. gnutls_x509_privkey_export (proxy_key,
  446. GNUTLS_X509_FMT_PEM,
  447. key,
  448. &key_buf_size);
  449. mhd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL
  450. | MHD_ALLOW_SUSPEND_RESUME, port,
  451. NULL, NULL,
  452. &mhd_ahc, NULL,
  453. MHD_OPTION_HTTPS_MEM_KEY, key,
  454. MHD_OPTION_HTTPS_MEM_CERT, cert,
  455. MHD_OPTION_END);
  456. GNUNET_assert (NULL != mhd);
  457. mhd_main ();
  458. GNUNET_SCHEDULER_add_now (&commence_testing,
  459. NULL);
  460. }
  461. int
  462. main (int argc, char *const *argv)
  463. {
  464. struct GNUNET_GETOPT_CommandLineOption options[] = {
  465. GNUNET_GETOPT_option_uint16 ('p',
  466. "port",
  467. NULL,
  468. gettext_noop (
  469. "listen on specified port (default: 7777)"),
  470. &port),
  471. GNUNET_GETOPT_option_string ('A',
  472. "curlcert",
  473. NULL,
  474. gettext_noop ("pem file to use as CA"),
  475. &cafile_opt),
  476. GNUNET_GETOPT_option_string ('S',
  477. "servercert",
  478. NULL,
  479. gettext_noop (
  480. "pem file to use for the server"),
  481. &cafile_srv),
  482. GNUNET_GETOPT_OPTION_END
  483. };
  484. if (0 != curl_global_init (CURL_GLOBAL_WIN32))
  485. {
  486. fprintf (stderr, "failed to initialize curl\n");
  487. return 2;
  488. }
  489. if (GNUNET_OK !=
  490. GNUNET_STRINGS_get_utf8_args (argc, argv,
  491. &argc, &argv))
  492. return 2;
  493. GNUNET_log_setup ("gnunet-gns-proxy-test",
  494. "WARNING",
  495. NULL);
  496. if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv,
  497. "gnunet-gns-proxy-test",
  498. _ ("GNUnet GNS proxy test"),
  499. options,
  500. &run, NULL))
  501. return 1;
  502. GNUNET_free_non_null ((char *) argv);
  503. return global_ret;
  504. }
  505. /* end of test_gns_proxy.c */