client_modal.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. {{define "clientsModal"}}
  2. <a-modal id="client-modal" v-model="clientModal.visible" :title="clientModal.title" @ok="clientModal.ok"
  3. :confirm-loading="clientModal.confirmLoading" :closable="true" :mask-closable="false"
  4. :class="themeSwitcher.darkCardClass"
  5. :ok-text="clientModal.okText" cancel-text='{{ i18n "close" }}'>
  6. {{template "form/client"}}
  7. </a-modal>
  8. <script>
  9. const clientModal = {
  10. visible: false,
  11. confirmLoading: false,
  12. title: '',
  13. okText: '',
  14. isEdit: false,
  15. dbInbound: new DBInbound(),
  16. inbound: new Inbound(),
  17. clients: [],
  18. clientStats: [],
  19. oldClientId: "",
  20. index: null,
  21. clientIps: null,
  22. isExpired: false,
  23. delayedStart: false,
  24. ok() {
  25. if (clientModal.isEdit) {
  26. ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id, clientModal.oldClientId);
  27. } else {
  28. ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id);
  29. }
  30. },
  31. show({ title = '', okText = '{{ i18n "sure" }}', index = null, dbInbound = null, confirm = () => { }, isEdit = false }) {
  32. this.visible = true;
  33. this.title = title;
  34. this.okText = okText;
  35. this.isEdit = isEdit;
  36. this.dbInbound = new DBInbound(dbInbound);
  37. this.inbound = dbInbound.toInbound();
  38. this.clients = this.getClients(this.inbound.protocol, this.inbound.settings);
  39. this.index = index === null ? this.clients.length : index;
  40. this.isExpired = isEdit ? this.inbound.isExpiry(this.index) : false;
  41. this.delayedStart = false;
  42. if (isEdit) {
  43. if (this.clients[index].expiryTime < 0) {
  44. this.delayedStart = true;
  45. }
  46. this.oldClientId = this.getClientId(dbInbound.protocol, clients[index]);
  47. } else {
  48. this.addClient(this.inbound.protocol, this.clients);
  49. }
  50. this.clientStats = this.dbInbound.clientStats.find(row => row.email === this.clients[this.index].email);
  51. this.confirm = confirm;
  52. },
  53. getClients(protocol, clientSettings) {
  54. switch (protocol) {
  55. case Protocols.VMESS: return clientSettings.vmesses;
  56. case Protocols.VLESS: return clientSettings.vlesses;
  57. case Protocols.TROJAN: return clientSettings.trojans;
  58. case Protocols.SHADOWSOCKS: return clientSettings.shadowsockses;
  59. default: return null;
  60. }
  61. },
  62. getClientId(protocol, client) {
  63. switch (protocol) {
  64. case Protocols.TROJAN: return client.password;
  65. case Protocols.SHADOWSOCKS: return client.email;
  66. default: return client.id;
  67. }
  68. },
  69. addClient(protocol, clients) {
  70. switch (protocol) {
  71. case Protocols.VMESS: return clients.push(new Inbound.VmessSettings.Vmess());
  72. case Protocols.VLESS: return clients.push(new Inbound.VLESSSettings.VLESS());
  73. case Protocols.TROJAN: return clients.push(new Inbound.TrojanSettings.Trojan());
  74. case Protocols.SHADOWSOCKS: return clients.push(new Inbound.ShadowsocksSettings.Shadowsocks());
  75. default: return null;
  76. }
  77. },
  78. close() {
  79. clientModal.visible = false;
  80. clientModal.loading(false);
  81. },
  82. loading(loading) {
  83. clientModal.confirmLoading = loading;
  84. },
  85. };
  86. const clientModalApp = new Vue({
  87. delimiters: ['[[', ']]'],
  88. el: '#client-modal',
  89. data: {
  90. clientModal,
  91. get inbound() {
  92. return this.clientModal.inbound;
  93. },
  94. get client() {
  95. return this.clientModal.clients[this.clientModal.index];
  96. },
  97. get clientStats() {
  98. return this.clientModal.clientStats;
  99. },
  100. get isEdit() {
  101. return this.clientModal.isEdit;
  102. },
  103. get isTrafficExhausted() {
  104. if (!clientStats) return false
  105. if (clientStats.total <= 0) return false
  106. if (clientStats.up + clientStats.down < clientStats.total) return false
  107. return true
  108. },
  109. get isExpiry() {
  110. return this.clientModal.isExpired
  111. },
  112. get statsColor() {
  113. if (!clientStats) return 'blue'
  114. if (clientStats.total <= 0) return 'blue'
  115. else if (clientStats.total > 0 && (clientStats.down + clientStats.up) < clientStats.total) return 'cyan'
  116. else return 'red'
  117. },
  118. get delayedExpireDays() {
  119. return this.client && this.client.expiryTime < 0 ? this.client.expiryTime / -86400000 : 0;
  120. },
  121. set delayedExpireDays(days) {
  122. this.client.expiryTime = -86400000 * days;
  123. },
  124. },
  125. methods: {
  126. getNewEmail(client) {
  127. var chars = 'abcdefghijklmnopqrstuvwxyz1234567890';
  128. var string = '';
  129. var len = 6 + Math.floor(Math.random() * 5);
  130. for (var ii = 0; ii < len; ii++) {
  131. string += chars[Math.floor(Math.random() * chars.length)];
  132. }
  133. client.email = string;
  134. },
  135. async getDBClientIps(email, event) {
  136. const msg = await HttpUtil.post('/panel/inbound/clientIps/' + email);
  137. if (!msg.success) {
  138. return;
  139. }
  140. try {
  141. ips = JSON.parse(msg.obj)
  142. ips = ips.join(",")
  143. event.target.value = ips
  144. } catch (error) {
  145. // text
  146. event.target.value = msg.obj
  147. }
  148. },
  149. async clearDBClientIps(email) {
  150. const msg = await HttpUtil.post('/panel/inbound/clearClientIps/' + email);
  151. if (!msg.success) {
  152. return;
  153. }
  154. document.getElementById("clientIPs").value = ""
  155. },
  156. resetClientTraffic(email, dbInboundId, iconElement) {
  157. this.$confirm({
  158. title: '{{ i18n "pages.inbounds.resetTraffic"}}',
  159. content: '{{ i18n "pages.inbounds.resetTrafficContent"}}',
  160. class: themeSwitcher.darkCardClass,
  161. okText: '{{ i18n "reset"}}',
  162. cancelText: '{{ i18n "cancel"}}',
  163. onOk: async () => {
  164. iconElement.disabled = true;
  165. const msg = await HttpUtil.postWithModal('/panel/inbound/' + dbInboundId + '/resetClientTraffic/' + email);
  166. if (msg.success) {
  167. this.clientModal.clientStats.up = 0;
  168. this.clientModal.clientStats.down = 0;
  169. }
  170. iconElement.disabled = false;
  171. },
  172. })
  173. },
  174. },
  175. });
  176. </script>
  177. {{end}}