Created
June 26, 2017 12:45
-
-
Save nickstenning/46311eb33f95db84fb54d69b68db1264 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import binascii | |
import sys | |
EAT_HEADERS = ( | |
b'CF-Connecting-IP:', | |
b'CF-IPCountry:', | |
b'CF-RAY:', | |
b'CF-Visitor:', | |
b'X-Amzn-Trace-Id:', | |
b'X-Forwarded-For:', | |
b'X-Forwarded-Port:', | |
b'X-Forwarded-Proto:', | |
) | |
def decode_request(line): | |
return binascii.unhexlify(line.strip().encode('utf-8')) | |
def encode_request(request): | |
return binascii.hexlify(request).decode('utf-8') + '\n' | |
def handle_request(request_text): | |
lines = request_text.splitlines(keepends=True) | |
output = [] | |
output.append(lines.pop(0)) # message header | |
for line in lines: | |
if not line.startswith(EAT_HEADERS): | |
output.append(line) | |
return b''.join(output) | |
def main(): | |
while True: | |
line = sys.stdin.readline() | |
if not line: | |
break | |
in_ = decode_request(line) | |
out = handle_request(in_) | |
if out is not None: | |
sys.stdout.write(encode_request(out)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment