http_proxy_fixture.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "test/core/end2end/fixtures/http_proxy_fixture.h"
  19. #include <string.h>
  20. #include "absl/strings/str_cat.h"
  21. #include <grpc/grpc.h>
  22. #include <grpc/slice_buffer.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/atm.h>
  25. #include <grpc/support/log.h>
  26. #include <grpc/support/sync.h>
  27. #include "src/core/lib/address_utils/sockaddr_utils.h"
  28. #include "src/core/lib/channel/channel_args.h"
  29. #include "src/core/lib/gpr/string.h"
  30. #include "src/core/lib/gprpp/host_port.h"
  31. #include "src/core/lib/gprpp/memory.h"
  32. #include "src/core/lib/gprpp/thd.h"
  33. #include "src/core/lib/http/parser.h"
  34. #include "src/core/lib/iomgr/closure.h"
  35. #include "src/core/lib/iomgr/combiner.h"
  36. #include "src/core/lib/iomgr/endpoint.h"
  37. #include "src/core/lib/iomgr/error.h"
  38. #include "src/core/lib/iomgr/exec_ctx.h"
  39. #include "src/core/lib/iomgr/pollset.h"
  40. #include "src/core/lib/iomgr/pollset_set.h"
  41. #include "src/core/lib/iomgr/resolve_address.h"
  42. #include "src/core/lib/iomgr/sockaddr.h"
  43. #include "src/core/lib/iomgr/tcp_client.h"
  44. #include "src/core/lib/iomgr/tcp_server.h"
  45. #include "src/core/lib/iomgr/timer.h"
  46. #include "src/core/lib/resource_quota/api.h"
  47. #include "src/core/lib/slice/b64.h"
  48. #include "src/core/lib/slice/slice_internal.h"
  49. #include "test/core/util/port.h"
  50. struct grpc_end2end_http_proxy {
  51. grpc_end2end_http_proxy()
  52. : server(nullptr), channel_args(nullptr), mu(nullptr), combiner(nullptr) {
  53. gpr_ref_init(&users, 1);
  54. combiner = grpc_combiner_create();
  55. }
  56. std::string proxy_name;
  57. grpc_core::Thread thd;
  58. grpc_tcp_server* server;
  59. const grpc_channel_args* channel_args;
  60. gpr_mu* mu;
  61. std::vector<grpc_pollset*> pollset;
  62. gpr_refcount users;
  63. grpc_core::Combiner* combiner;
  64. };
  65. //
  66. // Connection handling
  67. //
  68. // proxy_connection structure is only accessed in the closures which are all
  69. // scheduled under the same combiner lock. So there is no need for a mutex to
  70. // protect this structure.
  71. typedef struct proxy_connection {
  72. grpc_end2end_http_proxy* proxy;
  73. grpc_endpoint* client_endpoint;
  74. grpc_endpoint* server_endpoint;
  75. gpr_refcount refcount;
  76. grpc_pollset_set* pollset_set;
  77. // NOTE: All the closures execute under proxy->combiner lock. Which means
  78. // there will not be any data-races between the closures
  79. grpc_closure on_read_request_done;
  80. grpc_closure on_server_connect_done;
  81. grpc_closure on_write_response_done;
  82. grpc_closure on_client_read_done;
  83. grpc_closure on_client_write_done;
  84. grpc_closure on_server_read_done;
  85. grpc_closure on_server_write_done;
  86. bool client_read_failed : 1;
  87. bool client_write_failed : 1;
  88. bool client_shutdown : 1;
  89. bool server_read_failed : 1;
  90. bool server_write_failed : 1;
  91. bool server_shutdown : 1;
  92. grpc_slice_buffer client_read_buffer;
  93. grpc_slice_buffer client_deferred_write_buffer;
  94. bool client_is_writing;
  95. grpc_slice_buffer client_write_buffer;
  96. grpc_slice_buffer server_read_buffer;
  97. grpc_slice_buffer server_deferred_write_buffer;
  98. bool server_is_writing;
  99. grpc_slice_buffer server_write_buffer;
  100. grpc_http_parser http_parser;
  101. grpc_http_request http_request;
  102. } proxy_connection;
  103. static void proxy_connection_ref(proxy_connection* conn,
  104. const char* /*reason*/) {
  105. gpr_ref(&conn->refcount);
  106. }
  107. // Helper function to destroy the proxy connection.
  108. static void proxy_connection_unref(proxy_connection* conn,
  109. const char* /*reason*/) {
  110. if (gpr_unref(&conn->refcount)) {
  111. gpr_log(GPR_DEBUG, "endpoints: %p %p", conn->client_endpoint,
  112. conn->server_endpoint);
  113. grpc_endpoint_destroy(conn->client_endpoint);
  114. if (conn->server_endpoint != nullptr) {
  115. grpc_endpoint_destroy(conn->server_endpoint);
  116. }
  117. grpc_pollset_set_destroy(conn->pollset_set);
  118. grpc_slice_buffer_destroy_internal(&conn->client_read_buffer);
  119. grpc_slice_buffer_destroy_internal(&conn->client_deferred_write_buffer);
  120. grpc_slice_buffer_destroy_internal(&conn->client_write_buffer);
  121. grpc_slice_buffer_destroy_internal(&conn->server_read_buffer);
  122. grpc_slice_buffer_destroy_internal(&conn->server_deferred_write_buffer);
  123. grpc_slice_buffer_destroy_internal(&conn->server_write_buffer);
  124. grpc_http_parser_destroy(&conn->http_parser);
  125. grpc_http_request_destroy(&conn->http_request);
  126. gpr_unref(&conn->proxy->users);
  127. gpr_free(conn);
  128. }
  129. }
  130. enum failure_type {
  131. SETUP_FAILED, // To be used before we start proxying.
  132. CLIENT_READ_FAILED,
  133. CLIENT_WRITE_FAILED,
  134. SERVER_READ_FAILED,
  135. SERVER_WRITE_FAILED,
  136. };
  137. // Forward declarations
  138. static void on_client_write_done(void* arg, grpc_error_handle error);
  139. static void on_server_write_done(void* arg, grpc_error_handle error);
  140. static void on_client_read_done(void* arg, grpc_error_handle error);
  141. static void on_server_read_done(void* arg, grpc_error_handle error);
  142. static void on_server_connect_done(void* arg, grpc_error_handle error);
  143. static void on_read_request_done(void* arg, grpc_error_handle error);
  144. static void on_client_write_done_locked(void* arg, grpc_error_handle error);
  145. static void on_server_write_done_locked(void* arg, grpc_error_handle error);
  146. static void on_client_read_done_locked(void* arg, grpc_error_handle error);
  147. static void on_server_read_done_locked(void* arg, grpc_error_handle error);
  148. static void on_server_connect_done_locked(void* arg, grpc_error_handle error);
  149. static void on_read_request_done_locked(void* arg, grpc_error_handle error);
  150. // Helper function to shut down the proxy connection.
  151. static void proxy_connection_failed(proxy_connection* conn,
  152. failure_type failure, const char* prefix,
  153. grpc_error_handle error) {
  154. gpr_log(GPR_INFO, "%s: %s", prefix, grpc_error_std_string(error).c_str());
  155. // Decide whether we should shut down the client and server.
  156. bool shutdown_client = false;
  157. bool shutdown_server = false;
  158. if (failure == SETUP_FAILED) {
  159. shutdown_client = true;
  160. shutdown_server = true;
  161. } else {
  162. if ((failure == CLIENT_READ_FAILED && conn->client_write_failed) ||
  163. (failure == CLIENT_WRITE_FAILED && conn->client_read_failed) ||
  164. (failure == SERVER_READ_FAILED && !conn->client_is_writing)) {
  165. shutdown_client = true;
  166. }
  167. if ((failure == SERVER_READ_FAILED && conn->server_write_failed) ||
  168. (failure == SERVER_WRITE_FAILED && conn->server_read_failed) ||
  169. (failure == CLIENT_READ_FAILED && !conn->server_is_writing)) {
  170. shutdown_server = true;
  171. }
  172. }
  173. // If we decided to shut down either one and have not yet done so, do so.
  174. if (shutdown_client && !conn->client_shutdown) {
  175. grpc_endpoint_shutdown(conn->client_endpoint, GRPC_ERROR_REF(error));
  176. conn->client_shutdown = true;
  177. }
  178. if (shutdown_server && !conn->server_shutdown &&
  179. (conn->server_endpoint != nullptr)) {
  180. grpc_endpoint_shutdown(conn->server_endpoint, GRPC_ERROR_REF(error));
  181. conn->server_shutdown = true;
  182. }
  183. // Unref the connection.
  184. proxy_connection_unref(conn, "conn_failed");
  185. GRPC_ERROR_UNREF(error);
  186. }
  187. // Callback for writing proxy data to the client.
  188. static void on_client_write_done_locked(void* arg, grpc_error_handle error) {
  189. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  190. conn->client_is_writing = false;
  191. if (error != GRPC_ERROR_NONE) {
  192. proxy_connection_failed(conn, CLIENT_WRITE_FAILED,
  193. "HTTP proxy client write", GRPC_ERROR_REF(error));
  194. return;
  195. }
  196. // Clear write buffer (the data we just wrote).
  197. grpc_slice_buffer_reset_and_unref(&conn->client_write_buffer);
  198. // If more data was read from the server since we started this write,
  199. // write that data now.
  200. if (conn->client_deferred_write_buffer.length > 0) {
  201. grpc_slice_buffer_move_into(&conn->client_deferred_write_buffer,
  202. &conn->client_write_buffer);
  203. conn->client_is_writing = true;
  204. GRPC_CLOSURE_INIT(&conn->on_client_write_done, on_client_write_done, conn,
  205. grpc_schedule_on_exec_ctx);
  206. grpc_endpoint_write(conn->client_endpoint, &conn->client_write_buffer,
  207. &conn->on_client_write_done, nullptr);
  208. } else {
  209. // No more writes. Unref the connection.
  210. proxy_connection_unref(conn, "write_done");
  211. }
  212. }
  213. static void on_client_write_done(void* arg, grpc_error_handle error) {
  214. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  215. GRPC_CLOSURE_INIT(&conn->on_client_write_done, on_client_write_done_locked,
  216. conn, nullptr);
  217. conn->proxy->combiner->Run(&conn->on_client_write_done,
  218. GRPC_ERROR_REF(error));
  219. }
  220. // Callback for writing proxy data to the backend server.
  221. static void on_server_write_done_locked(void* arg, grpc_error_handle error) {
  222. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  223. conn->server_is_writing = false;
  224. if (error != GRPC_ERROR_NONE) {
  225. proxy_connection_failed(conn, SERVER_WRITE_FAILED,
  226. "HTTP proxy server write", GRPC_ERROR_REF(error));
  227. return;
  228. }
  229. // Clear write buffer (the data we just wrote).
  230. grpc_slice_buffer_reset_and_unref(&conn->server_write_buffer);
  231. // If more data was read from the client since we started this write,
  232. // write that data now.
  233. if (conn->server_deferred_write_buffer.length > 0) {
  234. grpc_slice_buffer_move_into(&conn->server_deferred_write_buffer,
  235. &conn->server_write_buffer);
  236. conn->server_is_writing = true;
  237. GRPC_CLOSURE_INIT(&conn->on_server_write_done, on_server_write_done, conn,
  238. grpc_schedule_on_exec_ctx);
  239. grpc_endpoint_write(conn->server_endpoint, &conn->server_write_buffer,
  240. &conn->on_server_write_done, nullptr);
  241. } else {
  242. // No more writes. Unref the connection.
  243. proxy_connection_unref(conn, "server_write");
  244. }
  245. }
  246. static void on_server_write_done(void* arg, grpc_error_handle error) {
  247. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  248. GRPC_CLOSURE_INIT(&conn->on_server_write_done, on_server_write_done_locked,
  249. conn, nullptr);
  250. conn->proxy->combiner->Run(&conn->on_server_write_done,
  251. GRPC_ERROR_REF(error));
  252. }
  253. // Callback for reading data from the client, which will be proxied to
  254. // the backend server.
  255. static void on_client_read_done_locked(void* arg, grpc_error_handle error) {
  256. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  257. if (error != GRPC_ERROR_NONE) {
  258. proxy_connection_failed(conn, CLIENT_READ_FAILED, "HTTP proxy client read",
  259. GRPC_ERROR_REF(error));
  260. return;
  261. }
  262. // If there is already a pending write (i.e., server_write_buffer is
  263. // not empty), then move the read data into server_deferred_write_buffer,
  264. // and the next write will be requested in on_server_write_done(), when
  265. // the current write is finished.
  266. //
  267. // Otherwise, move the read data into the write buffer and write it.
  268. if (conn->server_is_writing) {
  269. grpc_slice_buffer_move_into(&conn->client_read_buffer,
  270. &conn->server_deferred_write_buffer);
  271. } else {
  272. grpc_slice_buffer_move_into(&conn->client_read_buffer,
  273. &conn->server_write_buffer);
  274. proxy_connection_ref(conn, "client_read");
  275. conn->server_is_writing = true;
  276. GRPC_CLOSURE_INIT(&conn->on_server_write_done, on_server_write_done, conn,
  277. grpc_schedule_on_exec_ctx);
  278. grpc_endpoint_write(conn->server_endpoint, &conn->server_write_buffer,
  279. &conn->on_server_write_done, nullptr);
  280. }
  281. // Read more data.
  282. GRPC_CLOSURE_INIT(&conn->on_client_read_done, on_client_read_done, conn,
  283. grpc_schedule_on_exec_ctx);
  284. grpc_endpoint_read(conn->client_endpoint, &conn->client_read_buffer,
  285. &conn->on_client_read_done, /*urgent=*/false);
  286. }
  287. static void on_client_read_done(void* arg, grpc_error_handle error) {
  288. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  289. GRPC_CLOSURE_INIT(&conn->on_client_read_done, on_client_read_done_locked,
  290. conn, nullptr);
  291. conn->proxy->combiner->Run(&conn->on_client_read_done, GRPC_ERROR_REF(error));
  292. }
  293. // Callback for reading data from the backend server, which will be
  294. // proxied to the client.
  295. static void on_server_read_done_locked(void* arg, grpc_error_handle error) {
  296. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  297. if (error != GRPC_ERROR_NONE) {
  298. proxy_connection_failed(conn, SERVER_READ_FAILED, "HTTP proxy server read",
  299. GRPC_ERROR_REF(error));
  300. return;
  301. }
  302. // If there is already a pending write (i.e., client_write_buffer is
  303. // not empty), then move the read data into client_deferred_write_buffer,
  304. // and the next write will be requested in on_client_write_done(), when
  305. // the current write is finished.
  306. //
  307. // Otherwise, move the read data into the write buffer and write it.
  308. if (conn->client_is_writing) {
  309. grpc_slice_buffer_move_into(&conn->server_read_buffer,
  310. &conn->client_deferred_write_buffer);
  311. } else {
  312. grpc_slice_buffer_move_into(&conn->server_read_buffer,
  313. &conn->client_write_buffer);
  314. proxy_connection_ref(conn, "server_read");
  315. conn->client_is_writing = true;
  316. GRPC_CLOSURE_INIT(&conn->on_client_write_done, on_client_write_done, conn,
  317. grpc_schedule_on_exec_ctx);
  318. grpc_endpoint_write(conn->client_endpoint, &conn->client_write_buffer,
  319. &conn->on_client_write_done, nullptr);
  320. }
  321. // Read more data.
  322. GRPC_CLOSURE_INIT(&conn->on_server_read_done, on_server_read_done, conn,
  323. grpc_schedule_on_exec_ctx);
  324. grpc_endpoint_read(conn->server_endpoint, &conn->server_read_buffer,
  325. &conn->on_server_read_done, /*urgent=*/false);
  326. }
  327. static void on_server_read_done(void* arg, grpc_error_handle error) {
  328. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  329. GRPC_CLOSURE_INIT(&conn->on_server_read_done, on_server_read_done_locked,
  330. conn, nullptr);
  331. conn->proxy->combiner->Run(&conn->on_server_read_done, GRPC_ERROR_REF(error));
  332. }
  333. // Callback to write the HTTP response for the CONNECT request.
  334. static void on_write_response_done_locked(void* arg, grpc_error_handle error) {
  335. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  336. conn->client_is_writing = false;
  337. if (error != GRPC_ERROR_NONE) {
  338. proxy_connection_failed(conn, SETUP_FAILED, "HTTP proxy write response",
  339. GRPC_ERROR_REF(error));
  340. return;
  341. }
  342. // Clear write buffer.
  343. grpc_slice_buffer_reset_and_unref(&conn->client_write_buffer);
  344. // Start reading from both client and server. One of the read
  345. // requests inherits our ref to conn, but we need to take a new ref
  346. // for the other one.
  347. proxy_connection_ref(conn, "client_read");
  348. proxy_connection_ref(conn, "server_read");
  349. proxy_connection_unref(conn, "write_response");
  350. GRPC_CLOSURE_INIT(&conn->on_client_read_done, on_client_read_done, conn,
  351. grpc_schedule_on_exec_ctx);
  352. grpc_endpoint_read(conn->client_endpoint, &conn->client_read_buffer,
  353. &conn->on_client_read_done, /*urgent=*/false);
  354. GRPC_CLOSURE_INIT(&conn->on_server_read_done, on_server_read_done, conn,
  355. grpc_schedule_on_exec_ctx);
  356. grpc_endpoint_read(conn->server_endpoint, &conn->server_read_buffer,
  357. &conn->on_server_read_done, /*urgent=*/false);
  358. }
  359. static void on_write_response_done(void* arg, grpc_error_handle error) {
  360. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  361. GRPC_CLOSURE_INIT(&conn->on_write_response_done,
  362. on_write_response_done_locked, conn, nullptr);
  363. conn->proxy->combiner->Run(&conn->on_write_response_done,
  364. GRPC_ERROR_REF(error));
  365. }
  366. // Callback to connect to the backend server specified by the HTTP
  367. // CONNECT request.
  368. static void on_server_connect_done_locked(void* arg, grpc_error_handle error) {
  369. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  370. if (error != GRPC_ERROR_NONE) {
  371. // TODO(roth): Technically, in this case, we should handle the error
  372. // by returning an HTTP response to the client indicating that the
  373. // connection failed. However, for the purposes of this test code,
  374. // it's fine to pretend this is a client-side error, which will
  375. // cause the client connection to be dropped.
  376. proxy_connection_failed(conn, SETUP_FAILED, "HTTP proxy server connect",
  377. GRPC_ERROR_REF(error));
  378. return;
  379. }
  380. // We've established a connection, so send back a 200 response code to
  381. // the client.
  382. // The write callback inherits our reference to conn.
  383. grpc_slice slice =
  384. grpc_slice_from_copied_string("HTTP/1.0 200 connected\r\n\r\n");
  385. grpc_slice_buffer_add(&conn->client_write_buffer, slice);
  386. conn->client_is_writing = true;
  387. GRPC_CLOSURE_INIT(&conn->on_write_response_done, on_write_response_done, conn,
  388. grpc_schedule_on_exec_ctx);
  389. grpc_endpoint_write(conn->client_endpoint, &conn->client_write_buffer,
  390. &conn->on_write_response_done, nullptr);
  391. }
  392. static void on_server_connect_done(void* arg, grpc_error_handle error) {
  393. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  394. GRPC_CLOSURE_INIT(&conn->on_server_connect_done,
  395. on_server_connect_done_locked, conn, nullptr);
  396. conn->proxy->combiner->Run(&conn->on_server_connect_done,
  397. GRPC_ERROR_REF(error));
  398. }
  399. /**
  400. * Parses the proxy auth header value to check if it matches :-
  401. * Basic <base64_encoded_expected_cred>
  402. * Returns true if it matches, false otherwise
  403. */
  404. static bool proxy_auth_header_matches(char* proxy_auth_header_val,
  405. char* expected_cred) {
  406. GPR_ASSERT(proxy_auth_header_val != nullptr);
  407. GPR_ASSERT(expected_cred != nullptr);
  408. if (strncmp(proxy_auth_header_val, "Basic ", 6) != 0) {
  409. return false;
  410. }
  411. proxy_auth_header_val += 6;
  412. grpc_slice decoded_slice = grpc_base64_decode(proxy_auth_header_val, 0);
  413. const bool header_matches =
  414. grpc_slice_str_cmp(decoded_slice, expected_cred) == 0;
  415. grpc_slice_unref_internal(decoded_slice);
  416. return header_matches;
  417. }
  418. // Callback to read the HTTP CONNECT request.
  419. // TODO(roth): Technically, for any of the failure modes handled by this
  420. // function, we should handle the error by returning an HTTP response to
  421. // the client indicating that the request failed. However, for the purposes
  422. // of this test code, it's fine to pretend this is a client-side error,
  423. // which will cause the client connection to be dropped.
  424. static void on_read_request_done_locked(void* arg, grpc_error_handle error) {
  425. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  426. gpr_log(GPR_DEBUG, "on_read_request_done: %p %s", conn,
  427. grpc_error_std_string(error).c_str());
  428. if (error != GRPC_ERROR_NONE) {
  429. proxy_connection_failed(conn, SETUP_FAILED, "HTTP proxy read request",
  430. GRPC_ERROR_REF(error));
  431. return;
  432. }
  433. // Read request and feed it to the parser.
  434. for (size_t i = 0; i < conn->client_read_buffer.count; ++i) {
  435. if (GRPC_SLICE_LENGTH(conn->client_read_buffer.slices[i]) > 0) {
  436. error = grpc_http_parser_parse(
  437. &conn->http_parser, conn->client_read_buffer.slices[i], nullptr);
  438. if (error != GRPC_ERROR_NONE) {
  439. proxy_connection_failed(conn, SETUP_FAILED, "HTTP proxy request parse",
  440. GRPC_ERROR_REF(error));
  441. GRPC_ERROR_UNREF(error);
  442. return;
  443. }
  444. }
  445. }
  446. grpc_slice_buffer_reset_and_unref(&conn->client_read_buffer);
  447. // If we're not done reading the request, read more data.
  448. if (conn->http_parser.state != GRPC_HTTP_BODY) {
  449. GRPC_CLOSURE_INIT(&conn->on_read_request_done, on_read_request_done, conn,
  450. grpc_schedule_on_exec_ctx);
  451. grpc_endpoint_read(conn->client_endpoint, &conn->client_read_buffer,
  452. &conn->on_read_request_done, /*urgent=*/false);
  453. return;
  454. }
  455. // Make sure we got a CONNECT request.
  456. if (strcmp(conn->http_request.method, "CONNECT") != 0) {
  457. error = GRPC_ERROR_CREATE_FROM_CPP_STRING(absl::StrCat(
  458. "HTTP proxy got request method ", conn->http_request.method));
  459. proxy_connection_failed(conn, SETUP_FAILED, "HTTP proxy read request",
  460. GRPC_ERROR_REF(error));
  461. GRPC_ERROR_UNREF(error);
  462. return;
  463. }
  464. // If proxy auth is being used, check if the header is present and as expected
  465. const grpc_arg* proxy_auth_arg = grpc_channel_args_find(
  466. conn->proxy->channel_args, GRPC_ARG_HTTP_PROXY_AUTH_CREDS);
  467. char* proxy_auth_str = grpc_channel_arg_get_string(proxy_auth_arg);
  468. if (proxy_auth_str != nullptr) {
  469. bool client_authenticated = false;
  470. for (size_t i = 0; i < conn->http_request.hdr_count; i++) {
  471. if (strcmp(conn->http_request.hdrs[i].key, "Proxy-Authorization") == 0) {
  472. client_authenticated = proxy_auth_header_matches(
  473. conn->http_request.hdrs[i].value, proxy_auth_str);
  474. break;
  475. }
  476. }
  477. if (!client_authenticated) {
  478. const char* msg = "HTTP Connect could not verify authentication";
  479. error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(msg);
  480. proxy_connection_failed(conn, SETUP_FAILED, "HTTP proxy read request",
  481. GRPC_ERROR_REF(error));
  482. GRPC_ERROR_UNREF(error);
  483. return;
  484. }
  485. }
  486. // Resolve address.
  487. absl::StatusOr<std::vector<grpc_resolved_address>> addresses_or =
  488. grpc_core::GetDNSResolver()->ResolveNameBlocking(conn->http_request.path,
  489. "80");
  490. if (!addresses_or.ok()) {
  491. proxy_connection_failed(conn, SETUP_FAILED, "HTTP proxy DNS lookup",
  492. GRPC_ERROR_REF(error));
  493. return;
  494. }
  495. GPR_ASSERT(!addresses_or->empty());
  496. // Connect to requested address.
  497. // The connection callback inherits our reference to conn.
  498. const grpc_core::Timestamp deadline =
  499. grpc_core::ExecCtx::Get()->Now() + grpc_core::Duration::Seconds(10);
  500. GRPC_CLOSURE_INIT(&conn->on_server_connect_done, on_server_connect_done, conn,
  501. grpc_schedule_on_exec_ctx);
  502. const grpc_channel_args* args = grpc_core::CoreConfiguration::Get()
  503. .channel_args_preconditioning()
  504. .PreconditionChannelArgs(nullptr);
  505. grpc_tcp_client_connect(&conn->on_server_connect_done, &conn->server_endpoint,
  506. conn->pollset_set, args, &(*addresses_or)[0],
  507. deadline);
  508. grpc_channel_args_destroy(args);
  509. }
  510. static void on_read_request_done(void* arg, grpc_error_handle error) {
  511. proxy_connection* conn = static_cast<proxy_connection*>(arg);
  512. GRPC_CLOSURE_INIT(&conn->on_read_request_done, on_read_request_done_locked,
  513. conn, nullptr);
  514. conn->proxy->combiner->Run(&conn->on_read_request_done,
  515. GRPC_ERROR_REF(error));
  516. }
  517. static void on_accept(void* arg, grpc_endpoint* endpoint,
  518. grpc_pollset* /*accepting_pollset*/,
  519. grpc_tcp_server_acceptor* acceptor) {
  520. gpr_free(acceptor);
  521. grpc_end2end_http_proxy* proxy = static_cast<grpc_end2end_http_proxy*>(arg);
  522. // Instantiate proxy_connection.
  523. proxy_connection* conn = grpc_core::Zalloc<proxy_connection>();
  524. gpr_ref(&proxy->users);
  525. conn->client_endpoint = endpoint;
  526. conn->proxy = proxy;
  527. gpr_ref_init(&conn->refcount, 1);
  528. conn->pollset_set = grpc_pollset_set_create();
  529. grpc_pollset_set_add_pollset(conn->pollset_set, proxy->pollset[0]);
  530. grpc_endpoint_add_to_pollset_set(endpoint, conn->pollset_set);
  531. grpc_slice_buffer_init(&conn->client_read_buffer);
  532. grpc_slice_buffer_init(&conn->client_deferred_write_buffer);
  533. conn->client_is_writing = false;
  534. grpc_slice_buffer_init(&conn->client_write_buffer);
  535. grpc_slice_buffer_init(&conn->server_read_buffer);
  536. grpc_slice_buffer_init(&conn->server_deferred_write_buffer);
  537. conn->server_is_writing = false;
  538. grpc_slice_buffer_init(&conn->server_write_buffer);
  539. grpc_http_parser_init(&conn->http_parser, GRPC_HTTP_REQUEST,
  540. &conn->http_request);
  541. GRPC_CLOSURE_INIT(&conn->on_read_request_done, on_read_request_done, conn,
  542. grpc_schedule_on_exec_ctx);
  543. grpc_endpoint_read(conn->client_endpoint, &conn->client_read_buffer,
  544. &conn->on_read_request_done, /*urgent=*/false);
  545. }
  546. //
  547. // Proxy class
  548. //
  549. static void thread_main(void* arg) {
  550. grpc_end2end_http_proxy* proxy = static_cast<grpc_end2end_http_proxy*>(arg);
  551. grpc_core::ExecCtx exec_ctx;
  552. do {
  553. gpr_ref(&proxy->users);
  554. grpc_pollset_worker* worker = nullptr;
  555. gpr_mu_lock(proxy->mu);
  556. GRPC_LOG_IF_ERROR("grpc_pollset_work",
  557. grpc_pollset_work(proxy->pollset[0], &worker,
  558. grpc_core::ExecCtx::Get()->Now() +
  559. grpc_core::Duration::Seconds(1)));
  560. gpr_mu_unlock(proxy->mu);
  561. grpc_core::ExecCtx::Get()->Flush();
  562. } while (!gpr_unref(&proxy->users));
  563. }
  564. grpc_end2end_http_proxy* grpc_end2end_http_proxy_create(
  565. const grpc_channel_args* args) {
  566. grpc_core::ExecCtx exec_ctx;
  567. grpc_end2end_http_proxy* proxy = new grpc_end2end_http_proxy();
  568. // Construct proxy address.
  569. const int proxy_port = grpc_pick_unused_port_or_die();
  570. proxy->proxy_name = grpc_core::JoinHostPort("localhost", proxy_port);
  571. gpr_log(GPR_INFO, "Proxy address: %s", proxy->proxy_name.c_str());
  572. // Create TCP server.
  573. proxy->channel_args = grpc_core::CoreConfiguration::Get()
  574. .channel_args_preconditioning()
  575. .PreconditionChannelArgs(args);
  576. grpc_error_handle error =
  577. grpc_tcp_server_create(nullptr, proxy->channel_args, &proxy->server);
  578. GPR_ASSERT(error == GRPC_ERROR_NONE);
  579. // Bind to port.
  580. grpc_resolved_address resolved_addr;
  581. grpc_sockaddr_in* addr =
  582. reinterpret_cast<grpc_sockaddr_in*>(resolved_addr.addr);
  583. memset(&resolved_addr, 0, sizeof(resolved_addr));
  584. addr->sin_family = GRPC_AF_INET;
  585. grpc_sockaddr_set_port(&resolved_addr, proxy_port);
  586. int port;
  587. error = grpc_tcp_server_add_port(proxy->server, &resolved_addr, &port);
  588. GPR_ASSERT(error == GRPC_ERROR_NONE);
  589. GPR_ASSERT(port == proxy_port);
  590. // Start server.
  591. auto* pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
  592. grpc_pollset_init(pollset, &proxy->mu);
  593. proxy->pollset.push_back(pollset);
  594. grpc_tcp_server_start(proxy->server, &proxy->pollset, on_accept, proxy);
  595. // Start proxy thread.
  596. proxy->thd = grpc_core::Thread("grpc_http_proxy", thread_main, proxy);
  597. proxy->thd.Start();
  598. return proxy;
  599. }
  600. static void destroy_pollset(void* arg, grpc_error_handle /*error*/) {
  601. grpc_pollset* pollset = static_cast<grpc_pollset*>(arg);
  602. grpc_pollset_destroy(pollset);
  603. gpr_free(pollset);
  604. }
  605. void grpc_end2end_http_proxy_destroy(grpc_end2end_http_proxy* proxy) {
  606. gpr_unref(&proxy->users); // Signal proxy thread to shutdown.
  607. grpc_core::ExecCtx exec_ctx;
  608. proxy->thd.Join();
  609. grpc_tcp_server_shutdown_listeners(proxy->server);
  610. grpc_tcp_server_unref(proxy->server);
  611. grpc_channel_args_destroy(proxy->channel_args);
  612. grpc_pollset_shutdown(proxy->pollset[0],
  613. GRPC_CLOSURE_CREATE(destroy_pollset, proxy->pollset[0],
  614. grpc_schedule_on_exec_ctx));
  615. GRPC_COMBINER_UNREF(proxy->combiner, "test");
  616. delete proxy;
  617. }
  618. const char* grpc_end2end_http_proxy_get_proxy_name(
  619. grpc_end2end_http_proxy* proxy) {
  620. return proxy->proxy_name.c_str();
  621. }