aux.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package session
  2. import (
  3. "log"
  4. "net/http"
  5. "net/url"
  6. "strings"
  7. "codeberg.org/vnpower/pixivfe/v2/config"
  8. )
  9. func GetPixivToken(r *http.Request) string {
  10. return GetCookie(r, Cookie_Token)
  11. }
  12. func GetImageProxy(r *http.Request) url.URL {
  13. value := GetCookie(r, Cookie_ImageProxy)
  14. if value == "" {
  15. // fall through to default case
  16. } else {
  17. proxyUrl, err := url.Parse(value)
  18. if err != nil {
  19. // fall through to default case
  20. } else {
  21. return *proxyUrl
  22. }
  23. }
  24. return config.GlobalConfig.ProxyServer
  25. }
  26. func ProxyImageUrl(r *http.Request, s string) string {
  27. proxyOrigin := GetImageProxyPrefix(r)
  28. s = strings.ReplaceAll(s, `https:\/\/i.pximg.net`, proxyOrigin)
  29. // s = strings.ReplaceAll(s, `https:\/\/i.pximg.net`, "/proxy/i.pximg.net")
  30. s = strings.ReplaceAll(s, `https:\/\/s.pximg.net`, "/proxy/s.pximg.net")
  31. return s
  32. }
  33. func ProxyImageUrlNoEscape(r *http.Request, s string) string {
  34. proxyOrigin := GetImageProxyPrefix(r)
  35. s = strings.ReplaceAll(s, `https://i.pximg.net`, proxyOrigin)
  36. // s = strings.ReplaceAll(s, `https:\/\/i.pximg.net`, "/proxy/i.pximg.net")
  37. s = strings.ReplaceAll(s, `https://s.pximg.net`, "/proxy/s.pximg.net")
  38. return s
  39. }
  40. func GetImageProxyOrigin(r *http.Request) string {
  41. url := GetImageProxy(r)
  42. return urlAuthority(url)
  43. }
  44. func GetImageProxyPrefix(r *http.Request) string {
  45. url := GetImageProxy(r)
  46. return urlAuthority(url) + url.Path
  47. // note: not sure if url.EscapedPath() is useful here. go's standard library is trash at handling URL (:// should be part of the scheme)
  48. }
  49. // note: still cannot believe Go doesn't have this function built-in
  50. func urlAuthority(url url.URL) string {
  51. r := ""
  52. if (url.Scheme != "") != (url.Host != "") {
  53. log.Panicf("url must have both scheme and authority or neither: %s", url.String())
  54. }
  55. if url.Scheme != "" {
  56. r += url.Scheme + "://"
  57. }
  58. r += url.Host
  59. return r
  60. }