diff --git a/assets/views/settings.jet.html b/assets/views/settings.jet.html
index be5afb8..957b605 100644
--- a/assets/views/settings.jet.html
+++ b/assets/views/settings.jet.html
@@ -107,8 +107,7 @@
diff --git a/core/appapi.go b/core/appapi.go
index a51887c..e328769 100644
--- a/core/appapi.go
+++ b/core/appapi.go
@@ -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)
}
diff --git a/core/artwork.go b/core/artwork.go
index b81f458..0698fb7 100644
--- a/core/artwork.go
+++ b/core/artwork.go
@@ -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
diff --git a/core/pixivision.go b/core/pixivision.go
index 959411b..06a8892 100644
--- a/core/pixivision.go
+++ b/core/pixivision.go
@@ -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
diff --git a/core/requests.go b/core/requests.go
index d6d7cef..bc2e598 100644
--- a/core/requests.go
+++ b/core/requests.go
@@ -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)
diff --git a/semgrep.yml b/semgrep.yml
index 9564a07..b2bec7f 100644
--- a/semgrep.yml
+++ b/semgrep.yml
@@ -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(...)
diff --git a/server/middleware/recover.go b/server/middleware/recover.go
index 148ae56..13e7f88 100644
--- a/server/middleware/recover.go
+++ b/server/middleware/recover.go
@@ -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)
diff --git a/server/routes/pixivision.go b/server/routes/pixivision.go
index bec7144..2ee7554 100644
--- a/server/routes/pixivision.go
+++ b/server/routes/pixivision.go
@@ -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
}
diff --git a/server/routes/proxy.go b/server/routes/proxy.go
index dfef2ad..9275d30 100644
--- a/server/routes/proxy.go
+++ b/server/routes/proxy.go
@@ -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 {