Changeset

2532:2ddb74805f91

mod_firewall: Add 'file' backend for lists (read-only atm)
author Matthew Wild <mwild1@gmail.com>
date Mon, 20 Feb 2017 10:59:29 +0000
parents 2531:9d2bfff515b8
children 2533:9aed7f4e9f07
files mod_firewall/definitions.lib.lua
diffstat 1 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_firewall/definitions.lib.lua	Mon Feb 20 09:47:58 2017 +0000
+++ b/mod_firewall/definitions.lib.lua	Mon Feb 20 10:59:29 2017 +0000
@@ -139,6 +139,30 @@
 			return self.items and self.items[item] == true;
 		end;
 	};
+	file = {
+		init = function (list_backend, file_spec, opts)
+			local filename = file_spec:gsub("^file:");
+			local file, err = io.open(filename);
+			if not file then
+				module:log("warn", "Failed to open list from %s: %s", filename, err);
+				return;
+			end
+			local items = {};
+			for line in file:lines() do
+				items[line] = true;
+			end
+			self.items = items;
+		end;
+		add = function (self, item)
+			self.items[item] = true;
+		end;
+		remove = function (self, item)
+			self.items[item] = nil;
+		end;
+		contains = function (self, item)
+			return self.items and self.items[item] == true;
+		end;
+	};
 };
 list_backends.https = list_backends.http;