Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 19 | #include "src/core/ext/filters/client_channel/http_proxy.h" |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 20 | |
| 21 | #include <stdbool.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include <grpc/support/alloc.h> |
| 25 | #include <grpc/support/log.h> |
| 26 | #include <grpc/support/string_util.h> |
| 27 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 28 | #include "src/core/ext/filters/client_channel/http_connect_handshaker.h" |
| 29 | #include "src/core/ext/filters/client_channel/proxy_mapper_registry.h" |
| 30 | #include "src/core/ext/filters/client_channel/uri_parser.h" |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 31 | #include "src/core/lib/channel/channel_args.h" |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 32 | #include "src/core/lib/support/env.h" |
Yash Tibrewal | f7350ea | 2017-07-19 10:26:41 -0700 | [diff] [blame^] | 33 | #include "src/core/lib/support/string.h" |
| 34 | #include "src/core/lib/slice/b64.h" |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 35 | |
Yash Tibrewal | f7350ea | 2017-07-19 10:26:41 -0700 | [diff] [blame^] | 36 | static void grpc_get_http_proxy_server(grpc_exec_ctx* exec_ctx, |
| 37 | char **name_to_resolve, |
| 38 | char **user_cred) { |
| 39 | *name_to_resolve = NULL; |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 40 | char* uri_str = gpr_getenv("http_proxy"); |
Yash Tibrewal | f7350ea | 2017-07-19 10:26:41 -0700 | [diff] [blame^] | 41 | if (uri_str == NULL) return; |
David Garcia Quintas | dcb71e0 | 2017-03-06 10:16:26 -0800 | [diff] [blame] | 42 | grpc_uri* uri = |
| 43 | grpc_uri_parse(exec_ctx, uri_str, false /* suppress_errors */); |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 44 | if (uri == NULL || uri->authority == NULL) { |
| 45 | gpr_log(GPR_ERROR, "cannot parse value of 'http_proxy' env var"); |
| 46 | goto done; |
| 47 | } |
| 48 | if (strcmp(uri->scheme, "http") != 0) { |
| 49 | gpr_log(GPR_ERROR, "'%s' scheme not supported in proxy URI", uri->scheme); |
| 50 | goto done; |
| 51 | } |
Yash Tibrewal | f7350ea | 2017-07-19 10:26:41 -0700 | [diff] [blame^] | 52 | char *user_cred_end = strchr(uri->authority, '@'); |
| 53 | if (user_cred_end != NULL) { |
| 54 | *name_to_resolve = gpr_strdup(user_cred_end + 1); |
| 55 | *user_cred_end = '\0'; |
| 56 | *user_cred = gpr_strdup(uri->authority); |
| 57 | gpr_log(GPR_INFO, "userinfo found in proxy URI"); |
| 58 | } else { |
| 59 | *name_to_resolve = gpr_strdup(uri->authority); |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 60 | } |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 61 | done: |
| 62 | gpr_free(uri_str); |
| 63 | grpc_uri_destroy(uri); |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 64 | } |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 65 | |
| 66 | static bool proxy_mapper_map_name(grpc_exec_ctx* exec_ctx, |
| 67 | grpc_proxy_mapper* mapper, |
| 68 | const char* server_uri, |
| 69 | const grpc_channel_args* args, |
| 70 | char** name_to_resolve, |
| 71 | grpc_channel_args** new_args) { |
Yash Tibrewal | f7350ea | 2017-07-19 10:26:41 -0700 | [diff] [blame^] | 72 | char *user_cred = NULL; |
| 73 | grpc_get_http_proxy_server(exec_ctx, name_to_resolve, &user_cred); |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 74 | if (*name_to_resolve == NULL) return false; |
David Garcia Quintas | dcb71e0 | 2017-03-06 10:16:26 -0800 | [diff] [blame] | 75 | grpc_uri* uri = |
| 76 | grpc_uri_parse(exec_ctx, server_uri, false /* suppress_errors */); |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 77 | if (uri == NULL || uri->path[0] == '\0') { |
| 78 | gpr_log(GPR_ERROR, |
| 79 | "'http_proxy' environment variable set, but cannot " |
| 80 | "parse server URI '%s' -- not using proxy", |
| 81 | server_uri); |
Yash Tibrewal | f7350ea | 2017-07-19 10:26:41 -0700 | [diff] [blame^] | 82 | if (uri != NULL) { |
| 83 | gpr_free(user_cred); |
| 84 | grpc_uri_destroy(uri); |
| 85 | } |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 86 | return false; |
| 87 | } |
Mark D. Roth | 3878859 | 2017-02-09 09:28:53 -0800 | [diff] [blame] | 88 | if (strcmp(uri->scheme, "unix") == 0) { |
| 89 | gpr_log(GPR_INFO, "not using proxy for Unix domain socket '%s'", |
| 90 | server_uri); |
Yash Tibrewal | f7350ea | 2017-07-19 10:26:41 -0700 | [diff] [blame^] | 91 | gpr_free(user_cred); |
Mark D. Roth | 3878859 | 2017-02-09 09:28:53 -0800 | [diff] [blame] | 92 | grpc_uri_destroy(uri); |
| 93 | return false; |
| 94 | } |
Yash Tibrewal | f7350ea | 2017-07-19 10:26:41 -0700 | [diff] [blame^] | 95 | |
| 96 | grpc_arg args_to_add[2]; |
| 97 | args_to_add[0] = grpc_channel_arg_string_create( |
Mark D. Roth | 8d5e60b | 2017-06-09 09:08:23 -0700 | [diff] [blame] | 98 | GRPC_ARG_HTTP_CONNECT_SERVER, |
| 99 | uri->path[0] == '/' ? uri->path + 1 : uri->path); |
Yash Tibrewal | f7350ea | 2017-07-19 10:26:41 -0700 | [diff] [blame^] | 100 | |
| 101 | if(user_cred != NULL) { |
| 102 | /* Use base64 encoding for user credentials */ |
| 103 | char *encoded_user_cred = |
| 104 | grpc_base64_encode(user_auth, strlen(user_cred), 0, 0); |
| 105 | char *header; |
| 106 | gpr_asprintf(&header, "Proxy-Authorization:Basic %s", encoded_user_cred); |
| 107 | gpr_free(encoded_user_cred); |
| 108 | args_to_add[1] = grpc_channel_arg_string_create( |
| 109 | GRPC_ARG_HTTP_CONNECT_HEADERS, header); |
| 110 | *new_args = grpc_channel_args_copy_and_add(args, args_to_add, 2); |
| 111 | gpr_free(header); |
| 112 | } else { |
| 113 | *new_args = grpc_channel_args_copy_and_add(args, args_to_add, 1); |
| 114 | } |
| 115 | gpr_free(user_cred); |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 116 | grpc_uri_destroy(uri); |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | static bool proxy_mapper_map_address(grpc_exec_ctx* exec_ctx, |
| 121 | grpc_proxy_mapper* mapper, |
| 122 | const grpc_resolved_address* address, |
| 123 | const grpc_channel_args* args, |
| 124 | grpc_resolved_address** new_address, |
| 125 | grpc_channel_args** new_args) { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | static void proxy_mapper_destroy(grpc_proxy_mapper* mapper) {} |
| 130 | |
| 131 | static const grpc_proxy_mapper_vtable proxy_mapper_vtable = { |
| 132 | proxy_mapper_map_name, proxy_mapper_map_address, proxy_mapper_destroy}; |
| 133 | |
| 134 | static grpc_proxy_mapper proxy_mapper = {&proxy_mapper_vtable}; |
| 135 | |
| 136 | void grpc_register_http_proxy_mapper() { |
| 137 | grpc_proxy_mapper_register(true /* at_start */, &proxy_mapper); |
| 138 | } |