Skip to content

Commit

Permalink
automated traffic proxying via cloudflare workers (#8751)
Browse files Browse the repository at this point in the history
## 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
Vadman97 committed Jun 14, 2024
1 parent 84bab0a commit bd3ee37
Show file tree
Hide file tree
Showing 28 changed files with 860 additions and 55 deletions.
9 changes: 5 additions & 4 deletions backend/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 14 additions & 12 deletions backend/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions backend/integrations/cloudflare/cloudflare.go
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
}
1 change: 0 additions & 1 deletion backend/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package integrations
import (
"context"
"fmt"

log "github.com/sirupsen/logrus"

"github.com/highlight-run/highlight/backend/integrations/gitlab"
Expand Down
38 changes: 17 additions & 21 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"context"
"flag"
"fmt"
"github.com/aws/smithy-go/ptr"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"html/template"
"io"
"math/rand"
Expand All @@ -17,38 +14,25 @@ import (
"sync"
"time"

"github.com/highlight-run/highlight/backend/assets"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/marketplacemetering"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
"go.opentelemetry.io/otel/trace"

ghandler "github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/andybalholm/brotli"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/marketplacemetering"
"github.com/aws/smithy-go/ptr"
"github.com/clearbit/clearbit-go/clearbit"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/httplog"
"github.com/gorilla/websocket"
golang_lru "github.com/hashicorp/golang-lru/v2"
"github.com/highlight-run/go-resthooks"
"github.com/highlight-run/workerpool"
e "github.com/pkg/errors"
"github.com/rs/cors"
"github.com/sendgrid/sendgrid-go"
log "github.com/sirupsen/logrus"
"github.com/stripe/stripe-go/v78/client"
"gorm.io/gorm"

"github.com/highlight-run/highlight/backend/assets"
"github.com/highlight-run/highlight/backend/clickhouse"
dd "github.com/highlight-run/highlight/backend/datadog"
"github.com/highlight-run/highlight/backend/embeddings"

"github.com/highlight-run/highlight/backend/clickhouse"
highlightHttp "github.com/highlight-run/highlight/backend/http"
"github.com/highlight-run/highlight/backend/integrations"
kafkaqueue "github.com/highlight-run/highlight/backend/kafka-queue"
Expand All @@ -69,10 +53,22 @@ import (
"github.com/highlight-run/highlight/backend/vercel"
"github.com/highlight-run/highlight/backend/worker"
"github.com/highlight-run/highlight/backend/zapier"
"github.com/highlight-run/workerpool"
"github.com/highlight/highlight/sdk/highlight-go"
hlog "github.com/highlight/highlight/sdk/highlight-go/log"
highlightChi "github.com/highlight/highlight/sdk/highlight-go/middleware/chi"
htrace "github.com/highlight/highlight/sdk/highlight-go/trace"
e "github.com/pkg/errors"
"github.com/rs/cors"
"github.com/sendgrid/sendgrid-go"
log "github.com/sirupsen/logrus"
"github.com/stripe/stripe-go/v78/client"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
"go.opentelemetry.io/otel/trace"
"gorm.io/gorm"

_ "github.com/urfave/cli/v2"
_ "gorm.io/gorm"
Expand Down
1 change: 1 addition & 0 deletions backend/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ type Workspace struct {
LinearAccessToken *string
VercelAccessToken *string
VercelTeamID *string
CloudflareProxy *string
Projects []Project
MigratedFromProjectID *int // Column can be removed after migration is done
HubspotCompanyID *int
Expand Down
Loading

0 comments on commit bd3ee37

Please sign in to comment.