package httpserver import ( "fmt" "io" "net/http" "github.com/gin-gonic/gin" ) type Controller struct { Config *HttpServerConfig } /* Returns a new Controller struct to register routes to the gin router :param cfg: A pointer to an HttpServerConfig struct */ func NewController(cfg *HttpServerConfig) *Controller { return &Controller{Config: cfg} } /* This handler will be responsible for proxying out the GET requests that the server recieves */ func (c *Controller) Get(ctx *gin.Context) { url := fmt.Sprintf("https://%s%s", c.Config.RevProxySite, ctx.Param("ProxiedPath")) req, err := http.NewRequest("GET", url, nil) if err != nil { ctx.JSON(500, map[string]string{ "Error": "Error creating the request.", "Msg:": err.Error(), }) return } req.URL.Path = ctx.Param("ProxiedPath") req.Header.Add("user-agent", c.Config.UserAgent) req.Header.Add("Referer", c.Config.AllowedDomain) // fmt.Printf("%+v\n", req) client := &http.Client{} resp, err := client.Do(req) if err != nil { ctx.JSON(500, map[string]string{ "Error": "Error creating the request.", "Msg:": err.Error(), }) return } defer resp.Body.Close() b, err := io.ReadAll(resp.Body) if err != nil { ctx.JSON(500, map[string]string{ "Error": "Error creating the request.", "Msg:": err.Error(), }) return } ctx.Data(200, "text/html", b) } /* This handler will be responsible for proxying any POST requests */ func (c *Controller) Post(ctx *gin.Context) { url := fmt.Sprintf("%s/%s", c.Config.RevProxySite, ctx.FullPath()) req, err := http.NewRequest("POST", url, ctx.Request.Body) if err != nil { ctx.JSON(500, map[string]string{ "Error": "Error creating the request.", "Msg:": err.Error(), }) return } req.Header.Add("user-agent", c.Config.UserAgent) req.Header.Add("Referer", c.Config.AllowedDomain) fmt.Printf("%+v\n", req) }