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/handler.go | |
| parent | 175da8f22cd791e81338fe61e6099125868cf5a0 (diff) | |
| download | portgate-a32b9fe723c633cf5349bb0479d97e1f6d04445d.tar.zst portgate-a32b9fe723c633cf5349bb0479d97e1f6d04445d.zip | |
Refactor path into destination
Diffstat (limited to 'handlers/handler.go')
| -rw-r--r-- | handlers/handler.go | 33 |
1 files changed, 14 insertions, 19 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 |
