client_modal.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. group: {
  18. canGroup: true,
  19. isGroup: false,
  20. currentClient: null,
  21. inbounds: [],
  22. clients: [],
  23. editIds: []
  24. },
  25. dbInbound: new DBInbound(),
  26. dbInbounds: null,
  27. inbound: new Inbound(),
  28. clients: [],
  29. clientStats: [],
  30. oldClientId: "",
  31. index: null,
  32. clientIps: null,
  33. delayedStart: false,
  34. ok() {
  35. if (clientModal.group.isGroup && clientModal.group.canGroup) {
  36. const currentClient = clientModal.group.currentClient;
  37. clientModal.group.clients.forEach((client, index) => {
  38. const { email, limitIp, totalGB, expiryTime, reset, enable, subId, tgId, flow } = currentClient;
  39. const match = email.match(/^(.*?)__/);
  40. const new_email = match ? match[1] : email;
  41. client.email = `${new_email}__${index + 1}`;
  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({ title = '', okText = '{{ i18n "sure" }}', index = null, dbInbound = null, dbInbounds = null, confirm = () => { }, isEdit = false }) {
  71. this.group = {
  72. canGroup: true,
  73. isGroup: false,
  74. currentClient: null,
  75. inbounds: [],
  76. clients: [],
  77. editIds: []
  78. }
  79. this.dbInbounds = dbInbounds;
  80. this.visible = true;
  81. this.title = title;
  82. this.okText = okText;
  83. this.isEdit = isEdit;
  84. if (dbInbounds !== null && Array.isArray(dbInbounds)) {
  85. if (isEdit) {
  86. this.showProcess(dbInbound, index);
  87. let processSingleEdit = true
  88. if (this.group.canGroup){
  89. this.group.currentClient = this.clients[this.index]
  90. const response = this.getGroupInboundsClients(dbInbounds,this.group.currentClient)
  91. if (response.clients.length > 1){
  92. this.group.isGroup = true;
  93. this.group.inbounds = response.inbounds
  94. this.group.clients = response.clients
  95. this.group.editIds = response.editIds
  96. if (this.clients[index].expiryTime < 0) {
  97. this.delayedStart = true;
  98. }
  99. processSingleEdit = false
  100. }
  101. }
  102. if(processSingleEdit){
  103. this.singleEditClientProcess(index)
  104. }
  105. } else {
  106. this.group.isGroup = true;
  107. dbInbounds.forEach((dbInboundItem) => {
  108. this.showProcess(dbInboundItem);
  109. this.addClient(this.inbound.protocol, this.clients);
  110. this.group.inbounds.push(dbInboundItem.id)
  111. this.group.clients.push(this.clients[this.index])
  112. })
  113. this.group.currentClient = this.clients[this.index]
  114. }
  115. } else {
  116. this.showProcess(dbInbound, index);
  117. if (isEdit) {
  118. this.singleEditClientProcess(index)
  119. } else {
  120. this.addClient(this.inbound.protocol, this.clients);
  121. }
  122. }
  123. this.clientStats = this.dbInbound.clientStats.find(row => row.email === this.clients[this.index].email);
  124. this.confirm = confirm;
  125. },
  126. showProcess(dbInbound, index = null) {
  127. this.dbInbound = new DBInbound(dbInbound);
  128. this.inbound = dbInbound.toInbound();
  129. this.clients = this.inbound.clients;
  130. this.index = index === null ? this.clients.length : index;
  131. this.delayedStart = false;
  132. },
  133. singleEditClientProcess(index) {
  134. if (this.clients[index].expiryTime < 0) {
  135. this.delayedStart = true;
  136. }
  137. this.oldClientId = this.getClientId(this.dbInbound.protocol, this.clients[index]);
  138. },
  139. getGroupInboundsClients(dbInbounds, currentClient) {
  140. const response = {
  141. inbounds: [],
  142. clients: [],
  143. editIds: []
  144. }
  145. dbInbounds.forEach((dbInboundItem) => {
  146. const dbInbound = new DBInbound(dbInboundItem);
  147. const inbound = dbInbound.toInbound();
  148. const clients = inbound.clients;
  149. if (clients.length > 0){
  150. clients.forEach((client) => {
  151. if (client['subId'] === currentClient['subId']){
  152. response.inbounds.push(dbInboundItem.id)
  153. response.clients.push(client)
  154. response.editIds.push(this.getClientId(dbInbound.protocol, client))
  155. }
  156. })
  157. }
  158. })
  159. return response;
  160. },
  161. getClientId(protocol, client) {
  162. switch (protocol) {
  163. case Protocols.TROJAN: return client.password;
  164. case Protocols.SHADOWSOCKS: return client.email;
  165. default: return client.id;
  166. }
  167. },
  168. addClient(protocol, clients) {
  169. switch (protocol) {
  170. case Protocols.VMESS: return clients.push(new Inbound.VmessSettings.Vmess());
  171. case Protocols.VLESS: return clients.push(new Inbound.VLESSSettings.VLESS());
  172. case Protocols.TROJAN: return clients.push(new Inbound.TrojanSettings.Trojan());
  173. case Protocols.SHADOWSOCKS: return clients.push(new Inbound.ShadowsocksSettings.Shadowsocks(clients[0].method));
  174. default: return null;
  175. }
  176. },
  177. close() {
  178. clientModal.visible = false;
  179. clientModal.loading(false);
  180. },
  181. loading(loading) {
  182. clientModal.confirmLoading = loading;
  183. },
  184. };
  185. const clientModalApp = new Vue({
  186. delimiters: ['[[', ']]'],
  187. el: '#client-modal',
  188. data: {
  189. clientModal,
  190. get inbound() {
  191. return this.clientModal.inbound;
  192. },
  193. get client() {
  194. return this.clientModal.clients[this.clientModal.index];
  195. },
  196. get clientStats() {
  197. return this.clientModal.clientStats;
  198. },
  199. get isEdit() {
  200. return this.clientModal.isEdit;
  201. },
  202. get isGroup() {
  203. return this.clientModal.group.isGroup;
  204. },
  205. get isGroupEdit() {
  206. return this.clientModal.group.canGroup;
  207. },
  208. set isGroupEdit(value) {
  209. this.clientModal.group.canGroup = value;
  210. if (!value){
  211. this.clientModal.singleEditClientProcess(this.clientModal.index)
  212. }
  213. },
  214. get datepicker() {
  215. return app.datepicker;
  216. },
  217. get isTrafficExhausted() {
  218. if (!clientStats) return false
  219. if (clientStats.total <= 0) return false
  220. if (clientStats.up + clientStats.down < clientStats.total) return false
  221. return true
  222. },
  223. get isExpiry() {
  224. return this.clientModal.isEdit && this.client.expiryTime >0 ? (this.client.expiryTime < new Date().getTime()) : false;
  225. },
  226. get delayedStart() {
  227. return this.clientModal.delayedStart;
  228. },
  229. set delayedStart(value) {
  230. this.clientModal.delayedStart = value;
  231. },
  232. get delayedExpireDays() {
  233. return this.client && this.client.expiryTime < 0 ? this.client.expiryTime / -86400000 : 0;
  234. },
  235. set delayedExpireDays(days) {
  236. this.client.expiryTime = -86400000 * days;
  237. },
  238. },
  239. methods: {
  240. async getDBClientIps(email) {
  241. const msg = await HttpUtil.post(`/panel/inbound/clientIps/${email}`);
  242. if (!msg.success) {
  243. document.getElementById("clientIPs").value = msg.obj;
  244. return;
  245. }
  246. let ips = msg.obj;
  247. if (typeof ips === 'string' && ips.startsWith('[') && ips.endsWith(']')) {
  248. try {
  249. ips = JSON.parse(ips);
  250. ips = Array.isArray(ips) ? ips.join("\n") : ips;
  251. } catch (e) {
  252. console.error('Error parsing JSON:', e);
  253. }
  254. }
  255. document.getElementById("clientIPs").value = ips;
  256. },
  257. async clearDBClientIps(email) {
  258. try {
  259. const msg = await HttpUtil.post(`/panel/inbound/clearClientIps/${email}`);
  260. if (!msg.success) {
  261. return;
  262. }
  263. document.getElementById("clientIPs").value = "";
  264. } catch (error) {
  265. }
  266. },
  267. resetClientTraffic(email, dbInboundId, iconElement) {
  268. this.$confirm({
  269. title: '{{ i18n "pages.inbounds.resetTraffic"}}',
  270. content: '{{ i18n "pages.inbounds.resetTrafficContent"}}',
  271. class: themeSwitcher.currentTheme,
  272. okText: '{{ i18n "reset"}}',
  273. cancelText: '{{ i18n "cancel"}}',
  274. onOk: async () => {
  275. iconElement.disabled = true;
  276. const msg = await HttpUtil.postWithModal('/panel/inbound/' + dbInboundId + '/resetClientTraffic/' + email);
  277. if (msg.success) {
  278. this.clientModal.clientStats.up = 0;
  279. this.clientModal.clientStats.down = 0;
  280. }
  281. iconElement.disabled = false;
  282. },
  283. })
  284. },
  285. },
  286. });
  287. </script>
  288. {{end}}