This commit is contained in:
iacore
2024-10-05 14:21:57 +00:00
parent 07bc205d3c
commit c70f3aaa8f
9 changed files with 79 additions and 57 deletions
+1 -2
View File
@@ -107,8 +107,7 @@
<input id="app-b" type="radio" name="app" value="button" />
<label for="app-b">
Button
<small>Add a button at the bottom-right of the thumbnails and open the previewer when you click into
it</small>
<small>Add a button at the bottom-right of the thumbnails and open the previewer when you click into it</small>
</label>
<br />
<input id="app-d" type="radio" name="app" value="disable" />
+5 -5
View File
@@ -29,7 +29,7 @@ type AppAPICredentials struct {
}
// VnPower: This function must be run before any of the generators could be used.
func assertAvailablePRNG() {
func init() {
// Assert that a cryptographically secure PRNG is available.
// Panic otherwise.
buf := make([]byte, 1)
@@ -83,10 +83,10 @@ func oauth_pkce() (string, string) {
return code_verifier, code_challenge
}
func AppAPIRefresh(refresh_token string) AppAPICredentials {
func AppAPIRefresh(r *http.Request, refresh_token string) AppAPICredentials {
var credentials AppAPICredentials
var body = []byte(fmt.Sprintf(`client_id=%s&client_secret=%s&grant_type=refresh_token&include_policy=true&refresh_token=%s`, CLIENT_ID, CLIENT_SECRET, refresh_token))
req, err := http.NewRequest("POST", AUTH_TOKEN_URL, bytes.NewBuffer(body))
req, err := http.NewRequestWithContext(r.Context(), "POST", AUTH_TOKEN_URL, bytes.NewBuffer(body))
if err != nil {
panic(err)
}
@@ -110,7 +110,7 @@ func AppAPIRefresh(refresh_token string) AppAPICredentials {
return credentials
}
func AppAPILogin() AppAPICredentials {
func AppAPILogin(r *http.Request) AppAPICredentials {
var credentials AppAPICredentials
code_verifier, code_challenge := oauth_pkce()
@@ -121,7 +121,7 @@ func AppAPILogin() AppAPICredentials {
fmt.Scanln(&s)
var body = []byte(fmt.Sprintf(`client_id=%s&client_secret=%s&code=%s&code_verifier=%s&grant_type=authorization_code&include_policy=true&redirect_uri=%s`, CLIENT_ID, CLIENT_SECRET, s, code_verifier, REDIRECT_URI))
req, err := http.NewRequest("POST", AUTH_TOKEN_URL, bytes.NewBuffer(body))
req, err := http.NewRequestWithContext(r.Context(), "POST", AUTH_TOKEN_URL, bytes.NewBuffer(body))
if err != nil {
panic(err)
}
+36 -20
View File
@@ -3,33 +3,42 @@ package core
import (
"errors"
"fmt"
"log"
"sort"
"strings"
"sync"
"time"
"net/http"
"codeberg.org/vnpower/pixivfe/v2/server/session"
"github.com/goccy/go-json"
"net/http"
)
// Pixiv returns 0, 1, 2 to filter SFW and/or NSFW artworks.
// Those values are saved in `xRestrict`
// Those values are saved in `XRestrict`
// 0: Safe
// 1: R18
// 2: R18G
type xRestrict int
type XRestrict int
const (
Safe xRestrict = 0
R18 xRestrict = 1
R18G xRestrict = 2
Safe XRestrict = 0
R18 XRestrict = 1
R18G XRestrict = 2
)
var xRestrictModel = map[xRestrict]string{
Safe: "",
R18: "R18",
R18G: "R18G",
func (x XRestrict) String() string {
switch x {
case Safe:
return ""
case R18:
return "R18"
case R18G:
return "R18G"
}
log.Panicf("invalid value: %#v", int(x))
return ""
}
// Pixiv returns 0, 1, 2 to filter SFW and/or NSFW artworks.
@@ -38,18 +47,25 @@ var xRestrictModel = map[xRestrict]string{
// 1: Not AI-generated
// 2: AI-generated
type aiType int
type AiType int
const (
Unrated aiType = 0
NotAI aiType = 1
AI aiType = 2
Unrated AiType = 0
NotAI AiType = 1
AI AiType = 2
)
var aiTypeModel = map[aiType]string{
Unrated: "Unrated",
NotAI: "Not AI",
AI: "AI",
func (x AiType) String() string {
switch x {
case Unrated:
return "Unrated"
case NotAI:
return "Not AI"
case AI:
return "AI"
}
log.Panicf("invalid value: %#v", int(x))
return ""
}
type ImageResponse struct {
@@ -118,8 +134,8 @@ type Illust struct {
Views int `json:"viewCount"`
CommentDisabled int `json:"commentOff"`
SanityLevel int `json:"sl"`
XRestrict xRestrict `json:"xRestrict"`
AiType aiType `json:"aiType"`
XRestrict XRestrict `json:"xRestrict"`
AiType AiType `json:"aiType"`
BookmarkData any `json:"bookmarkData"`
Liked bool `json:"likeData"`
User UserBrief
+13 -12
View File
@@ -14,14 +14,15 @@ import (
const PixivDatetimeLayout = "2006-01-02"
func generateRequest(link, method string, body io.Reader) *http.Request {
req, err := http.NewRequest(method, link, body)
var re_lang = regexp.MustCompile(`.*\/\/.*?\/(.*?)\/`)
func generateRequest(r *http.Request, link, method string, body io.Reader) *http.Request {
req, err := http.NewRequestWithContext(r.Context(), method, link, body)
if err != nil {
panic(err)
}
r := regexp.MustCompile(`.*\/\/.*?\/(.*?)\/`)
lang := r.FindStringSubmatch(link)[1]
lang := re_lang.FindStringSubmatch(link)[1]
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0")
req.Header.Set("Connection", "keep-alive")
@@ -112,11 +113,11 @@ func parseBackgroundImage(link string) string {
return r.FindStringSubmatch(link)[1]
}
func PixivisionGetHomepage(page string, lang ...string) ([]PixivisionArticle, error) {
func PixivisionGetHomepage(r *http.Request, page string, lang ...string) ([]PixivisionArticle, error) {
var articles []PixivisionArticle
URL := generatePixivisionURL(fmt.Sprintf("?p=%s", page), lang)
req := generateRequest(URL, "GET", nil)
req := generateRequest(r, URL, "GET", nil)
resp, err := executeRequest(req)
if err != nil {
return articles, err
@@ -157,11 +158,11 @@ func PixivisionGetHomepage(page string, lang ...string) ([]PixivisionArticle, er
return articles, nil
}
func PixivisionGetTag(id string, page string, lang ...string) (PixivisionTag, error) {
func PixivisionGetTag(r *http.Request, id string, page string, lang ...string) (PixivisionTag, error) {
var tag PixivisionTag
URL := generatePixivisionURL(fmt.Sprintf("t/%s/?p=%s", id, page), lang)
req := generateRequest(URL, "GET", nil)
req := generateRequest(r, URL, "GET", nil)
resp, err := executeRequest(req)
if err != nil {
return tag, err
@@ -203,11 +204,11 @@ func PixivisionGetTag(id string, page string, lang ...string) (PixivisionTag, er
return tag, nil
}
func PixivisionGetArticle(id string, lang ...string) (PixivisionArticle, error) {
func PixivisionGetArticle(r *http.Request, id string, lang ...string) (PixivisionArticle, error) {
var article PixivisionArticle
URL := generatePixivisionURL(fmt.Sprintf("a/%s", id), lang)
req := generateRequest(URL, "GET", nil)
req := generateRequest(r, URL, "GET", nil)
resp, err := executeRequest(req)
if err != nil {
return article, err
@@ -265,11 +266,11 @@ func PixivisionGetArticle(id string, lang ...string) (PixivisionArticle, error)
return article, nil
}
func PixivisionGetCategory(id string, page string, lang ...string) (PixivisionCategory, error) {
func PixivisionGetCategory(r *http.Request, id string, page string, lang ...string) (PixivisionCategory, error) {
var category PixivisionCategory
URL := generatePixivisionURL(fmt.Sprintf("c/%s/?p=%s", id, page), lang)
req := generateRequest(URL, "GET", nil)
req := generateRequest(r, URL, "GET", nil)
resp, err := executeRequest(req)
if err != nil {
return category, err
+6 -6
View File
@@ -86,9 +86,12 @@ func retryRequest(ctx context.Context, reqFunc func(context.Context, string) (*r
start := time.Now()
resp, err := retryClient.Do(req)
end := time.Now()
// Unwrap the body here so that we could log stuff correctly
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
@@ -105,7 +108,7 @@ func retryRequest(ctx context.Context, reqFunc func(context.Context, string) (*r
EndTime: end,
})
if err == nil && resp.StatusCode == http.StatusOK {
if resp.StatusCode == http.StatusOK {
if userToken == "" && !isPost {
tokenManager.MarkTokenStatus(token, token_manager.Good)
}
@@ -115,10 +118,7 @@ func retryRequest(ctx context.Context, reqFunc func(context.Context, string) (*r
}, nil
}
lastErr = err
if err == nil {
lastErr = fmt.Errorf("HTTP status code: %d", resp.StatusCode)
}
lastErr = fmt.Errorf("HTTP status code: %d", resp.StatusCode)
if userToken == "" && !isPost {
tokenManager.MarkTokenStatus(token, token_manager.TimedOut)
+9
View File
@@ -164,3 +164,12 @@ rules:
- pattern-not-inside: |
...
defer $RESP.Body.Close()
- id: rule-13
message: "error string"
languages: [go]
severity: INFO
pattern-either:
- pattern: |
errors.New(...)
- pattern: |
fmt.Errorf(...)
+5 -6
View File
@@ -1,7 +1,6 @@
package middleware
import (
"errors"
"net/http"
)
@@ -9,19 +8,19 @@ import (
// If a panic occurs, it sends an HTTP 500 Internal Server Error response with the panic message.
func RecoverFromPanic(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
var errorString string
defer func() {
r := recover()
if r != nil {
switch t := r.(type) {
case string:
err = errors.New(t)
errorString = t
case error:
err = t
errorString = t.Error()
default:
err = errors.New("Unknown error")
errorString = "Unknown error"
}
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, errorString, http.StatusInternalServerError)
}
}()
h.ServeHTTP(w, r)
+4 -4
View File
@@ -8,7 +8,7 @@ import (
)
func PixivisionHomePage(w http.ResponseWriter, r *http.Request) error {
data, err := core.PixivisionGetHomepage("1", "en")
data, err := core.PixivisionGetHomepage(r, "1", "en")
if err != nil {
return err
}
@@ -22,7 +22,7 @@ func PixivisionHomePage(w http.ResponseWriter, r *http.Request) error {
func PixivisionArticlePage(w http.ResponseWriter, r *http.Request) error {
id := GetPathVar(r, "id")
data, err := core.PixivisionGetArticle(id, "en")
data, err := core.PixivisionGetArticle(r, id, "en")
if err != nil {
return err
}
@@ -38,7 +38,7 @@ func PixivisionArticlePage(w http.ResponseWriter, r *http.Request) error {
func PixivisionCategoryPage(w http.ResponseWriter, r *http.Request) error {
id := GetPathVar(r, "id")
data, err := core.PixivisionGetCategory(id, "1", "en")
data, err := core.PixivisionGetCategory(r, id, "1", "en")
if err != nil {
return err
}
@@ -52,7 +52,7 @@ func PixivisionCategoryPage(w http.ResponseWriter, r *http.Request) error {
func PixivisionTagPage(w http.ResponseWriter, r *http.Request) error {
id := GetPathVar(r, "id")
data, err := core.PixivisionGetTag(id, "1", "en")
data, err := core.PixivisionGetTag(r, id, "1", "en")
if err != nil {
return err
}
-2
View File
@@ -15,7 +15,6 @@ func SPximgProxy(w http.ResponseWriter, r *http.Request) error {
}
core.ProxyRequest(w, req)
return nil
}
func IPximgProxy(w http.ResponseWriter, r *http.Request) error {
@@ -27,7 +26,6 @@ func IPximgProxy(w http.ResponseWriter, r *http.Request) error {
req.Header.Add("Referer", "https://www.pixiv.net/")
core.ProxyRequest(w, req)
return nil
}
func UgoiraProxy(w http.ResponseWriter, r *http.Request) error {