client_modal.html 14 KB

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