settings.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package pages
  2. import (
  3. "errors"
  4. "io"
  5. "net/http"
  6. "regexp"
  7. session "codeberg.org/vnpower/pixivfe/v2/core/config"
  8. httpc "codeberg.org/vnpower/pixivfe/v2/core/http"
  9. "codeberg.org/vnpower/pixivfe/v2/doc"
  10. "github.com/gofiber/fiber/v2"
  11. )
  12. func setToken(c *fiber.Ctx) error {
  13. // Parse the value from the form
  14. token := c.FormValue("token")
  15. if token != "" {
  16. URL := httpc.GetNewestFromFollowingURL("all", "1")
  17. _, err := httpc.UnwrapWebAPIRequest(URL, token)
  18. if err != nil {
  19. return errors.New("Cannot authorize with supplied token.")
  20. }
  21. // Make a test request to verify the token.
  22. // THE TEST URL IS NSFW!
  23. req, _ := http.NewRequest("GET", "https://www.pixiv.net/en/artworks/115365120", nil)
  24. req.Header.Add("User-Agent", "Mozilla/5.0")
  25. req.AddCookie(&http.Cookie{
  26. Name: "PHPSESSID",
  27. Value: token,
  28. })
  29. resp, err := http.DefaultClient.Do(req)
  30. if err != nil {
  31. return errors.New("Cannot authorize with supplied token.")
  32. }
  33. body, err := io.ReadAll(resp.Body)
  34. if err != nil {
  35. return errors.New("Cannot parse the response from Pixiv. Please report this issue.")
  36. }
  37. // CSRF token
  38. r := regexp.MustCompile(`"token":"([0-9a-f]+)"`)
  39. csrf := r.FindStringSubmatch(string(body))[1]
  40. if csrf == "" {
  41. return errors.New("Cannot authorize with supplied token.")
  42. }
  43. // Set the token
  44. if err := session.SetSessionValue(c, "Token", token); err != nil {
  45. return err
  46. }
  47. if err := session.SetSessionValue(c, "CSRF", csrf); err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. return errors.New("You submitted an empty/invalid form.")
  53. }
  54. func setImageServer(c *fiber.Ctx) error {
  55. // Parse the value from the form
  56. token := c.FormValue("image-proxy")
  57. if token != "" {
  58. if err := session.SetSessionValue(c, "ImageProxy", token); err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. return errors.New("You submitted an empty/invalid form.")
  64. }
  65. func setLogout(c *fiber.Ctx) error {
  66. session.RemoveSessionValue(c, "Token")
  67. return nil
  68. }
  69. func SettingsPage(c *fiber.Ctx) error {
  70. return c.Render("pages/settings", fiber.Map{
  71. "ProxyList": doc.BuiltinProxyList,
  72. })
  73. }
  74. func SettingsPost(c *fiber.Ctx) error {
  75. t := c.Params("type")
  76. var err error
  77. switch t {
  78. case "image_server":
  79. err = setImageServer(c)
  80. case "token":
  81. err = setToken(c)
  82. case "logout":
  83. err = setLogout(c)
  84. default:
  85. err = errors.New("no such setting available")
  86. }
  87. if err != nil {
  88. return err
  89. }
  90. c.Redirect("/")
  91. return nil
  92. }