Skip to content

Commit 47d11d3

Browse files
committed
use user-agent for searching a client
1 parent 71758d7 commit 47d11d3

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

tempesta_fw/client.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ tfw_client_addr_eq(TdbRec *rec, void *data)
8383
}
8484

8585
/**
86-
* Find a client corresponding to @addr and @cli_addr (e.g. from X-Forwarded-For).
87-
* More advanced identification is possible based on User-Agent,
88-
* Cookie and other HTTP headers.
86+
* Find a client corresponding to @addr, @cli_addr (e.g. from X-Forwarded-For)
87+
* and @user_agent.
8988
*
9089
* The returned TfwClient reference must be released via tfw_client_put()
9190
* when the @sk is closed.
9291
* TODO #515 employ eviction strategy for the table
9392
*/
9493
TfwClient *
95-
tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, void (*init)(TfwClient *))
94+
tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, TfwStr *user_agent,
95+
void (*init)(TfwClient *))
9696
{
9797
TfwClientEntry *ent;
9898
TfwClient *cli;
@@ -109,6 +109,12 @@ tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, void (*init)(TfwClient *))
109109
if (cli_addr)
110110
key ^= hash_calc((const char *)&cli_addr->sin6_addr,
111111
sizeof(cli_addr->sin6_addr));
112+
if (user_agent) {
113+
char buf[256];
114+
size_t len = tfw_str_to_cstr(user_agent, buf, sizeof(buf));
115+
116+
key ^= hash_calc(buf, len);
117+
}
112118

113119
len = sizeof(*ent);
114120
rec = tdb_rec_get_alloc(client_db, key, &len, &tfw_client_addr_eq,

tempesta_fw/client.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ typedef struct {
4040
TfwClassifierPrvt class_prvt;
4141
} TfwClient;
4242

43-
TfwClient *tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr, void (*init)(TfwClient *));
43+
TfwClient *tfw_client_obtain(TfwAddr addr, TfwAddr *cli_addr,
44+
TfwStr *user_agent, void (*init)(TfwClient *));
4445
void tfw_client_put(TfwClient *cli);
4546
int tfw_client_for_each(int (*fn)(void *));
4647
void tfw_cli_conn_release(TfwCliConn *cli_conn);

tempesta_fw/http.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,7 +2855,7 @@ tfw_http_req_process(TfwConn *conn, const TfwFsmData *data)
28552855
TfwHttpParser *parser;
28562856
TfwFsmData data_up;
28572857
TfwAddr addr;
2858-
TfwStr s_ip;
2858+
TfwStr s_ip, s_user_agent;
28592859

28602860
BUG_ON(!conn->msg);
28612861
BUG_ON(off >= skb->len);
@@ -2962,12 +2962,17 @@ tfw_http_req_process(TfwConn *conn, const TfwFsmData *data)
29622962
s_ip = tfw_http_get_ip_from_xff(req);
29632963
if (!TFW_STR_EMPTY(&s_ip)) {
29642964
TfwClient *cli, *conn_cli;
2965+
TfwStr *ua;
29652966

29662967
if (tfw_addr_pton(&s_ip, &addr) != 0)
29672968
return TFW_BLOCK;
29682969

29692970
conn_cli = (TfwClient *)conn->peer;
2970-
cli = tfw_client_obtain(conn_cli->addr, &addr, NULL);
2971+
ua = &req->h_tbl->tbl[TFW_HTTP_HDR_USER_AGENT];
2972+
tfw_http_msg_clnthdr_val(ua, TFW_HTTP_HDR_USER_AGENT,
2973+
&s_user_agent);
2974+
cli = tfw_client_obtain(conn_cli->addr, &addr, &s_user_agent,
2975+
NULL);
29712976
if (cli && cli != conn_cli)
29722977
req->peer = cli;
29732978
else

tempesta_fw/http_limits.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ frang_conn_new(struct sock *sk)
338338
TfwAddr addr;
339339

340340
ss_getpeername(sk, &addr);
341-
cli = tfw_client_obtain(addr, NULL, __frang_init_acc);
341+
cli = tfw_client_obtain(addr, NULL, NULL, __frang_init_acc);
342342
if (unlikely(!cli)) {
343343
TFW_ERR("can't obtain a client for frang accounting\n");
344344
return TFW_BLOCK;

tempesta_fw/sock_clnt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ tfw_sock_clnt_new(struct sock *sk)
160160
tfw_connection_unlink_from_sk(sk);
161161

162162
ss_getpeername(sk, &addr);
163-
cli = tfw_client_obtain(addr, NULL, NULL);
163+
cli = tfw_client_obtain(addr, NULL, NULL, NULL);
164164
if (!cli) {
165165
TFW_ERR("can't obtain a client for the new socket\n");
166166
return -ENOENT;

0 commit comments

Comments
 (0)