Is it possible add a custom rule to block the traffic from 6pm to 9am

What is the name of the domain?

any

What is the issue you’re encountering

I want to block the traffic to our website after office hour, from 6pm to 9am. Is it possible to do it using custom rule?

What is the current SSL/TLS setting?

Off

Firewall rules don’t include time as a condition. The only thing I can think of is to use a Worker that blocks requests outside of a UTC time range. Something like this from ChatGPT seems to work:

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
    // Get the current UTC time
    const currentDate = new Date();
    const currentHour = currentDate.getUTCHours();

    // Define the blocking hours
    const startBlockHour = 17; // 17:00 UTC
    const endBlockHour = 9;    // 09:00 UTC

    // Check if the current time falls within the blocking range
    if (currentHour >= startBlockHour || currentHour < endBlockHour) {
        return new Response('Access is restricted during this time.', {
            status: 403,
            headers: { 'Content-Type': 'text/plain' }
        });
    }

    // Allow the request to proceed if it's outside the blocking hours
    return fetch(request);
}

Or a cronjob that makes an API call to toggle a firewall rule on and off, but that would be a more complex setup. With the Worker, you’d just have to update start and end times when Daylight savings begins ends.

1 Like

thx. It works. One more question, I want to allow my office IP address to access the website in non office hour. How can I add the source IP address in worker?

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.