summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/portgate.go (renamed from main.go)12
-rw-r--r--config.go8
-rw-r--r--handler.go82
-rw-r--r--handlers/handler.go61
-rw-r--r--handlers/passthrough.go29
-rw-r--r--handlers/portgate.go10
-rw-r--r--path.go9
7 files changed, 114 insertions, 97 deletions
diff --git a/main.go b/cmd/portgate.go
index 9870dee..afa756a 100644
--- a/main.go
+++ b/cmd/portgate.go
@@ -3,26 +3,26 @@ package main
import (
"github.com/valyala/fasthttp"
"log"
+
+ "portgate"
+ "portgate/handlers"
)
func main() {
log.Print("Starting Portgate...")
// Get global Portgate config.
- config, err := GetConfig()
+ config, err := portgate.GetConfig()
if err != nil {
log.Fatal("Failed to get Portgate config.")
}
// Create handler for requests
- handler := RequestHandler{
- config: &config,
- client: fasthttp.Client{},
- }
+ handler := handlers.NewRequestHandler(&config)
// Start to listen to the outside world.
log.Print("Listening for requests on port 8080.")
- err = fasthttp.ListenAndServe(config.PortgateAddress(), handler.handleRequest)
+ err = fasthttp.ListenAndServe(config.PortgateAddress(), handler.HandleRequest)
if err != nil {
log.Fatalf("Portgate server could not be started: %s", err)
}
diff --git a/config.go b/config.go
index cd315c0..b4e0754 100644
--- a/config.go
+++ b/config.go
@@ -1,4 +1,4 @@
-package main
+package portgate
import "fmt"
@@ -37,3 +37,9 @@ func (c *Config) PortgateAddress() string {
func (c *Config) TargetAddress(port int) string {
return fmt.Sprintf("%s:%d", c.targetHost, port)
}
+
+// MakeUrl creates the URL on the destination host that the user wants to access.
+func (c *Config) MakeUrl(p Path) string {
+ // TODO: Figure out what to do with TLS
+ return fmt.Sprintf("http://%s:%d%s", c.targetHost, p.DestinationIdentifier, p.ResourcePath)
+}
diff --git a/handler.go b/handler.go
deleted file mode 100644
index 76ea010..0000000
--- a/handler.go
+++ /dev/null
@@ -1,82 +0,0 @@
-package main
-
-import (
- "fmt"
- "github.com/valyala/fasthttp"
- "net/http"
-)
-
-// 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 *Config
- // HTTP Client for requesting resources from the destination host.
- client fasthttp.Client
-}
-
-// handleRequest handles all types of requests and delegates to more specific handlers.
-func (h *RequestHandler) handleRequest(ctx *fasthttp.RequestCtx) {
- path := 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 := 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)
- }
-}
-
-// 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 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(p.MakeUrl(h.config.targetHost))
- 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.")
- }
-}
-
-// 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.")
-}
-
-// 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/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.")
+}
diff --git a/path.go b/path.go
index 4b60dfe..3aa7816 100644
--- a/path.go
+++ b/path.go
@@ -1,7 +1,6 @@
-package main
+package portgate
import (
- "fmt"
"net/url"
"path"
"strconv"
@@ -69,9 +68,3 @@ func ParsePathFromReferer(p Path, r string) (Path, error) {
ResourcePath: p.ResourcePath,
}, nil
}
-
-// MakeUrl creates the URL on the destination host that the user wants to access.
-func (p *Path) MakeUrl(targetHost string) string {
- // TODO: Figure out what to do with TLS
- return fmt.Sprintf("http://%s:%d%s", targetHost, p.DestinationIdentifier, p.ResourcePath)
-}