getAccount.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. let isAccountChanged = false;
  61. if (ACCOUNT_OPTIONS.nextChangeTime == 0) {
  62. ACCOUNT_OPTIONS.currentIndex = 0;
  63. ACCOUNT_OPTIONS.nextChangeTime = getRandomTime();
  64. }
  65. else if (nowTime > ACCOUNT_OPTIONS.nextChangeTime) {
  66. ACCOUNT_OPTIONS.currentIndex++;
  67. if (ACCOUNT_OPTIONS.currentIndex >= accounts.length) {
  68. ACCOUNT_OPTIONS.currentIndex = 0;
  69. }
  70. ACCOUNT_OPTIONS.nextChangeTime = getRandomTime();
  71. isAccountChanged = true;
  72. saveGlobalDataToCache();
  73. }
  74. const accountInfo = accounts[ACCOUNT_OPTIONS.currentIndex];
  75. if (isAccountChanged) {
  76. const { password, ...accountInfoRest } = accountInfo;
  77. Logs.out('account changed', ACCOUNT_OPTIONS, accountInfoRest);
  78. }
  79. return accountInfo;
  80. }
  81. /**
  82. * 监听进程退出事件
  83. * 保存GLOBAL_DATA数据
  84. */
  85. const saveExit = (code) => {
  86. saveGlobalDataToCache()
  87. .then(() => {
  88. Logs.out('account options saved');
  89. })
  90. .catch(err => {
  91. Logs.err('failed to save account options', err.message);
  92. })
  93. .finally(() => {
  94. process.exit(code);
  95. });
  96. }
  97. process.on('SIGINT', () => {
  98. saveExit(0);
  99. });
  100. process.on('SIGTERM', () => {
  101. saveExit(0);
  102. });
  103. process.on('SIGUSR2', () => {
  104. saveExit(0);
  105. });
  106. loadGlobalDataFromCache();