1
0

warp_modal.html 11 KB

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