client_modal.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.currentTheme"
  5. :ok-text="clientModal.okText" cancel-text='{{ i18n "close" }}'>
  6. <template v-if="isEdit">
  7. <a-tag v-if="isExpiry || isTrafficExhausted" color="red" style="margin-bottom: 10px;display: block;text-align: center;">Account is (Expired|Traffic Ended) And Disabled</a-tag>
  8. </template>
  9. {{template "form/client"}}
  10. </a-modal>
  11. <script>
  12. const clientModal = {
  13. visible: false,
  14. confirmLoading: false,
  15. title: '',
  16. okText: '',
  17. isEdit: false,
  18. group: {
  19. canGroup: true,
  20. isGroup: false,
  21. currentClient: null,
  22. inbounds: [],
  23. clients: [],
  24. editIds: []
  25. },
  26. dbInbound: new DBInbound(),
  27. dbInbounds: null,
  28. inbound: new Inbound(),
  29. clients: [],
  30. clientStats: [],
  31. oldClientId: "",
  32. index: null,
  33. clientIps: null,
  34. delayedStart: false,
  35. ok() {
  36. if (app.subSettings.enable && clientModal.group.isGroup && clientModal.group.canGroup) {
  37. const currentClient = clientModal.group.currentClient;
  38. const { limitIp, comment, totalGB, expiryTime, reset, enable, subId, tgId, flow } = currentClient;
  39. const uniqueEmails = clientModalApp.makeGroupEmailsUnique(clientModal.dbInbounds, currentClient.email, clientModal.group.clients);
  40. clientModal.group.clients.forEach((client, index) => {
  41. client.email = uniqueEmails[index];
  42. client.limitIp = limitIp;
  43. client.comment = comment;
  44. client.totalGB = totalGB;
  45. client.expiryTime = expiryTime;
  46. client.reset = reset;
  47. client.enable = enable;
  48. if (subId) {
  49. client.subId = subId;
  50. }
  51. if (tgId) {
  52. client.tgId = tgId;
  53. }
  54. if (flow) {
  55. client.flow = flow;
  56. }
  57. });
  58. if (clientModal.isEdit) {
  59. ObjectUtil.execute(clientModal.confirm, clientModal.group.clients, clientModal.group.inbounds, clientModal.group.editIds);
  60. } else {
  61. ObjectUtil.execute(clientModal.confirm, clientModal.group.clients, clientModal.group.inbounds);
  62. }
  63. } else {
  64. if (clientModal.isEdit) {
  65. ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id, clientModal.oldClientId);
  66. } else {
  67. ObjectUtil.execute(clientModal.confirm, clientModalApp.client, clientModal.dbInbound.id);
  68. }
  69. }
  70. },
  71. show({
  72. title = '',
  73. okText = '{{ i18n "sure" }}',
  74. index = null,
  75. dbInbound = null,
  76. dbInbounds = null,
  77. confirm = () => {
  78. },
  79. isEdit = false
  80. }) {
  81. this.group = {
  82. canGroup: true,
  83. isGroup: false,
  84. currentClient: null,
  85. inbounds: [],
  86. clients: [],
  87. editIds: []
  88. }
  89. this.dbInbounds = dbInbounds;
  90. this.visible = true;
  91. this.title = title;
  92. this.okText = okText;
  93. this.isEdit = isEdit;
  94. if (app.subSettings.enable && dbInbounds !== null && Array.isArray(dbInbounds)) {
  95. if (isEdit) {
  96. this.showProcess(dbInbound, index);
  97. let processSingleEdit = true
  98. if (this.group.canGroup) {
  99. this.group.currentClient = this.clients[this.index]
  100. const response = app.getSubGroupClients(dbInbounds, this.group.currentClient)
  101. if (response.clients.length > 1) {
  102. this.group.isGroup = true;
  103. this.group.inbounds = response.inbounds
  104. this.group.clients = response.clients
  105. this.group.editIds = response.editIds
  106. if (this.clients[index].expiryTime < 0) {
  107. this.delayedStart = true;
  108. }
  109. processSingleEdit = false
  110. }
  111. }
  112. if (processSingleEdit) {
  113. this.singleEditClientProcess(index)
  114. }
  115. } else {
  116. this.group.isGroup = true;
  117. dbInbounds.forEach((dbInboundItem) => {
  118. this.showProcess(dbInboundItem);
  119. this.addClient(this.inbound.protocol, this.clients);
  120. this.group.inbounds.push(dbInboundItem.id)
  121. this.group.clients.push(this.clients[this.index])
  122. })
  123. this.group.currentClient = this.clients[this.index]
  124. }
  125. } else {
  126. this.showProcess(dbInbound, index);
  127. if (isEdit) {
  128. this.singleEditClientProcess(index)
  129. } else {
  130. this.addClient(this.inbound.protocol, this.clients);
  131. }
  132. }
  133. this.clientStats = this.dbInbound.clientStats.find(row => row.email === this.clients[this.index].email);
  134. this.confirm = confirm;
  135. },
  136. showProcess(dbInbound, index = null) {
  137. this.dbInbound = new DBInbound(dbInbound);
  138. this.inbound = dbInbound.toInbound();
  139. this.clients = this.inbound.clients;
  140. this.index = index === null ? this.clients.length : index;
  141. this.delayedStart = false;
  142. },
  143. singleEditClientProcess(index) {
  144. if (this.clients[index].expiryTime < 0) {
  145. this.delayedStart = true;
  146. }
  147. this.oldClientId = this.getClientId(this.dbInbound.protocol, this.clients[index]);
  148. },
  149. getClientId(protocol, client) {
  150. switch (protocol) {
  151. case Protocols.TROJAN: return client.password;
  152. case Protocols.SHADOWSOCKS: return client.email;
  153. default: return client.id;
  154. }
  155. },
  156. addClient(protocol, clients) {
  157. switch (protocol) {
  158. case Protocols.VMESS: return clients.push(new Inbound.VmessSettings.VMESS());
  159. case Protocols.VLESS: return clients.push(new Inbound.VLESSSettings.VLESS());
  160. case Protocols.TROJAN: return clients.push(new Inbound.TrojanSettings.Trojan());
  161. case Protocols.SHADOWSOCKS: return clients.push(new Inbound.ShadowsocksSettings.Shadowsocks(clients[0].method));
  162. default: return null;
  163. }
  164. },
  165. close() {
  166. clientModal.visible = false;
  167. clientModal.loading(false);
  168. },
  169. loading(loading = true) {
  170. clientModal.confirmLoading = loading;
  171. },
  172. };
  173. const clientModalApp = new Vue({
  174. delimiters: ['[[', ']]'],
  175. el: '#client-modal',
  176. data: {
  177. clientModal,
  178. get inbound() {
  179. return this.clientModal.inbound;
  180. },
  181. get client() {
  182. return this.clientModal.clients[this.clientModal.index];
  183. },
  184. get clientStats() {
  185. return this.clientModal.clientStats;
  186. },
  187. get isEdit() {
  188. return this.clientModal.isEdit;
  189. },
  190. get isGroup() {
  191. return this.clientModal.group.isGroup;
  192. },
  193. get isGroupEdit() {
  194. return this.clientModal.group.canGroup;
  195. },
  196. set isGroupEdit(value) {
  197. this.clientModal.group.canGroup = value;
  198. if (!value) {
  199. this.clientModal.singleEditClientProcess(this.clientModal.index)
  200. }
  201. },
  202. get datepicker() {
  203. return app.datepicker;
  204. },
  205. get isTrafficExhausted() {
  206. if (!clientStats) return false
  207. if (clientStats.total <= 0) return false
  208. if (clientStats.up + clientStats.down < clientStats.total) return false
  209. return true
  210. },
  211. get isExpiry() {
  212. return this.clientModal.isEdit && this.client.expiryTime >0 ? (this.client.expiryTime < new Date().getTime()) : false;
  213. },
  214. get delayedStart() {
  215. return this.clientModal.delayedStart;
  216. },
  217. set delayedStart(value) {
  218. this.clientModal.delayedStart = value;
  219. },
  220. get delayedExpireDays() {
  221. return this.client && this.client.expiryTime < 0 ? this.client.expiryTime / -86400000 : 0;
  222. },
  223. set delayedExpireDays(days) {
  224. this.client.expiryTime = -86400000 * days;
  225. },
  226. },
  227. methods: {
  228. makeGroupEmailsUnique(dbInbounds, baseEmail, groupClients) {
  229. // Extract the base part of the email (before the "__" if present)
  230. const match = baseEmail.match(/^(.*?)__/);
  231. const base = match ? match[1] : baseEmail;
  232. // Generate initial emails for each client in the group
  233. const generatedEmails = groupClients.map((_, index) => `${base}__${index + 1}`);
  234. // Function to check if an email already exists in dbInbounds but belongs to a different subId
  235. const isDuplicate = (emailToCheck, clientSubId) => {
  236. return dbInbounds.some((dbInbound) => {
  237. const settings = JSON.parse(dbInbound.settings);
  238. const clients = settings && settings.clients ? settings.clients : [];
  239. return clients.some(client => client.email === emailToCheck && client.subId !== clientSubId);
  240. });
  241. };
  242. // Check if any of the generated emails are duplicates
  243. const hasDuplicates = generatedEmails.some((email, index) => {
  244. return isDuplicate(email, groupClients[index].subId);
  245. });
  246. // If duplicates exist, add a random string to the base email to ensure uniqueness
  247. if (hasDuplicates) {
  248. const randomString = `-${RandomUtil.randomLowerAndNum(4)}`;
  249. return groupClients.map((_, index) => `${base}${randomString}__${index + 1}`);
  250. }
  251. return generatedEmails;
  252. },
  253. async getDBClientIps(email) {
  254. const msg = await HttpUtil.post(`/panel/inbound/clientIps/${email}`);
  255. if (!msg.success) {
  256. document.getElementById("clientIPs").value = msg.obj;
  257. return;
  258. }
  259. let ips = msg.obj;
  260. if (typeof ips === 'string' && ips.startsWith('[') && ips.endsWith(']')) {
  261. try {
  262. ips = JSON.parse(ips);
  263. ips = Array.isArray(ips) ? ips.join("\n") : ips;
  264. } catch (e) {
  265. console.error('Error parsing JSON:', e);
  266. }
  267. }
  268. document.getElementById("clientIPs").value = ips;
  269. },
  270. async clearDBClientIps(email) {
  271. try {
  272. const msg = await HttpUtil.post(`/panel/inbound/clearClientIps/${email}`);
  273. if (!msg.success) {
  274. return;
  275. }
  276. document.getElementById("clientIPs").value = "";
  277. } catch (error) {
  278. }
  279. },
  280. async resetClientTrafficHandler(client, dbInboundId, clients = []) {
  281. if (clients.length > 0) {
  282. const resetRequests = clients
  283. .filter(client => {
  284. const inbound = clientModal.dbInbounds.find(inbound => inbound.id === client.inboundId);
  285. return inbound && app.hasClientStats(inbound, client.email);
  286. }).map(client => ({ inboundId: client.inboundId, email: client.email}));
  287. return HttpUtil.postWithModalJson('/panel/inbound/resetGroupClientTraffic', resetRequests, null)
  288. } else {
  289. return HttpUtil.postWithModal('/panel/inbound/' + dbInboundId + '/resetClientTraffic/' + client.email)
  290. }
  291. },
  292. resetClientTraffic(client, dbInboundId, iconElement) {
  293. const subGroup = app.subSettings.enable && clientModal.group.isGroup && clientModal.group.canGroup && clientModal.dbInbounds && clientModal.dbInbounds.length > 0 ? app.getSubGroupClients(clientModal.dbInbounds, client) : [];
  294. const clients = subGroup && subGroup.clients && subGroup.clients.length > 1 ? subGroup.clients : [];
  295. this.$confirm({
  296. title: '{{ i18n "pages.inbounds.resetTraffic"}}',
  297. content: '{{ i18n "pages.inbounds.resetTrafficContent"}}',
  298. class: themeSwitcher.currentTheme,
  299. okText: '{{ i18n "reset"}}',
  300. cancelText: '{{ i18n "cancel"}}',
  301. onOk: async () => {
  302. iconElement.disabled = true;
  303. const msg = await this.resetClientTrafficHandler(client, dbInboundId, clients);
  304. if (msg && msg.success) {
  305. this.clientModal.clientStats.up = 0;
  306. this.clientModal.clientStats.down = 0;
  307. }
  308. iconElement.disabled = false;
  309. },
  310. })
  311. },
  312. },
  313. });
  314. </script>
  315. {{end}}