diff --git a/README.md b/README.md index 5a8f5d7..44e43f8 100644 --- a/README.md +++ b/README.md @@ -71,3 +71,5 @@ http://localhost:8080/debug/https://www.google.com | --- | --- | --- | | `PORT` | Port to listen on | `8080` | | `PREFORK` | Spawn multiple server instances | `false` | +| `USER_AGENT` | User agent to emulate | `Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)` | +| `X_FORWARDED_FOR` | IP Forwarder address | `66.249.66.1` | diff --git a/docker-compose.yaml b/docker-compose.yaml index 7ada104..7eb47da 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -10,6 +10,8 @@ services: environment: - PORT=8080 - PREFORK=true + - X_FORWARDED_FOR=66.249.66.1 + - USER_AGENT=Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) #- GODEBUG=netdns=go+4 ports: - "8080:8080" diff --git a/handlers/api.go b/handlers/api.go index 3e3cd78..c196515 100644 --- a/handlers/api.go +++ b/handlers/api.go @@ -23,8 +23,8 @@ func Api(c *fiber.Ctx) error { // 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("User-Agent", UserAgent) + req.Header.Set("X-Forwarded-For", ForwardedFor) req.Header.Set("Referer", u.String()) req.Header.Set("Host", u.Host) resp, err := client.Do(req) diff --git a/handlers/proxy.go b/handlers/proxy.go index 6c6e74b..71b0e06 100644 --- a/handlers/proxy.go +++ b/handlers/proxy.go @@ -6,12 +6,16 @@ import ( "log" "net/http" "net/url" + "os" "regexp" "strings" "github.com/gofiber/fiber/v2" ) +var UserAgent = getenv("USER_AGENT", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)") +var ForwardedFor = getenv("X_FORWARDED_FOR", "66.249.66.1") + func ProxySite(c *fiber.Ctx) error { // Get the url from the URL urlQuery := c.Params("*") @@ -28,8 +32,8 @@ func ProxySite(c *fiber.Ctx) error { // 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("User-Agent", UserAgent) + req.Header.Set("X-Forwarded-For", ForwardedFor) req.Header.Set("Referer", u.String()) req.Header.Set("Host", u.Host) resp, err := client.Do(req) @@ -73,3 +77,11 @@ func rewriteHtml(bodyB []byte, u *url.URL) string { return body } + +func getenv(key, fallback string) string { + value := os.Getenv(key) + if len(value) == 0 { + return fallback + } + return value +} diff --git a/handlers/debug.go b/handlers/raw.go similarity index 82% rename from handlers/debug.go rename to handlers/raw.go index 5721ea6..7e0939c 100644 --- a/handlers/debug.go +++ b/handlers/raw.go @@ -23,8 +23,8 @@ func Raw(c *fiber.Ctx) error { // 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("User-Agent", UserAgent) + req.Header.Set("X-Forwarded-For", ForwardedFor) req.Header.Set("Referer", u.String()) req.Header.Set("Host", u.Host) resp, err := client.Do(req) diff --git a/handlers/debug.test.go b/handlers/raw.test.go similarity index 100% rename from handlers/debug.test.go rename to handlers/raw.test.go