Skip to content

Commit 360075a

Browse files
feat: route shared envd host through client proxy
1 parent 37dcec5 commit 360075a

4 files changed

Lines changed: 18 additions & 8 deletions

File tree

packages/client-proxy/internal/proxy/proxy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func normalizeNodeIP(nodeIP string) (string, error) {
6262
return nodeIP, nil
6363
}
6464

65-
func legacySandboxHostForEnvdRequest(host string, sandboxID string, port uint64) *string {
65+
func orchestratorSandboxHost(host string, sandboxID string, port uint64) *string {
6666
hostname := strings.Split(host, ":")[0]
6767
domain, ok := strings.CutPrefix(hostname, "envd.")
6868
if !ok || domain == "" {
@@ -143,7 +143,7 @@ func handlePausedSandbox(
143143
}
144144

145145
func NewClientProxy(meterProvider metric.MeterProvider, serviceName string, port uint16, catalog catalog.SandboxesCatalog, pausedSandboxResumer PausedSandboxResumer, featureFlagsClient *featureflags.Client) (*reverseproxy.Proxy, error) {
146-
getTargetFromRequest := reverseproxy.GetTargetFromRequest(true)
146+
getTargetFromRequest := reverseproxy.GetTargetFromRequest(reverseproxy.HeaderRoutingEnabled)
147147
proxy := reverseproxy.New(
148148
port,
149149
// Retries that are needed to handle port forwarding delays in sandbox envd are handled by the orchestrator proxy
@@ -208,7 +208,7 @@ func NewClientProxy(meterProvider metric.MeterProvider, serviceName string, port
208208
SandboxPort: port,
209209
ConnectionKey: pool.ClientProxyConnectionKey,
210210
Url: url,
211-
MaskRequestHost: legacySandboxHostForEnvdRequest(
211+
MaskRequestHost: orchestratorSandboxHost(
212212
r.Host,
213213
sandboxId,
214214
port,

packages/orchestrator/pkg/proxy/proxy.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/e2b-dev/infra/packages/orchestrator/pkg/sandbox"
1616
"github.com/e2b-dev/infra/packages/shared/pkg/connlimit"
1717
"github.com/e2b-dev/infra/packages/shared/pkg/consts"
18-
"github.com/e2b-dev/infra/packages/shared/pkg/env"
1918
"github.com/e2b-dev/infra/packages/shared/pkg/featureflags"
2019
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
2120
reverseproxy "github.com/e2b-dev/infra/packages/shared/pkg/proxy"
@@ -40,7 +39,7 @@ type SandboxProxy struct {
4039
}
4140

4241
func NewSandboxProxy(meterProvider metric.MeterProvider, port uint16, sandboxes *sandbox.Map, featureFlags *featureflags.Client) (*SandboxProxy, error) {
43-
getTargetFromRequest := reverseproxy.GetTargetFromRequest(env.IsLocal())
42+
getTargetFromRequest := reverseproxy.GetTargetFromRequest(reverseproxy.HeaderRoutingDisabled)
4443
limiter := connlimit.NewConnectionLimiter()
4544
metrics := NewMetrics(meterProvider)
4645

packages/shared/pkg/proxy/host.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ import (
99
"github.com/e2b-dev/infra/packages/shared/pkg/id"
1010
)
1111

12-
func GetTargetFromRequest(processHeaders bool) func(r *http.Request) (sandboxId string, port uint64, err error) {
12+
type HeaderRoutingMode uint8
13+
14+
const (
15+
HeaderRoutingDisabled HeaderRoutingMode = iota
16+
HeaderRoutingEnabled
17+
)
18+
19+
func GetTargetFromRequest(headerRouting HeaderRoutingMode) func(r *http.Request) (sandboxId string, port uint64, err error) {
1320
return func(r *http.Request) (sandboxId string, port uint64, err error) {
14-
if processHeaders && shouldParseHeaders(r.Host) {
21+
if headerRouting == HeaderRoutingEnabled && shouldParseHeaders(r.Host) && hasRoutingHeaders(r.Header) {
1522
var ok bool
1623
sandboxId, port, ok, err = parseHeaders(r.Header)
1724
if err != nil {
@@ -43,6 +50,10 @@ func shouldParseHeaders(host string) bool {
4350
return host == "localhost" || strings.HasPrefix(host, "envd.")
4451
}
4552

53+
func hasRoutingHeaders(h http.Header) bool {
54+
return h.Get(headerSandboxID) != "" || h.Get(headerSandboxPort) != ""
55+
}
56+
4657
func parseHost(host string) (sandboxID string, port uint64, err error) {
4758
dot := strings.Index(host, ".")
4859

packages/shared/pkg/proxy/host_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func TestGetTargetFromRequest(t *testing.T) { //nolint:tparallel // cannot call t.Setenv with t.Parallel
1111
t.Setenv("ENVIRONMENT", "local")
1212

13-
getTargetFromRequest := GetTargetFromRequest(true)
13+
getTargetFromRequest := GetTargetFromRequest(HeaderRoutingEnabled)
1414

1515
tests := []struct {
1616
name string

0 commit comments

Comments
 (0)