1+
12#include <ngx_config.h>
23#include <ngx_core.h>
34#include <ngx_http.h>
45
5- extern int ngx_ssl_ja3 (ngx_connection_t * c );
6- extern int ngx_ssl_ja3_hash (ngx_connection_t * c );
7- extern int ngx_http2_fingerprint (ngx_connection_t * c , ngx_http_v2_connection_t * h2c );
6+ #include <nginx_ssl_fingerprint.h>
87
98static ngx_int_t ngx_http_ssl_fingerprint_init (ngx_conf_t * cf );
9+ static ngx_int_t ngx_http_ssl_greased (ngx_http_request_t * r ,
10+ ngx_http_variable_value_t * v , uintptr_t data );
11+ static ngx_int_t ngx_http_ssl_fingerprint (ngx_http_request_t * r ,
12+ ngx_http_variable_value_t * v , uintptr_t data );
13+ static ngx_int_t ngx_http_ssl_fingerprint_hash (ngx_http_request_t * r ,
14+ ngx_http_variable_value_t * v , uintptr_t data );
15+ static ngx_int_t ngx_http_http2_fingerprint (ngx_http_request_t * r ,
16+ ngx_http_variable_value_t * v , uintptr_t data );
1017
1118static ngx_http_module_t ngx_http_ssl_fingerprint_module_ctx = {
12- NULL , /* preconfiguration */
13- ngx_http_ssl_fingerprint_init , /* postconfiguration */
14- NULL , /* create main configuration */
15- NULL , /* init main configuration */
16- NULL , /* create server configuration */
17- NULL , /* merge server configuration */
18- NULL , /* create location configuration */
19- NULL /* merge location configuration */
19+ ngx_http_ssl_fingerprint_init , /* preconfiguration */
20+ NULL , /* postconfiguration */
21+ NULL , /* create main configuration */
22+ NULL , /* init main configuration */
23+ NULL , /* create server configuration */
24+ NULL , /* merge server configuration */
25+ NULL , /* create location configuration */
26+ NULL /* merge location configuration */
2027};
2128
2229ngx_module_t ngx_http_ssl_fingerprint_module = {
2330 NGX_MODULE_V1 ,
2431 & ngx_http_ssl_fingerprint_module_ctx , /* module context */
25- NULL , /* module directives */
26- NGX_HTTP_MODULE , /* module type */
27- NULL , /* init master */
28- NULL , /* init module */
29- NULL , /* init process */
30- NULL , /* init thread */
31- NULL , /* exit thread */
32- NULL , /* exit process */
33- NULL , /* exit master */
32+ NULL , /* module directives */
33+ NGX_HTTP_MODULE , /* module type */
34+ NULL , /* init master */
35+ NULL , /* init module */
36+ NULL , /* init process */
37+ NULL , /* init thread */
38+ NULL , /* exit thread */
39+ NULL , /* exit process */
40+ NULL , /* exit master */
3441 NGX_MODULE_V1_PADDING };
3542
43+ static ngx_http_variable_t ngx_http_ssl_fingerprint_variables_list [] = {
44+ {ngx_string ("http_ssl_greased" ), NULL , ngx_http_ssl_greased ,
45+ 0 , NGX_HTTP_VAR_NOCACHEABLE , 0 },
46+ {ngx_string ("http_ssl_ja3" ), NULL , ngx_http_ssl_fingerprint ,
47+ 0 , NGX_HTTP_VAR_NOCACHEABLE , 0 },
48+ {ngx_string ("http_ssl_ja3_hash" ), NULL , ngx_http_ssl_fingerprint_hash ,
49+ 0 , NGX_HTTP_VAR_NOCACHEABLE , 0 },
50+ {ngx_string ("http2_fingerprint" ), NULL , ngx_http_http2_fingerprint ,
51+ 0 , NGX_HTTP_VAR_NOCACHEABLE , 0 },
52+ ngx_http_null_variable
53+ };
3654
3755static ngx_int_t
3856ngx_http_ssl_greased (ngx_http_request_t * r ,
3957 ngx_http_variable_value_t * v , uintptr_t data )
4058{
41- if (r -> connection == NULL )
42- {
43- return NGX_OK ;
44- }
59+ /* For access.log's map $http2_fingerpring {}:
60+ * if it's not found, then user could add a defined string */
61+ v -> not_found = 1 ;
4562
46- if (r -> connection -> ssl == NULL )
47- {
63+ if (ngx_ssl_ja3 (r -> connection ) != NGX_OK ) {
4864 return NGX_OK ;
4965 }
5066
51- if (ngx_ssl_ja3 (r -> connection ) == NGX_DECLINED )
52- {
53- return NGX_ERROR ;
54- }
55-
5667 v -> len = 1 ;
57- v -> data = (u_char * )(r -> connection -> ssl -> fp_tls_greased ? "1" : "0" );
58-
68+ v -> data = (u_char * ) (r -> connection -> ssl -> fp_tls_greased ? "1" : "0" );
5969 v -> valid = 1 ;
6070 v -> no_cacheable = 1 ;
6171 v -> not_found = 0 ;
@@ -67,26 +77,19 @@ static ngx_int_t
6777ngx_http_ssl_fingerprint (ngx_http_request_t * r ,
6878 ngx_http_variable_value_t * v , uintptr_t data )
6979{
70- if (r -> connection == NULL )
71- {
72- return NGX_OK ;
73- }
80+ /* For access.log's map $http2_fingerpring {}:
81+ * if it's not found, then user could add a defined string */
82+ v -> not_found = 1 ;
7483
75- if (r -> connection -> ssl == NULL )
76- {
84+ if (ngx_ssl_ja3 (r -> connection ) != NGX_OK ) {
7785 return NGX_OK ;
7886 }
7987
80- if (ngx_ssl_ja3 (r -> connection ) == NGX_DECLINED )
81- {
82- return NGX_ERROR ;
83- }
84-
8588 v -> data = r -> connection -> ssl -> fp_ja3_str .data ;
8689 v -> len = r -> connection -> ssl -> fp_ja3_str .len ;
87- v -> valid = 1 ;
8890 v -> no_cacheable = 1 ;
8991 v -> not_found = 0 ;
92+ v -> valid = 1 ;
9093
9194 return NGX_OK ;
9295}
@@ -95,26 +98,19 @@ static ngx_int_t
9598ngx_http_ssl_fingerprint_hash (ngx_http_request_t * r ,
9699 ngx_http_variable_value_t * v , uintptr_t data )
97100{
98- if (r -> connection == NULL )
99- {
100- return NGX_OK ;
101- }
101+ /* For access.log's map $http2_fingerpring {}:
102+ * if it's not found, then user could add a defined string */
103+ v -> not_found = 1 ;
102104
103- if (r -> connection -> ssl == NULL )
104- {
105+ if (ngx_ssl_ja3_hash (r -> connection ) != NGX_OK ) {
105106 return NGX_OK ;
106107 }
107108
108- if (ngx_ssl_ja3_hash (r -> connection ) == NGX_DECLINED )
109- {
110- return NGX_ERROR ;
111- }
112-
113109 v -> data = r -> connection -> ssl -> fp_ja3_hash .data ;
114110 v -> len = r -> connection -> ssl -> fp_ja3_hash .len ;
115- v -> valid = 1 ;
116111 v -> no_cacheable = 1 ;
117112 v -> not_found = 0 ;
113+ v -> valid = 1 ;
118114
119115 return NGX_OK ;
120116}
@@ -123,77 +119,45 @@ static ngx_int_t
123119ngx_http_http2_fingerprint (ngx_http_request_t * r ,
124120 ngx_http_variable_value_t * v , uintptr_t data )
125121{
126- if (r -> connection == NULL )
127- {
128- return NGX_OK ;
129- }
122+ /* For access.log's map $http2_fingerpring {}:
123+ * if it's not found, then user could add a defined string */
124+ v -> not_found = 1 ;
130125
131- if (r -> stream == NULL )
132- {
126+ if (r -> stream == NULL ) {
133127 return NGX_OK ;
134128 }
135129
136- if (r -> stream -> connection == NULL )
130+ if (ngx_http2_fingerprint (r -> connection , r -> stream -> connection )
131+ != NGX_OK )
137132 {
138133 return NGX_OK ;
139134 }
140135
141- if (ngx_http2_fingerprint (r -> connection , r -> stream -> connection ) == NGX_DECLINED )
142- {
143- return NGX_ERROR ;
144- }
145-
146136 v -> data = r -> stream -> connection -> fp_str .data ;
147137 v -> len = r -> stream -> connection -> fp_str .len ;
148138 v -> valid = 1 ;
149- v -> no_cacheable = 1 ;
150139 v -> not_found = 0 ;
140+ v -> no_cacheable = 1 ;
151141
152142 return NGX_OK ;
153143}
154144
155- static ngx_http_variable_t ngx_http_ssl_fingerprint_variables_list [] = {
156- {ngx_string ("http_ssl_greased" ),
157- NULL ,
158- ngx_http_ssl_greased ,
159- 0 , 0 , 0 },
160- {ngx_string ("http_ssl_ja3" ),
161- NULL ,
162- ngx_http_ssl_fingerprint ,
163- 0 , 0 , 0 },
164- {ngx_string ("http_ssl_ja3_hash" ),
165- NULL ,
166- ngx_http_ssl_fingerprint_hash ,
167- 0 , 0 , 0 },
168- {ngx_string ("http2_fingerprint" ),
169- NULL ,
170- ngx_http_http2_fingerprint ,
171- 0 , 0 , 0 },
172- };
173-
174145static ngx_int_t
175146ngx_http_ssl_fingerprint_init (ngx_conf_t * cf )
176147{
148+ ngx_http_variable_t * var , * v ;
177149
178- ngx_http_variable_t * v ;
179- size_t l = 0 ;
180- size_t vars_len ;
181-
182- vars_len = (sizeof (ngx_http_ssl_fingerprint_variables_list ) /
183- sizeof (ngx_http_ssl_fingerprint_variables_list [0 ]));
150+ for (v = ngx_http_ssl_fingerprint_variables_list ; v -> name .len ; v ++ ) {
184151
185- /* Register variables */
186- for (l = 0 ; l < vars_len ; ++ l )
187- {
188- v = ngx_http_add_variable (cf ,
189- & ngx_http_ssl_fingerprint_variables_list [l ].name ,
190- ngx_http_ssl_fingerprint_variables_list [l ].flags );
191- if (v == NULL )
192- {
193- continue ;
152+ var = ngx_http_add_variable (cf , & v -> name , v -> flags );
153+ if (var == NULL ) {
154+ return NGX_ERROR ;
194155 }
195- * v = ngx_http_ssl_fingerprint_variables_list [l ];
156+ /** NOTE: update it, if set_handler will be needed */
157+ var -> get_handler = v -> get_handler ;
158+ var -> data = v -> data ;
196159 }
197160
198161 return NGX_OK ;
199162}
163+
0 commit comments