consts.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package consts
  2. import (
  3. "encoding/json"
  4. "log/slog"
  5. "maps"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "github.com/diamondburned/arikawa/v3/discord"
  10. "github.com/diamondburned/arikawa/v3/gateway"
  11. "github.com/google/uuid"
  12. )
  13. const Name = "discordo"
  14. const identifyPropertiesURL = "https://cordapi.dolfi.es/api/v2/properties/web"
  15. var defaultIdentifyProps = gateway.IdentifyProperties{
  16. gateway.IdentifyDevice: "",
  17. gateway.IdentifyBrowser: "Chrome",
  18. "browser_version": "140.0.0.0",
  19. "browser_user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36",
  20. gateway.IdentifyOS: "Windows",
  21. "os_version": "10",
  22. "client_build_number": 439729,
  23. "client_event_source": nil,
  24. "client_launch_id": uuid.NewString(),
  25. "client_app_state": "focused",
  26. "launch_signature": uuid.NewString(),
  27. "system_locale": discord.EnglishUS,
  28. "release_channel": "stable",
  29. "has_client_mods": false,
  30. "is_fast_connect": false,
  31. "gateway_connect_reasons": "AppSkeleton",
  32. "referrer": "",
  33. "referrer_current": "",
  34. "referring_domain": "",
  35. "referring_domain_current": "",
  36. }
  37. type Properties struct {
  38. Client struct {
  39. Type string `json:"type"`
  40. BuildNumber int `json:"build_number"`
  41. BuildHash string `json:"build_hash"`
  42. ReleaseChannel string `json:"release_channel"`
  43. } `json:"client"`
  44. Browser struct {
  45. Type string `json:"type"`
  46. UserAgent string `json:"user_agent"`
  47. Version string `json:"version"`
  48. OS struct {
  49. Type string `json:"type"`
  50. Version string `json:"version"`
  51. } `json:"os"`
  52. } `json:"browser"`
  53. }
  54. func GetIdentifyProps() gateway.IdentifyProperties {
  55. resp, err := http.Get(identifyPropertiesURL)
  56. if err != nil {
  57. return defaultIdentifyProps
  58. }
  59. defer resp.Body.Close()
  60. var props Properties
  61. if err := json.NewDecoder(resp.Body).Decode(&props); err != nil {
  62. return defaultIdentifyProps
  63. }
  64. p := maps.Clone(defaultIdentifyProps)
  65. p[gateway.IdentifyBrowser] = props.Browser.Type
  66. p["browser_version"] = props.Browser.Version
  67. p["browser_user_agent"] = props.Browser.UserAgent
  68. p[gateway.IdentifyOS] = props.Browser.OS.Type
  69. p["os_version"] = props.Browser.OS.Version
  70. p["release_channel"] = props.Client.ReleaseChannel
  71. p["client_build_number"] = props.Client.BuildNumber
  72. return p
  73. }
  74. var cacheDir string
  75. func CacheDir() string {
  76. return cacheDir
  77. }
  78. func init() {
  79. userCacheDir, err := os.UserCacheDir()
  80. if err != nil {
  81. userCacheDir = os.TempDir()
  82. slog.Warn("failed to get user cache dir; falling back to temp dir", "err", err, "path", userCacheDir)
  83. }
  84. cacheDir = filepath.Join(userCacheDir, Name)
  85. if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
  86. slog.Error("failed to create cache dir", "err", err, "path", cacheDir)
  87. }
  88. }