summary refs log tree commit diff
path: root/handlers
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
parent3fe27839f0418d13a42524fd10102d0ef62c05f5 (diff)
downloadportgate-7819a23171145e8a626e8357e88446817c8785dc.tar.zst
portgate-7819a23171145e8a626e8357e88446817c8785dc.zip
Refactor into packages
Diffstat (limited to 'handlers')
-rw-r--r--handlers/handler.go61
-rw-r--r--handlers/passthrough.go29
-rw-r--r--handlers/portgate.go10
3 files changed, 100 insertions, 0 deletions
diff --git a/handlers/handler.go b/handlers/handler.go
new file mode 100644
index 0000000..6484c61
--- /dev/null
+++ b/handlers/handler.go
@@ -0,0 +1,61 @@
+package handlers
+
+import (
+	"fmt"
+	"github.com/valyala/fasthttp"
+	"net/http"
+	"portgate"
+)
+
+// RequestHandler keeps data relevant to the request handlers.
+type RequestHandler struct {
+	// Pointer to the global Portgate config, the values of which can change at runtime.
+	config *portgate.Config
+	// HTTP Client for requesting resources from the destination host.
+	client fasthttp.Client
+}
+
+func NewRequestHandler(config *portgate.Config) RequestHandler {
+	return RequestHandler{
+		config: config,
+		client: fasthttp.Client{},
+	}
+}
+
+// HandleRequest handles all types of requests and delegates to more specific handlers.
+func (h *RequestHandler) HandleRequest(ctx *fasthttp.RequestCtx) {
+	path := portgate.ParsePath(string(ctx.Path()))
+
+	if path.DestinationIdentifier == -1 {
+		// We were not given a port.
+
+		if path.ResourcePath == "/_portgate" {
+			h.handlePortgateRequest(ctx)
+		} else {
+			// Try to grab actual destination from Referer header.
+			refererPath, err := portgate.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.
+
+		h.handlePassthroughRequest(ctx, path)
+	}
+}
+
+// handleUnknownRequest handles any request which could not be processed due to missing
+// information.
+func (h *RequestHandler) handleUnknownRequest(ctx *fasthttp.RequestCtx) {
+	// TODO: Show error page
+	ctx.SetStatusCode(http.StatusNotFound)
+	_, _ = ctx.WriteString("Unknown request.")
+}
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.")
+	}
+}
diff --git a/handlers/portgate.go b/handlers/portgate.go
new file mode 100644
index 0000000..9d4f3ef
--- /dev/null
+++ b/handlers/portgate.go
@@ -0,0 +1,10 @@
+package handlers
+
+import "github.com/valyala/fasthttp"
+
+// handlePortgateRequest handles all Portgate specific request for either showing Portgate
+// specific pages or handling creation of authorization tokens.
+func (h *RequestHandler) handlePortgateRequest(ctx *fasthttp.RequestCtx) {
+	// TODO: Implement authentication, authorization
+	_, _ = ctx.WriteString("Portgate request.")
+}