Feature: custom image proxy server

This commit is contained in:
VnPower
2023-06-23 19:33:46 +07:00
parent fb1393c6db
commit e6bdc31442
6 changed files with 92 additions and 223 deletions
+1 -3
View File
@@ -6,9 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"pixivfe/configs"
"pixivfe/models"
"strings"
)
type PixivClient struct {
@@ -92,7 +90,7 @@ func (p *PixivClient) PixivRequest(URL string) (json.RawMessage, error) {
var response models.PixivResponse
body, err := p.TextRequest(URL)
body = strings.ReplaceAll(body, "i.pximg.net", configs.ProxyServer)
// body = strings.ReplaceAll(body, "i.pximg.net", configs.ProxyServer)
if err != nil {
return nil, err
}
+9 -9
View File
@@ -6,6 +6,7 @@ import (
"math/rand"
"regexp"
"strconv"
"strings"
)
func GetRandomColor() string {
@@ -56,17 +57,16 @@ func GetTemplateFunctions() template.FuncMap {
return n
},
"proxyImage": func(url string) string {
// if strings.Contains(url, "s.pximg.net") {
// // This subdomain didn't get proxied
// return url
// }
"proxyImage": func(url string, target string) string {
if strings.Contains(url, "s.pximg.net") {
// This subdomain didn't get proxied
return url
}
// regex := regexp.MustCompile(`.*?pximg\.net`)
// proxy := "https://" + configs.ProxyServer
regex := regexp.MustCompile(`.*?pximg\.net`)
proxy := "https://" + target
// return regex.ReplaceAllString(url, proxy)
return url
return regex.ReplaceAllString(url, proxy)
},
"parseEmojis": func(s string) template.HTML {
regex := regexp.MustCompile(`\(([^)]+)\)`)
+28 -2
View File
@@ -18,7 +18,7 @@ import (
"github.com/gofiber/template/jet/v2"
)
func setupRouter() *fiber.App {
func setup_router() *fiber.App {
// HTML templates, automatically loaded
engine := jet.New("./template", ".jet.html")
@@ -67,6 +67,32 @@ func setupRouter() *fiber.App {
return c.Next()
})
// server.Use(func(c *fiber.Ctx) error {
// sess, err := configs.Store.Get(c)
// if err != nil {
// return err
// }
// var token_string, image_string string
// token := sess.Get("token")
// if token != nil {
// token_string = token.(string)
// } else {
// token_string = configs.ProxyServer
// }
// image := sess.Get("image-proxy")
// if image != nil {
// image_string = image.(string)
// }
// c.Bind(fiber.Map{
// "Token": token_string,
// "ImageProxy": image_string,
// })
// return c.Next()
// })
// Static files
server.Static("/favicon.ico", "./template/favicon.ico")
@@ -90,7 +116,7 @@ func main() {
panic(err)
}
r := setupRouter()
r := setup_router()
if strings.Contains(configs.Port, "/") {
ln, err := net.Listen("unix", configs.Port)
+25
View File
@@ -114,6 +114,19 @@ type Illust struct {
RecentWorks []IllustShort
}
func (s *Illust) ProxyImages(proxy string) {
for i := range s.Images {
s.Images[i].Small = ProxyImage(s.Images[i].Small, proxy)
s.Images[i].Medium = ProxyImage(s.Images[i].Medium, proxy)
s.Images[i].Large = ProxyImage(s.Images[i].Large, proxy)
s.Images[i].Original = ProxyImage(s.Images[i].Original, proxy)
}
for i := range s.RecentWorks {
s.RecentWorks[i].Thumbnail = ProxyImage(s.RecentWorks[i].Thumbnail, proxy)
}
s.User.Avatar = ProxyImage(s.User.Avatar, proxy)
}
type IllustShort struct {
ID string `json:"id"`
Title string `json:"title"`
@@ -150,6 +163,12 @@ type User struct {
FrequentTags []FrequentTag
}
func (s *User) ProxyImages(proxy string) {
s.Avatar = ProxyImage(s.Avatar, proxy)
s.BackgroundImage = ProxyImage(s.BackgroundImage, proxy)
s.Artworks = ProxyShortArtworkSlice(s.Artworks, proxy)
}
type UserShort struct {
ID string `json:"userId"`
Name string `json:"name"`
@@ -188,3 +207,9 @@ type SearchResult struct {
Popular PopularArtworks `json:"popular"`
RelatedTags []string `json:"relatedTags"`
}
func (s *SearchResult) ProxyImages(proxy string) {
s.Artworks.Artworks = ProxyShortArtworkSlice(s.Artworks.Artworks, proxy)
s.Popular.Permanent = ProxyShortArtworkSlice(s.Popular.Permanent, proxy)
s.Popular.Recent = ProxyShortArtworkSlice(s.Popular.Recent, proxy)
}
+25 -21
View File
@@ -1,40 +1,44 @@
<div class="container">
<h2>Settings</h2>
<form action="/setting" method="post">
<form action="/settings/token" method="post">
<fieldset class="settings-fieldset">
<legend>Custom token</legend>
<input type="text" name="token" autocomplete="off" />
<input type="submit" />
<input type="submit" value="Apply" />
</fieldset>
</form>
<form action="/setting" method="post">
<fieldset class="settings-fieldset">
<legend>Proxy server</legend>
<input type="radio" name="image-proxy-server" id="image-proxy-server" value="i.pixiv.cat" />
<label for="image-proxy-server">i.pixiv.cat</label>
<fieldset class="settings-fieldset">
<form action="/settings/image_server" method="post">
<legend>Image proxy server</legend>
<input type="radio" name="image-proxy" id="image-proxy" value="i.pixiv.cat" />
<label for="image-proxy">i.pixiv.cat</label>
<br />
<input type="radio" name="image-proxy-server" id="image-proxy-server" value="px2.rainchan.win" />
<label for="image-proxy-server">px2.rainchan.win</label>
<input type="radio" name="image-proxy" id="image-proxy" value="px2.rainchan.win" />
<label for="image-proxy">px2.rainchan.win</label>
<br />
<input type="radio" name="image-proxy-server" id="image-proxy-server" value="px3.rainchan.win" />
<label for="image-proxy-server">px3.rainchan.win</label>
<input type="radio" name="image-proxy" id="image-proxy" value="px3.rainchan.win" />
<label for="image-proxy">px3.rainchan.win</label>
<br />
<input type="radio" name="image-proxy-server" id="image-proxy-server" value="px.s.rainchan.win" />
<label for="image-proxy-server">px.s.rainchan.win</label>
<input type="radio" name="image-proxy" id="image-proxy" value="px.s.rainchan.win" />
<label for="image-proxy">px.s.rainchan.win</label>
<br />
<input type="radio" name="image-proxy-server" id="image-proxy-server" value="sex.nyan.xyz" />
<label for="image-proxy-server">___.nyan.xyz</label>
<input type="radio" name="image-proxy" id="image-proxy" value="sex.nyan.xyz" />
<label for="image-proxy">___.nyan.xyz</label>
<br />
<input type="radio" name="image-proxy-server" id="image-proxy-server" value="pximg.wjghj.cn" />
<label for="image-proxy-server">pximg.wjghj.cn</label>
<input type="radio" name="image-proxy" id="image-proxy" value="pximg.wjghj.cn" />
<label for="image-proxy">pximg.wjghj.cn</label>
<br />
<br />
<label for="image-proxy-server">Custom image proxy server</label>
<input type="submit" value="Apply" />
</form>
<form action="/settings/image_server" method="post">
<label for="image-proxy">Custom image proxy server</label>
<br />
<input type="text" name="image-proxy-server" id="image-proxy-server" placeholder="Paste the address here..."
<input type="text" name="image-proxy" id="image-proxy" placeholder="Paste the address here..."
autocomplete="off" />
<br />
<input type="submit" value="Apply" />
</fieldset>
</form>
</form>
</fieldset>
</div>
+4 -188
View File
@@ -1,12 +1,9 @@
package views
import (
"errors"
"math"
"net/http"
"pixivfe/configs"
"pixivfe/handler"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
@@ -14,189 +11,6 @@ import (
var PC *handler.PixivClient
func artwork_page(c *fiber.Ctx) error {
id := c.Params("id")
if _, err := strconv.Atoi(id); err != nil {
return errors.New("Bad id")
}
illust, err := PC.GetArtworkByID(id)
if err != nil {
return err
}
related, err := PC.GetRelatedArtworks(id)
if err != nil {
return err
}
comments, _ := PC.GetArtworkComments(id)
// Optimize this
return c.Render("artwork", fiber.Map{
"Illust": illust,
"Related": related,
"Comments": comments,
"Title": illust.Title,
})
}
func index_page(c *fiber.Ctx) error {
// recommended, _ := handler.GetRecommendedIllust(c)
// ranking, _ := handler.GetRankingIllust(c, "day")
// spotlight := handler.GetSpotlightArticle(c)
// newest, _ := handler.GetNewestIllust(c)
// return c.Render(http.StatusOK, "index.html", fiber.Map{
// "Recommended": recommended,
// "Rankings": ranking,
// "Spotlights": spotlight,
// "Newest": newest,
// })
sess, err := configs.Store.Get(c)
if err != nil {
panic(err)
}
token := sess.Get("token")
if token != nil {
println(token.(string))
}
return c.Render("temp", fiber.Map{"Title": "Landing"})
}
func user_page(c *fiber.Ctx) error {
id := c.Params("id")
if _, err := strconv.Atoi(id); err != nil {
return err
}
category := c.Params("category", "artworks")
if !(category == "artworks" || category == "illustrations" || category == "manga" || category == "bookmarks") {
return errors.New("Invalid work category: only illustrations, manga, artworks and bookmarks are available")
}
page := c.Query("page", "1")
pageInt, _ := strconv.Atoi(page)
user, err := PC.GetUserInformation(id, category, pageInt)
if err != nil {
return err
}
var worksCount int
worksCount = user.ArtworksCount
pageLimit := math.Ceil(float64(worksCount)/30.0) + 1.0
return c.Render("user", fiber.Map{"Title": user.Name, "User": user, "Category": category, "PageLimit": int(pageLimit), "Page": pageInt})
}
func ranking_page(c *fiber.Ctx) error {
mode := c.Query("mode", "daily")
content := c.Query("content", "all")
page := c.Query("page", "1")
pageInt, _ := strconv.Atoi(page)
response, err := PC.GetRanking(mode, content, page)
if err != nil {
return err
}
return c.Render("rank", fiber.Map{
"Title": "Ranking",
"Items": response.Artworks,
"Mode": mode,
"Content": content,
"Page": pageInt})
}
func newest_artworks_page(c *fiber.Ctx) error {
worktype := c.Query("type", "illust")
r18 := c.Query("r18", "false")
works, err := PC.GetNewestArtworks(worktype, r18)
if err != nil {
return err
}
return c.Render("newest", fiber.Map{
"Items": works,
"Title": "Newest works",
})
}
func search_page(c *fiber.Ctx) error {
name := c.Params("name")
page := c.Query("page", "1")
order := c.Query("order", "date_d")
mode := c.Query("mode", "safe")
category := c.Query("category", "artworks")
tag, err := PC.GetTagData(name)
if err != nil {
return err
}
result, err := PC.GetSearch(category, name, order, mode, page)
if err != nil {
return err
}
queries := map[string]string{
"Page": page,
"Order": order,
"Mode": mode,
"Category": category,
}
return c.Render("tag", fiber.Map{"Title": "Results for " + tag.Name, "Tag": tag, "Data": result, "Queries": queries})
}
func search(c *fiber.Ctx) error {
name := c.FormValue("name")
return c.Redirect("/tags/"+name, http.StatusFound)
}
func discovery_page(c *fiber.Ctx) error {
mode := c.Query("mode", "safe")
artworks, err := PC.GetDiscoveryArtwork(mode, 100)
if err != nil {
return err
}
return c.Render("discovery", fiber.Map{"Title": "Discovery", "Artworks": artworks})
}
func settings_page(c *fiber.Ctx) error {
return c.Render("settings", fiber.Map{})
}
func settings_handler(c *fiber.Ctx) error {
sess, err := configs.Store.Get(c)
if err != nil {
panic(err)
}
token := c.FormValue("token")
if token != "" {
sess.Set("token", token)
if err := sess.Save(); err != nil {
panic(err)
}
return c.Redirect("/settings/", http.StatusAccepted)
}
return c.Redirect("/settings/", http.StatusNoContent)
}
// func not_found_page(c *fiber.Ctx) {
// return c.Render(http.StatusNotFound, "error.html", fiber.Map{
// "Title": "Not found",
@@ -236,8 +50,10 @@ func SetupRoutes(r *fiber.App) {
r.Get("discovery", discovery_page)
r.Post("tags", search)
r.Get("settings", settings_page)
r.Post("setting", settings_handler)
settings := r.Group("settings")
settings.Get("/", settings_page)
settings.Post("/token", set_token)
settings.Post("/image_server", set_image_server)
// 404 page
// r.NoRoute(not_found_page)