dbinbound.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. class DBInbound {
  2. constructor(data) {
  3. this.id = 0;
  4. this.userId = 0;
  5. this.up = 0;
  6. this.down = 0;
  7. this.total = 0;
  8. this.allTime = 0;
  9. this.remark = "";
  10. this.enable = true;
  11. this.expiryTime = 0;
  12. this.trafficReset = "never";
  13. this.lastTrafficResetTime = 0;
  14. this.listen = "";
  15. this.port = 0;
  16. this.protocol = "";
  17. this.settings = "";
  18. this.streamSettings = "";
  19. this.tag = "";
  20. this.sniffing = "";
  21. this.clientStats = ""
  22. if (data == null) {
  23. return;
  24. }
  25. ObjectUtil.cloneProps(this, data);
  26. }
  27. get totalGB() {
  28. return NumberFormatter.toFixed(this.total / SizeFormatter.ONE_GB, 2);
  29. }
  30. set totalGB(gb) {
  31. this.total = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  32. }
  33. get isVMess() {
  34. return this.protocol === Protocols.VMESS;
  35. }
  36. get isVLess() {
  37. return this.protocol === Protocols.VLESS;
  38. }
  39. get isTrojan() {
  40. return this.protocol === Protocols.TROJAN;
  41. }
  42. get isSS() {
  43. return this.protocol === Protocols.SHADOWSOCKS;
  44. }
  45. get isMixed() {
  46. return this.protocol === Protocols.MIXED;
  47. }
  48. get isHTTP() {
  49. return this.protocol === Protocols.HTTP;
  50. }
  51. get isWireguard() {
  52. return this.protocol === Protocols.WIREGUARD;
  53. }
  54. get address() {
  55. let address = location.hostname;
  56. if (!ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0") {
  57. address = this.listen;
  58. }
  59. return address;
  60. }
  61. get _expiryTime() {
  62. if (this.expiryTime === 0) {
  63. return null;
  64. }
  65. return moment(this.expiryTime);
  66. }
  67. set _expiryTime(t) {
  68. if (t == null) {
  69. this.expiryTime = 0;
  70. } else {
  71. this.expiryTime = t.valueOf();
  72. }
  73. }
  74. get isExpiry() {
  75. return this.expiryTime < new Date().getTime();
  76. }
  77. invalidateCache() {
  78. this._cachedInbound = null;
  79. this._clientStatsMap = null;
  80. }
  81. toInbound() {
  82. if (this._cachedInbound) {
  83. return this._cachedInbound;
  84. }
  85. let settings = {};
  86. if (!ObjectUtil.isEmpty(this.settings)) {
  87. settings = JSON.parse(this.settings);
  88. }
  89. let streamSettings = {};
  90. if (!ObjectUtil.isEmpty(this.streamSettings)) {
  91. streamSettings = JSON.parse(this.streamSettings);
  92. }
  93. let sniffing = {};
  94. if (!ObjectUtil.isEmpty(this.sniffing)) {
  95. sniffing = JSON.parse(this.sniffing);
  96. }
  97. const config = {
  98. port: this.port,
  99. listen: this.listen,
  100. protocol: this.protocol,
  101. settings: settings,
  102. streamSettings: streamSettings,
  103. tag: this.tag,
  104. sniffing: sniffing,
  105. clientStats: this.clientStats,
  106. };
  107. this._cachedInbound = Inbound.fromJson(config);
  108. return this._cachedInbound;
  109. }
  110. getClientStats(email) {
  111. if (!this._clientStatsMap) {
  112. this._clientStatsMap = new Map();
  113. if (this.clientStats && Array.isArray(this.clientStats)) {
  114. for (const stats of this.clientStats) {
  115. this._clientStatsMap.set(stats.email, stats);
  116. }
  117. }
  118. }
  119. return this._clientStatsMap.get(email);
  120. }
  121. isMultiUser() {
  122. switch (this.protocol) {
  123. case Protocols.VMESS:
  124. case Protocols.VLESS:
  125. case Protocols.TROJAN:
  126. case Protocols.HYSTERIA:
  127. return true;
  128. case Protocols.SHADOWSOCKS:
  129. return this.toInbound().isSSMultiUser;
  130. default:
  131. return false;
  132. }
  133. }
  134. hasLink() {
  135. switch (this.protocol) {
  136. case Protocols.VMESS:
  137. case Protocols.VLESS:
  138. case Protocols.TROJAN:
  139. case Protocols.SHADOWSOCKS:
  140. case Protocols.HYSTERIA:
  141. return true;
  142. default:
  143. return false;
  144. }
  145. }
  146. genInboundLinks(remarkModel) {
  147. const inbound = this.toInbound();
  148. return inbound.genInboundLinks(this.remark, remarkModel);
  149. }
  150. }