client_modal.html 14 KB

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