Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions packages/api/internal/dns/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package dns

import (
"fmt"
"log"
"net"
"strings"
"sync"

resolver "github.com/miekg/dns"

"github.com/e2b-dev/infra/packages/shared/pkg/smap"
)

const ttl = 0

const defaultRoutingIP = "127.0.0.1"

type DNS struct {
mu sync.Mutex
records *smap.Map[string]
}

func New() *DNS {
return &DNS{
records: smap.New[string](),
}
}

func (d *DNS) Add(sandboxID, ip string) {
d.records.Insert(d.hostname(sandboxID), ip)
}

func (d *DNS) Remove(sandboxID, ip string) {
d.records.RemoveCb(d.hostname(sandboxID), func(key string, v string, exists bool) bool {
return v == ip
})
}

func (d *DNS) get(hostname string) (string, bool) {
return d.records.Get(hostname)
}

func (*DNS) hostname(sandboxID string) string {
return fmt.Sprintf("%s.", sandboxID)
}

func (d *DNS) handleDNSRequest(w resolver.ResponseWriter, r *resolver.Msg) {
m := new(resolver.Msg)
m.SetReply(r)
m.Compress = false
m.Authoritative = true

for _, q := range m.Question {
if q.Qtype == resolver.TypeA {
a := &resolver.A{
Hdr: resolver.RR_Header{
Name: q.Name,
Rrtype: resolver.TypeA,
Class: resolver.ClassINET,
Ttl: ttl,
},
}

sandboxID := strings.Split(q.Name, "-")[0]
ip, found := d.get(sandboxID)
if found {
a.A = net.ParseIP(ip).To4()
} else {
a.A = net.ParseIP(defaultRoutingIP).To4()
}

m.Answer = append(m.Answer, a)
}
}

err := w.WriteMsg(m)
if err != nil {
log.Printf("Failed to write message: %s\n", err.Error())
}
}

func (d *DNS) Start(address string, port int) error {
mux := resolver.NewServeMux()

mux.HandleFunc(".", d.handleDNSRequest)

server := resolver.Server{Addr: fmt.Sprintf("%s:%d", address, port), Net: "udp", Handler: mux}

err := server.ListenAndServe()
if err != nil {
return fmt.Errorf("failed to start DNS server: %w", err)
}

return nil
}
2 changes: 1 addition & 1 deletion packages/api/internal/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

analyticscollector "github.com/e2b-dev/infra/packages/api/internal/analytics_collector"
"github.com/e2b-dev/infra/packages/api/internal/cache/instance"
"github.com/e2b-dev/infra/packages/shared/pkg/dns"
"github.com/e2b-dev/infra/packages/api/internal/dns"
"github.com/e2b-dev/infra/packages/shared/pkg/env"
"github.com/e2b-dev/infra/packages/shared/pkg/smap"
)
Expand Down
10 changes: 9 additions & 1 deletion packages/nomad/proxies/client.conf
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ server {
location / {
if ($node_ip = "") {
# If you set any text, the header will be set to `application/octet-stream` and then browser won't be able to render the content
return 404;
return 404; # Invalid sandbox url
}


Expand All @@ -85,6 +85,14 @@ server {
}
}

# Mock for sandbox server when the sandbox is not running, 127.0.0.1 is returned by the DNS resolver
server {
listen 3003;

default_type text/plain;
return 502 'Sandbox does not exist.';
}

server {
listen 3001;
location /health {
Expand Down
2 changes: 1 addition & 1 deletion packages/orchestrator/cmd/mock-sandbox/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

"go.opentelemetry.io/otel"

"github.com/e2b-dev/infra/packages/orchestrator/internal/dns"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/network"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/template"
"github.com/e2b-dev/infra/packages/shared/pkg/dns"
"github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
"github.com/e2b-dev/infra/packages/shared/pkg/logs"
)
Expand Down
2 changes: 1 addition & 1 deletion packages/orchestrator/cmd/mock-snapshot/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"go.opentelemetry.io/otel"
"golang.org/x/sync/errgroup"

"github.com/e2b-dev/infra/packages/orchestrator/internal/dns"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/network"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/template"
"github.com/e2b-dev/infra/packages/shared/pkg/dns"
"github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
"github.com/e2b-dev/infra/packages/shared/pkg/logs"
"github.com/e2b-dev/infra/packages/shared/pkg/storage"
Expand Down
2 changes: 1 addition & 1 deletion packages/orchestrator/internal/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
"golang.org/x/mod/semver"
"golang.org/x/sys/unix"

"github.com/e2b-dev/infra/packages/orchestrator/internal/dns"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/build"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/fc"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/network"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/rootfs"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/stats"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/template"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/uffd"
"github.com/e2b-dev/infra/packages/shared/pkg/dns"
"github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
"github.com/e2b-dev/infra/packages/shared/pkg/logs"
"github.com/e2b-dev/infra/packages/shared/pkg/storage"
Expand Down
2 changes: 1 addition & 1 deletion packages/orchestrator/internal/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"

"github.com/e2b-dev/infra/packages/orchestrator/internal/dns"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/network"
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/template"
"github.com/e2b-dev/infra/packages/shared/pkg/dns"
e2bgrpc "github.com/e2b-dev/infra/packages/shared/pkg/grpc"
"github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
"github.com/e2b-dev/infra/packages/shared/pkg/smap"
Expand Down