1
+
1
2
#include <ngx_config.h>
2
3
#include <ngx_core.h>
3
4
#include <ngx_http.h>
4
5
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>
8
7
9
8
static 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 );
10
17
11
18
static 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 */
20
27
};
21
28
22
29
ngx_module_t ngx_http_ssl_fingerprint_module = {
23
30
NGX_MODULE_V1 ,
24
31
& 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 */
34
41
NGX_MODULE_V1_PADDING };
35
42
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
+ };
36
54
37
55
static ngx_int_t
38
56
ngx_http_ssl_greased (ngx_http_request_t * r ,
39
57
ngx_http_variable_value_t * v , uintptr_t data )
40
58
{
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 ;
45
62
46
- if (r -> connection -> ssl == NULL )
47
- {
63
+ if (ngx_ssl_ja3 (r -> connection ) != NGX_OK ) {
48
64
return NGX_OK ;
49
65
}
50
66
51
- if (ngx_ssl_ja3 (r -> connection ) == NGX_DECLINED )
52
- {
53
- return NGX_ERROR ;
54
- }
55
-
56
67
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" );
59
69
v -> valid = 1 ;
60
70
v -> no_cacheable = 1 ;
61
71
v -> not_found = 0 ;
@@ -67,26 +77,19 @@ static ngx_int_t
67
77
ngx_http_ssl_fingerprint (ngx_http_request_t * r ,
68
78
ngx_http_variable_value_t * v , uintptr_t data )
69
79
{
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 ;
74
83
75
- if (r -> connection -> ssl == NULL )
76
- {
84
+ if (ngx_ssl_ja3 (r -> connection ) != NGX_OK ) {
77
85
return NGX_OK ;
78
86
}
79
87
80
- if (ngx_ssl_ja3 (r -> connection ) == NGX_DECLINED )
81
- {
82
- return NGX_ERROR ;
83
- }
84
-
85
88
v -> data = r -> connection -> ssl -> fp_ja3_str .data ;
86
89
v -> len = r -> connection -> ssl -> fp_ja3_str .len ;
87
- v -> valid = 1 ;
88
90
v -> no_cacheable = 1 ;
89
91
v -> not_found = 0 ;
92
+ v -> valid = 1 ;
90
93
91
94
return NGX_OK ;
92
95
}
@@ -95,26 +98,19 @@ static ngx_int_t
95
98
ngx_http_ssl_fingerprint_hash (ngx_http_request_t * r ,
96
99
ngx_http_variable_value_t * v , uintptr_t data )
97
100
{
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 ;
102
104
103
- if (r -> connection -> ssl == NULL )
104
- {
105
+ if (ngx_ssl_ja3_hash (r -> connection ) != NGX_OK ) {
105
106
return NGX_OK ;
106
107
}
107
108
108
- if (ngx_ssl_ja3_hash (r -> connection ) == NGX_DECLINED )
109
- {
110
- return NGX_ERROR ;
111
- }
112
-
113
109
v -> data = r -> connection -> ssl -> fp_ja3_hash .data ;
114
110
v -> len = r -> connection -> ssl -> fp_ja3_hash .len ;
115
- v -> valid = 1 ;
116
111
v -> no_cacheable = 1 ;
117
112
v -> not_found = 0 ;
113
+ v -> valid = 1 ;
118
114
119
115
return NGX_OK ;
120
116
}
@@ -123,77 +119,45 @@ static ngx_int_t
123
119
ngx_http_http2_fingerprint (ngx_http_request_t * r ,
124
120
ngx_http_variable_value_t * v , uintptr_t data )
125
121
{
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 ;
130
125
131
- if (r -> stream == NULL )
132
- {
126
+ if (r -> stream == NULL ) {
133
127
return NGX_OK ;
134
128
}
135
129
136
- if (r -> stream -> connection == NULL )
130
+ if (ngx_http2_fingerprint (r -> connection , r -> stream -> connection )
131
+ != NGX_OK )
137
132
{
138
133
return NGX_OK ;
139
134
}
140
135
141
- if (ngx_http2_fingerprint (r -> connection , r -> stream -> connection ) == NGX_DECLINED )
142
- {
143
- return NGX_ERROR ;
144
- }
145
-
146
136
v -> data = r -> stream -> connection -> fp_str .data ;
147
137
v -> len = r -> stream -> connection -> fp_str .len ;
148
138
v -> valid = 1 ;
149
- v -> no_cacheable = 1 ;
150
139
v -> not_found = 0 ;
140
+ v -> no_cacheable = 1 ;
151
141
152
142
return NGX_OK ;
153
143
}
154
144
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
-
174
145
static ngx_int_t
175
146
ngx_http_ssl_fingerprint_init (ngx_conf_t * cf )
176
147
{
148
+ ngx_http_variable_t * var , * v ;
177
149
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 ++ ) {
184
151
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 ;
194
155
}
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 ;
196
159
}
197
160
198
161
return NGX_OK ;
199
162
}
163
+
0 commit comments