条件付き応答
受信リクエストのURL、HTTPメソッド、ユーザーエージェント、IPアドレス、ASN、またはデバイスタイプに基づいて応答を返します。
export default { async fetch(request) { const BLOCKED_HOSTNAMES = ["nope.mywebsite.com", "bye.website.com"]; // URLのホスト名に基づいて新しいResponseを返します const url = new URL(request.url); if (BLOCKED_HOSTNAMES.includes(url.hostname)) { return new Response("ブロックされたホスト", { status: 403 }); } // URLのファイル拡張子に基づいて.docまたは.xmlで終わるパスをブロックします const forbiddenExtRegExp = new RegExp(/\.(doc|xml)$/); if (forbiddenExtRegExp.test(url.pathname)) { return new Response("ブロックされた拡張子", { status: 403 }); } // HTTPメソッドに基づいて if (request.method === "POST") { return new Response("POST用の応答"); } // ユーザーエージェントに基づいて const userAgent = request.headers.get("User-Agent") || ""; if (userAgent.includes("bot")) { return new Response("botを含むユーザーエージェントをブロック", { status: 403 }); } // クライアントのIPアドレスに基づいて const clientIP = request.headers.get("CF-Connecting-IP"); if (clientIP === "1.2.3.4") { return new Response("IP 1.2.3.4をブロック", { status: 403 }); } // ASNに基づいて if (request.cf && request.cf.asn == 64512) { return new Response("ASN 64512の応答をブロック"); } // デバイスタイプに基づいて // エンタープライズ「CF-Device-Type Header」ゾーン設定または // 「デバイスタイプでキャッシュ」設定が適用されたページルールが必要です。 const device = request.headers.get("CF-Device-Type"); if (device === "mobile") { return Response.redirect("https://mobile.example.com"); } console.error( "クライアントのIPアドレス、デバイスタイプ、およびASNの取得はプレイグラウンドではサポートされていません。ライブワーカーでテストする必要があります", ); return fetch(request); },};export default { async fetch(request): Promise<Response> { const BLOCKED_HOSTNAMES = ["nope.mywebsite.com", "bye.website.com"]; // URLのホスト名に基づいて新しいResponseを返します const url = new URL(request.url); if (BLOCKED_HOSTNAMES.includes(url.hostname)) { return new Response("ブロックされたホスト", { status: 403 }); } // URLのファイル拡張子に基づいて.docまたは.xmlで終わるパスをブロックします const forbiddenExtRegExp = new RegExp(/\.(doc|xml)$/); if (forbiddenExtRegExp.test(url.pathname)) { return new Response("ブロックされた拡張子", { status: 403 }); } // HTTPメソッドに基づいて if (request.method === "POST") { return new Response("POST用の応答"); } // ユーザーエージェントに基づいて const userAgent = request.headers.get("User-Agent") || ""; if (userAgent.includes("bot")) { return new Response("botを含むユーザーエージェントをブロック", { status: 403 }); } // クライアントのIPアドレスに基づいて const clientIP = request.headers.get("CF-Connecting-IP"); if (clientIP === "1.2.3.4") { return new Response("IP 1.2.3.4をブロック", { status: 403 }); } // ASNに基づいて if (request.cf && request.cf.asn == 64512) { return new Response("ASN 64512の応答をブロック"); } // デバイスタイプに基づいて // エンタープライズ「CF-Device-Type Header」ゾーン設定または // 「デバイスタイプでキャッシュ」設定が適用されたページルールが必要です。 const device = request.headers.get("CF-Device-Type"); if (device === "mobile") { return Response.redirect("https://mobile.example.com"); } console.error( "クライアントのIPアドレス、デバイスタイプ、およびASNの取得はプレイグラウンドではサポートされていません。ライブワーカーでテストする必要があります", ); return fetch(request); },} satisfies ExportedHandler;import refrom js import Response, URL, fetch
async def on_fetch(request): blocked_hostnames = ["nope.mywebsite.com", "bye.website.com"] url = URL.new(request.url)
# ホスト名に基づいてブロック if url.hostname in blocked_hostnames: return Response.new("ブロックされたホスト", status=403)
# .docまたは.xmlで終わるパスに基づいて if re.search(r'\.(doc|xml)$', url.pathname): return Response.new("ブロックされた拡張子", status=403)
# HTTPメソッドに基づいて if "POST" in request.method: return Response.new("POST用の応答")
# ユーザーエージェントに基づいて user_agent = request.headers["User-Agent"] or "" if "bot" in user_agent: return Response.new("botを含むユーザーエージェントをブロック", status=403)
# クライアントのIPアドレスに基づいて client_ip = request.headers["CF-Connecting-IP"] if client_ip == "1.2.3.4": return Response.new("IP 1.2.3.4をブロック", status=403)
# ASNに基づいて if request.cf and request.cf.asn == 64512: return Response.new("ASN 64512の応答をブロック")
# デバイスタイプに基づいて # エンタープライズ「CF-Device-Type Header」ゾーン設定または # 「デバイスタイプでキャッシュ」設定が適用されたページルールが必要です。 device = request.headers["CF-Device-Type"] if device == "mobile": return Response.redirect("https://mobile.example.com")
return fetch(request)