diff --git a/conntrackt/utils.py b/conntrackt/utils.py new file mode 100644 --- /dev/null +++ b/conntrackt/utils.py @@ -0,0 +1,33 @@ +# Standard library imports. +import re + +# Django-specific imports. +from django.template import Context, loader + + +def generate_entity_iptables(entity): + """ + Generates full iptables rules for the supplied entity. The generated rules + can be fed directly to the iptables-restore utility. + + Arguments: + + entity - An Entity instance for which the iptables rules should be + generated. + + Returns: + + String containing the iptables rules for entity. + """ + + # Render the iptables template. + template = loader.get_template('conntrackt/entity_iptables.html') + context = Context({'entity': entity}) + content = template.render(context) + + # Remove the blank lines. + content = re.sub('^\s*\n', '', content) + content = re.sub('\n\s*\n', '\n', content) + + return content +