diff options
Diffstat (limited to 'api/handlers/reverseProxy.go')
| -rw-r--r-- | api/handlers/reverseProxy.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/api/handlers/reverseProxy.go b/api/handlers/reverseProxy.go new file mode 100644 index 0000000..4ac4c87 --- /dev/null +++ b/api/handlers/reverseProxy.go @@ -0,0 +1,35 @@ +package handlers + +import ( + "fmt" + "net/http" + "net/http/httputil" + "net/url" + "strings" + "time" + + "github.com/LMBishop/gunnel/pkg/store" +) + +func ReverseProxy(storeService store.Service) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + hostParts := strings.Split(r.Host, ".") + + slug := hostParts[0] + rule := storeService.GetRuleBySlug(slug) + if rule == nil { + http.Error(w, fmt.Sprintf("Unknown peer '%s'", slug), http.StatusNotFound) + return + } + + targetURL, err := url.Parse("http://" + rule.Peer.IPAddr.String() + ":" + rule.Port) + rule.LastUsed = time.Now() + if err != nil { + http.Error(w, "Invalid target URL", http.StatusInternalServerError) + return + } + + proxy := httputil.NewSingleHostReverseProxy(targetURL) + proxy.ServeHTTP(w, r) + } +} |
