diff options
author | bai | 2020-07-24 05:30:23 -0400 |
---|---|---|
committer | bai | 2020-07-24 05:30:23 -0400 |
commit | 7293ea47717c49d66651edef02c5c8def7df9333 (patch) | |
tree | 50ddcd111793def89faa23936d1986edb5973777 /cgi | |
parent | e67fc0c6ac081b71c819844591d7b1d317071b05 (diff) | |
download | weabot-7293ea47717c49d66651edef02c5c8def7df9333.tar.gz weabot-7293ea47717c49d66651edef02c5c8def7df9333.tar.xz weabot-7293ea47717c49d66651edef02c5c8def7df9333.zip |
Optimizado listado de bans para nginx usando el módulo geo
Diffstat (limited to 'cgi')
-rw-r--r-- | cgi/post.py | 37 | ||||
-rw-r--r-- | cgi/templates/bans_geo | 14 | ||||
-rw-r--r-- | cgi/templates/bans_locations | 10 | ||||
-rw-r--r-- | cgi/templates/bansconf | 7 |
4 files changed, 48 insertions, 20 deletions
diff --git a/cgi/post.py b/cgi/post.py index bcbda67..da2ad47 100644 --- a/cgi/post.py +++ b/cgi/post.py @@ -1081,22 +1081,21 @@ def regenerateAccess(): if not Settings.HTACCESS_GEN: return False - bans = FetchAll("SELECT `ipstr`, `boards` FROM `bans` WHERE `blind` = '1'") - listbans = {} + bans = FetchAll("SELECT `ipstr`, `boards` FROM `bans` WHERE `blind` = '1' ORDER BY `ipstart` ASC") - boarddirs = FetchAll('SELECT `dir` FROM `boards`') - for board in boarddirs: - listbans[board['dir']] = [] + boards = FetchAll('SELECT `dir` FROM `boards`') + global_boards = [board['dir'] for board in boards if board['dir'] not in Settings.EXCLUDE_GLOBAL_BANS] + + global_bans = [] + board_bans = {} for ban in bans: - if ban["boards"] != "": + if ban["boards"]: boards = pickle.loads(ban["boards"]) for board in boards: - listbans[board].append(ban["ipstr"]) + board_bans.setdefault(board, []).append(ban["ipstr"]) else: - for board in boarddirs: - if board['dir'] not in Settings.EXCLUDE_GLOBAL_BANS: - listbans[board['dir']].append(ban["ipstr"]) + global_bans.append(ban["ipstr"]) # Generate .htaccess for each board """ TODO: htaccess @@ -1112,11 +1111,23 @@ def regenerateAccess(): f.write(page_rendered) finally: f.close()""" - - page_rendered = renderTemplate('bansconf', {'bans': listbans}) - with open(Settings.ROOT_DIR + "/bans.conf", "w") as f: + + template_values = { + 'global_boards': global_boards, + 'global_bans': global_bans, + 'board_bans': board_bans, + } + + page_rendered = renderTemplate('bans_geo', template_values) + with open(Settings.ROOT_DIR + "/bans_geo.conf", "w") as f: + f.write(page_rendered) + + page_rendered = renderTemplate('bans_locations', template_values) + with open(Settings.ROOT_DIR + "/bans_locations.conf", "w") as f: f.write(page_rendered) + os.utime(Settings.ROOT_DIR + "/bans.conf", None) + return True def regenerateKako(): diff --git a/cgi/templates/bans_geo b/cgi/templates/bans_geo new file mode 100644 index 0000000..64687e8 --- /dev/null +++ b/cgi/templates/bans_geo @@ -0,0 +1,14 @@ +geo $bans_global { + default 0; +<?py for ip in global_bans: ?> + #{ip} 1; +<?py #endfor ?> +} +<?py for board, bans in board_bans.iteritems(): ?> +geo $bans_#{board} { + default 0; + <?py for ip in bans: ?> + #{ip} 1; + <?py #endfor ?> +} +<?py #endfor ?> diff --git a/cgi/templates/bans_locations b/cgi/templates/bans_locations new file mode 100644 index 0000000..a514ccf --- /dev/null +++ b/cgi/templates/bans_locations @@ -0,0 +1,10 @@ +<?py for board in set().union(global_boards, board_bans.keys()): ?> +location /#{board}/ { + <?py if board in global_boards: ?> + if ($bans_global) { rewrite ^ /cgi/banned; } + <?py #endif ?> + <?py if board in board_bans: ?> + if ($bans_#{board}) { rewrite ^ /cgi/banned; } + <?py #endif ?> +} +<?py #endfor ?> diff --git a/cgi/templates/bansconf b/cgi/templates/bansconf deleted file mode 100644 index 07e8afd..0000000 --- a/cgi/templates/bansconf +++ /dev/null @@ -1,7 +0,0 @@ -<?py for board, ips in bans.iteritems(): ?> -location /#{board}/ { - <?py for ip in ips: ?> - deny #{ip}; - <?py #endfor ?> -} -<?py #endfor ?> |