diff --git a/core/config/session.go b/core/config/session.go index de0ad1d..4abc95c 100644 --- a/core/config/session.go +++ b/core/config/session.go @@ -55,7 +55,7 @@ func GetToken(c *fiber.Ctx) string { func CheckToken(c *fiber.Ctx) string { sess, err := Store.Get(c) if err != nil { - log.Fatalln("Failed to get current session and its values! Falling back to server default!") + log.Fatalln("Failed to get current session and its values!") return "" } value := sess.Get("Token") @@ -66,6 +66,20 @@ func CheckToken(c *fiber.Ctx) string { return "" } +func GetCSRFToken(c *fiber.Ctx) string { + sess, err := Store.Get(c) + if err != nil { + log.Fatalln("Failed to get current session and its values!") + return "" + } + value := sess.Get("CSRF") + if value != nil { + return value.(string) + } + + return "" +} + func SetSessionValue(c *fiber.Ctx, name, value string) error { sess, err := Store.Get(c) if err != nil { diff --git a/main.go b/main.go index 7877e49..b9e9f5a 100644 --- a/main.go +++ b/main.go @@ -130,6 +130,7 @@ func main() { self.Get("/", pages.LoginUserPage) self.Get("/followingWorks", pages.FollowingWorksPage) self.Get("/bookmarks", pages.LoginBookmarkPage) + self.Get("/addBookmark/:id", pages.AddBookmarkRoute) server.Get("tags/:name", pages.TagPage) server.Post("tags", diff --git a/pages/actions.go b/pages/actions.go new file mode 100644 index 0000000..0d9fa49 --- /dev/null +++ b/pages/actions.go @@ -0,0 +1,82 @@ +package pages + +import ( + "bytes" + "errors" + "fmt" + "io" + "net/http" + + session "codeberg.org/vnpower/pixivfe/v2/core/config" + "github.com/gofiber/fiber/v2" + "github.com/tidwall/gjson" +) + +func pixivPostRequest(id, token, csrf string) error { + URL := "https://www.pixiv.net/ajax/illusts/bookmarks/add" + + requestBody := []byte(fmt.Sprintf(`{ +"illust_id": "%s", +"restrict": 0, +"comment": "", +"tags": [] +}`, id)) + + req, _ := http.NewRequest("POST", URL, bytes.NewBuffer(requestBody)) + req.Header.Add("User-Agent", "Mozilla/5.0") + req.Header.Add("Accept", "application/json") + req.Header.Add("Content-Type", "application/json; charset=utf-8") + req.Header.Add("Cookie", "PHPSESSID="+token) + req.Header.Add("x-csrf-token", csrf) + // req.AddCookie(&http.Cookie{ + // Name: "PHPSESSID", + // Value: token, + // }) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return errors.New("Failed to do this action.") + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return errors.New("Cannot parse the response from Pixiv. Please report this issue.") + } + + errr := gjson.Get(string(body), "error") + + if !errr.Exists() { + return errors.New("Incompatible request body.") + } + + if errr.Bool() { + return errors.New("Pixiv: Invalid request.") + } + return nil +} + +func AddBookmarkRoute(c *fiber.Ctx) error { + token := session.CheckToken(c) + if token == "" { + return c.Redirect("/login") + } + + csrf := session.GetCSRFToken(c) + if csrf == "" { + return c.Redirect("/login") + } + + id := c.Params("id") + if id == "" { + return errors.New("No ID provided.") + } + println("2") + + if err := pixivPostRequest(id, token, csrf); err != nil { + return err + } + + println("1") + + return c.SendString("Success") +} diff --git a/pages/index.go b/pages/index.go index 8fba718..d4bd2db 100644 --- a/pages/index.go +++ b/pages/index.go @@ -1,9 +1,17 @@ package pages import ( + core "codeberg.org/vnpower/pixivfe/v2/core/webapi" "github.com/gofiber/fiber/v2" ) func IndexPage(c *fiber.Ctx) error { - return c.Render("pages/index", fiber.Map{"Title": "Landing", "Token": false}) + works, err := core.GetRanking(c, "daily", "illust", "", "1") + if err != nil { + return err + } + + return c.Render("pages/index", fiber.Map{ + "Title": "Landing", "Token": false, "Data": works, + }) } diff --git a/pages/settings.go b/pages/settings.go index faefb26..39cb3d2 100644 --- a/pages/settings.go +++ b/pages/settings.go @@ -2,9 +2,12 @@ package pages import ( "errors" + "io" + "net/http" + "regexp" + "strings" session "codeberg.org/vnpower/pixivfe/v2/core/config" - http "codeberg.org/vnpower/pixivfe/v2/core/http" "github.com/gofiber/fiber/v2" ) @@ -12,12 +15,41 @@ func setToken(c *fiber.Ctx) error { // Parse the value from the form token := c.FormValue("token") if token != "" { - if _, err := http.UnwrapWebAPIRequest("https://www.pixiv.net/ajax/user/extra", token); err != nil { + // Make a test request to verify the token. + // THE TEST URL IS NSFW! + req, _ := http.NewRequest("GET", "https://www.pixiv.net/en/artworks/115365120", nil) + req.Header.Add("User-Agent", "Mozilla/5.0") + req.AddCookie(&http.Cookie{ + Name: "PHPSESSID", + Value: token, + }) + + resp, err := http.DefaultClient.Do(req) + if err != nil { return errors.New("Cannot authorize with supplied token.") } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return errors.New("Cannot parse the response from Pixiv. Please report this issue.") + } + + // CSRF token + r := regexp.MustCompile(`"token":"([0-9a-f]+)"`) + csrf := strings.Split(r.FindString(string(body)), ":")[1] + csrf = csrf[1 : len(csrf)-1] + + if csrf == "" { + return errors.New("Cannot authorize with supplied token.") + } + + // Set the tokens if err := session.SetSessionValue(c, "Token", token); err != nil { return err } + if err := session.SetSessionValue(c, "CSRF", csrf); err != nil { + return err + } return nil } diff --git a/views/pages/index.jet.html b/views/pages/index.jet.html index 8905d48..d13794f 100644 --- a/views/pages/index.jet.html +++ b/views/pages/index.jet.html @@ -1,4 +1,31 @@
-

Welcome!

-

Please check out the sidebar for more features.
There used to be a small ranking container here... but I am planning to add more than just that!

+
+

Today's illustration rankings

+ See more +
+
+ {{ range Data.Contents }} +
+
+
{{ .Rank }}
+ {{ if toInt(.Pages) > 1 }} +
⧉ {{ .Pages }}
+ {{ end }} +
+ + + {{ .Title }} + + + +
+ {{ end }} +