Skip to content
Closed
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ serves the worktree and `https://acme.ddev.site` goes on serving the parent.
service's `VIRTUAL_HOST` into the mkcert SAN list too — verified on v1.25.2
with no hostshift config file on disk. Everything not named there keeps going
to `web`, so the canonical site stays reachable alongside the variant.
- **Mailpit.** `https://wt-a--acme.ddev.site:8026` is the worktree's own
Mailpit, passed through unrewritten, so a test that reads mail from its base
URL works on a variant. It needs both the add-on and an image that knows
`HOSTSHIFT_PASSTHROUGH`.
- **Multiple hostnames.** A multisite needs nothing extra. If the parent
declares `additional_hostnames`, `init` derives a variant for each one.
- **Keeping the two projects apart.** `HOSTSHIFT_WEB_HOSTS` narrows `web` back
Expand Down
13 changes: 13 additions & 0 deletions cmd/hostshift/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,19 @@ func cmdProxy(args []string) (int, error) {
// No daemonising: run in the foreground and let DDEV or a supervisor own the
// lifecycle. SIGTERM drains (PLAN §5.8).
srv := &http.Server{Addr: *listen, Handler: p.Handler()}
side, err := newPassthrough(os.Getenv(passthroughEnv))
if err != nil {
return exitConfig, err
}
if side != nil {
ln, err := net.Listen("tcp", side.Addr)
if err != nil {
return exitRuntime, err
}
fmt.Fprintf(os.Stderr, "hostshift: passing through %s\n", os.Getenv(passthroughEnv))
go side.Serve(ln)
defer side.Close()
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)
go func() {
Expand Down
36 changes: 36 additions & 0 deletions cmd/hostshift/passthrough.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)

// passthroughEnv names a second listener, as LISTEN=URL, forwarded untouched.
//
// The add-on uses it for Mailpit: a variant hostname is routed to this service
// and not to web, so `<variant>:8026` has nowhere else to go. An environment
// variable rather than a flag, because the compose file is not versioned with
// the `:latest` image and an older binary has to keep starting.
const passthroughEnv = "HOSTSHIFT_PASSTHROUGH"

// newPassthrough returns nil for an empty spec.
func newPassthrough(spec string) (*http.Server, error) {
if spec == "" {
return nil, nil
}
listen, target, _ := strings.Cut(spec, "=")
up, err := url.Parse(target)
if listen == "" || err != nil || up.Host == "" {
return nil, fmt.Errorf("%s %q: want LISTEN=URL", passthroughEnv, spec)
}
return &http.Server{Addr: listen, Handler: &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(up)
// Mailpit's websocket refuses an Origin that does not match Host.
r.Out.Host = r.In.Host
},
}}, nil
}
58 changes: 58 additions & 0 deletions cmd/hostshift/passthrough_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)

func TestPassthroughKeepsHostAndBody(t *testing.T) {
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, r.Host+" "+r.URL.RequestURI()+" https://acme.ddev.site/")
}))
defer up.Close()
side, err := newPassthrough("127.0.0.1:0=" + up.URL)
if err != nil {
t.Fatal(err)
}

w := httptest.NewRecorder()
side.Handler.ServeHTTP(w, httptest.NewRequest("GET", "http://wt--acme.ddev.site:8026/api/v1/search?query=a", nil))

// Nothing mapped: the hostname arrives as asked, and the mail is as sent.
want := "wt--acme.ddev.site:8026 /api/v1/search?query=a https://acme.ddev.site/"
if got := w.Body.String(); got != want {
t.Errorf("got %q, want %q", got, want)
}
}

func TestPassthroughSpec(t *testing.T) {
if side, err := newPassthrough(""); side != nil || err != nil {
t.Errorf("unset: got %v, %v", side, err)
}
for _, bad := range []string{"0.0.0.0:8025", "=http://web:8025", "0.0.0.0:8025=web"} {
if _, err := newPassthrough(bad); err == nil {
t.Errorf("%q was accepted", bad)
}
}
}

// The ports the router sends to the service have to be the one it listens on.
func TestAddonRoutesMailpitToThePassthrough(t *testing.T) {
b, err := os.ReadFile("../../ddev/docker-compose.hostshift.yaml")
if err != nil {
t.Fatal(err)
}
for _, want := range []string{
"${DDEV_MAILPIT_PORT:-8025}:8025",
"${DDEV_MAILPIT_HTTPS_PORT:-8026}:8025",
passthroughEnv + ": 0.0.0.0:8025=http://ddev-${DDEV_SITENAME}-web:8025",
} {
if !strings.Contains(string(b), want) {
t.Errorf("the compose file lacks %q", want)
}
}
}
10 changes: 8 additions & 2 deletions ddev/docker-compose.hostshift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,16 @@ services:
VIRTUAL_HOST: ${HOSTSHIFT_VARIANTS:-}
# external:internal. hostshift listens plain on 8080 and never terminates
# TLS; DDEV's router does that, as it does for `web`.
HTTP_EXPOSE: 80:8080
HTTPS_EXPOSE: 443:8080
# The second pair on each is Mailpit: a variant hostname is routed here and
# not to web, so `<variant>:8026` is passed through to web's, as a test
# reading mail from its own base URL expects. An image older than the
# variable ignores it and the port answers 502.
HTTP_EXPOSE: 80:8080,${DDEV_MAILPIT_PORT:-8025}:8025
HTTPS_EXPOSE: 443:8080,${DDEV_MAILPIT_HTTPS_PORT:-8026}:8025
HOSTSHIFT_PASSTHROUGH: 0.0.0.0:8025=http://ddev-${DDEV_SITENAME}-web:8025
expose:
- "8080"
- "8025"
volumes:
# Read-only, and only for hostshift.yaml and .ddev/config.yaml — the two
# files §5.3's config layering reads.
Expand Down
3 changes: 3 additions & 0 deletions test/integration-ddev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ contains "the worktree's own hostname still serves the worktree" "PROJECT=worktr
# Mailpit rides web's VIRTUAL_HOST, which the add-on narrows.
code="$(curl -sk -o /dev/null -w '%{http_code}' --max-time 20 https://${tag}-wt-a.ddev.site:8026/ 2>/dev/null || true)"
[ "$code" = "200" ] && pass "mailpit still routes" || fail "mailpit still routes" "http $code"
# The variant goes to hostshift, not web, so its :8026 is a separate route.
code="$(curl -sk -o /dev/null -w '%{http_code}' --max-time 20 https://wt-a--${tag}.ddev.site:8026/api/v1/info 2>/dev/null || true)"
[ "$code" = "200" ] && pass "mailpit routes on the variant" || fail "mailpit routes on the variant" "http $code"

out="$(cd "$wt" && ddev hostshift check 2>&1)" && pass "check passes a live worktree" \
|| fail "check passes a live worktree" "$out"
Expand Down
Loading