Skip to content

Commit 37dcec5

Browse files
feat: enable shared envd host routing
1 parent ed90bd3 commit 37dcec5

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"google.golang.org/grpc/codes"
1717
"google.golang.org/grpc/status"
1818

19-
"github.com/e2b-dev/infra/packages/shared/pkg/env"
2019
"github.com/e2b-dev/infra/packages/shared/pkg/featureflags"
2120
proxygrpc "github.com/e2b-dev/infra/packages/shared/pkg/grpc/proxy"
2221
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
@@ -63,6 +62,17 @@ func normalizeNodeIP(nodeIP string) (string, error) {
6362
return nodeIP, nil
6463
}
6564

65+
func legacySandboxHostForEnvdRequest(host string, sandboxID string, port uint64) *string {
66+
hostname := strings.Split(host, ":")[0]
67+
domain, ok := strings.CutPrefix(hostname, "envd.")
68+
if !ok || domain == "" {
69+
return nil
70+
}
71+
72+
legacyHost := fmt.Sprintf("%d-%s.%s", port, sandboxID, domain)
73+
return &legacyHost
74+
}
75+
6676
func catalogResolution(ctx context.Context, sandboxId string, sandboxPort uint64, trafficAccessToken string, envdAccessToken string, c catalog.SandboxesCatalog, pausedChecker PausedSandboxResumer, featureFlags *featureflags.Client) (string, error) {
6777
s, err := c.GetSandbox(ctx, sandboxId)
6878
if err != nil {
@@ -133,7 +143,7 @@ func handlePausedSandbox(
133143
}
134144

135145
func NewClientProxy(meterProvider metric.MeterProvider, serviceName string, port uint16, catalog catalog.SandboxesCatalog, pausedSandboxResumer PausedSandboxResumer, featureFlagsClient *featureflags.Client) (*reverseproxy.Proxy, error) {
136-
getTargetFromRequest := reverseproxy.GetTargetFromRequest(env.IsLocal())
146+
getTargetFromRequest := reverseproxy.GetTargetFromRequest(true)
137147
proxy := reverseproxy.New(
138148
port,
139149
// Retries that are needed to handle port forwarding delays in sandbox envd are handled by the orchestrator proxy
@@ -198,6 +208,11 @@ func NewClientProxy(meterProvider metric.MeterProvider, serviceName string, port
198208
SandboxPort: port,
199209
ConnectionKey: pool.ClientProxyConnectionKey,
200210
Url: url,
211+
MaskRequestHost: legacySandboxHostForEnvdRequest(
212+
r.Host,
213+
sandboxId,
214+
port,
215+
),
201216
}, nil
202217
},
203218
nil,

packages/shared/pkg/proxy/host.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func GetTargetFromRequest(processHeaders bool) func(r *http.Request) (sandboxId string, port uint64, err error) {
1313
return func(r *http.Request) (sandboxId string, port uint64, err error) {
14-
if processHeaders {
14+
if processHeaders && shouldParseHeaders(r.Host) {
1515
var ok bool
1616
sandboxId, port, ok, err = parseHeaders(r.Header)
1717
if err != nil {
@@ -38,6 +38,11 @@ func GetTargetFromRequest(processHeaders bool) func(r *http.Request) (sandboxId
3838
}
3939
}
4040

41+
func shouldParseHeaders(host string) bool {
42+
host = strings.Split(host, ":")[0]
43+
return host == "localhost" || strings.HasPrefix(host, "envd.")
44+
}
45+
4146
func parseHost(host string) (sandboxID string, port uint64, err error) {
4247
dot := strings.Index(host, ".")
4348

packages/shared/pkg/proxy/host_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,26 @@ func TestGetTargetFromRequest(t *testing.T) { //nolint:tparallel // cannot call
142142
},
143143
wantErrIs: MissingHeaderError{Header: headerSandboxPort},
144144
},
145+
{
146+
name: "headers: envd shared host",
147+
host: "envd.e2b.app",
148+
headers: http.Header{
149+
headerSandboxID: []string{"isv6ril5xadwn1k9t2jye"},
150+
headerSandboxPort: []string{"49983"},
151+
},
152+
wantID: "isv6ril5xadwn1k9t2jye",
153+
wantPort: 49983,
154+
},
155+
{
156+
name: "headers: ignored on regular sandbox host",
157+
host: "49983-isv6ril5xadwn1k9t2jye.e2b.app",
158+
headers: http.Header{
159+
headerSandboxID: []string{"iother5b5aiixd410phsjv"},
160+
headerSandboxPort: []string{"3000"},
161+
},
162+
wantID: "isv6ril5xadwn1k9t2jye",
163+
wantPort: 49983,
164+
},
145165
}
146166

147167
for _, tt := range tests {

0 commit comments

Comments
 (0)