settings.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package routes
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "slices"
  8. "strings"
  9. "codeberg.org/vnpower/pixivfe/v2/config"
  10. "codeberg.org/vnpower/pixivfe/v2/core"
  11. "codeberg.org/vnpower/pixivfe/v2/session"
  12. "codeberg.org/vnpower/pixivfe/v2/utils"
  13. )
  14. func setToken(w http.ResponseWriter, r *http.Request) error {
  15. token := r.FormValue("token")
  16. if token != "" {
  17. URL := core.GetNewestFromFollowingURL("all", "1")
  18. _, err := core.API_GET_UnwrapJson(r.Context(), URL, token)
  19. if err != nil {
  20. return errors.New("Cannot authorize with supplied token.")
  21. }
  22. // Make a test request to verify the token.
  23. // THE TEST URL IS NSFW!
  24. resp, err := core.API_GET(r.Context(), "https://www.pixiv.net/en/artworks/115365120", token)
  25. if err != nil {
  26. return err
  27. }
  28. if resp.StatusCode != 200 {
  29. return errors.New("Cannot authorize with supplied token.")
  30. }
  31. // CSRF token
  32. r := regexp.MustCompile(`"token":"([0-9a-f]+)"`)
  33. csrf := r.FindStringSubmatch(resp.Body)[1]
  34. if csrf == "" {
  35. return errors.New("Cannot authorize with supplied token.")
  36. }
  37. // Set the token
  38. session.SetCookie(w, session.Cookie_Token, token)
  39. session.SetCookie(w, session.Cookie_CSRF, csrf)
  40. return nil
  41. }
  42. return errors.New("You submitted an empty/invalid form.")
  43. }
  44. func setImageServer(w http.ResponseWriter, r *http.Request) error {
  45. token := r.FormValue("image-proxy")
  46. if token != "" {
  47. session.SetCookie(w, session.Cookie_ImageProxy, token)
  48. } else {
  49. session.ClearCookie(w, session.Cookie_ImageProxy)
  50. }
  51. return nil
  52. }
  53. func setNovelFontType(w http.ResponseWriter, r *http.Request) error {
  54. fontType := r.FormValue("font-type")
  55. if fontType != "" {
  56. session.SetCookie(w, session.Cookie_NovelFontType, fontType)
  57. }
  58. return nil
  59. }
  60. func setNovelViewMode(w http.ResponseWriter, r *http.Request) error {
  61. viewMode := r.FormValue("view-mode")
  62. if viewMode == "1" || viewMode == "2" || viewMode == "" {
  63. session.SetCookie(w, session.Cookie_NovelViewMode, viewMode)
  64. }
  65. return nil
  66. }
  67. func setThumbnailToNewTab(w http.ResponseWriter, r *http.Request) error {
  68. ttnt := r.FormValue("ttnt")
  69. if ttnt == "_blank" || ttnt == "_self" {
  70. session.SetCookie(w, session.Cookie_ThumbnailToNewTab, ttnt)
  71. }
  72. return nil
  73. }
  74. func setArtworkPreview(w http.ResponseWriter, r *http.Request) error {
  75. value := r.FormValue("app")
  76. if value == "cover" || value == "button" || value == "" {
  77. session.SetCookie(w, session.Cookie_ArtworkPreview, value)
  78. }
  79. return nil
  80. }
  81. func setLogout(w http.ResponseWriter, _ *http.Request) error {
  82. session.ClearCookie(w, session.Cookie_Token)
  83. session.ClearCookie(w, session.Cookie_CSRF)
  84. return nil
  85. }
  86. func setCookie(w http.ResponseWriter, r *http.Request) error {
  87. key := r.FormValue("key")
  88. value := r.FormValue("value")
  89. for _, cookie_name := range session.AllCookieNames {
  90. if string(cookie_name) == key {
  91. session.SetCookie(w, cookie_name, value)
  92. return nil
  93. }
  94. }
  95. return fmt.Errorf("Invalid Cookie Name: %s", key)
  96. }
  97. func setRawCookie(w http.ResponseWriter, r *http.Request) error {
  98. raw := r.FormValue("raw")
  99. lines := strings.Split(raw, "\n")
  100. for _, line := range lines {
  101. sub := strings.Split(line, "=")
  102. if len(sub) != 2 {
  103. continue
  104. }
  105. name := session.CookieName(sub[0])
  106. value := sub[1]
  107. if !slices.Contains(session.AllCookieNames, name) {
  108. continue
  109. }
  110. session.SetCookie(w, name, value)
  111. }
  112. return nil
  113. }
  114. func resetAll(w http.ResponseWriter, _ *http.Request) error {
  115. session.ClearAllCookies(w)
  116. return nil
  117. }
  118. func SettingsPage(w http.ResponseWriter, r *http.Request) error {
  119. return Render(w, r, Data_settings{WorkingProxyList: config.GetWorkingProxies(), ProxyList: config.BuiltinProxyList})
  120. }
  121. func SettingsPost(w http.ResponseWriter, r *http.Request) error {
  122. t := GetPathVar(r, "type")
  123. var err error
  124. switch t {
  125. case "imageServer":
  126. err = setImageServer(w, r)
  127. case "token":
  128. err = setToken(w, r)
  129. case "logout":
  130. err = setLogout(w, r)
  131. case "reset-all":
  132. err = resetAll(w, r)
  133. case "novelFontType":
  134. err = setNovelFontType(w, r)
  135. case "thumbnailToNewTab":
  136. err = setThumbnailToNewTab(w, r)
  137. case "novelViewMode":
  138. err = setNovelViewMode(w, r)
  139. case "artworkPreview":
  140. err = setArtworkPreview(w, r)
  141. case "set-cookie":
  142. err = setCookie(w, r)
  143. case "raw":
  144. err = setRawCookie(w, r)
  145. default:
  146. err = errors.New("No such setting is available.")
  147. }
  148. if err != nil {
  149. return err
  150. }
  151. utils.RedirectToWhenceYouCame(w, r)
  152. return nil
  153. }