فهرست منبع

more progress on the routing part of the project

svcs 1 سال پیش
والد
کامیت
c54d078637
4فایلهای تغییر یافته به همراه55 افزوده شده و 30 حذف شده
  1. 44 7
      pkg/client.go
  2. 3 19
      pkg/controller.go
  3. 7 3
      pkg/include.go
  4. 1 1
      pkg/routing.go

+ 44 - 7
pkg/client.go

@@ -41,7 +41,25 @@ Generic site call to the semrush site
 */
 func (c *Controller) RequestGeneric(method string, host string, path string, hdrs *http.Header, body io.Reader) ([]byte, *http.Header, int, error) {
 	reqUrl := fmt.Sprintf("https://%s%s", host, path)
+	if method == "POST" {
+		req, err := http.NewRequest(method, reqUrl, c.requestBodyRewrites(body))
+		if err != nil {
+			return nil, nil, 500, err
+		}
+		c.setHeaders(req, hdrs)
+		resp, err := c.Client.Do(req)
+		if err != nil {
+			return nil, nil, 500, err
+		}
+		defer resp.Body.Close()
+		b, err := io.ReadAll(resp.Body)
+		if err != nil {
+			return nil, nil, 500, err
+		}
 
+		return c.pageMod(b), &resp.Header, resp.StatusCode, nil
+
+	}
 	req, err := http.NewRequest(method, reqUrl, body)
 	if err != nil {
 		return nil, nil, 500, err
@@ -56,16 +74,16 @@ func (c *Controller) RequestGeneric(method string, host string, path string, hdr
 	if err != nil {
 		return nil, nil, 500, err
 	}
-
 	altPage := c.pageMod(b)
+	resp.Header.Set("content-length", string(len(altPage)))
 	if !strings.Contains(path, "?") {
 		if resp.StatusCode == 200 {
 			if method == "GET" {
 				c.CacheResource(path, NewCachedResource(altPage, &resp.Header, resp.StatusCode))
+				fmt.Print("\n")
 			}
 		}
 	}
-
 	return altPage, &resp.Header, resp.StatusCode, nil
 
 }
@@ -90,6 +108,24 @@ func (c *Controller) TryHosts(method string, path string, hdrs *http.Header, bod
 
 }
 
+/*
+perform any request body rewrites as per described in the pagemod config
+
+	:param data: a byte array to modify
+*/
+func (c *Controller) requestBodyRewrites(data io.Reader) io.Reader {
+	b, err := io.ReadAll(data)
+	if err != nil {
+		log.Fatal("couldnt read POST body data: ", err)
+	}
+	for idx := range c.PageMods.Content {
+		if c.PageMods.Content[idx].Target == "body" {
+			b = bytes.ReplaceAll(b, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
+		}
+	}
+	return bytes.NewReader(b)
+}
+
 /*
 Sets the request headers to whatever is defined in this private method
 
@@ -116,11 +152,12 @@ Rewrite all occurences of these values into the response body
 */
 func (c *Controller) pageMod(data []byte) []byte {
 	for idx := range c.PageMods.Content {
-		data = bytes.ReplaceAll(data, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
+		if c.PageMods.Content[idx].Target == "content" {
+			data = bytes.ReplaceAll(data, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
+
+		}
+
 	}
-	data = bytes.ReplaceAll(data, []byte(c.Config.AllowedDomain), []byte(c.Config.ProxyAddr))
-	data = bytes.ReplaceAll(data, []byte(c.Config.AltAllowedDomain), []byte(c.Config.ProxyAddr))
-	data = bytes.ReplaceAll(data, []byte("api-iam.intercom.io"), []byte(c.Config.ProxyAddr))
-	return data
 
+	return data
 }

+ 3 - 19
pkg/controller.go

@@ -92,12 +92,6 @@ This handler will be responsible for proxying out the GET requests that the serv
 */
 func (c *Controller) Get(ctx *gin.Context) {
 	incomingPath := ctx.Param("ProxiedPath")
-	if incomingPath == "/" {
-		ctx.Header("Location", "https://sem.bunnytools.shop/analytics/overview/")
-	}
-	if incomingPath == "/_compatibility/traffic/overview/" {
-		ctx.Header("Location", "https://sem.bunnytools.shop/analytics/traffic/overview/ebay.com")
-	}
 	cacheHit := c.GetResource(incomingPath)
 	if cacheHit != nil {
 		for k, v := range *cacheHit.Headers {
@@ -114,18 +108,6 @@ func (c *Controller) Get(ctx *gin.Context) {
 
 	dname, ok := c.RouteMaps.GetMappedDomain(incomingPath)
 	if ok { // below, RequestURI() returns the whole URI with the query
-		/*
-			if ctx.Request.Body != nil {
-				data, err := io.ReadAll(ctx.Request.Body)
-				if err != nil {
-					log.Fatal("error reading byte array: ", err)
-				}
-
-				data = bytes.ReplaceAll(data, []byte(c.Config.ProxyAddr), []byte("api-iam.intercom.io"))
-				ctx.Request.Body = io.NopCloser(bytes.NewReader(data))
-
-			}
-		*/
 		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)
@@ -137,8 +119,10 @@ func (c *Controller) Get(ctx *gin.Context) {
 			}
 		}
 		ctx.Header("access-control-allow-origin", c.Config.FullProxyDomain)
+		if err != nil {
+			log.Fatal(err)
+		}
 		ctx.Data(rcode, headers.Get("content-type"), data)
-		//	c.RouteMaps.ExportRouteMap()
 		return
 	}
 

+ 7 - 3
pkg/include.go

@@ -83,7 +83,7 @@ type RouteMap struct {
 type RouteMapper interface {
 	mapUriToDomain(string, string)
 	GetMappedDomain(string) (string, bool)
-	ExportRouteMaps()
+	ExportRouteMaps(string)
 }
 
 /*
@@ -123,21 +123,25 @@ func (r *RouteMap) populateRouteMaps() {
 }
 
 // Exports the cache into a JSON-friendly data structure (so that it can be written to the file system)
-func (r *RouteMap) ExportRouteMap() {
+func (r *RouteMap) ExportRouteMap(loc string) {
 	routeMapOut := &RouteMap{
 		Mappings: map[string]string{},
+		Shotgun:  map[string]string{},
 	}
 
 	cachedRoutes := r.MapCache.Items()
 	for k, v := range cachedRoutes {
 		routeMapOut.Mappings[k] = fmt.Sprint(v.Object)
 	}
+	for k, v := range r.Shotgun {
+		routeMapOut.Shotgun[k] = v
+	}
 
 	b, err := json.Marshal(routeMapOut)
 	if err != nil {
 		log.Fatal("failed to marshal struct: ", err)
 	}
-	os.WriteFile("./MadeItOutTheJug.json", b, os.ModePerm)
+	os.WriteFile(loc, b, os.ModePerm)
 
 }
 

+ 1 - 1
pkg/routing.go

@@ -46,7 +46,7 @@ func RegisterRoutes(e *gin.Engine, cfg *HttpServerConfig, rmaps *RouteMap) {
 	signal.Notify(sigChanel, os.Interrupt, syscall.SIGINT)
 	go func(core *Controller) {
 		<-sigChanel
-		c.RouteMaps.ExportRouteMap()
+		c.RouteMaps.ExportRouteMap(c.Config.RouteMapPath)
 		os.Exit(1)
 	}(c)
 	web := e.Group("")