settings.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package views
  2. import (
  3. "net/http"
  4. "time"
  5. "codeberg.org/vnpower/pixivfe/configs"
  6. "github.com/gofiber/fiber/v2"
  7. "github.com/gofiber/fiber/v2/middleware/session"
  8. )
  9. func get_storage(c *fiber.Ctx) *session.Session {
  10. // Get the storage
  11. sess, err := configs.Store.Get(c)
  12. if err != nil {
  13. panic(err)
  14. }
  15. return sess
  16. }
  17. func save_storage(sess *session.Session) {
  18. if err := sess.Save(); err != nil {
  19. panic(err)
  20. }
  21. }
  22. func test_request(URL string) error {
  23. req, _ := http.NewRequest("GET", URL, nil)
  24. transport := &http.Transport{Proxy: http.ProxyFromEnvironment}
  25. client := &http.Client{
  26. Timeout: time.Duration(5000) * time.Millisecond,
  27. Transport: transport,
  28. }
  29. // Add headers
  30. // for k, v := range p.Header {
  31. // req.Header.Add(k, v)
  32. // }
  33. // for k, v := range p.Cookie {
  34. // }
  35. // Make a request
  36. resp, err := client.Do(req)
  37. if err != nil {
  38. return err
  39. }
  40. if resp.StatusCode != 200 {
  41. return err
  42. }
  43. return nil
  44. }
  45. func set_token(c *fiber.Ctx) string {
  46. name := "token"
  47. sess := get_storage(c)
  48. // Parse the value from the form
  49. token := c.FormValue(name)
  50. if token != "" {
  51. client := NewPixivClient(3000)
  52. client.SetSessionID(token)
  53. client.SetUserAgent(configs.UserAgent)
  54. // If token is valid, save it
  55. if _, err := client.PixivRequest("https://www.pixiv.net/ajax/top/illust?mode=r18"); err != nil {
  56. return "Invalid token"
  57. }
  58. sess.Set(name, token)
  59. save_storage(sess)
  60. return ""
  61. }
  62. return "Empty form"
  63. }
  64. func set_image_server(c *fiber.Ctx) string {
  65. name := "image-proxy"
  66. sess := get_storage(c)
  67. // Parse the value from the form
  68. token := c.FormValue(name)
  69. if token != "" {
  70. if err := test_request("https://" + token + "/img-original/img/2023/06/10/19/51/55/108894158_p0.jpg"); err != nil {
  71. return "Cannot get image from " + token
  72. }
  73. // If token is valid, save it
  74. sess.Set(name, token)
  75. save_storage(sess)
  76. return ""
  77. }
  78. return "Empty form"
  79. }
  80. func set_logout(c *fiber.Ctx) string {
  81. name := "token"
  82. sess := get_storage(c)
  83. sess.Delete(name)
  84. save_storage(sess)
  85. return ""
  86. }