forked from presswizards/cloudflare-waf-rules-wizard
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnginx-rules.php
More file actions
233 lines (220 loc) · 7.28 KB
/
nginx-rules.php
File metadata and controls
233 lines (220 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Nginx Rules page
* Regenerates nginx/bot-blocking.conf and nginx/bot-blocking-wp-login.conf
* on every page load, then displays both for copy/paste.
*/
defined( 'CLOUDFLARE_API_KEY' ) || exit( 'No direct script access allowed' );
// ── Regenerate both conf files ────────────────────────────────────────────────
require_once __DIR__ . '/generate-nginx-rules.php';
// ── Read generated files ──────────────────────────────────────────────────────
$nginx_dir = __DIR__ . '/nginx';
$file_standard = $nginx_dir . '/bot-blocking.conf';
$file_wp_login = $nginx_dir . '/bot-blocking-wp-login.conf';
$conf_standard = file_exists( $file_standard ) ? file_get_contents( $file_standard ) : '(file not found)';
$conf_wp_login = file_exists( $file_wp_login ) ? file_get_contents( $file_wp_login ) : '(file not found)';
$generated_at = file_exists( $file_standard ) ? date( 'Y-m-d H:i:s', filemtime( $file_standard ) ) : 'unknown';
?>
<h2>Nginx Bot-Blocking Rules</h2>
<p>Rules are regenerated from <code>rules.php</code> each time this page is loaded. Copy the appropriate file contents into your nginx server block via an <code>include</code> directive.</p>
<p class="nginx-meta">Last generated: <strong><?php echo htmlspecialchars( $generated_at, ENT_QUOTES, 'UTF-8' ); ?></strong></p>
<div class="nginx-tabs">
<div class="nginx-tab-buttons">
<button class="nginx-tab-btn active" data-tab="standard">
<code>bot-blocking.conf</code>
<span class="nginx-tab-desc">Safe for any site — wp-login block disabled</span>
</button>
<button class="nginx-tab-btn" data-tab="wplogin">
<code>bot-blocking-wp-login.conf</code>
<span class="nginx-tab-desc">Includes wp-login block — requires WPS Hide Login</span>
</button>
</div>
<div class="nginx-tab-panel active" id="nginx-tab-standard">
<div class="nginx-copy-row">
<button class="button-primary nginx-copy-btn" data-target="nginx-conf-standard">Copy to Clipboard</button>
<span class="nginx-copy-confirm" id="nginx-copy-confirm-standard"></span>
</div>
<textarea id="nginx-conf-standard" class="nginx-conf-textarea" readonly spellcheck="false"><?php echo htmlspecialchars( $conf_standard, ENT_QUOTES, 'UTF-8' ); ?></textarea>
</div>
<div class="nginx-tab-panel" id="nginx-tab-wplogin">
<div class="nginx-copy-row">
<button class="button-primary nginx-copy-btn" data-target="nginx-conf-wplogin">Copy to Clipboard</button>
<span class="nginx-copy-confirm" id="nginx-copy-confirm-wplogin"></span>
</div>
<textarea id="nginx-conf-wplogin" class="nginx-conf-textarea" readonly spellcheck="false"><?php echo htmlspecialchars( $conf_wp_login, ENT_QUOTES, 'UTF-8' ); ?></textarea>
</div>
</div>
<h3>Deploy via SCP</h3>
<p>Run from the project root to push <code>bot-blocking.conf</code> to each server:</p>
<div class="nginx-scp-list">
<?php foreach ( FAIL2BAN_SERVERS as $server ) : ?>
<?php
$scp_cmd = 'scp nginx/bot-blocking.conf '
. $server['ssh_user'] . '@' . $server['hostname']
. ':/etc/nginx/firewall/';
?>
<div class="nginx-scp-row">
<span class="nginx-scp-label"><?php echo htmlspecialchars( $server['name'], ENT_QUOTES, 'UTF-8' ); ?></span>
<code class="nginx-scp-cmd" id="nginx-scp-<?php echo htmlspecialchars( $server['hostname'], ENT_QUOTES, 'UTF-8' ); ?>"><?php echo htmlspecialchars( $scp_cmd, ENT_QUOTES, 'UTF-8' ); ?></code>
<button class="nginx-scp-copy-btn" data-cmd="<?php echo htmlspecialchars( $scp_cmd, ENT_QUOTES, 'UTF-8' ); ?>">Copy</button>
<span class="nginx-copy-confirm nginx-scp-confirm"></span>
</div>
<?php endforeach; ?>
</div>
<style>
.nginx-meta {
color: #666;
font-size: 0.9em;
margin-bottom: 1.5em;
}
.nginx-tabs {
margin-top: 1em;
}
.nginx-tab-buttons {
display: flex;
gap: 0.5em;
flex-wrap: wrap;
margin-bottom: 0;
}
.nginx-tab-btn {
background: #e2e2e2;
color: #333;
border: 2px solid #bbb;
border-bottom: none;
border-radius: 4px 4px 0 0;
padding: 0.5em 1em 0.6em;
cursor: pointer;
font-size: 0.9em;
font-weight: 400;
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 0.15em;
transition: background 0.15s;
}
.nginx-tab-btn code {
font-size: 1em;
font-weight: 700;
}
.nginx-tab-desc {
font-size: 0.8em;
color: #666;
}
.nginx-tab-btn.active {
background: #fff;
border-color: #333;
color: #000;
position: relative;
z-index: 1;
}
.nginx-tab-btn.active .nginx-tab-desc {
color: #444;
}
.nginx-tab-panel {
display: none;
border: 2px solid #333;
border-radius: 0 4px 4px 4px;
padding: 1em;
background: #fff;
}
.nginx-tab-panel.active {
display: block;
}
.nginx-copy-row {
display: flex;
align-items: center;
gap: 1em;
margin-bottom: 0.75em;
}
.nginx-copy-confirm {
font-size: 0.9em;
color: #175319;
font-weight: 600;
opacity: 0;
transition: opacity 0.3s;
}
.nginx-copy-confirm.visible {
opacity: 1;
}
.nginx-conf-textarea {
width: 100%;
height: 32em;
font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
font-size: 0.8em;
line-height: 1.5;
background: #f6f6f6;
border: 1px solid #ccc;
border-radius: 3px;
padding: 0.75em;
resize: vertical;
box-sizing: border-box;
margin: 0;
}
.nginx-scp-list {
display: flex;
flex-direction: column;
gap: 0.5em;
margin-top: 0.75em;
}
.nginx-scp-row {
display: flex;
align-items: center;
gap: 0.75em;
flex-wrap: wrap;
}
.nginx-scp-label {
font-weight: 600;
min-width: 12em;
flex-shrink: 0;
}
.nginx-scp-cmd {
font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
font-size: 0.85em;
background: #f1f1f1;
border: 1px solid #ccc;
border-radius: 3px;
padding: 0.25em 0.5em;
flex: 1;
word-break: break-all;
}
.nginx-scp-copy-btn {
flex-shrink: 0;
cursor: pointer;
}
</style>
<script>
(function () {
// Tab switching
document.querySelectorAll('.nginx-tab-btn').forEach(function (btn) {
btn.addEventListener('click', function () {
document.querySelectorAll('.nginx-tab-btn').forEach(function (b) { b.classList.remove('active'); });
document.querySelectorAll('.nginx-tab-panel').forEach(function (p) { p.classList.remove('active'); });
btn.classList.add('active');
document.getElementById('nginx-tab-' + btn.dataset.tab).classList.add('active');
});
});
// Copy to clipboard — conf textareas
document.querySelectorAll('.nginx-copy-btn').forEach(function (btn) {
btn.addEventListener('click', function () {
var textarea = document.getElementById(btn.dataset.target);
var confirmEl = document.getElementById('nginx-copy-confirm-' + btn.dataset.target.replace('nginx-conf-', ''));
navigator.clipboard.writeText(textarea.value).then(function () {
confirmEl.textContent = '✓ Copied!';
confirmEl.classList.add('visible');
setTimeout(function () { confirmEl.classList.remove('visible'); }, 2500);
});
});
});
// Copy to clipboard — scp commands
document.querySelectorAll('.nginx-scp-copy-btn').forEach(function (btn) {
btn.addEventListener('click', function () {
var confirmEl = btn.nextElementSibling;
navigator.clipboard.writeText(btn.dataset.cmd).then(function () {
confirmEl.textContent = '✓ Copied!';
confirmEl.classList.add('visible');
setTimeout(function () { confirmEl.classList.remove('visible'); }, 2500);
});
});
});
}());
</script>