summary refs log tree commit diff
path: root/handlers/passthrough.go
diff options
context:
space:
mode:
authorMelonai <einebeere@gmail.com>2021-07-26 14:12:26 +0200
committerMelonai <einebeere@gmail.com>2021-07-26 14:12:26 +0200
commit7819a23171145e8a626e8357e88446817c8785dc (patch)
treef8834b1952583844f62c520ab0d8285b8ec2416f /handlers/passthrough.go
parent3fe27839f0418d13a42524fd10102d0ef62c05f5 (diff)
downloadportgate-7819a23171145e8a626e8357e88446817c8785dc.tar.zst
portgate-7819a23171145e8a626e8357e88446817c8785dc.zip
Refactor into packages
Diffstat (limited to 'handlers/passthrough.go')
-rw-r--r--handlers/passthrough.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/handlers/passthrough.go b/handlers/passthrough.go
new file mode 100644
index 0000000..f322d3a
--- /dev/null
+++ b/handlers/passthrough.go
@@ -0,0 +1,29 @@
+package handlers
+
+import (
+	"github.com/valyala/fasthttp"
+	"net/http"
+	"portgate"
+)
+
+// handlePassthroughRequest handles requests which are supposed to be proxied to the destination host.
+// If the user is authorized they are allowed to pass, otherwise they should be redirected to
+// the authentication page. (/_portgate)
+func (h *RequestHandler) handlePassthroughRequest(ctx *fasthttp.RequestCtx, p portgate.Path) {
+	// TODO: Check authorization.
+	// TODO: Check whether port is allowed to be accessed.
+
+	// We reuse the request given to us by the user with minor changes to route it to the
+	// destination host.
+	ctx.Request.SetRequestURI(h.config.MakeUrl(p))
+	ctx.Request.Header.Set("Host", h.config.TargetAddress(p.DestinationIdentifier))
+
+	// We pipe the response given to us by the destination host back to the user.
+	// Since it's possible that we get a redirect, we take this into account,
+	// but only allow upto 10 redirects.
+	err := h.client.DoRedirects(&ctx.Request, &ctx.Response, 10)
+	if err != nil {
+		ctx.SetStatusCode(http.StatusInternalServerError)
+		_, _ = ctx.WriteString("An error occurred.")
+	}
+}