mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
clean up requests, move everything into core/requests.go
This commit is contained in:
+6
-2
@@ -39,13 +39,17 @@ func GetRanking(r *http.Request, mode, content, date, page string) (Ranking, err
|
||||
|
||||
var ranking Ranking
|
||||
|
||||
resp := WebAPIRequest(r.Context(), URL, "")
|
||||
resp, err := PixivGetRequest(r.Context(), URL, "")
|
||||
if err != nil {
|
||||
return ranking, err
|
||||
}
|
||||
|
||||
if !resp.Ok {
|
||||
return ranking, errors.New(resp.Message)
|
||||
}
|
||||
proxiedResp := session.ProxyImageUrl(r, resp.Body)
|
||||
|
||||
err := json.Unmarshal([]byte(proxiedResp), &ranking)
|
||||
err = json.Unmarshal([]byte(proxiedResp), &ranking)
|
||||
if err != nil {
|
||||
return ranking, err
|
||||
}
|
||||
|
||||
+5
-18
@@ -3,14 +3,13 @@ package core
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"github.com/andybalholm/cascadia"
|
||||
|
||||
"codeberg.org/vnpower/pixivfe/v2/session"
|
||||
"codeberg.org/vnpower/pixivfe/v2/utils"
|
||||
"golang.org/x/net/html"
|
||||
|
||||
"codeberg.org/vnpower/pixivfe/v2/session"
|
||||
)
|
||||
|
||||
func get_weekday(n time.Weekday) int {
|
||||
@@ -42,25 +41,13 @@ func GetRankingCalendar(r *http.Request, mode string, year, month int) (HTML, er
|
||||
token := session.GetPixivToken(r)
|
||||
URL := GetRankingCalendarURL(mode, year, month)
|
||||
|
||||
req, err := http.NewRequestWithContext(r.Context(), "GET", URL, nil)
|
||||
if err != nil {
|
||||
return HTML(""), err
|
||||
}
|
||||
|
||||
req.Header.Add("User-Agent", "Mozilla/5.0")
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "PHPSESSID",
|
||||
Value: token,
|
||||
})
|
||||
|
||||
resp, err := utils.HttpClient.Do(req)
|
||||
resp, err := PixivGetRequest(r.Context(), URL, token)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Use the html package to parse the response body from the request
|
||||
doc, err := html.Parse(resp.Body)
|
||||
doc, err := html.Parse(strings.NewReader(resp.Body))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
+89
-31
@@ -1,6 +1,7 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -16,11 +17,9 @@ import (
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type HttpResponse struct {
|
||||
type SimpleHTTPResponse struct {
|
||||
Ok bool
|
||||
StatusCode int
|
||||
|
||||
// @iacore: this not being []byte might come back to bite us
|
||||
Body string
|
||||
Message string
|
||||
}
|
||||
@@ -45,8 +44,12 @@ func logResponseBody(body string) (string, error) {
|
||||
return filename, nil
|
||||
}
|
||||
|
||||
func WebAPIRequest(context context.Context, URL, token string) HttpResponse {
|
||||
resp := webAPIRequest(context, URL, token)
|
||||
// send GET
|
||||
func PixivGetRequest(context context.Context, URL, token string) (SimpleHTTPResponse, error) {
|
||||
resp, err := _PixivGETRequest(context, URL, token)
|
||||
if err != nil {
|
||||
return SimpleHTTPResponse{}, fmt.Errorf("While sending request to %s: %w", URL, err)
|
||||
}
|
||||
if config.GlobalServerConfig.InDevelopment {
|
||||
if resp.Ok {
|
||||
filename, err := logResponseBody(resp.Body)
|
||||
@@ -61,18 +64,13 @@ func WebAPIRequest(context context.Context, URL, token string) HttpResponse {
|
||||
log.Println("->", URL, "ERR", resp.Message)
|
||||
}
|
||||
}
|
||||
return resp
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func webAPIRequest(context context.Context, URL, token string) HttpResponse {
|
||||
func _PixivGETRequest(context context.Context, URL, token string) (SimpleHTTPResponse, error) {
|
||||
req, err := http.NewRequestWithContext(context, "GET", URL, nil)
|
||||
if err != nil {
|
||||
return HttpResponse{
|
||||
Ok: false,
|
||||
StatusCode: 0,
|
||||
Body: "",
|
||||
Message: fmt.Sprintf("Failed to create a request to %s\n.", URL),
|
||||
}
|
||||
return SimpleHTTPResponse{}, err
|
||||
}
|
||||
|
||||
req.Header.Add("User-Agent", config.GlobalServerConfig.UserAgent)
|
||||
@@ -93,37 +91,30 @@ func webAPIRequest(context context.Context, URL, token string) HttpResponse {
|
||||
// Make the request
|
||||
resp, err := utils.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return HttpResponse{
|
||||
Ok: false,
|
||||
StatusCode: 0,
|
||||
Body: "",
|
||||
Message: fmt.Sprintf("Failed to send a request to %s\n.", URL),
|
||||
}
|
||||
return SimpleHTTPResponse{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return HttpResponse{
|
||||
Ok: false,
|
||||
StatusCode: 0,
|
||||
Body: "",
|
||||
Message: fmt.Sprintln("Failed to parse request data."),
|
||||
}
|
||||
return SimpleHTTPResponse{}, err
|
||||
}
|
||||
|
||||
resp2 := HttpResponse{
|
||||
resp2 := SimpleHTTPResponse{
|
||||
Ok: true,
|
||||
StatusCode: resp.StatusCode,
|
||||
Body: string(body),
|
||||
Message: "",
|
||||
}
|
||||
|
||||
return resp2
|
||||
return resp2, nil
|
||||
}
|
||||
|
||||
func UnwrapWebAPIRequest(context context.Context, URL, token string) (string, error) {
|
||||
resp := WebAPIRequest(context, URL, token)
|
||||
resp, err := PixivGetRequest(context, URL, token)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !resp.Ok {
|
||||
return "", errors.New(resp.Message)
|
||||
@@ -132,15 +123,82 @@ func UnwrapWebAPIRequest(context context.Context, URL, token string) (string, er
|
||||
return "", fmt.Errorf("Invalid JSON: %v", resp.Body)
|
||||
}
|
||||
|
||||
err := gjson.Get(resp.Body, "error")
|
||||
err2 := gjson.Get(resp.Body, "error")
|
||||
|
||||
if !err.Exists() {
|
||||
if !err2.Exists() {
|
||||
return "", errors.New("Incompatible request body")
|
||||
}
|
||||
|
||||
if err.Bool() {
|
||||
if err2.Bool() {
|
||||
return "", errors.New(gjson.Get(resp.Body, "message").String())
|
||||
}
|
||||
|
||||
return gjson.Get(resp.Body, "body").String(), nil
|
||||
}
|
||||
|
||||
// send POST
|
||||
func PixivPostRequest(r *http.Request, url, payload, token, csrf string, isJSON bool) error {
|
||||
requestBody := []byte(payload)
|
||||
|
||||
req, err := http.NewRequestWithContext(r.Context(), "POST", url, bytes.NewBuffer(requestBody))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("User-Agent", "Mozilla/5.0")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("x-csrf-token", csrf)
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "PHPSESSID",
|
||||
Value: token,
|
||||
})
|
||||
|
||||
if isJSON {
|
||||
req.Header.Add("Content-Type", "application/json; charset=utf-8")
|
||||
} else {
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
|
||||
}
|
||||
|
||||
resp, err := utils.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return errors.New("Failed to do this action.")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return errors.New("Cannot parse the response from Pixiv. Please report this issue.")
|
||||
}
|
||||
body_s := string(body)
|
||||
if !gjson.Valid(body_s) {
|
||||
return fmt.Errorf("Invalid JSON: %v", body_s)
|
||||
}
|
||||
errr := gjson.Get(body_s, "error")
|
||||
|
||||
if !errr.Exists() {
|
||||
return errors.New("Incompatible request body.")
|
||||
}
|
||||
|
||||
if errr.Bool() {
|
||||
return errors.New("Pixiv: Invalid request.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ProxyRequest(w http.ResponseWriter, req *http.Request) error {
|
||||
// Make the request
|
||||
resp, err := utils.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// copy headers
|
||||
header := w.Header()
|
||||
for k, v := range resp.Header {
|
||||
header[k] = v
|
||||
}
|
||||
|
||||
// copy body
|
||||
_, err = io.Copy(w, resp.Body)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ require (
|
||||
codeberg.org/vnpower/pixivision v0.1.1
|
||||
github.com/CloudyKit/jet/v6 v6.2.0
|
||||
github.com/andybalholm/cascadia v1.3.2
|
||||
github.com/go-chi/httprate v0.14.0
|
||||
github.com/go-faker/faker/v4 v4.5.0
|
||||
github.com/goccy/go-json v0.10.3
|
||||
github.com/gorilla/mux v1.8.1
|
||||
@@ -19,39 +18,13 @@ require (
|
||||
require (
|
||||
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect
|
||||
github.com/PuerkitoBio/goquery v1.9.2 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/creack/pty v1.1.23 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
|
||||
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
|
||||
github.com/go-stack/stack v1.8.1 // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/h2non/filetype v1.1.3 // indirect
|
||||
github.com/kr/pretty v0.3.1 // indirect
|
||||
github.com/kr/pty v1.1.8 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/mitchellh/go-ps v1.0.0 // indirect
|
||||
github.com/orisano/pixelmatch v0.0.0-20230914042517-fa304d1dc785 // indirect
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/rogpeppe/go-internal v1.12.0 // indirect
|
||||
github.com/stretchr/objx v0.5.2 // indirect
|
||||
github.com/stretchr/testify v1.9.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.1 // indirect
|
||||
github.com/yuin/goldmark v1.7.4 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/crypto v0.26.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect
|
||||
golang.org/x/mod v0.20.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.24.0 // indirect
|
||||
golang.org/x/telemetry v0.0.0-20240828005417-e553cd4b65f1 // indirect
|
||||
golang.org/x/term v0.23.0 // indirect
|
||||
golang.org/x/text v0.17.0 // indirect
|
||||
golang.org/x/tools v0.24.0 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@@ -8,21 +8,11 @@ github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4
|
||||
github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk=
|
||||
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
|
||||
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/creack/pty v1.1.23 h1:4M6+isWdcStXEf15G/RbrMPOQj1dZ7HPZCGwE4kOeP0=
|
||||
github.com/creack/pty v1.1.23/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
|
||||
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
|
||||
github.com/go-chi/httprate v0.14.0 h1:c8szLJc+Gn+1EC1jjv3q88Om4a9USAqU9lL8wQFVX2M=
|
||||
github.com/go-chi/httprate v0.14.0/go.mod h1:TUepLXaz/pCjmCtf/obgOQJ2Sz6rC8fSf5cAt5cnTt0=
|
||||
github.com/go-faker/faker/v4 v4.4.2 h1:96WeU9QKEqRUVYdjHquY2/5bAqmVM0IfGKHV5mbfqmQ=
|
||||
github.com/go-faker/faker/v4 v4.4.2/go.mod h1:4K3v4AbKXYNHMQNaREMc9/kRB9j5JJzpFo6KHRvrcIw=
|
||||
github.com/go-faker/faker/v4 v4.5.0 h1:ARzAY2XoOL9tOUK+KSecUQzyXQsUaZHefjyF8x6YFHc=
|
||||
github.com/go-faker/faker/v4 v4.5.0/go.mod h1:p3oq1GRjG2PZ7yqeFFfQI20Xm61DoBDlCA8RiSyZ48M=
|
||||
github.com/go-jose/go-jose/v3 v3.0.3 h1:fFKWeig/irsp7XD2zBxvnmA/XaRWp5V3CBsZXJF7G7k=
|
||||
@@ -32,42 +22,16 @@ github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP
|
||||
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
||||
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
|
||||
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI=
|
||||
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
|
||||
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
|
||||
github.com/orisano/pixelmatch v0.0.0-20230914042517-fa304d1dc785 h1:J1//5K/6QF10cZ59zLcVNFGmBfiSrH8Cho/lNrViK9s=
|
||||
github.com/orisano/pixelmatch v0.0.0-20230914042517-fa304d1dc785/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/playwright-community/playwright-go v0.4501.1 h1:kz8SIfR6nEI8blk77nTVD0K5/i37QP5rY/o8a1fG+4c=
|
||||
github.com/playwright-community/playwright-go v0.4501.1/go.mod h1:bpArn5TqNzmP0jroCgw4poSOG9gSeQg490iLqWAaa7w=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
||||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
|
||||
@@ -78,23 +42,15 @@ github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhso
|
||||
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
||||
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
|
||||
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
|
||||
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
|
||||
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
|
||||
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
@@ -106,8 +62,6 @@ golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
@@ -117,18 +71,12 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
|
||||
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/telemetry v0.0.0-20240828005417-e553cd4b65f1 h1:7uSb+CIaUqSL5+OXe5BKwrxIrf0FegtoD19b3rhGaJI=
|
||||
golang.org/x/telemetry v0.0.0-20240828005417-e553cd4b65f1/go.mod h1:m7R/r+o5h7UvF2JD9n2iLSGY4v8v+zNSyTJ6xynLrqs=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
|
||||
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
@@ -143,14 +91,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
|
||||
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9 h1:LLhsEBxRTBLuKlQxFBYUOU8xyFgXv6cOTp2HASDlsDk=
|
||||
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
+4
-53
@@ -1,64 +1,15 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"codeberg.org/vnpower/pixivfe/v2/core"
|
||||
"codeberg.org/vnpower/pixivfe/v2/session"
|
||||
"codeberg.org/vnpower/pixivfe/v2/utils"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func pixivPostRequest(r *http.Request, url, payload, token, csrf string, isJSON bool) error {
|
||||
requestBody := []byte(payload)
|
||||
|
||||
req, err := http.NewRequestWithContext(r.Context(), "POST", url, bytes.NewBuffer(requestBody))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("User-Agent", "Mozilla/5.0")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("x-csrf-token", csrf)
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "PHPSESSID",
|
||||
Value: token,
|
||||
})
|
||||
|
||||
if isJSON {
|
||||
req.Header.Add("Content-Type", "application/json; charset=utf-8")
|
||||
} else {
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
|
||||
}
|
||||
|
||||
resp, err := utils.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return errors.New("Failed to do this action.")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return errors.New("Cannot parse the response from Pixiv. Please report this issue.")
|
||||
}
|
||||
body_s := string(body)
|
||||
if !gjson.Valid(body_s) {
|
||||
return fmt.Errorf("Invalid JSON: %v", body_s)
|
||||
}
|
||||
errr := gjson.Get(body_s, "error")
|
||||
|
||||
if !errr.Exists() {
|
||||
return errors.New("Incompatible request body.")
|
||||
}
|
||||
|
||||
if errr.Bool() {
|
||||
return errors.New("Pixiv: Invalid request.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
|
||||
token := session.GetPixivToken(r)
|
||||
csrf := session.GetCookie(r, session.Cookie_CSRF)
|
||||
@@ -79,7 +30,7 @@ func AddBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
|
||||
"comment": "",
|
||||
"tags": []
|
||||
}`, id)
|
||||
if err := pixivPostRequest(r, URL, payload, token, csrf, true); err != nil {
|
||||
if err := core.PixivPostRequest(r, URL, payload, token, csrf, true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -103,7 +54,7 @@ func DeleteBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
|
||||
// You can't unlike
|
||||
URL := "https://www.pixiv.net/ajax/illusts/bookmarks/delete"
|
||||
payload := fmt.Sprintf(`bookmark_id=%s`, id)
|
||||
if err := pixivPostRequest(r, URL, payload, token, csrf, false); err != nil {
|
||||
if err := core.PixivPostRequest(r, URL, payload, token, csrf, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -126,7 +77,7 @@ func LikeRoute(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
URL := "https://www.pixiv.net/ajax/illusts/like"
|
||||
payload := fmt.Sprintf(`{"illust_id": "%s"}`, id)
|
||||
if err := pixivPostRequest(r, URL, payload, token, csrf, true); err != nil {
|
||||
if err := core.PixivPostRequest(r, URL, payload, token, csrf, true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
+4
-24
@@ -2,38 +2,18 @@ package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"codeberg.org/vnpower/pixivfe/v2/utils"
|
||||
"codeberg.org/vnpower/pixivfe/v2/core"
|
||||
)
|
||||
|
||||
func copyRequest(w http.ResponseWriter, req *http.Request) error {
|
||||
// Make the request
|
||||
resp, err := utils.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// copy headers
|
||||
header := w.Header()
|
||||
for k, v := range resp.Header {
|
||||
header[k] = v
|
||||
}
|
||||
|
||||
// copy body
|
||||
_, err = io.Copy(w, resp.Body)
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
return copyRequest(w, req)
|
||||
return core.ProxyRequest(w, req)
|
||||
}
|
||||
|
||||
func IPximgProxy(w http.ResponseWriter, r *http.Request) error {
|
||||
@@ -43,7 +23,7 @@ func IPximgProxy(w http.ResponseWriter, r *http.Request) error {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("Referer", "https://www.pixiv.net/")
|
||||
return copyRequest(w, req)
|
||||
return core.ProxyRequest(w, req)
|
||||
}
|
||||
|
||||
func UgoiraProxy(w http.ResponseWriter, r *http.Request) error {
|
||||
@@ -52,5 +32,5 @@ func UgoiraProxy(w http.ResponseWriter, r *http.Request) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return copyRequest(w, req)
|
||||
return core.ProxyRequest(w, req)
|
||||
}
|
||||
|
||||
+3
-16
@@ -27,30 +27,17 @@ func setToken(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
// Make a test request to verify the token.
|
||||
// THE TEST URL IS NSFW!
|
||||
req, err := http.NewRequestWithContext(r.Context(), "GET", "https://www.pixiv.net/en/artworks/115365120", nil)
|
||||
resp, err := core.PixivGetRequest(r.Context(), "https://www.pixiv.net/en/artworks/115365120", token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("User-Agent", "Mozilla/5.0")
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "PHPSESSID",
|
||||
Value: token,
|
||||
})
|
||||
|
||||
resp, err := utils.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
if !resp.Ok {
|
||||
return errors.New("Cannot authorize with supplied token.")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return errors.New("Cannot fetch the response body from Pixiv. Please report this issue.")
|
||||
}
|
||||
|
||||
// CSRF token
|
||||
r := regexp.MustCompile(`"token":"([0-9a-f]+)"`)
|
||||
csrf := r.FindStringSubmatch(string(body))[1]
|
||||
csrf := r.FindStringSubmatch(resp.Body)[1]
|
||||
|
||||
if csrf == "" {
|
||||
return errors.New("Cannot authorize with supplied token.")
|
||||
|
||||
Reference in New Issue
Block a user