|
@@ -32,7 +32,6 @@ import (
|
|
"net/http"
|
|
"net/http"
|
|
"net/http/cookiejar"
|
|
"net/http/cookiejar"
|
|
"net/url"
|
|
"net/url"
|
|
- "os"
|
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
@@ -56,11 +55,9 @@ type TokenUpdate struct {
|
|
}
|
|
}
|
|
|
|
|
|
type Controller struct {
|
|
type Controller struct {
|
|
- Config *HttpServerConfig
|
|
|
|
|
|
+ Config HttpServerConfig
|
|
RouteMaps *RouteMap
|
|
RouteMaps *RouteMap
|
|
- PageMods *AllPageMods
|
|
|
|
Client *http.Client
|
|
Client *http.Client
|
|
- SiteUrl *url.URL
|
|
|
|
cache *cache.Cache
|
|
cache *cache.Cache
|
|
}
|
|
}
|
|
|
|
|
|
@@ -73,7 +70,7 @@ Returns a new Controller struct to register routes to the gin router
|
|
|
|
|
|
:param cfg: A pointer to an HttpServerConfig struct
|
|
:param cfg: A pointer to an HttpServerConfig struct
|
|
*/
|
|
*/
|
|
-func NewController(cfg *HttpServerConfig, routeMap *RouteMap) *Controller {
|
|
|
|
|
|
+func NewController(cfg HttpServerConfig, routeMap *RouteMap) *Controller {
|
|
|
|
|
|
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
|
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -81,11 +78,10 @@ func NewController(cfg *HttpServerConfig, routeMap *RouteMap) *Controller {
|
|
}
|
|
}
|
|
|
|
|
|
sessCookies := cfg.CookieJar
|
|
sessCookies := cfg.CookieJar
|
|
- domain, err := url.Parse(cfg.FullDomain)
|
|
|
|
|
|
+ domain, err := url.Parse(cfg.ProxyDomain)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
- pgMod := LoadPageMods(cfg.PageModPath)
|
|
|
|
jar.SetCookies(domain, sessCookies)
|
|
jar.SetCookies(domain, sessCookies)
|
|
|
|
|
|
var resCache *cache.Cache
|
|
var resCache *cache.Cache
|
|
@@ -96,8 +92,7 @@ func NewController(cfg *HttpServerConfig, routeMap *RouteMap) *Controller {
|
|
fmt.Printf("Starting server with resource caching DISABLED.\n")
|
|
fmt.Printf("Starting server with resource caching DISABLED.\n")
|
|
resCache = nil
|
|
resCache = nil
|
|
}
|
|
}
|
|
- return &Controller{Config: cfg, Client: &http.Client{Jar: jar, CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }},
|
|
|
|
- SiteUrl: domain, cache: resCache, RouteMaps: routeMap, PageMods: pgMod}
|
|
|
|
|
|
+ return &Controller{Config: cfg, Client: &http.Client{Jar: jar, CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }}, cache: resCache, RouteMaps: routeMap}
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
@@ -105,28 +100,6 @@ This handler will be responsible for proxying out the GET requests that the serv
|
|
*/
|
|
*/
|
|
func (c *Controller) HandleAny(ctx *gin.Context) {
|
|
func (c *Controller) HandleAny(ctx *gin.Context) {
|
|
incomingPath := ctx.Param("ProxiedPath")
|
|
incomingPath := ctx.Param("ProxiedPath")
|
|
- for idx := range c.Config.Redirects {
|
|
|
|
- if incomingPath == c.Config.Redirects[idx].From {
|
|
|
|
- ctx.Header("Location", c.Config.Redirects[idx].To)
|
|
|
|
- ctx.Status(302)
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if incomingPath == "/update" {
|
|
|
|
- if ctx.Request.Method == "POST" {
|
|
|
|
- c.UpdatePost(ctx)
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if c.Config.CustomFserve != nil {
|
|
|
|
- for idx := range c.Config.CustomFserve.Config {
|
|
|
|
- if incomingPath == c.Config.CustomFserve.Config[idx].Request {
|
|
|
|
- fmt.Print("Custom file server path hit.\n")
|
|
|
|
- ctx.Data(200, c.Config.CustomFserve.Config[idx].ContentType, c.Config.CustomFserve.Config[idx].FileData)
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
if c.Config.Caching {
|
|
if c.Config.Caching {
|
|
cacheHit := c.GetResource(incomingPath)
|
|
cacheHit := c.GetResource(incomingPath)
|
|
if cacheHit != nil {
|
|
if cacheHit != nil {
|
|
@@ -145,50 +118,22 @@ func (c *Controller) HandleAny(ctx *gin.Context) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- dname, ok := c.RouteMaps.GetMappedDomain(incomingPath)
|
|
|
|
- if ok { // below, RequestURI() returns the whole URI with the query
|
|
|
|
- data, headers, rcode, err := c.RequestGeneric(ctx.Request.Method, dname, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body)
|
|
|
|
- if err != nil {
|
|
|
|
- log.Fatal(err, " failed to route the request: ", incomingPath, " to the target domain: ", dname, " Error: ", err)
|
|
|
|
- }
|
|
|
|
- for k, v := range *headers {
|
|
|
|
- _, ok := NonmutableHeaders[k]
|
|
|
|
- if !ok {
|
|
|
|
- ctx.Header(k, v[0])
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- ctx.Header("access-control-allow-origin", c.Config.FullProxyDomain)
|
|
|
|
- ctx.Data(rcode, headers.Get("content-type"), data)
|
|
|
|
- return
|
|
|
|
|
|
+ //dname, ok := c.RouteMaps.GetMappedDomain(incomingPath)
|
|
|
|
+ //if ok { // below, RequestURI() returns the whole URI with the query
|
|
|
|
+ resp := c.RequestGeneric(ctx.Request.Method, c.Config.ProxyDomain, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body)
|
|
|
|
+ if resp.Error != nil {
|
|
|
|
+ log.Fatal(resp.Error, " failed to route the request: ", incomingPath, " to the target domain: ", ctx.Request.Host, " Error: ", resp.Error)
|
|
}
|
|
}
|
|
-
|
|
|
|
- c.TryHosts(ctx.Request.Method, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body, c.Config.KnownHosts)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/*
|
|
|
|
-This function handles the updating of cookie values, meant to be extendable down the road // TODO: Make this more configurable
|
|
|
|
-
|
|
|
|
- :param ctx: pointer to a gin Context struct
|
|
|
|
-*/
|
|
|
|
-func (c *Controller) UpdatePost(ctx *gin.Context) {
|
|
|
|
- tk := TokenUpdate{
|
|
|
|
- Code: ctx.PostForm("code"),
|
|
|
|
- Content: ctx.PostForm("content"),
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if tk.Code != c.Config.TkUpdateCode {
|
|
|
|
- ctx.JSON(401, map[string]string{
|
|
|
|
- "msg": "UNAUTHORIZED",
|
|
|
|
- })
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- err := os.WriteFile(c.Config.TokenSaveLoc, []byte(tk.Content), os.ModePerm)
|
|
|
|
- if err != nil {
|
|
|
|
- ctx.JSON(500, map[string]string{
|
|
|
|
- "Error": fmt.Sprintf("couldnt write token to disk. Error: %s", err),
|
|
|
|
- })
|
|
|
|
- return
|
|
|
|
|
|
+ for k, v := range resp.Headers {
|
|
|
|
+ _, ok := NonmutableHeaders[k]
|
|
|
|
+ if !ok {
|
|
|
|
+ ctx.Header(k, v[0])
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- ctx.String(200, "Token updated.")
|
|
|
|
|
|
+ ctx.Header("access-control-allow-origin", c.Config.ServerDomain)
|
|
|
|
+ ctx.Data(resp.StatusCode, resp.Headers.Get("content-type"), resp.Data)
|
|
|
|
+ return
|
|
|
|
+ //}
|
|
|
|
|
|
|
|
+ //c.TryHosts(ctx.Request.Method, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body, c.Config.KnownHosts)
|
|
}
|
|
}
|