feat(bots.php, htaccess.liquid): share files with specific bots
This commit is contained in:
parent
79464d6769
commit
92e123cd57
6 changed files with 66 additions and 18 deletions
|
@ -38,4 +38,5 @@ export default {
|
|||
return `${string}s${trailing ? `${trailing}` : ''}`;
|
||||
},
|
||||
jsonEscape: (string) => JSON.stringify(string),
|
||||
regexEscape: (string) => string.replace(/[.*+?^${}()[]\\]/g, '\\$&')
|
||||
};
|
||||
|
|
|
@ -2,7 +2,7 @@ import htmlmin from 'html-minifier-terser';
|
|||
|
||||
export const htmlConfig = (eleventyConfig) => {
|
||||
eleventyConfig.addTransform('html-minify', (content, path) => {
|
||||
if (path && (path.endsWith('.html') || path.endsWith('.php'))) {
|
||||
if (path && (path.endsWith('.html') || path.endsWith('.php') && !path.includes("api"))) {
|
||||
return htmlmin.minify(content, {
|
||||
collapseBooleanAttributes: true,
|
||||
collapseWhitespace: true,
|
||||
|
|
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "coryd.dev",
|
||||
"version": "8.1.9",
|
||||
"version": "8.2.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "coryd.dev",
|
||||
"version": "8.1.9",
|
||||
"version": "8.2.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"minisearch": "^7.1.2",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "coryd.dev",
|
||||
"version": "8.1.9",
|
||||
"version": "8.2.0",
|
||||
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
||||
"type": "module",
|
||||
"engines": {
|
||||
|
|
48
src/meta/bots.php.liquid
Normal file
48
src/meta/bots.php.liquid
Normal file
|
@ -0,0 +1,48 @@
|
|||
---
|
||||
permalink: "/api/bots.php"
|
||||
layout: null
|
||||
eleventyExcludeFromCollections: true
|
||||
excludeFromSitemap: true
|
||||
---
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../server/utils/init.php';
|
||||
|
||||
$blockedAgents = [
|
||||
{%- assign allAgents = "" -%}
|
||||
{%- for robot in robots -%}
|
||||
{%- for userAgent in robot.user_agents -%}
|
||||
{%- if userAgent != "*" and userAgent != "NaN" -%}
|
||||
'{{ userAgent | escape }}'{% unless forloop.last %},{% endunless %}
|
||||
{%- endif -%}
|
||||
{%- endfor -%}
|
||||
{%- endfor -%}
|
||||
];
|
||||
|
||||
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
|
||||
$shouldShareFile = false;
|
||||
|
||||
foreach ($blockedAgents as $bot) {
|
||||
if (stripos($ua, $bot) !== false) {
|
||||
$shouldShareFile = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($shouldShareFile) {
|
||||
header('Content-Type: application/octet-stream');
|
||||
header('Content-Disposition: attachment; filename="10GB_stream.dat"');
|
||||
|
||||
$chunkSize = 1024 * 1024; // 1MB
|
||||
$totalChunks = 10240;
|
||||
|
||||
for ($i = 0; $i < $totalChunks; $i++) {
|
||||
echo random_bytes($chunkSize);
|
||||
flush();
|
||||
usleep(50000);
|
||||
}
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
redirectTo404();
|
|
@ -83,23 +83,22 @@ Redirect {{ redirect.status_code | default: "301" }} {{ redirect.source_url }} {
|
|||
</FilesMatch>
|
||||
{% endfor %}
|
||||
|
||||
{%- assign userAgents = "" -%}
|
||||
{% for robot in robots -%}
|
||||
{%- for userAgent in robot.user_agents -%}
|
||||
{%- if userAgent != "*" and userAgent != "NaN" -%}
|
||||
{%- assign userAgents = userAgents | append: userAgent %}
|
||||
{%- unless forloop.last -%}
|
||||
{%- assign userAgents = userAgents | append: "|" -%}
|
||||
{%- endunless -%}
|
||||
{%- assign escapedAgents = "" -%}
|
||||
{%- for robot in robots -%}
|
||||
{%- for agent in robot.user_agents -%}
|
||||
{%- if agent != "*" and agent != "NaN" -%}
|
||||
{%- assign escaped = agent | regexEscape -%}
|
||||
{%- assign escapedAgents = escapedAgents | append: escaped | append: "|" -%}
|
||||
{%- endif -%}
|
||||
{%- endfor -%}
|
||||
{%- endfor %}
|
||||
{% if userAgents != "" or referers != "" -%}
|
||||
RewriteCond %{HTTP_USER_AGENT} "{{ userAgents }}" [NC]
|
||||
{%- endfor -%}
|
||||
{%- assign userAgentsRegex = escapedAgents | slice: 0, escapedAgents.size | rstrip: "|" -%}
|
||||
{% if userAgentsRegex != "" %}
|
||||
RewriteEngine On
|
||||
RewriteCond %{HTTP_USER_AGENT} "{{ userAgentsRegex }}" [NC]
|
||||
RewriteCond %{REQUEST_URI} !^/robots\.txt$ [NC]
|
||||
RewriteRule .* /403/index.html [L,R=403]
|
||||
{%- endif %}
|
||||
|
||||
RewriteRule .* /api/bots.php [L]
|
||||
{% endif %}
|
||||
<IfModule mod_deflate.c>
|
||||
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
|
||||
</IfModule>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue