warp_modal.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. kernelMode: false,
  149. kernelTun: false,
  150. }
  151. });
  152. }
  153. },
  154. getAddresses(addrs) {
  155. let addresses = [];
  156. if (addrs.v4) addresses.push(addrs.v4 + "/32");
  157. if (addrs.v6) addresses.push(addrs.v6 + "/128");
  158. return addresses;
  159. },
  160. getResolved(client_id) {
  161. let reserved = [];
  162. let decoded = atob(client_id);
  163. let hexString = '';
  164. for (let i = 0; i < decoded.length; i++) {
  165. let hex = decoded.charCodeAt(i).toString(16);
  166. hexString += (hex.length === 1 ? '0' : '') + hex;
  167. }
  168. for (let i = 0; i < hexString.length; i += 2) {
  169. let hexByte = hexString.slice(i, i + 2);
  170. let decValue = parseInt(hexByte, 16);
  171. reserved.push(decValue);
  172. }
  173. return reserved;
  174. },
  175. async register() {
  176. warpModal.loading(true);
  177. keys = Wireguard.generateKeypair();
  178. const msg = await HttpUtil.post('/panel/xray/warp/reg', keys);
  179. if (msg.success) {
  180. resp = JSON.parse(msg.obj);
  181. warpModal.warpData = resp.data;
  182. warpModal.warpConfig = resp.config;
  183. this.collectConfig();
  184. }
  185. warpModal.loading(false);
  186. },
  187. async updateLicense(l) {
  188. warpModal.loading(true);
  189. const msg = await HttpUtil.post('/panel/xray/warp/license', { license: l });
  190. if (msg.success) {
  191. warpModal.warpData = JSON.parse(msg.obj);
  192. warpModal.warpConfig = null;
  193. this.warpPlus = '';
  194. }
  195. warpModal.loading(false);
  196. },
  197. async getConfig() {
  198. warpModal.loading(true);
  199. const msg = await HttpUtil.post('/panel/xray/warp/config');
  200. warpModal.loading(false);
  201. if (msg.success) {
  202. warpModal.warpConfig = JSON.parse(msg.obj);
  203. this.collectConfig();
  204. }
  205. },
  206. async delConfig() {
  207. warpModal.loading(true);
  208. const msg = await HttpUtil.post('/panel/xray/warp/del');
  209. warpModal.loading(false);
  210. if (msg.success) {
  211. warpModal.warpData = null;
  212. warpModal.warpConfig = null;
  213. this.delOutbound();
  214. }
  215. },
  216. addOutbound() {
  217. app.templateSettings.outbounds.push(warpModal.warpOutbound.toJson());
  218. app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
  219. warpModal.close();
  220. },
  221. resetOutbound() {
  222. app.templateSettings.outbounds[this.warpOutboundIndex] = warpModal.warpOutbound.toJson();
  223. app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
  224. warpModal.close();
  225. },
  226. delOutbound() {
  227. if (this.warpOutboundIndex != -1) {
  228. app.templateSettings.outbounds.splice(this.warpOutboundIndex, 1);
  229. app.outboundSettings = JSON.stringify(app.templateSettings.outbounds);
  230. }
  231. warpModal.close();
  232. }
  233. },
  234. computed: {
  235. warpOutboundIndex: {
  236. get: function () {
  237. return app.templateSettings ? app.templateSettings.outbounds.findIndex((o) => o.tag == 'warp') : -1;
  238. }
  239. }
  240. }
  241. });
  242. </script>
  243. {{end}}