From 4d06de3eb3e03e3519c5a0be7a26f2d7d08aaa59 Mon Sep 17 00:00:00 2001 From: VnPower Date: Mon, 22 Jan 2024 19:45:21 +0700 Subject: [PATCH] Proxy s.pximg.net #45 --- core/webapi/newest.go | 3 ++- main.go | 2 ++ pages/proxy.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 pages/proxy.go diff --git a/core/webapi/newest.go b/core/webapi/newest.go index 239b6b2..47004fb 100644 --- a/core/webapi/newest.go +++ b/core/webapi/newest.go @@ -22,7 +22,8 @@ type ArtworkBrief struct { } func ProxyImages(s, proxy string) string { - s = strings.ReplaceAll(s, `i.pximg.net`, proxy) + s = strings.ReplaceAll(s, `https:\/\/i.pximg.net`, "https://"+proxy) + s = strings.ReplaceAll(s, `https:\/\/s.pximg.net`, "/s.pximg.net") return s } diff --git a/main.go b/main.go index 34f6c66..919cfd1 100644 --- a/main.go +++ b/main.go @@ -144,6 +144,8 @@ func main() { return c.Redirect("artworks/" + c.Query("illust_id")) }) + server.Get("/s.pximg.net/*", pages.SPximgProxy) + // Listen if config.GlobalServerConfig.UnixSocket != "" { ln, err := net.Listen("unix", config.GlobalServerConfig.UnixSocket) diff --git a/pages/proxy.go b/pages/proxy.go new file mode 100644 index 0000000..48a6186 --- /dev/null +++ b/pages/proxy.go @@ -0,0 +1,30 @@ +package pages + +import ( + "fmt" + "io" + "net/http" + + "github.com/gofiber/fiber/v2" +) + +func SPximgProxy(c *fiber.Ctx) error { + URL := fmt.Sprintf("https://s.pximg.net/%s", c.Params("*")) + req, _ := http.NewRequest("GET", URL, nil) + + // Make the request + resp, err := http.DefaultClient.Do(req) + + if err != nil { + return err + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + + c.Set("Content-Type", "image/jpg, image/png, image/jpeg") + + return c.Send([]byte(body)) +}