Explorar o código

proxying is kinda working, not returning the following pages correctly though.

AETH-erial hai 1 ano
pai
achega
1b008b21c3
Modificáronse 3 ficheiros con 54 adicións e 5 borrados
  1. 7 1
      cmd/server.go
  2. 37 4
      pkg/controller.go
  3. 10 0
      pkg/routing.go

+ 7 - 1
cmd/server.go

@@ -3,6 +3,9 @@ package main
 import (
 	"fmt"
 	"log"
+
+	httpserver "git.aetherial.dev/aeth/http-proxy/pkg"
+	"github.com/gin-gonic/gin"
 )
 
 func main() {
@@ -10,6 +13,9 @@ func main() {
 	if err != nil {
 		log.Fatal("Couldnt read config: ", err)
 	}
-	fmt.Println("%+v\n", cfg)
+	fmt.Printf("%+v\n", cfg)
+	e := gin.Default()
+	httpserver.RegisterRoutes(e, cfg)
+	e.Run(fmt.Sprintf("%s:%v", "0.0.0.0", cfg.HttpPort))
 
 }

+ 37 - 4
pkg/controller.go

@@ -1,6 +1,8 @@
 package httpserver
 
 import (
+	"fmt"
+	"io"
 	"net/http"
 
 	"github.com/gin-gonic/gin"
@@ -23,6 +25,38 @@ func NewController(cfg *HttpServerConfig) *Controller {
 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)
 
 }
 
@@ -30,7 +64,8 @@ func (c *Controller) Get(ctx *gin.Context) {
 This handler will be responsible for proxying any POST requests
 */
 func (c *Controller) Post(ctx *gin.Context) {
-	req, err := http.NewRequest()
+	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.",
@@ -40,8 +75,6 @@ func (c *Controller) Post(ctx *gin.Context) {
 	}
 	req.Header.Add("user-agent", c.Config.UserAgent)
 	req.Header.Add("Referer", c.Config.AllowedDomain)
-	req.Host = c.Config.RevProxySite
-	req.Proto = ctx.Request.Method
-	req.RequestURI = ctx.FullPath()
+	fmt.Printf("%+v\n", req)
 
 }

+ 10 - 0
pkg/routing.go

@@ -1 +1,11 @@
 package httpserver
+
+import "github.com/gin-gonic/gin"
+
+func RegisterRoutes(e *gin.Engine, cfg *HttpServerConfig) {
+	c := NewController(cfg)
+	web := e.Group("")
+	//	web.POST("/", c.Post)
+	web.Any("/*ProxiedPath", c.Get)
+
+}