Skip to content

Commit 1a1ea94

Browse files
committed
Add ip trigger
1 parent 9fbd7aa commit 1a1ea94

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

nginx-test.conf.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ http {
3636
redirectionio_set_header X-Host-Ter $host;
3737
redirectionio_set_header X-Host-Ter-2 $host;
3838
redirectionio_set_header X-Host-Ter-3 YOLOLOLO;
39+
redirectionio_trusted_proxies "127.0.0.1,172.18.0.0/24";
3940

4041
location /noredirection {
4142
redirectionio off;
@@ -62,6 +63,7 @@ http {
6263
redirectionio_set_header X-Host-Bis $host;
6364
redirectionio_set_header X-Host-Bis-2 $host;
6465
redirectionio_set_header X-Host-Bis-3 YOLOLOLO;
66+
redirectionio_trusted_proxies "127.0.0.1,172.18.0.0/24";
6567

6668
location / {
6769
proxy_set_header Host $host;

src/ngx_http_redirectionio_module.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static void *ngx_http_redirectionio_create_conf(ngx_conf_t *cf);
1313
static char *ngx_http_redirectionio_merge_conf(ngx_conf_t *cf, void *parent, void *child);
1414
static char *ngx_http_redirectionio_set_url(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
1515
static char *ngx_http_redirectionio_set_header(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
16+
static char *ngx_http_redirectionio_set_trusted_proxies(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
1617

1718
static ngx_int_t ngx_http_redirectionio_postconfiguration(ngx_conf_t *cf);
1819

@@ -95,6 +96,14 @@ static ngx_command_t ngx_http_redirectionio_commands[] = {
9596
offsetof(ngx_http_redirectionio_conf_t, headers_set),
9697
NULL
9798
},
99+
{
100+
ngx_string("redirectionio_trusted_proxies"),
101+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
102+
ngx_http_redirectionio_set_trusted_proxies,
103+
NGX_HTTP_LOC_CONF_OFFSET,
104+
offsetof(ngx_http_redirectionio_conf_t, headers_set),
105+
NULL
106+
},
98107
ngx_null_command /* command termination */
99108
};
100109

@@ -448,6 +457,10 @@ static char *ngx_http_redirectionio_merge_conf(ngx_conf_t *cf, void *parent, voi
448457
conf->host = prev->host;
449458
}
450459

460+
if (conf->trusted_proxies == NULL) {
461+
conf->trusted_proxies = prev->trusted_proxies;
462+
}
463+
451464
phs = prev->headers_set.elts;
452465

453466
for (i = 0; i < prev->headers_set.nelts ; i++) {
@@ -647,6 +660,19 @@ static char *ngx_http_redirectionio_set_header(ngx_conf_t *cf, ngx_command_t *cm
647660
return NGX_CONF_OK;
648661
}
649662

663+
static char *ngx_http_redirectionio_set_trusted_proxies(ngx_conf_t *cf, ngx_command_t *cmd, void *c) {
664+
ngx_http_redirectionio_conf_t *conf = c;
665+
char *trusted_proxies_str;
666+
ngx_str_t *value;
667+
668+
value = cf->args->elts;
669+
trusted_proxies_str = ngx_http_redirectionio_str_to_char(&value[1], cf->pool);
670+
671+
conf->trusted_proxies = (struct REDIRECTIONIO_TrustedProxies *) redirectionio_trusted_proxies_create((const char *)trusted_proxies_str);
672+
673+
return NGX_CONF_OK;
674+
}
675+
650676
static ngx_int_t ngx_http_redirectionio_write_match_action(ngx_event_t *wev) {
651677
ngx_http_redirectionio_ctx_t *ctx;
652678
ngx_connection_t *c;
@@ -746,3 +772,13 @@ static void ngx_http_redirectionio_context_cleanup(void *context) {
746772
ctx->body_filter = NULL;
747773
}
748774
}
775+
776+
char* ngx_http_redirectionio_str_to_char(ngx_str_t *src, ngx_pool_t *pool) {
777+
char *str;
778+
779+
str = (char *)ngx_pcalloc(pool, src->len + 1);
780+
ngx_memcpy(str, src->data, src->len);
781+
*((char *)str + src->len) = '\0';
782+
783+
return str;
784+
}

src/ngx_http_redirectionio_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ typedef struct {
6060
ngx_http_redirectionio_server_t server;
6161
ngx_array_t headers_set;
6262
ngx_reslist_t *connection_pool;
63+
struct REDIRECTIONIO_TrustedProxies *trusted_proxies;
6364
} ngx_http_redirectionio_conf_t;
6465

6566
typedef void (*ngx_http_redirectionio_read_handler_t)(ngx_event_t *rev, const char *json);
@@ -125,5 +126,6 @@ void ngx_http_redirectionio_protocol_free_log(ngx_http_redirectionio_log_t *log)
125126
void ngx_http_redirectionio_protocol_send_filter_header(ngx_connection_t *c, ngx_http_request_t *r, ngx_str_t *project_key, ngx_str_t *rule_id);
126127
ngx_uint_t ngx_http_redirectionio_protocol_send_filter_body(ngx_connection_t *c, ngx_chain_t *in, ngx_str_t *project_key, ngx_str_t *rule_id, ngx_uint_t is_first);
127128

129+
char* ngx_http_redirectionio_str_to_char(ngx_str_t *src, ngx_pool_t *pool);
128130

129131
#endif

src/ngx_http_redirectionio_protocol.c

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
static void ngx_str_copy(ngx_str_t *src, ngx_str_t *dest);
44

5-
static char* ngx_str_to_char(ngx_str_t *src, ngx_pool_t *pool);
6-
75
static ngx_int_t ngx_http_redirectionio_send_uint8(ngx_connection_t *c, uint8_t uint8);
86

97
static ngx_int_t ngx_http_redirectionio_send_uint16(ngx_connection_t *c, uint16_t uint16);
@@ -20,7 +18,7 @@ ngx_int_t ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_ht
2018
ngx_list_part_t *part;
2119
struct REDIRECTIONIO_HeaderMap *first_header = NULL, *current_header = NULL;
2220
const char *request_serialized;
23-
char *method, *uri, *host = NULL, *scheme = NULL;
21+
char *method, *uri, *host = NULL, *scheme = NULL, *client_ip;
2422
ngx_uint_t i;
2523
ngx_http_redirectionio_conf_t *conf;
2624
ngx_http_redirectionio_header_set_t *hs;
@@ -49,8 +47,8 @@ ngx_int_t ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_ht
4947
}
5048

5149
current_header = (struct REDIRECTIONIO_HeaderMap *)ngx_pcalloc(r->pool, sizeof(struct REDIRECTIONIO_HeaderMap));
52-
current_header->name = ngx_str_to_char(&h[i].key, r->pool);
53-
current_header->value = ngx_str_to_char(&h[i].value, r->pool);
50+
current_header->name = ngx_http_redirectionio_str_to_char(&h[i].key, r->pool);
51+
current_header->value = ngx_http_redirectionio_str_to_char(&h[i].value, r->pool);
5452
current_header->next = first_header;
5553

5654
first_header = current_header;
@@ -69,15 +67,15 @@ ngx_int_t ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_ht
6967
}
7068

7169
current_header = (struct REDIRECTIONIO_HeaderMap *)ngx_pcalloc(r->pool, sizeof(struct REDIRECTIONIO_HeaderMap));
72-
current_header->name = ngx_str_to_char(&hsn, r->pool);
73-
current_header->value = ngx_str_to_char(&hsv, r->pool);
70+
current_header->name = ngx_http_redirectionio_str_to_char(&hsn, r->pool);
71+
current_header->value = ngx_http_redirectionio_str_to_char(&hsv, r->pool);
7472
current_header->next = first_header;
7573

7674
first_header = current_header;
7775
}
7876

7977
if (ctx->scheme.len > 0) {
80-
scheme = ngx_str_to_char(&ctx->scheme, r->pool);
78+
scheme = ngx_http_redirectionio_str_to_char(&ctx->scheme, r->pool);
8179
} else {
8280
scheme = "http";
8381
}
@@ -88,13 +86,13 @@ ngx_int_t ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_ht
8886
}
8987
#endif
9088

91-
uri = ngx_str_to_char(&r->unparsed_uri, r->pool);
92-
method = ngx_str_to_char(&r->method_name, r->pool);
89+
uri = ngx_http_redirectionio_str_to_char(&r->unparsed_uri, r->pool);
90+
method = ngx_http_redirectionio_str_to_char(&r->method_name, r->pool);
9391

9492
if (ctx->host.len > 0) {
95-
host = ngx_str_to_char(&ctx->host, r->pool);
93+
host = ngx_http_redirectionio_str_to_char(&ctx->host, r->pool);
9694
} else if (r->headers_in.host != NULL) {
97-
host = ngx_str_to_char(&r->headers_in.host->value, r->pool);
95+
host = ngx_http_redirectionio_str_to_char(&r->headers_in.host->value, r->pool);
9896
}
9997

10098
// Create redirection io request
@@ -104,6 +102,9 @@ ngx_int_t ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_ht
104102
return NGX_ERROR;
105103
}
106104

105+
client_ip = ngx_http_redirectionio_str_to_char(&r->connection->addr_text, r->pool);
106+
redirectionio_request_set_remote_addr(ctx->request, (const char *)client_ip, conf->trusted_proxies);
107+
107108
// Serialize request
108109
request_serialized = redirectionio_request_json_serialize(ctx->request);
109110

@@ -183,7 +184,7 @@ ngx_http_redirectionio_log_t* ngx_http_redirectionio_protocol_create_log(ngx_htt
183184
const char *client_ip, *log_serialized;
184185
ngx_http_redirectionio_log_t *log;
185186

186-
client_ip = ngx_str_to_char(&r->connection->addr_text, r->pool);
187+
client_ip = ngx_http_redirectionio_str_to_char(&r->connection->addr_text, r->pool);
187188
log_serialized = redirectionio_api_create_log_in_json(ctx->request, r->headers_out.status, ctx->response_headers, ctx->action, PROXY_VERSION_STR(PROXY_VERSION), r->start_msec, client_ip);
188189

189190
if (log_serialized == NULL) {
@@ -217,16 +218,6 @@ static void ngx_str_copy(ngx_str_t *src, ngx_str_t *dest) {
217218
ngx_memcpy(dest->data, src->data, dest->len);
218219
}
219220

220-
static char* ngx_str_to_char(ngx_str_t *src, ngx_pool_t *pool) {
221-
char *str;
222-
223-
str = (char *)ngx_pcalloc(pool, src->len + 1);
224-
ngx_memcpy(str, src->data, src->len);
225-
*((char *)str + src->len) = '\0';
226-
227-
return str;
228-
}
229-
230221
static ngx_int_t ngx_http_redirectionio_send_uint8(ngx_connection_t *c, uint8_t uint8) {
231222
ssize_t slen;
232223

0 commit comments

Comments
 (0)