From aa1caaca98c87919a80992e766abe9094c5807f0 Mon Sep 17 00:00:00 2001 From: Gianni Carafa Date: Thu, 2 Nov 2023 18:25:31 +0100 Subject: [PATCH] add api path --- README.md | 9 ++++-- cmd/main.go | 1 + handlers/api.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++ handlers/proxy.go | 2 +- 4 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 handlers/api.go diff --git a/README.md b/README.md index 1bde029..3f22c6e 100644 --- a/README.md +++ b/README.md @@ -10,18 +10,21 @@ Freedom of information is an essential pillar of democracy and informed decision-making. While media organizations have legitimate financial interests, it is crucial to strike a balance between profitability and the public's right to access information. The proliferation of paywalls raises concerns about the erosion of this fundamental freedom, and it is imperative for society to find innovative ways to preserve access to vital information without compromising the sustainability of journalism. In a world where knowledge should be shared and not commodified, paywalls should be critically examined to ensure that they do not undermine the principles of an open and informed society. -Some site might have missing images or other formating issues. This is due to the fact that the site using javascript or CSS to load the images/JS/CSS. This is a limitation of this proxy. If you prefer a full experience, please concider buying a subscription for the site. +Certain sites may display missing images or encounter formatting issues. This can be attributed to the site's reliance on JavaScript or CSS for image and resource loading, which presents a limitation when accessed through this proxy. If you prefer a full experience, please concider buying a subscription for the site. ### Features - [x] Bypass Paywalls -- [x] Remove CORS +- [x] Remove CORS Headers from Responses, Assets, and Images ... - [x] Keep Site browsable +- [x] Add a debug path +- [x] Add a API - [x] Docker Container - [x] Linux Binary - [x] Mac OS Binary - [x] Windows Binary (Untested) +- [ ] Basic Auth -## How to use +## Installation ### Binary 1) Download Binary diff --git a/cmd/main.go b/cmd/main.go index 1127fb1..1f2ad4f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,6 +20,7 @@ func main() { app.Get("/", handlers.Form) app.Get("debug/*", handlers.Debug) + app.Get("api/*", handlers.Api) app.Get("/*", handlers.ProxySite) port := os.Getenv("PORT") diff --git a/handlers/api.go b/handlers/api.go new file mode 100644 index 0000000..3e3cd78 --- /dev/null +++ b/handlers/api.go @@ -0,0 +1,73 @@ +package handlers + +import ( + "io" + "log" + "net/http" + "net/url" + + "github.com/gofiber/fiber/v2" +) + +func Api(c *fiber.Ctx) error { + // Get the url from the URL + urlQuery := c.Params("*") + + u, err := url.Parse(urlQuery) + if err != nil { + return c.SendString(err.Error()) + } + + log.Println(u.String()) + + // Fetch the site + client := &http.Client{} + req, _ := http.NewRequest("GET", u.String(), nil) + req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)") + req.Header.Set("X-Forwarded-For", "66.249.66.1") + req.Header.Set("Referer", u.String()) + req.Header.Set("Host", u.Host) + resp, err := client.Do(req) + + if err != nil { + return c.SendString(err.Error()) + } + defer resp.Body.Close() + + bodyB, err := io.ReadAll(resp.Body) + if err != nil { + log.Println("ERROR", err) + return c.SendString(err.Error()) + } + body := rewriteHtml(bodyB, u) + response := Response{ + Body: body, + } + response.Request.Headers = make([]interface{}, 0) + for k, v := range req.Header { + response.Request.Headers = append(response.Request.Headers, map[string]string{ + "key": k, + "value": v[0], + }) + } + + response.Response.Headers = make([]interface{}, 0) + for k, v := range resp.Header { + response.Response.Headers = append(response.Response.Headers, map[string]string{ + "key": k, + "value": v[0], + }) + } + + return c.JSON(response) +} + +type Response struct { + Body string `json:"body"` + Request struct { + Headers []interface{} `json:"headers"` + } `json:"request"` + Response struct { + Headers []interface{} `json:"headers"` + } `json:"response"` +} diff --git a/handlers/proxy.go b/handlers/proxy.go index 42b4ebb..6c6e74b 100644 --- a/handlers/proxy.go +++ b/handlers/proxy.go @@ -63,7 +63,7 @@ func rewriteHtml(bodyB []byte, u *url.URL) string { // scripts scriptPattern := `]*\s+)?src="(/)([^"]*)"` reScript := regexp.MustCompile(scriptPattern) - body = reScript.ReplaceAllString(body, fmt.Sprintf(`