Use browser headers for image requests
This commit is contained in:
parent
5e7cf6ec52
commit
9525106ccc
|
@ -1,8 +1,6 @@
|
|||
package pages
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
@ -43,24 +41,13 @@ func handleMedia(c *fiber.Ctx, url string) error {
|
|||
url = url + "?" + strings.Split(c.OriginalURL(), "?")[1]
|
||||
}
|
||||
|
||||
optionsHash := ""
|
||||
if utils.Config.ImageCache {
|
||||
hasher := sha256.New()
|
||||
hasher.Write([]byte(url))
|
||||
optionsHash = hex.EncodeToString(hasher.Sum(nil))
|
||||
|
||||
image, err := os.ReadFile(utils.Config.CacheDir + "/" + optionsHash)
|
||||
if err == nil {
|
||||
_, err := c.Write(image)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
utils.SetReqHeaders(req)
|
||||
|
||||
if c.Get("Range") != "" {
|
||||
req.Header.Set("Range", c.Get("Range"))
|
||||
}
|
||||
|
@ -70,9 +57,11 @@ func handleMedia(c *fiber.Ctx, url string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
c.Status(res.StatusCode)
|
||||
if res.StatusCode == 404 {
|
||||
c.Status(404)
|
||||
return c.Render("errors/404", nil)
|
||||
} else if res.StatusCode == 429 {
|
||||
return c.Render("errors/429", nil)
|
||||
}
|
||||
|
||||
c.Set("Accept-Ranges", "bytes")
|
||||
|
|
|
@ -15,16 +15,7 @@ func GetJSON(url string) (gjson.Result, error) {
|
|||
return gjson.Result{}, err
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "*/*")
|
||||
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
|
||||
req.Header.Set("Cache-Control", "no-cache")
|
||||
req.Header.Set("Connection", "keep-alive")
|
||||
req.Header.Set("Origin", "https://imgur.com")
|
||||
req.Header.Set("Pragma", "no-cache")
|
||||
req.Header.Set("Sec-Fetch-Dest", "empty")
|
||||
req.Header.Set("Sec-Fetch-Mode", "cors")
|
||||
req.Header.Set("Sec-Fetch-Site", "same-site")
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0")
|
||||
SetReqHeaders(req)
|
||||
|
||||
client := http.Client{}
|
||||
res, err := client.Do(req)
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package utils
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func SetHeaders(c *fiber.Ctx) {
|
||||
c.Set("Referrer-Policy", "no-referrer")
|
||||
|
@ -9,3 +13,16 @@ func SetHeaders(c *fiber.Ctx) {
|
|||
c.Set("Strict-Transport-Security", "max-age=31557600")
|
||||
c.Set("Permissions-Policy", "accelerometer=(), ambient-light-sensor=(), autoplay=(), battery=(), camera=(), cross-origin-isolated=(), display-capture=(), document-domain=(), encrypted-media=(), execution-while-not-rendered=(), execution-while-out-of-viewport=(), fullscreen=(self), geolocation=(), gyroscope=(), interest-cohort=(), magnetometer=(), microphone=(), midi=(), navigation-override=(), payment=(), picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), web-share=(), xr-spatial-tracking=()")
|
||||
}
|
||||
|
||||
func SetReqHeaders(req *http.Request) {
|
||||
req.Header.Set("Accept", "*/*")
|
||||
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
|
||||
req.Header.Set("Cache-Control", "no-cache")
|
||||
req.Header.Set("Connection", "keep-alive")
|
||||
req.Header.Set("Origin", "https://imgur.com")
|
||||
req.Header.Set("Pragma", "no-cache")
|
||||
req.Header.Set("Sec-Fetch-Dest", "empty")
|
||||
req.Header.Set("Sec-Fetch-Mode", "cors")
|
||||
req.Header.Set("Sec-Fetch-Site", "same-site")
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0")
|
||||
}
|
Loading…
Reference in New Issue