Diff

mod_firewall/definitions.lib.lua @ 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
parent 2530:84e103fd8039
child 2535:b85d88737a32
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;