mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
refactor proxy.go
This commit is contained in:
+29
-22
@@ -1,39 +1,46 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"codeberg.org/vnpower/pixivfe/v2/core"
|
||||
"codeberg.org/vnpower/pixivfe/v2/i18n"
|
||||
)
|
||||
|
||||
// proxyHandler is a helper function that constructs and proxies a request.
|
||||
func proxyHandler(w http.ResponseWriter, r *http.Request, baseURL string, headers map[string]string) error {
|
||||
targetURL := i18n.Sprintf("%s/%s", baseURL, r.URL.Path)
|
||||
|
||||
req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, targetURL, nil)
|
||||
if err != nil {
|
||||
return i18n.Errorf("failed to create request for %s: %w", baseURL, err)
|
||||
}
|
||||
|
||||
for key, value := range headers {
|
||||
req.Header.Add(key, value)
|
||||
}
|
||||
|
||||
if err := core.ProxyRequest(w, req); err != nil {
|
||||
return i18n.Errorf("failed to proxy request to %s: %w", baseURL, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SPximgProxy handles requests for static assets from s.pximg.net.
|
||||
func SPximgProxy(w http.ResponseWriter, r *http.Request) error {
|
||||
URL := fmt.Sprintf("https://s.pximg.net/%s", r.URL.Path)
|
||||
req, err := http.NewRequestWithContext(r.Context(), "GET", URL, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
core.ProxyRequest(w, req)
|
||||
return nil
|
||||
return proxyHandler(w, r, "https://s.pximg.net", nil)
|
||||
}
|
||||
|
||||
// IPximgProxy handles requests for image assets from i.pximg.net.
|
||||
func IPximgProxy(w http.ResponseWriter, r *http.Request) error {
|
||||
URL := fmt.Sprintf("https://i.pximg.net/%s", r.URL.Path)
|
||||
req, err := http.NewRequestWithContext(r.Context(), "GET", URL, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
headers := map[string]string{
|
||||
"Referer": "https://www.pixiv.net/",
|
||||
}
|
||||
req.Header.Add("Referer", "https://www.pixiv.net/")
|
||||
core.ProxyRequest(w, req)
|
||||
return nil
|
||||
return proxyHandler(w, r, "https://i.pximg.net", headers)
|
||||
}
|
||||
|
||||
// UgoiraProxy handles requests for video assets from ugoira.com.
|
||||
func UgoiraProxy(w http.ResponseWriter, r *http.Request) error {
|
||||
URL := fmt.Sprintf("https://ugoira.com/api/mp4/%s", r.URL.Path)
|
||||
req, err := http.NewRequestWithContext(r.Context(), "GET", URL, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
core.ProxyRequest(w, req)
|
||||
return nil
|
||||
return proxyHandler(w, r, "https://ugoira.com/api/mp4", nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user