dbinbound.js 4.6 KB

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