Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add UpstreamHostOverride support for plugins and enhance reverse proxy handling #7289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sedkis wants to merge 1 commit into master
base: master
Choose a base branch
Loading
from feat/add-dynamic-host-rewrite-from-plugins
Open
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
2 changes: 2 additions & 0 deletions ctx/ctx.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
CacheOptions
OASDefinition
SelfLooping
// Used by plugins to override the upstream host
UpstreamHostOverride
)

func ctxSetSession(r *http.Request, s *user.SessionState, scheduleUpdate bool, hashKey bool) {
Expand Down
21 changes: 20 additions & 1 deletion gateway/reverse_proxy.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,27 @@ func (gw *Gateway) TykNewSingleHostReverseProxy(target *url.URL, spec *APISpec,
}
}

// Simple upstream host override from plugins via context.
// Accepts either a host (e.g. "api.example.com") or full URL (e.g. "https://api.example.com").
if v := req.Context().Value(ctx.UpstreamHostOverride); v != nil {
if hostOrURL, ok := v.(string); ok && hostOrURL != "" {
if strings.Contains(hostOrURL, "://") {
if u, err := url.Parse(hostOrURL); err == nil {
if u.Scheme != "" {
req.URL.Scheme = u.Scheme
}
if u.Host != "" {
req.URL.Host = u.Host
}
}
} else {
req.URL.Host = hostOrURL
}
}
}

if !spec.Proxy.PreserveHostHeader {
req.Host = targetToUse.Host
req.Host = req.URL.Host
}

if targetQuery == "" || req.URL.RawQuery == "" {
Expand Down
29 changes: 29 additions & 0 deletions test/goplugins/url_redirect_plugin.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"context"
"net/http"

"github.com/TykTechnologies/tyk/ctx"
"github.com/TykTechnologies/tyk/log"
)

var logger = log.Get()

func HeaderBasedRedirect(rw http.ResponseWriter, r *http.Request) {

Check failure on line 13 in test/goplugins/url_redirect_plugin.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unused-parameter: parameter 'rw' seems to be unused, consider removing or renaming it to match ^_ (revive)

Check failure on line 13 in test/goplugins/url_redirect_plugin.go

View workflow job for this annotation

GitHub Actions / Go 1.24.x Redis 7

unused-parameter: parameter 'rw' seems to be unused, consider removing or renaming it to match ^_ (revive)
routeTo := r.Header.Get("X-Route-To")
if routeTo == "" {
return
}

logger.Info("[PLUGIN] X-Route-To header: ", routeTo)

// Use the UpstreamHostOverride context key
// This can be either a host or a full URL
newCtx := context.WithValue(r.Context(), ctx.UpstreamHostOverride, routeTo)
*r = *r.WithContext(newCtx)

logger.Info("[PLUGIN] Set UpstreamHostOverride to: ", routeTo)
}

func main() {}
Loading

AltStyle によって変換されたページ (->オリジナル) /