Majority times the blockchain node urls APIs are protected with authentication, maybe from CloudFare ore some other service. But sometimes while you want to sync fast from a particular node and as part of the node software you can’t pass the authentication headers. In such cases even if you have the API key or auth token you are unable to sync your node using a trusted provider, since you might not be able to alter the node software to pass the headers.

This created a bit of a problem since syncing over the P2P network is slow and might take even a week to sync to the head of the chain. But you might need the network history in a day for some work. So in such cases you can use a proxy server to bypass the authentication and get the data from the node. So what will the proxy server do ? It will route all traffic to a url and add the authentication headers to the request and then send the response back to the node client.

Traditionally this is done using nginx, but sometimes this won’t work since you might want to receive the repsonse in the format of a application/octet-stream or some other format which is not supported by nginx. In such cases you can use a simple nodejs server to act as a proxy server. Does this middle layer bring some latency ? Yes, but it’s better than waiting for a week to sync the node.

You can set up a proxy server in nodejs using the following code :

const express = require('express');
const axios = require('axios');

const app = express();
const baseUrl = 'https://beacon.sepolia.mynodeurl.io';

// Define headers to mimic a legitimate browser request
const customHeaders = {
    'accept': 'application/octet-stream',
    'CF-Access-Client-Id': '<somekey>.access',
    'CF-Access-Client-Secret': '<someotherkey>',
};

app.use('*', async (req, res) => {
    const targetUrl = `${baseUrl}${req.originalUrl}`;
    console.log('Request:', targetUrl);
    try {
        const response = await axios.get(targetUrl, {
            timeout: 30000,
            headers: customHeaders,
            responseType: 'arraybuffer'
        });

        // Forward the response from the target server to the client
        res.status(response.status).send(response.data);
    } catch (error) {
        if (error.response) {
            // Server responded with a status other than 2xx
            console.error('Error response from target:', error.response.status, error.response.data);
            res.status(error.response.status).send(error.response.data);
        } else {
            // Other errors
            console.error('Error:', error.message);
            res.status(500).send(error.message);
        }
    }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Proxy server is running on port ${PORT}`);
});

This code is a rough example for how you can set up a proxy server in nodejs. You can modify the headers and the target url to match your requirements. This particular piece of code proxies the request to a node having cloudflare authentication, and then expects receiving the response in the format of application/octet-stream. You can modify the headers and the response type to match your requirements.

After running this proxy server locally you can pass the localhost url with the port to your node software and it can now query all the node APIs without having to deal with the authentication. This kind of a hacky fix but can be really helpful with dealing with situations where you can’t alter the node software to pass the headers, and you want to get going quickly.