getAccount.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Logs } from "./logs.js";
  2. import { getData, setData } from "./cache.js";
  3. const accountOptionCacheFile = 'data/accountOptionCache.json';
  4. const ACCOUNT_OPTIONS = {
  5. currentIndex: 0,
  6. nextChangeTime: 0,
  7. };
  8. const IS_DEV = process.env.NODE_ENV == 'development';
  9. /**
  10. * 随机一个毫秒数
  11. * 范围:4小时 ~ 7小时
  12. * 测试环境范围:4分钟 ~ 7分钟
  13. * 加上当前时间戳
  14. * 计算下次切换时间
  15. */
  16. const getRandomTime = () => {
  17. const nowTime = Date.now();
  18. const min = 1000 * 60 * (IS_DEV ? 1 : 60) * 4;
  19. const max = 1000 * 60 * (IS_DEV ? 1 : 60) * 7;
  20. const randomTime = Math.floor(Math.random() * (max - min + 1)) + min;
  21. return nowTime + randomTime;
  22. }
  23. /**
  24. * 缓存GLOBAL_DATA数据到文件
  25. * @returns {Promise<void>}
  26. */
  27. const saveGlobalDataToCache = async () => {
  28. return setData(accountOptionCacheFile, ACCOUNT_OPTIONS);
  29. }
  30. /**
  31. * 从文件中加载GLOBAL_DATA数据
  32. * @returns {Promise<void>}
  33. */
  34. const loadGlobalDataFromCache = async () => {
  35. return getData(accountOptionCacheFile)
  36. .then(data => {
  37. if (!data) {
  38. return;
  39. }
  40. Object.keys(ACCOUNT_OPTIONS).forEach(key => {
  41. if (key in data) {
  42. ACCOUNT_OPTIONS[key] = data[key];
  43. }
  44. });
  45. });
  46. }
  47. /**
  48. * 获取当前账号信息
  49. * @returns
  50. */
  51. export const getAccountInfo = () => {
  52. const nowTime = Date.now();
  53. const accounts = process.env.PINNACLE_ACCOUNTS?.split(',').map(item => {
  54. const [username, password, localAddress, platform] = item.split(':');
  55. return { username, password, localAddress, platform };
  56. }) ?? [];
  57. if (accounts.length == 0) {
  58. return null;
  59. }
  60. if (ACCOUNT_OPTIONS.nextChangeTime == 0) {
  61. ACCOUNT_OPTIONS.currentIndex = 0;
  62. ACCOUNT_OPTIONS.nextChangeTime = getRandomTime();
  63. }
  64. else if (nowTime > ACCOUNT_OPTIONS.nextChangeTime) {
  65. ACCOUNT_OPTIONS.currentIndex++;
  66. if (ACCOUNT_OPTIONS.currentIndex >= accounts.length) {
  67. ACCOUNT_OPTIONS.currentIndex = 0;
  68. }
  69. ACCOUNT_OPTIONS.nextChangeTime = getRandomTime();
  70. Logs.out('account changed', ACCOUNT_OPTIONS);
  71. saveGlobalDataToCache();
  72. }
  73. const accountInfo = accounts[ACCOUNT_OPTIONS.currentIndex];
  74. return accountInfo;
  75. }
  76. /**
  77. * 监听进程退出事件
  78. * 保存GLOBAL_DATA数据
  79. */
  80. const saveExit = (code) => {
  81. saveGlobalDataToCache()
  82. .then(() => {
  83. Logs.out('account options saved');
  84. })
  85. .catch(err => {
  86. Logs.err('failed to save account options', err.message);
  87. })
  88. .finally(() => {
  89. process.exit(code);
  90. });
  91. }
  92. process.on('SIGINT', () => {
  93. saveExit(0);
  94. });
  95. process.on('SIGTERM', () => {
  96. saveExit(0);
  97. });
  98. process.on('SIGUSR2', () => {
  99. saveExit(0);
  100. });
  101. loadGlobalDataFromCache();