summary refs log tree commit diff
path: root/handler.go
diff options
context:
space:
mode:
authorMelonai <einebeere@gmail.com>2021-07-26 00:57:43 +0200
committerMelonai <einebeere@gmail.com>2021-07-26 00:57:43 +0200
commit3fe27839f0418d13a42524fd10102d0ef62c05f5 (patch)
treeb0bd2c2f15337c2c54a83c28e75d56cc80d48a2d /handler.go
parent309490948bea7cdfc4ba8b0b11966185fdd35aa9 (diff)
downloadportgate-3fe27839f0418d13a42524fd10102d0ef62c05f5.tar.zst
portgate-3fe27839f0418d13a42524fd10102d0ef62c05f5.zip
Guess destination from Referer header
Diffstat (limited to 'handler.go')
-rw-r--r--handler.go15
1 files changed, 13 insertions, 2 deletions
diff --git a/handler.go b/handler.go
index edb0ff2..76ea010 100644
--- a/handler.go
+++ b/handler.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"fmt"
 	"github.com/valyala/fasthttp"
 	"net/http"
 )
@@ -23,8 +24,18 @@ func (h *RequestHandler) handleRequest(ctx *fasthttp.RequestCtx) {
 		if path.ResourcePath == "/_portgate" {
 			h.handlePortgateRequest(ctx)
 		} else {
-			// TODO: Try to grab actual destination from Referer header.
-			h.handleUnknownRequest(ctx)
+			// Try to grab actual destination from Referer header.
+			refererPath, err := ParsePathFromReferer(path, string(ctx.Request.Header.Referer()))
+			if err != nil || refererPath.DestinationIdentifier == -1 {
+				// The referer path also has no destination
+				h.handleUnknownRequest(ctx)
+			} else {
+				// We found the destination from the referer path, so we
+				// redirect the user to the Portgate URL they should've requested.
+
+				portgateUrl := fmt.Sprintf("/%d%s", refererPath.DestinationIdentifier, refererPath.ResourcePath)
+				ctx.Redirect(portgateUrl, http.StatusTemporaryRedirect)
+			}
 		}
 	} else {
 		// We were given a port, so we have to pass the request through to the destination host.