28
28
import tornado
29
29
import ssl
30
30
import signal
31
+ import json
31
32
from typing import Any
32
33
33
34
signal .signal (signal .SIGINT , signal .SIG_IGN )
34
35
signal .signal (signal .SIGTERM , signal .SIG_IGN )
35
- WEBHOOK_QUERY_NV_PREFIX = "webhook.query."
36
+ WEBHOOK_NV_PREFIX = "webhook."
37
+ WEBHOOK_QUERY_NV_PREFIX = WEBHOOK_NV_PREFIX + "query."
38
+ WEBHOOK_HEADERS_KEY = WEBHOOK_NV_PREFIX + "headers"
36
39
37
40
WEBHOOK_PROXY_SOURCEIP_HEADERS = [
38
41
"x-forwarded-for" ,
45
48
"x-forwarded" ,
46
49
]
47
50
51
+
48
52
class Handler (tornado .web .RequestHandler ):
49
53
def initialize (self , source ) -> None :
50
54
self .source = source
@@ -69,7 +73,7 @@ def _set_proxied_ip(self, msg: LogMessage) -> None:
69
73
if header and len (header ) > 0 :
70
74
# the closest/last IP (the behind_proxy flag implies that the last one can be trusted)
71
75
msg .set_source_ipaddress (header [- 1 ])
72
- msg ["webhook. proxy_ip" ] = self .request .remote_ip
76
+ msg [WEBHOOK_NV_PREFIX + " proxy_ip" ] = self .request .remote_ip
73
77
return
74
78
75
79
msg .set_source_ipaddress (self .request .remote_ip )
@@ -85,6 +89,9 @@ def _construct_msg(self, request, path_arguments) -> LogMessage:
85
89
for key , value in path_arguments .items ():
86
90
msg [key ] = value
87
91
92
+ if self .source .include_request_headers :
93
+ msg [WEBHOOK_HEADERS_KEY ] = json .dumps (list (self .request .headers .get_all ()))
94
+
88
95
if self .source .behind_proxy :
89
96
self ._set_proxied_ip (msg )
90
97
else :
@@ -211,6 +218,7 @@ def init_options(self, options: dict[str, Any]) -> bool:
211
218
self .tls_ca_dir = options .get ("tls_ca_dir" )
212
219
213
220
self .behind_proxy = bool (options .get ("behind_proxy" , False ))
221
+ self .include_request_headers = bool (options .get ("include_request_headers" , False ))
214
222
return True
215
223
except KeyError as e :
216
224
self .logger .error (f"Missing option '{ e .args [0 ]} '" )
0 commit comments