diff options
| author | Melonai <einebeere@gmail.com> | 2021-09-10 23:40:49 +0200 |
|---|---|---|
| committer | Melonai <einebeere@gmail.com> | 2021-09-10 23:43:57 +0200 |
| commit | a32b9fe723c633cf5349bb0479d97e1f6d04445d (patch) | |
| tree | f45869da615f5efcd0b2dd8d1baade6831dd3f4f /handlers | |
| parent | 175da8f22cd791e81338fe61e6099125868cf5a0 (diff) | |
| download | portgate-a32b9fe723c633cf5349bb0479d97e1f6d04445d.tar.zst portgate-a32b9fe723c633cf5349bb0479d97e1f6d04445d.zip | |
Refactor path into destination
Diffstat (limited to 'handlers')
| -rw-r--r-- | handlers/handler.go | 33 | ||||
| -rw-r--r-- | handlers/passthrough.go | 4 | ||||
| -rw-r--r-- | handlers/portgate.go | 8 |
3 files changed, 21 insertions, 24 deletions
diff --git a/handlers/handler.go b/handlers/handler.go index d6e1b5b..7989766 100644 --- a/handlers/handler.go +++ b/handlers/handler.go @@ -42,34 +42,29 @@ func NewRequestHandler(config *portgate.Config, templates portgate.Templates) Re // 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())) + destination := portgate.DestinationFromURL(string(ctx.Path())) - if path.IsPortgatePath() { - h.handlePortgateRequest(ctx, path) + if destination.IsPortgatePath { + h.handlePortgateRequest(ctx, destination) return } - if path.DestinationIdentifier == -1 { - // We were not given a destination. + if destination.Port == 0 { + // Try to get the port from the Referer. + destination = destination.AddReferer(string(ctx.Request.Header.Referer())) - // Try to grab actual destination from Referer header. - // This can help us if the user followed an absolute link on a proxied page. - refererPath, err := portgate.ParsePathFromReferer(path, string(ctx.Request.Header.Referer())) - if err != nil || refererPath.DestinationIdentifier == -1 { - // The referer path also has no destination + // Still no port? + if destination.Port == 0 { 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) + return } - } else { - // We were given a port, so we have to pass the request through to the destination host. - h.handlePassthroughRequest(ctx, path) + portgateUrl := fmt.Sprintf("/%d%s", destination.Port, destination.Path) + ctx.Redirect(portgateUrl, http.StatusTemporaryRedirect) + return } + + h.handlePassthroughRequest(ctx, destination) } // handleUnknownRequest handles any request which could not be processed due to missing diff --git a/handlers/passthrough.go b/handlers/passthrough.go index 3f8aafc..30cc7df 100644 --- a/handlers/passthrough.go +++ b/handlers/passthrough.go @@ -9,7 +9,7 @@ import ( // 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) { +func (h *RequestHandler) handlePassthroughRequest(ctx *fasthttp.RequestCtx, p portgate.Destination) { // TODO: Check whether port is allowed to be accessed. // Check whether given cookie is ok, if not redirect to the authentication page. @@ -21,7 +21,7 @@ func (h *RequestHandler) handlePassthroughRequest(ctx *fasthttp.RequestCtx, p po // 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.SetHost(h.config.TargetAddress(p.DestinationIdentifier)) + ctx.Request.Header.SetHost(h.config.TargetAddress(p.Port)) // 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, diff --git a/handlers/portgate.go b/handlers/portgate.go index 6e002e7..2cf8e13 100644 --- a/handlers/portgate.go +++ b/handlers/portgate.go @@ -1,16 +1,18 @@ package handlers import ( - "github.com/valyala/fasthttp" "net/http" "portgate" + "strings" "time" + + "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, path portgate.Path) { - if path.IsPortgateStaticPath() { +func (h *RequestHandler) handlePortgateRequest(ctx *fasthttp.RequestCtx, destination portgate.Destination) { + if strings.HasPrefix(destination.Path, "/_portgate/static") { h.staticHandler(ctx) } else { // TODO: Implement authentication, authorization |
