Blogs
...ith_do_function.md
Free and Private CORS Proxy with Digital Ocean Function

I found a cheapest and easiest way to setup your own CORS Proxy. By using Digital Ocean Functions you can setup a Golang based proxy literally for free. Digital Ocean itself providing free 3 million API call/month and beyond that will still so cheap.

Create new Namespace

Name space is like a group of a script (They call it Function). To create new function got to Functions > Create Namespace. Then choose label name (this is up to you, and can be left random), and choose nearest datacenter for better performance. Click Create Namespace.

Create new functions

Click Action > Create Function, choose Go:1.20 as a Runtime. And function name cors_proxy. Make sure the Web Function is checked. Then click Create.

The script

And paste this Golang script then click Run to save.

go
package main

import (
    "encoding/base64"
    "io"
    "net/http"
)

func Main(args map[string]interface{}) map[string]interface{} {
    url, ok := args["url"].(string)
    if !ok {
        return map[string]interface{}{
            "statusCode": 400,
            "body":      "URL parameter required",
        }
    }

    client := &http.Client{}
    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        return map[string]interface{}{
            "statusCode": 400,
            "body":      err.Error(),
        }
    }

    req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/91.0.4472.124")

    resp, err := client.Do(req)
    if err != nil {
        return map[string]interface{}{
            "statusCode": 500,
            "body":      err.Error(),
        }
    }
    defer resp.Body.Close()

    imageData, err := io.ReadAll(resp.Body)
    if err != nil {
        return map[string]interface{}{
            "statusCode": 500,
            "body":      err.Error(),
        }
    }

    return map[string]interface{}{
        "statusCode":      200,
        "body":           base64.StdEncoding.EncodeToString(imageData),
        "headers":        map[string]string{"Content-Type": resp.Header.Get("Content-Type")},
        "isBase64Encoded": true,
    }
}

Testing the proxy

Copy the URL above the code, then simply add your URL after then ?url= query like this.

plaintext
https://faas-sgp1-xxxxxx.doserverless.co/api/v1/web/fn-xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx/default/cors_proxy?url=https://huedaya.com/tweet"

Since it's run as a server-less and using compiled language like Golang, I found out it quite reliable, like about 100-200ms.

That's it!

Last update: 2025-02-07 02:56:48 UTC (2 weeks ago)