1
0

warp_modal.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. {{define "warpModal"}}
  2. <a-modal id="warp-modal" v-model="warpModal.visible" title="Cloudflare WARP"
  3. :confirm-loading="warpModal.confirmLoading" :closable="true" :mask-closable="true"
  4. :footer="null" :class="themeSwitcher.currentTheme">
  5. <template v-if="ObjectUtil.isEmpty(warpModal.warpData)">
  6. <a-button icon="api" @click="register" :loading="warpModal.confirmLoading">{{ i18n "pages.inbounds.create" }}</a-button>
  7. </template>
  8. <template v-else>
  9. <table style="margin: 5px 0; width: 100%;">
  10. <tr class="client-table-odd-row">
  11. <td>Access Token</td>
  12. <td>[[ warpModal.warpData.access_token ]]</td>
  13. </tr>
  14. <tr>
  15. <td>Device ID</td>
  16. <td>[[ warpModal.warpData.device_id ]]</td>
  17. </tr>
  18. <tr class="client-table-odd-row">
  19. <td>License Key</td>
  20. <td>[[ warpModal.warpData.license_key ]]</td>
  21. </tr>
  22. <tr>
  23. <td>Private Key</td>
  24. <td>[[ warpModal.warpData.private_key ]]</td>
  25. </tr>
  26. </table>
  27. <a-button @click="delConfig" :loading="warpModal.confirmLoading" type="danger">{{ i18n "delete" }}</a-button>
  28. <a-divider style="margin: 0;">{{ i18n "pages.xray.outbound.settings" }}</a-divider>
  29. <a-collapse style="margin: 10px 0;">
  30. <a-collapse-panel header='WARP/WARP+ License Key'>
  31. <a-form :colon="false" :label-col="{ md: {span:6} }" :wrapper-col="{ md: {span:14} }">
  32. <a-form-item label="Key">
  33. <a-input v-model="warpPlus"></a-input>
  34. <a-button @click="updateLicense(warpPlus)" :disabled="warpPlus.length<26"
  35. :loading="warpModal.confirmLoading">{{ i18n "pages.inbounds.update" }}</a-button>
  36. </a-form-item>
  37. </a-form>
  38. </a-collapse-panel>
  39. </a-collapse>
  40. <a-divider style="margin: 0;">{{ i18n "pages.xray.outbound.accountInfo" }}</a-divider>
  41. <a-button icon="sync" @click="getConfig" style="margin-top: 5px; margin-bottom: 10px;"
  42. :loading="warpModal.confirmLoading" type="primary">{{ i18n "info" }}</a-button>
  43. <template v-if="!ObjectUtil.isEmpty(warpModal.warpConfig)">
  44. <table style="width: 100%">
  45. <tr class="client-table-odd-row">
  46. <td>Device Name</td>
  47. <td>[[ warpModal.warpConfig.name ]]</td>
  48. </tr>
  49. <tr>
  50. <td>Device Model</td>
  51. <td>[[ warpModal.warpConfig.model ]]</td>
  52. </tr>
  53. <tr class="client-table-odd-row">
  54. <td>Device Enabled</td>
  55. <td>[[ warpModal.warpConfig.enabled ]]</td>
  56. </tr>
  57. <template v-if="!ObjectUtil.isEmpty(warpModal.warpConfig.account)">
  58. <tr>
  59. <td>Account Type</td>
  60. <td>[[ warpModal.warpConfig.account.account_type ]]</td>
  61. </tr>
  62. <tr class="client-table-odd-row">
  63. <td>Role</td>
  64. <td>[[ warpModal.warpConfig.account.role ]]</td>
  65. </tr>
  66. <tr>
  67. <td>WARP+ Data</td>
  68. <td>[[ sizeFormat(warpModal.warpConfig.account.premium_data) ]]</td>
  69. </tr>
  70. <tr class="client-table-odd-row">
  71. <td>Quota</td>
  72. <td>[[ sizeFormat(warpModal.warpConfig.account.quota) ]]</td>
  73. </tr>
  74. <tr v-if="!ObjectUtil.isEmpty(warpModal.warpConfig.account.usage)">
  75. <td>Usage</td>
  76. <td>[[ sizeFormat(warpModal.warpConfig.account.usage) ]]</td>
  77. </tr>
  78. </template>
  79. </table>
  80. <a-divider style="margin: 10px 0;">{{ i18n "pages.xray.outbound.outboundStatus" }}</a-divider>
  81. <a-form :label-col="{ md: {span:8} }" :wrapper-col="{ md: {span:14} }">
  82. <template v-if="warpOutboundIndex>=0">
  83. <a-tag color="green" style="line-height: 31px;">{{ i18n "enabled" }}</a-tag>
  84. <a-button @click="resetOutbound" :loading="warpModal.confirmLoading" type="danger">{{ i18n "reset" }}</a-button>
  85. </template>
  86. <template v-else>
  87. <a-tag color="orange" style="line-height: 31px;">{{ i18n "disabled" }}</a-tag>
  88. <a-button @click="addOutbound" :loading="warpModal.confirmLoading" type="primary">{{ i18n "pages.xray.outbound.addOutbound" }}</a-button>
  89. </template>
  90. </a-form-item>
  91. </a-form>
  92. </template>
  93. </template>
  94. </a-modal>
  95. <script>
  96. const warpModal = {
  97. visible: false,
  98. confirmLoading: false,
  99. warpData: null,
  100. warpConfig: null,
  101. warpOutbound: null,
  102. show() {
  103. this.visible = true;
  104. this.warpConfig = null;
  105. this.getData();
  106. },
  107. close() {
  108. this.visible = false;
  109. this.loading(false);
  110. },
  111. loading(loading = true) {
  112. this.confirmLoading = loading;
  113. },
  114. async getData() {
  115. this.loading(true);
  116. const msg = await HttpUtil.post('/panel/xray/warp/data');
  117. this.loading(false);
  118. if (msg.success) {
  119. this.warpData = msg.obj.length > 0 ? JSON.parse(msg.obj) : null;
  120. }
  121. },
  122. };
  123. new Vue({
  124. delimiters: ['[[', ']]'],
  125. el: '#warp-modal',
  126. data: {
  127. warpModal: warpModal,
  128. warpPlus: '',
  129. },
  130. methods: {
  131. collectConfig() {
  132. config = warpModal.warpConfig.config;
  133. peer = config.peers[0];
  134. if (config) {
  135. warpModal.warpOutbound = Outbound.fromJson({
  136. tag: 'warp',
  137. protocol: Protocols.Wireguard,
  138. settings: {
  139. mtu: 1420,
  140. secretKey: warpModal.warpData.private_key,
  141. address: this.getAddresses(config.interface.addresses),
  142. reserved: this.getResolved(config.client_id),
  143. domainStrategy: 'ForceIP',
  144. peers: [{
  145. publicKey: peer.public_key,
  146. endpoint: peer.endpoint.host,
  147. }],
  148. noKernelTun: false,
  149. }
  150. });
  151. }
  152. },
  153. getAddresses(addrs) {
  154. let addresses = [];
  155. if (addrs.v4) addresses.push(addrs.v4 + "/32");
  156. if (addrs.v6) addresses.push(addrs.v6 + "/128");
  157. return addresses;
  158. },
  159. getResolved(client_id) {
  160. let reserved = [];
  161. let decoded = atob(client_id);
  162. let hexString = '';
  163. for (let i = 0; i < decoded.length; i++) {
  164. let hex = decoded.charCodeAt(i).toString(16);
  165. hexString += (hex.length === 1 ? '0' : '') + hex;
  166. }
  167. for (let i = 0; i < hexString.length; i += 2) {
  168. let hexByte = hexString.slice(i, i + 2);
  169. let decValue = parseInt(hexByte, 16);
  170. reserved.push(decValue);
  171. }
  172. return reserved;
  173. },
  174. async register() {
  175. warpModal.loading(true);
  176. keys = Wireguard.generateKeypair();
  177. const msg = await HttpUtil.post('/panel/xray/warp/reg', keys);
  178. if (msg.success) {
  179. resp = JSON.parse(msg.obj);
  180. warpModal.warpData = resp.data;
  181. warpModal.warpConfig = resp.config;
  182. this.collectConfig();
  183. }
  184. warpModal.loading(false);
  185. },
  186. async updateLicense(l) {
  187. warpModal.loading(true);
  188. const msg = await HttpUtil.post('/panel/xray/warp/license', { license: l });
  189. if (msg.success) {
  190. warpModal.warpData = JSON.parse(msg.obj);
  191. warpModal.warpConfig = null;
  192. this.warpPlus = '';
  193. }
  194. warpModal.loading(false);
  195. },
  196. async getConfig() {
  197. warpModal.loading(true);
  198. const msg = await HttpUtil.post('/panel/xray/warp/config');
  199. warpModal.loading(false);
  200. if (msg.success) {
  201. warpModal.warpConfig = JSON.parse(msg.obj);
  202. this.collectConfig();
  203. }
  204. },
  205. async delConfig() {
  206. warpModal.loading(true);
  207. const msg = await HttpUtil.post('/panel/xray/warp/del');
  208. warpModal.loading(false);
  209. if (msg.success) {
  210. warpModal.warpData = null;
  211. warpModal.warpConfig = null;
  212. this.delOutbound();
  213. }
  214. },
  215. addOutbound() {
  216. app.templateSettings.outbounds.push(warpModal.warpOutbound.toJson());
  217. app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
  218. warpModal.close();
  219. },
  220. resetOutbound() {
  221. app.templateSettings.outbounds[this.warpOutboundIndex] = warpModal.warpOutbound.toJson();
  222. app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
  223. warpModal.close();
  224. },
  225. delOutbound() {
  226. if (this.warpOutboundIndex != -1) {
  227. app.templateSettings.outbounds.splice(this.warpOutboundIndex, 1);
  228. app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
  229. }
  230. warpModal.close();
  231. }
  232. },
  233. computed: {
  234. warpOutboundIndex: {
  235. get: function () {
  236. return app.templateSettings ? app.templateSettings.outbounds.findIndex((o) => o.tag == 'warp') : -1;
  237. }
  238. }
  239. }
  240. });
  241. </script>
  242. {{end}}