Last active 1589045305

Encoding and decoding files with txt records

Revision 9be66fefc391159b9b1fc4e394152f89b6cb098b

txtrecord_decode.py Raw
1#!/usr/bin/env python3
2# txtrecord_decode.py output.png logo.example.com
3import dns.resolver # pip3 install dnspython
4import base64
5import sys
6q = dns.resolver.query(sys.argv[2],"TXT").response.answer[0]
7out = b""
8for line in sorted(_.strings[0] for _ in q):
9 if chr(line[0]) == "{":
10 try:
11 out += line.split(b"}",1)[1]
12 except IndexError:
13 pass
14with open(sys.argv[1],"wb") as f:
15 f.write(base64.b64decode(out))
txtrecord_encode.py Raw
1#!/usr/bin/env python3
2# txtrecord_encode.py input.png logo.example.com
3import base64
4import sys
5from textwrap import wrap
6with open(sys.argv[1],"rb") as f:
7 enc = base64.b64encode(f.read()).decode("utf-8")
8
9# '%04d' and '247' may need to be updated for large files to ensure the line count doesn't push the length
10# longer than 255 bytes
11print("\n".join("%s. IN TXT '{%04d}%s'" % (sys.argv[2], n,s) for n,s in enumerate(wrap(enc,247))))