-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automated traffic proxying via cloudflare workers (#8751)
## Summary Automates creating a cloudflare proxy endpoint for highlight data to avoid adblockers. ## How did you test this change? https://www.loom.com/share/f126c827b57e40848545244dd149bdd8 <img width="726" alt="image" src="https://github.com/highlight/highlight/assets/1351531/2211be39-bee0-47af-b3e4-226845a37227"> <img width="1194" alt="image" src="https://github.com/highlight/highlight/assets/1351531/6d4a9360-a96f-48de-b0f3-1952298c6dde"> <img width="1229" alt="image" src="https://github.com/highlight/highlight/assets/1351531/532f4c48-9d0c-4b66-acca-b3f84858f9db"> ## Are there any deployment considerations? no ## Does this work require review from our design team? no
- Loading branch information
Showing
28 changed files
with
860 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/cloudflare/cloudflare-go" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
const ScriptName = "highlight-proxy" | ||
const Script = ` | ||
async function handleRequest(request, ctx) { | ||
const url = new URL(request.url) | ||
const pathname = url.pathname | ||
const search = url.search | ||
const pathWithParams = pathname + search | ||
return forwardRequest(request, pathWithParams) | ||
} | ||
async function forwardRequest(request, pathWithSearch) { | ||
const originRequest = new Request(request) | ||
originRequest.headers.delete("cookie") | ||
return await fetch("https://pub.highlight.io", originRequest) | ||
} | ||
export default { | ||
async fetch(request, env, ctx) { | ||
return handleRequest(request, ctx); | ||
} | ||
}; | ||
` | ||
|
||
type Client struct { | ||
api *cloudflare.API | ||
accountID, zoneID, zoneName string | ||
} | ||
|
||
func (c *Client) CreateWorker(ctx context.Context, proxySubdomain string) (string, error) { | ||
r1, err := c.api.UploadWorker(ctx, cloudflare.AccountIdentifier(c.accountID), cloudflare.CreateWorkerParams{ | ||
ScriptName: ScriptName, | ||
Script: Script, | ||
Module: true, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
log.WithField("response", r1).Info("UploadWorker") | ||
|
||
route := fmt.Sprintf("%s.%s", proxySubdomain, c.zoneName) | ||
r2, err := c.api.CreateWorkerRoute(ctx, cloudflare.ZoneIdentifier(c.zoneID), cloudflare.CreateWorkerRouteParams{ | ||
Pattern: fmt.Sprintf("%s/*", route), | ||
Script: ScriptName, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
log.WithField("response", r2).Info("CreateWorkerRoute") | ||
|
||
return route, nil | ||
} | ||
|
||
func New(ctx context.Context, apiToken string) (*Client, error) { | ||
// requires apiToken to have | ||
//account.workers_scripts.edit, zone.workers_routes.edit | ||
api, err := cloudflare.NewWithAPIToken(apiToken) | ||
if err != nil { | ||
log.WithContext(ctx).Error(err) | ||
return nil, err | ||
} | ||
|
||
accounts, r1, err := api.Accounts(ctx, cloudflare.AccountsListParams{}) | ||
if err != nil { | ||
log.WithContext(ctx).Error(err) | ||
return nil, err | ||
} | ||
log.WithField("response", r1).Info("Accounts") | ||
|
||
zones, err := api.ListZones(ctx) | ||
if err != nil { | ||
log.WithContext(ctx).Error(err) | ||
return nil, err | ||
} | ||
log.WithField("response", zones).Info("ListZones") | ||
|
||
return &Client{ | ||
api: api, | ||
accountID: accounts[0].ID, | ||
zoneID: zones[0].ID, | ||
zoneName: zones[0].Name, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.