settings.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package routes
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "regexp"
  8. "slices"
  9. "strings"
  10. "codeberg.org/vnpower/pixivfe/v2/config"
  11. httpc "codeberg.org/vnpower/pixivfe/v2/core"
  12. "codeberg.org/vnpower/pixivfe/v2/session"
  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 setThumbnailToNewTab(c *fiber.Ctx) error {
  84. ttnt := c.FormValue("ttnt")
  85. if ttnt == "_blank" || ttnt == "_self" {
  86. session.SetCookie(c, session.Cookie_ThumbnailToNewTab, ttnt)
  87. }
  88. return nil
  89. }
  90. func setArtworkPreview(c *fiber.Ctx) error {
  91. value := c.FormValue("app")
  92. if value == "cover" || value == "button" || value == "" {
  93. session.SetCookie(c, session.Cookie_ArtworkPreview, value)
  94. }
  95. return nil
  96. }
  97. func setLogout(c *fiber.Ctx) error {
  98. session.ClearCookie(c, session.Cookie_Token)
  99. session.ClearCookie(c, session.Cookie_CSRF)
  100. return nil
  101. }
  102. func setCookie(c *fiber.Ctx) error {
  103. key := c.FormValue("key")
  104. value := c.FormValue("value")
  105. for _, cookie_name := range session.AllCookieNames {
  106. if string(cookie_name) == key {
  107. session.SetCookie(c, cookie_name, value)
  108. return nil
  109. }
  110. }
  111. return fmt.Errorf("Invalid Cookie Name: %s", key)
  112. }
  113. func setRawCookie(c *fiber.Ctx) error {
  114. raw := c.FormValue("raw")
  115. lines := strings.Split(raw, "\n")
  116. for _, line := range lines {
  117. sub := strings.Split(line, "=")
  118. if len(sub) != 2 {
  119. continue
  120. }
  121. name := session.CookieName(sub[0])
  122. value := sub[1]
  123. if !slices.Contains(session.AllCookieNames, name) {
  124. continue
  125. }
  126. session.SetCookie(c, name, value)
  127. }
  128. return nil
  129. }
  130. func resetAll(c *fiber.Ctx) error {
  131. session.ClearAllCookies(c)
  132. return nil
  133. }
  134. func SettingsPage(c *fiber.Ctx) error {
  135. return Render(c, Data_settings{WorkingProxyList: config.GetWorkingProxies(), ProxyList: config.BuiltinProxyList})
  136. }
  137. func SettingsPost(c *fiber.Ctx) error {
  138. // NOTE: VnPower: Future maintainers should leave this function alone.
  139. t := c.Params("type")
  140. noredirect := c.FormValue("noredirect", "") == ""
  141. var err error
  142. switch t {
  143. case "imageServer":
  144. err = setImageServer(c)
  145. case "token":
  146. err = setToken(c)
  147. case "logout":
  148. err = setLogout(c)
  149. case "reset-all":
  150. err = resetAll(c)
  151. case "novelFontType":
  152. err = setNovelFontType(c)
  153. case "thumbnailToNewTab":
  154. err = setThumbnailToNewTab(c)
  155. case "novelViewMode":
  156. err = setNovelViewMode(c)
  157. case "artworkPreview":
  158. err = setArtworkPreview(c)
  159. case "set-cookie":
  160. err = setCookie(c)
  161. case "raw":
  162. err = setRawCookie(c)
  163. default:
  164. err = errors.New("No such setting is available.")
  165. }
  166. if err != nil {
  167. return err
  168. }
  169. if !noredirect {
  170. return nil
  171. }
  172. return c.Redirect("/settings", http.StatusSeeOther)
  173. }