settings.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package pages
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "regexp"
  8. "slices"
  9. "strings"
  10. httpc "codeberg.org/vnpower/pixivfe/v2/core/http"
  11. session "codeberg.org/vnpower/pixivfe/v2/core/session"
  12. "codeberg.org/vnpower/pixivfe/v2/doc"
  13. "github.com/gofiber/fiber/v2"
  14. )
  15. // todo: allow clear proxy
  16. // todo: allow clear all settings
  17. func setToken(c *fiber.Ctx) error {
  18. // Parse the value from the form
  19. token := c.FormValue("token")
  20. if token != "" {
  21. URL := httpc.GetNewestFromFollowingURL("all", "1")
  22. _, err := httpc.UnwrapWebAPIRequest(c.Context(), URL, token)
  23. if err != nil {
  24. return errors.New("Cannot authorize with supplied token.")
  25. }
  26. // Make a test request to verify the token.
  27. // THE TEST URL IS NSFW!
  28. req, err := http.NewRequest("GET", "https://www.pixiv.net/en/artworks/115365120", nil)
  29. if err != nil {
  30. return err
  31. }
  32. req = req.WithContext(c.Context())
  33. req.Header.Add("User-Agent", "Mozilla/5.0")
  34. req.AddCookie(&http.Cookie{
  35. Name: "PHPSESSID",
  36. Value: token,
  37. })
  38. resp, err := http.DefaultClient.Do(req)
  39. if err != nil {
  40. return errors.New("Cannot authorize with supplied token.")
  41. }
  42. body, err := io.ReadAll(resp.Body)
  43. if err != nil {
  44. return errors.New("Cannot parse the response from Pixiv. Please report this issue.")
  45. }
  46. // CSRF token
  47. r := regexp.MustCompile(`"token":"([0-9a-f]+)"`)
  48. csrf := r.FindStringSubmatch(string(body))[1]
  49. if csrf == "" {
  50. return errors.New("Cannot authorize with supplied token.")
  51. }
  52. // Set the token
  53. session.SetCookie(c, session.Cookie_Token, token)
  54. session.SetCookie(c, session.Cookie_CSRF, csrf)
  55. return nil
  56. }
  57. return errors.New("You submitted an empty/invalid form.")
  58. }
  59. func setImageServer(c *fiber.Ctx) error {
  60. // Parse the value from the form
  61. token := c.FormValue("image-proxy")
  62. if token != "" {
  63. session.SetCookie(c, session.Cookie_ImageProxy, token)
  64. } else {
  65. session.ClearCookie(c, session.Cookie_ImageProxy)
  66. }
  67. return nil
  68. }
  69. func setNovelFontType(c *fiber.Ctx) error {
  70. fontType := c.FormValue("font-type")
  71. if fontType != "" {
  72. session.SetCookie(c, session.Cookie_NovelFontType, fontType)
  73. }
  74. return nil
  75. }
  76. func setNovelViewMode(c *fiber.Ctx) error {
  77. viewMode := c.FormValue("view-mode")
  78. if viewMode != "" {
  79. session.SetCookie(c, session.Cookie_NovelViewMode, viewMode)
  80. }
  81. return nil
  82. }
  83. func setLogout(c *fiber.Ctx) error {
  84. session.ClearCookie(c, session.Cookie_Token)
  85. session.ClearCookie(c, session.Cookie_CSRF)
  86. return nil
  87. }
  88. func setCookie(c *fiber.Ctx) error {
  89. key := c.FormValue("key")
  90. value := c.FormValue("value")
  91. for _, cookie_name := range session.AllCookieNames {
  92. if string(cookie_name) == key {
  93. session.SetCookie(c, cookie_name, value)
  94. return nil
  95. }
  96. }
  97. return fmt.Errorf("Invalid Cookie Name: %s", key)
  98. }
  99. func setRawCookie(c *fiber.Ctx) error {
  100. raw := c.FormValue("raw")
  101. lines := strings.Split(raw, "\n")
  102. for _, line := range lines {
  103. sub := strings.Split(line, "=")
  104. if len(sub) != 2 {
  105. continue
  106. }
  107. name := session.CookieName(sub[0])
  108. value := sub[1]
  109. if !slices.Contains(session.AllCookieNames, name) {
  110. continue
  111. }
  112. session.SetCookie(c, name, value)
  113. }
  114. return nil
  115. }
  116. func resetAll(c *fiber.Ctx) error {
  117. session.ClearAllCookies(c)
  118. return nil
  119. }
  120. func SettingsPage(c *fiber.Ctx) error {
  121. cookies := []fiber.Map{}
  122. for _, name := range session.AllCookieNames {
  123. value := session.GetCookie(c, name)
  124. cookies = append(cookies, fiber.Map{
  125. "Key": name,
  126. "Value": value,
  127. })
  128. }
  129. return c.Render("settings", fiber.Map{
  130. "CookieList": cookies,
  131. "ProxyList": doc.BuiltinProxyList,
  132. })
  133. }
  134. func SettingsPost(c *fiber.Ctx) error {
  135. t := c.Params("type")
  136. var noredirect bool = false
  137. var err error
  138. switch t {
  139. case "imageServer":
  140. err = setImageServer(c)
  141. case "token":
  142. err = setToken(c)
  143. case "logout":
  144. err = setLogout(c)
  145. case "reset-all":
  146. err = resetAll(c)
  147. case "novelFontType":
  148. err = setNovelFontType(c)
  149. noredirect = true
  150. case "novelViewMode":
  151. err = setNovelViewMode(c)
  152. noredirect = true
  153. case "set-cookie":
  154. err = setCookie(c)
  155. case "raw":
  156. err = setRawCookie(c)
  157. default:
  158. err = errors.New("No such setting is available.")
  159. }
  160. if err != nil {
  161. return err
  162. }
  163. if !noredirect {
  164. c.Redirect("/settings", http.StatusSeeOther)
  165. }
  166. return nil
  167. }