1
0

xray.html 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. {{ template "page/head_start" .}}
  2. <link rel="stylesheet"
  3. href="{{ .base_path }}assets/codemirror/codemirror.min.css?{{ .cur_ver }}">
  4. <link rel="stylesheet"
  5. href="{{ .base_path }}assets/codemirror/fold/foldgutter.css">
  6. <link rel="stylesheet"
  7. href="{{ .base_path }}assets/codemirror/xq.min.css?{{ .cur_ver }}">
  8. <link rel="stylesheet" href="{{ .base_path }}assets/codemirror/lint/lint.css">
  9. {{ template "page/head_end" .}}
  10. {{ template "page/body_start" .}}
  11. <a-layout id="app" v-cloak :class="themeSwitcher.currentTheme + ' xray-page'">
  12. <a-sidebar></a-sidebar>
  13. <a-layout id="content-layout">
  14. <a-layout-content>
  15. <a-spin :spinning="loadingStates.spinning" :delay="500"
  16. tip='{{ i18n "loading"}}'>
  17. <transition name="list" appear>
  18. <a-alert type="error" v-if="showAlert && loadingStates.fetched"
  19. :style="{ marginBottom: '10px' }"
  20. message='{{ i18n "secAlertTitle" }}' color="red"
  21. description='{{ i18n "secAlertSsl" }}' show-icon closable>
  22. </a-alert>
  23. </transition>
  24. <transition name="list" appear>
  25. <a-row v-if="!loadingStates.fetched">
  26. <a-card
  27. :style="{ textAlign: 'center', padding: '30px 0', marginTop: '10px', background: 'transparent', border: 'none' }">
  28. <a-spin tip='{{ i18n "loading" }}'></a-spin>
  29. </a-card>
  30. </a-row>
  31. <a-row :gutter="[isMobile ? 8 : 16, isMobile ? 0 : 12]" v-else>
  32. <a-col>
  33. <a-card hoverable>
  34. <a-row
  35. :style="{ display: 'flex', flexWrap: 'wrap', alignItems: 'center' }">
  36. <a-col :xs="24" :sm="10" :style="{ padding: '4px' }">
  37. <a-space direction="horizontal">
  38. <a-button type="primary" :disabled="saveBtnDisable"
  39. @click="updateXraySetting">
  40. {{ i18n "pages.xray.save" }}
  41. </a-button>
  42. <a-button type="danger" :disabled="!saveBtnDisable"
  43. @click="restartXray">
  44. {{ i18n "pages.xray.restart" }}
  45. </a-button>
  46. <a-popover v-if="restartResult"
  47. :overlay-class-name="themeSwitcher.currentTheme">
  48. <span slot="title">{{ i18n
  49. "pages.index.xrayErrorPopoverTitle" }}</span>
  50. <template slot="content">
  51. <span :style="{ maxWidth: '400px' }"
  52. v-for="line in restartResult.split('\n')">[[ line
  53. ]]</span>
  54. </template>
  55. <a-icon type="question-circle"></a-icon>
  56. </a-popover>
  57. </a-space>
  58. </a-col>
  59. <a-col :xs="24" :sm="14">
  60. <template>
  61. <div>
  62. <a-back-top
  63. :target="() => document.getElementById('content-layout')"
  64. visibility-height="200"></a-back-top>
  65. <a-alert type="warning"
  66. :style="{ float: 'right', width: 'fit-content' }"
  67. message='{{ i18n "pages.settings.infoDesc" }}'
  68. show-icon>
  69. </a-alert>
  70. </div>
  71. </template>
  72. </a-col>
  73. </a-row>
  74. </a-card>
  75. </a-col>
  76. <a-col>
  77. <a-tabs default-active-key="tpl-basic"
  78. @change="(activeKey) => { this.changePage(activeKey); }"
  79. :class="themeSwitcher.currentTheme">
  80. <a-tab-pane key="tpl-basic" :style="{ paddingTop: '20px' }">
  81. <template #tab>
  82. <a-icon type="setting"></a-icon>
  83. <span>{{ i18n "pages.xray.basicTemplate"}}</span>
  84. </template>
  85. {{ template "settings/xray/basics" . }}
  86. </a-tab-pane>
  87. <a-tab-pane key="tpl-routing" :style="{ paddingTop: '20px' }">
  88. <template #tab>
  89. <a-icon type="swap"></a-icon>
  90. <span>{{ i18n "pages.xray.Routings"}}</span>
  91. </template>
  92. {{ template "settings/xray/routing" . }}
  93. </a-tab-pane>
  94. <a-tab-pane key="tpl-outbound" force-render="true">
  95. <template #tab>
  96. <a-icon type="upload"></a-icon>
  97. <span>{{ i18n "pages.xray.Outbounds"}}</span>
  98. </template>
  99. {{ template "settings/xray/outbounds" . }}
  100. </a-tab-pane>
  101. <a-tab-pane key="tpl-reverse" :style="{ paddingTop: '20px' }"
  102. force-render="true">
  103. <template #tab>
  104. <a-icon type="import"></a-icon>
  105. <span>{{ i18n "pages.xray.outbound.reverse"}}</span>
  106. </template>
  107. {{ template "settings/xray/reverse" . }}
  108. </a-tab-pane>
  109. <a-tab-pane key="tpl-balancer" :style="{ paddingTop: '20px' }"
  110. force-render="true">
  111. <template #tab>
  112. <a-icon type="cluster"></a-icon>
  113. <span>{{ i18n "pages.xray.Balancers"}}</span>
  114. </template>
  115. {{ template "settings/xray/balancers" . }}
  116. </a-tab-pane>
  117. <a-tab-pane key="tpl-dns" :style="{ paddingTop: '20px' }"
  118. force-render="true">
  119. <template #tab>
  120. <a-icon type="database"></a-icon>
  121. <span>DNS</span>
  122. </template>
  123. {{ template "settings/xray/dns" . }}
  124. </a-tab-pane>
  125. <a-tab-pane key="tpl-advanced" force-render="true">
  126. <template #tab>
  127. <a-icon type="code"></a-icon>
  128. <span>{{ i18n "pages.xray.advancedTemplate"}}</span>
  129. </template>
  130. {{ template "settings/xray/advanced" . }}
  131. </a-tab-pane>
  132. </a-tabs>
  133. </a-col>
  134. </a-row>
  135. </transition>
  136. </a-spin>
  137. </a-layout-content>
  138. </a-layout>
  139. </a-layout>
  140. {{template "page/body_scripts" .}}
  141. <script
  142. src="{{ .base_path }}assets/js/model/outbound.js?{{ .cur_ver }}"></script>
  143. <script
  144. src="{{ .base_path }}assets/codemirror/codemirror.min.js?{{ .cur_ver }}"></script>
  145. <script src="{{ .base_path }}assets/codemirror/javascript.js"></script>
  146. <script src="{{ .base_path }}assets/codemirror/jshint.js"></script>
  147. <script src="{{ .base_path }}assets/codemirror/jsonlint.js"></script>
  148. <script src="{{ .base_path }}assets/codemirror/lint/lint.js"></script>
  149. <script
  150. src="{{ .base_path }}assets/codemirror/lint/javascript-lint.js"></script>
  151. <script
  152. src="{{ .base_path }}assets/codemirror/hint/javascript-hint.js"></script>
  153. <script src="{{ .base_path }}assets/codemirror/fold/foldcode.js"></script>
  154. <script src="{{ .base_path }}assets/codemirror/fold/foldgutter.js"></script>
  155. <script src="{{ .base_path }}assets/codemirror/fold/brace-fold.js"></script>
  156. {{template "component/aSidebar" .}}
  157. {{template "component/aThemeSwitch" .}}
  158. {{template "component/aTableSortable" .}}
  159. {{template "component/aSettingListItem" .}}
  160. {{template "modals/ruleModal"}}
  161. {{template "modals/outModal"}}
  162. {{template "modals/reverseModal"}}
  163. {{template "modals/balancerModal"}}
  164. {{template "modals/dnsModal"}}
  165. {{template "modals/dnsPresetsModal"}}
  166. {{template "modals/fakednsModal"}}
  167. {{template "modals/warpModal"}}
  168. <script>
  169. const rulesColumns = [
  170. { title: "#", align: 'center', width: 15, scopedSlots: { customRender: 'action' } },
  171. {
  172. title: '{{ i18n "pages.xray.rules.source"}}', children: [
  173. { title: 'IP', dataIndex: "sourceIP", align: 'center', width: 20, ellipsis: true },
  174. { title: '{{ i18n "pages.inbounds.port" }}', dataIndex: 'sourcePort', align: 'center', width: 10, ellipsis: true },
  175. { title: 'VLESS Route', dataIndex: 'vlessRoute', align: 'center', width: 15, ellipsis: true }]
  176. },
  177. {
  178. title: '{{ i18n "pages.inbounds.network"}}', children: [
  179. { title: 'L4', dataIndex: 'network', align: 'center', width: 10 },
  180. { title: '{{ i18n "protocol" }}', dataIndex: 'protocol', align: 'center', width: 15, ellipsis: true },
  181. { title: 'Attrs', dataIndex: 'attrs', align: 'center', width: 10, ellipsis: true }]
  182. },
  183. {
  184. title: '{{ i18n "pages.xray.rules.dest"}}', children: [
  185. { title: 'IP', dataIndex: 'ip', align: 'center', width: 20, ellipsis: true },
  186. { title: '{{ i18n "pages.xray.outbound.domain" }}', dataIndex: 'domain', align: 'center', width: 20, ellipsis: true },
  187. { title: '{{ i18n "pages.inbounds.port" }}', dataIndex: 'port', align: 'center', width: 10, ellipsis: true }]
  188. },
  189. {
  190. title: '{{ i18n "pages.xray.rules.inbound"}}', children: [
  191. { title: '{{ i18n "pages.xray.outbound.tag" }}', dataIndex: 'inboundTag', align: 'center', width: 15, ellipsis: true },
  192. { title: '{{ i18n "pages.inbounds.client" }}', dataIndex: 'user', align: 'center', width: 20, ellipsis: true }]
  193. },
  194. { title: '{{ i18n "pages.xray.rules.outbound"}}', dataIndex: 'outboundTag', align: 'center', width: 17 },
  195. { title: '{{ i18n "pages.xray.rules.balancer"}}', dataIndex: 'balancerTag', align: 'center', width: 15 },
  196. ];
  197. const rulesMobileColumns = [
  198. { title: "#", align: 'center', width: 20, scopedSlots: { customRender: 'action' } },
  199. { title: '{{ i18n "pages.xray.rules.inbound"}}', align: 'center', width: 50, ellipsis: true, scopedSlots: { customRender: 'inbound' } },
  200. { title: '{{ i18n "pages.xray.rules.outbound"}}', align: 'center', width: 50, ellipsis: true, scopedSlots: { customRender: 'outbound' } },
  201. { title: '{{ i18n "pages.xray.rules.info"}}', align: 'center', width: 50, ellipsis: true, scopedSlots: { customRender: 'info' } },
  202. ];
  203. const outboundColumns = [
  204. { title: "#", align: 'center', width: 60, scopedSlots: { customRender: 'action' } },
  205. { title: '{{ i18n "pages.xray.outbound.tag"}}', dataIndex: 'tag', align: 'center', width: 50 },
  206. { title: '{{ i18n "protocol"}}', align: 'center', width: 50, scopedSlots: { customRender: 'protocol' } },
  207. { title: '{{ i18n "pages.xray.outbound.address"}}', align: 'center', width: 50, scopedSlots: { customRender: 'address' } },
  208. { title: '{{ i18n "pages.inbounds.traffic" }}', align: 'center', width: 50, scopedSlots: { customRender: 'traffic' } },
  209. { title: '{{ i18n "pages.xray.outbound.testResult" }}', align: 'center', width: 120, scopedSlots: { customRender: 'testResult' } },
  210. { title: '{{ i18n "pages.xray.outbound.test" }}', align: 'center', width: 60, scopedSlots: { customRender: 'test' } },
  211. ];
  212. const reverseColumns = [
  213. { title: "#", align: 'center', width: 20, scopedSlots: { customRender: 'action' } },
  214. { title: '{{ i18n "pages.xray.outbound.type"}}', dataIndex: 'type', align: 'center', width: 50 },
  215. { title: '{{ i18n "pages.xray.outbound.tag"}}', dataIndex: 'tag', align: 'center', width: 50 },
  216. { title: '{{ i18n "pages.xray.outbound.domain"}}', dataIndex: 'domain', align: 'center', width: 50 },
  217. ];
  218. const balancerColumns = [
  219. { title: "#", align: 'center', width: 20, scopedSlots: { customRender: 'action' } },
  220. { title: '{{ i18n "pages.xray.balancer.tag"}}', dataIndex: 'tag', align: 'center', width: 50 },
  221. { title: '{{ i18n "pages.xray.balancer.balancerStrategy"}}', align: 'center', width: 50, scopedSlots: { customRender: 'strategy' } },
  222. { title: '{{ i18n "pages.xray.balancer.balancerSelectors"}}', align: 'center', width: 100, scopedSlots: { customRender: 'selector' } },
  223. ];
  224. const dnsColumns = [
  225. { title: "#", align: 'center', width: 20, scopedSlots: { customRender: 'action' } },
  226. { title: '{{ i18n "pages.xray.outbound.address"}}', align: 'center', width: 50, scopedSlots: { customRender: 'address' } },
  227. { title: '{{ i18n "pages.xray.dns.domains"}}', align: 'center', width: 50, scopedSlots: { customRender: 'domain' } },
  228. { title: '{{ i18n "pages.xray.dns.expectIPs"}}', align: 'center', width: 50, scopedSlots: { customRender: 'expectIPs' } },
  229. ];
  230. const fakednsColumns = [
  231. { title: "#", align: 'center', width: 20, scopedSlots: { customRender: 'action' } },
  232. { title: '{{ i18n "pages.xray.fakedns.ipPool"}}', dataIndex: 'ipPool', align: 'center', width: 50 },
  233. { title: '{{ i18n "pages.xray.fakedns.poolSize"}}', dataIndex: 'poolSize', align: 'center', width: 50 },
  234. ];
  235. const app = new Vue({
  236. delimiters: ['[[', ']]'],
  237. mixins: [MediaQueryMixin],
  238. el: '#app',
  239. data: {
  240. themeSwitcher,
  241. isDarkTheme: themeSwitcher.isDarkTheme,
  242. loadingStates: {
  243. fetched: false,
  244. spinning: false
  245. },
  246. oldXraySetting: '',
  247. xraySetting: '',
  248. outboundTestUrl: 'https://www.google.com/generate_204',
  249. oldOutboundTestUrl: 'https://www.google.com/generate_204',
  250. inboundTags: [],
  251. outboundsTraffic: [],
  252. outboundTestStates: {}, // Track testing state and results for each outbound
  253. saveBtnDisable: true,
  254. refreshing: false,
  255. restartResult: '',
  256. showAlert: false,
  257. advSettings: 'xraySetting',
  258. obsSettings: '',
  259. cm: null,
  260. cmOptions: {
  261. lineNumbers: true,
  262. mode: "application/json",
  263. lint: true,
  264. styleActiveLine: true,
  265. matchBrackets: true,
  266. theme: "xq",
  267. autoCloseTags: true,
  268. lineWrapping: true,
  269. indentUnit: 2,
  270. indentWithTabs: true,
  271. smartIndent: true,
  272. tabSize: 2,
  273. lineWiseCopyCut: false,
  274. foldGutter: true,
  275. gutters: [
  276. "CodeMirror-lint-markers",
  277. "CodeMirror-linenumbers",
  278. "CodeMirror-foldgutter",
  279. ],
  280. },
  281. ipv4Settings: {
  282. tag: "IPv4",
  283. protocol: "freedom",
  284. settings: {
  285. domainStrategy: "UseIPv4"
  286. }
  287. },
  288. directSettings: {
  289. tag: "direct",
  290. protocol: "freedom"
  291. },
  292. routingDomainStrategies: ["AsIs", "IPIfNonMatch", "IPOnDemand"],
  293. log: {
  294. loglevel: ["none", "debug", "info", "warning", "error"],
  295. access: ["none", "./access.log"],
  296. error: ["none", "./error.log"],
  297. dnsLog: false,
  298. maskAddress: ["quarter", "half", "full"],
  299. },
  300. settingsData: {
  301. protocols: {
  302. bittorrent: ["bittorrent"],
  303. },
  304. IPsOptions: [
  305. { label: 'Private IPs', value: 'geoip:private' },
  306. { label: '🇮🇷 Iran', value: 'ext:geoip_IR.dat:ir' },
  307. { label: '🇨🇳 China', value: 'geoip:cn' },
  308. { label: '🇷🇺 Russia', value: 'ext:geoip_RU.dat:ru' },
  309. { label: '🇻🇳 Vietnam', value: 'geoip:vn' },
  310. { label: '🇪🇸 Spain', value: 'geoip:es' },
  311. { label: '🇮🇩 Indonesia', value: 'geoip:id' },
  312. { label: '🇺🇦 Ukraine', value: 'geoip:ua' },
  313. { label: '🇹🇷 Türkiye', value: 'geoip:tr' },
  314. { label: '🇧🇷 Brazil', value: 'geoip:br' },
  315. ],
  316. DomainsOptions: [
  317. { label: '🇮🇷 Iran', value: 'ext:geosite_IR.dat:ir' },
  318. { label: '🇮🇷 .ir', value: 'regexp:.*\\.ir$' },
  319. { label: '🇮🇷 .ایران', value: 'regexp:.*\\.xn--mgba3a4f16a$' },
  320. { label: '🇨🇳 China', value: 'geosite:cn' },
  321. { label: '🇨🇳 .cn', value: 'regexp:.*\\.cn$' },
  322. { label: '🇷🇺 Russia', value: 'ext:geosite_RU.dat:ru-available-only-inside' },
  323. { label: '🇷🇺 .ru', value: 'regexp:.*\\.ru$' },
  324. { label: '🇷🇺 .su', value: 'regexp:.*\\.su$' },
  325. { label: '🇷🇺 .рф', value: 'regexp:.*\\.xn--p1ai$' },
  326. { label: '🇻🇳 .vn', value: 'regexp:.*\\.vn$' },
  327. ],
  328. BlockDomainsOptions: [
  329. { label: 'Ads All', value: 'geosite:category-ads-all' },
  330. { label: 'Ads IR 🇮🇷', value: 'ext:geosite_IR.dat:category-ads-all' },
  331. { label: 'Ads RU 🇷🇺', value: 'ext:geosite_RU.dat:category-ads-all' },
  332. { label: 'Malware 🇮🇷', value: 'ext:geosite_IR.dat:malware' },
  333. { label: 'Phishing 🇮🇷', value: 'ext:geosite_IR.dat:phishing' },
  334. { label: 'Cryptominers 🇮🇷', value: 'ext:geosite_IR.dat:cryptominers' },
  335. { label: 'Adult +18', value: 'geosite:category-porn' },
  336. { label: '🇮🇷 Iran', value: 'ext:geosite_IR.dat:ir' },
  337. { label: '🇮🇷 .ir', value: 'regexp:.*\\.ir$' },
  338. { label: '🇮🇷 .ایران', value: 'regexp:.*\\.xn--mgba3a4f16a$' },
  339. { label: '🇨🇳 China', value: 'geosite:cn' },
  340. { label: '🇨🇳 .cn', value: 'regexp:.*\\.cn$' },
  341. { label: '🇷🇺 Russia', value: 'ext:geosite_RU.dat:ru-available-only-inside' },
  342. { label: '🇷🇺 .ru', value: 'regexp:.*\\.ru$' },
  343. { label: '🇷🇺 .su', value: 'regexp:.*\\.su$' },
  344. { label: '🇷🇺 .рф', value: 'regexp:.*\\.xn--p1ai$' },
  345. { label: '🇻🇳 .vn', value: 'regexp:.*\\.vn$' },
  346. ],
  347. ServicesOptions: [
  348. { label: 'Apple', value: 'geosite:apple' },
  349. { label: 'Meta', value: 'geosite:meta' },
  350. { label: 'Google', value: 'geosite:google' },
  351. { label: 'OpenAI', value: 'geosite:openai' },
  352. { label: 'Spotify', value: 'geosite:spotify' },
  353. { label: 'Netflix', value: 'geosite:netflix' },
  354. { label: 'Reddit', value: 'geosite:reddit' },
  355. { label: 'Speedtest', value: 'geosite:speedtest' },
  356. ]
  357. },
  358. defaultObservatory: {
  359. subjectSelector: [],
  360. probeURL: "https://www.google.com/generate_204",
  361. probeInterval: "10m",
  362. enableConcurrency: true
  363. },
  364. defaultBurstObservatory: {
  365. subjectSelector: [],
  366. pingConfig: {
  367. destination: "https://www.google.com/generate_204",
  368. interval: "30m",
  369. connectivity: "http://connectivitycheck.platform.hicloud.com/generate_204",
  370. timeout: "10s",
  371. sampling: 2
  372. }
  373. }
  374. },
  375. methods: {
  376. loading(spinning = true) {
  377. this.loadingStates.spinning = spinning;
  378. },
  379. async getOutboundsTraffic() {
  380. const msg = await HttpUtil.get("/panel/xray/getOutboundsTraffic");
  381. if (msg.success) {
  382. this.outboundsTraffic = msg.obj;
  383. }
  384. },
  385. async getXraySetting() {
  386. const msg = await HttpUtil.post("/panel/xray/");
  387. if (msg.success) {
  388. if (!this.loadingStates.fetched) {
  389. this.loadingStates.fetched = true
  390. }
  391. result = JSON.parse(msg.obj);
  392. xs = JSON.stringify(result.xraySetting, null, 2);
  393. this.oldXraySetting = xs;
  394. this.xraySetting = xs;
  395. this.inboundTags = result.inboundTags;
  396. this.outboundTestUrl = result.outboundTestUrl || 'https://www.google.com/generate_204';
  397. this.oldOutboundTestUrl = this.outboundTestUrl;
  398. this.saveBtnDisable = true;
  399. }
  400. },
  401. async updateXraySetting() {
  402. this.loading(true);
  403. const msg = await HttpUtil.post("/panel/xray/update", {
  404. xraySetting: this.xraySetting,
  405. outboundTestUrl: this.outboundTestUrl || 'https://www.google.com/generate_204'
  406. });
  407. this.loading(false);
  408. if (msg.success) {
  409. await this.getXraySetting();
  410. }
  411. },
  412. async restartXray() {
  413. this.loading(true);
  414. const msg = await HttpUtil.post("/panel/api/server/restartXrayService");
  415. this.loading(false);
  416. if (msg.success) {
  417. await PromiseUtil.sleep(500);
  418. await this.getXrayResult();
  419. }
  420. this.loading(false);
  421. },
  422. async getXrayResult() {
  423. const msg = await HttpUtil.get("/panel/xray/getXrayResult");
  424. if (msg.success) {
  425. this.restartResult = msg.obj;
  426. if (msg.obj.length > 1) Vue.prototype.$message.error(msg.obj);
  427. }
  428. },
  429. async resetXrayConfigToDefault() {
  430. this.loading(true);
  431. const msg = await HttpUtil.get("/panel/setting/getDefaultJsonConfig");
  432. this.loading(false);
  433. if (msg.success) {
  434. this.templateSettings = JSON.parse(JSON.stringify(msg.obj, null, 2));
  435. this.saveBtnDisable = true;
  436. }
  437. },
  438. changePage(pageKey) {
  439. if (pageKey == 'tpl-advanced') this.changeCode();
  440. if (pageKey == 'tpl-balancer') this.changeObsCode();
  441. },
  442. syncRulesWithOutbound(tag, setting) {
  443. const newTemplateSettings = this.templateSettings;
  444. const haveRules = newTemplateSettings.routing.rules.some((r) => r?.outboundTag === tag);
  445. const outboundIndex = newTemplateSettings.outbounds.findIndex((o) => o.tag === tag);
  446. if (!haveRules && outboundIndex > 0) {
  447. newTemplateSettings.outbounds.splice(outboundIndex);
  448. }
  449. if (haveRules && outboundIndex < 0) {
  450. newTemplateSettings.outbounds.push(setting);
  451. }
  452. this.templateSettings = newTemplateSettings;
  453. },
  454. templateRuleGetter(routeSettings) {
  455. const { property, outboundTag } = routeSettings;
  456. let result = [];
  457. if (this.templateSettings != null) {
  458. this.templateSettings.routing.rules.forEach(
  459. (routingRule) => {
  460. if (
  461. routingRule.hasOwnProperty(property) &&
  462. routingRule.hasOwnProperty("outboundTag") &&
  463. routingRule.outboundTag === outboundTag
  464. ) {
  465. result.push(...routingRule[property]);
  466. }
  467. }
  468. );
  469. }
  470. return result;
  471. },
  472. templateRuleSetter(routeSettings) {
  473. const { data, property, outboundTag } = routeSettings;
  474. const oldTemplateSettings = this.templateSettings;
  475. const newTemplateSettings = oldTemplateSettings;
  476. currentProperty = this.templateRuleGetter({ outboundTag, property })
  477. if (currentProperty.length == 0) {
  478. const propertyRule = {
  479. type: "field",
  480. outboundTag,
  481. [property]: data
  482. };
  483. newTemplateSettings.routing.rules.push(propertyRule);
  484. }
  485. else {
  486. const newRules = [];
  487. insertedOnce = false;
  488. newTemplateSettings.routing.rules.forEach(
  489. (routingRule) => {
  490. if (
  491. routingRule.hasOwnProperty(property) &&
  492. routingRule.hasOwnProperty("outboundTag") &&
  493. routingRule.outboundTag === outboundTag
  494. ) {
  495. if (!insertedOnce && data.length > 0) {
  496. insertedOnce = true;
  497. routingRule[property] = data;
  498. newRules.push(routingRule);
  499. }
  500. }
  501. else {
  502. newRules.push(routingRule);
  503. }
  504. }
  505. );
  506. newTemplateSettings.routing.rules = newRules;
  507. }
  508. this.templateSettings = newTemplateSettings;
  509. },
  510. changeCode() {
  511. if (this.cm != null) {
  512. this.cm.toTextArea();
  513. }
  514. textAreaObj = document.getElementById('xraySetting');
  515. textAreaObj.value = this[this.advSettings];
  516. this.cm = CodeMirror.fromTextArea(textAreaObj, this.cmOptions);
  517. this.cm.on('change', editor => {
  518. value = editor.getValue();
  519. if (this.isJsonString(value)) {
  520. this[this.advSettings] = value;
  521. }
  522. });
  523. },
  524. changeObsCode() {
  525. if (this.cm != null) {
  526. this.cm.toTextArea();
  527. }
  528. if (this.obsSettings == '') {
  529. this.cm = null;
  530. return
  531. }
  532. textAreaObj = document.getElementById('obsSetting');
  533. textAreaObj.value = this[this.obsSettings];
  534. this.cm = CodeMirror.fromTextArea(textAreaObj, this.cmOptions);
  535. this.cm.on('change', editor => {
  536. value = editor.getValue();
  537. if (this.isJsonString(value)) {
  538. this[this.obsSettings] = value;
  539. }
  540. });
  541. },
  542. isJsonString(str) {
  543. try {
  544. JSON.parse(str);
  545. } catch (e) {
  546. return false;
  547. }
  548. return true;
  549. },
  550. findOutboundTraffic(o) {
  551. for (const otraffic of this.outboundsTraffic) {
  552. if (otraffic.tag == o.tag) {
  553. return `↑ ${SizeFormatter.sizeFormat(otraffic.up)} / ${SizeFormatter.sizeFormat(otraffic.down)} ↓`
  554. }
  555. }
  556. return `${SizeFormatter.sizeFormat(0)} / ${SizeFormatter.sizeFormat(0)}`
  557. },
  558. findOutboundAddress(o) {
  559. serverObj = null;
  560. switch (o.protocol) {
  561. case Protocols.VMess:
  562. serverObj = o.settings.vnext;
  563. break;
  564. case Protocols.VLESS:
  565. return [o.settings?.address + ':' + o.settings?.port];
  566. case Protocols.HTTP:
  567. case Protocols.Socks:
  568. case Protocols.Shadowsocks:
  569. case Protocols.Trojan:
  570. serverObj = o.settings.servers;
  571. break;
  572. case Protocols.DNS:
  573. return [o.settings?.address + ':' + o.settings?.port];
  574. case Protocols.Wireguard:
  575. return o.settings.peers.map(peer => peer.endpoint);
  576. default:
  577. return null;
  578. }
  579. return serverObj ? serverObj.map(obj => obj.address + ':' + obj.port) : null;
  580. },
  581. addOutbound() {
  582. outModal.show({
  583. title: '{{ i18n "pages.xray.outbound.addOutbound"}}',
  584. okText: '{{ i18n "pages.xray.outbound.addOutbound" }}',
  585. confirm: (outbound) => {
  586. outModal.loading();
  587. if (outbound.tag.length > 0) {
  588. this.templateSettings.outbounds.push(outbound);
  589. this.outboundSettings = JSON.stringify(this.templateSettings.outbounds);
  590. }
  591. outModal.close();
  592. },
  593. isEdit: false,
  594. tags: this.templateSettings.outbounds.map(obj => obj.tag)
  595. });
  596. },
  597. editOutbound(index) {
  598. outModal.show({
  599. title: '{{ i18n "pages.xray.outbound.editOutbound"}} ' + (index + 1),
  600. outbound: app.templateSettings.outbounds[index],
  601. confirm: (outbound) => {
  602. outModal.loading();
  603. this.templateSettings.outbounds[index] = outbound;
  604. this.outboundSettings = JSON.stringify(this.templateSettings.outbounds);
  605. outModal.close();
  606. },
  607. isEdit: true,
  608. tags: this.outboundData.filter((o) => o.key != index).map(obj => obj.tag)
  609. });
  610. },
  611. deleteOutbound(index) {
  612. outbounds = this.templateSettings.outbounds;
  613. outbounds.splice(index, 1);
  614. this.outboundSettings = JSON.stringify(outbounds);
  615. },
  616. setFirstOutbound(index) {
  617. outbounds = this.templateSettings.outbounds;
  618. outbounds.splice(0, 0, outbounds.splice(index, 1)[0]);
  619. this.outboundSettings = JSON.stringify(outbounds);
  620. },
  621. async testOutbound(index) {
  622. const outbound = this.templateSettings.outbounds[index];
  623. if (!outbound) {
  624. Vue.prototype.$message.error('{{ i18n "pages.xray.outbound.testError" }}');
  625. return;
  626. }
  627. if (outbound.protocol === 'blackhole' || outbound.tag === 'blocked') {
  628. Vue.prototype.$message.warning('{{ i18n "pages.xray.outbound.testError" }}: blocked/blackhole outbound');
  629. return;
  630. }
  631. // Initialize test state for this outbound if not exists
  632. if (!this.outboundTestStates[index]) {
  633. this.$set(this.outboundTestStates, index, {
  634. testing: false,
  635. result: null
  636. });
  637. }
  638. // Set testing state
  639. this.$set(this.outboundTestStates[index], 'testing', true);
  640. this.$set(this.outboundTestStates[index], 'result', null);
  641. try {
  642. const outboundJSON = JSON.stringify(outbound);
  643. const testURL = this.outboundTestUrl || 'https://www.google.com/generate_204';
  644. const allOutboundsJSON = JSON.stringify(this.templateSettings.outbounds || []);
  645. const msg = await HttpUtil.post("/panel/xray/testOutbound", {
  646. outbound: outboundJSON,
  647. testURL: testURL,
  648. allOutbounds: allOutboundsJSON
  649. });
  650. // Update test state
  651. this.$set(this.outboundTestStates[index], 'testing', false);
  652. if (msg.success && msg.obj) {
  653. const result = msg.obj;
  654. this.$set(this.outboundTestStates[index], 'result', result);
  655. if (result.success) {
  656. Vue.prototype.$message.success(
  657. `{{ i18n "pages.xray.outbound.testSuccess" }}: ${result.delay}ms (${result.statusCode})`
  658. );
  659. } else {
  660. Vue.prototype.$message.error(
  661. `{{ i18n "pages.xray.outbound.testFailed" }}: ${result.error || 'Unknown error'}`
  662. );
  663. }
  664. } else {
  665. this.$set(this.outboundTestStates[index], 'result', {
  666. success: false,
  667. error: msg.msg || '{{ i18n "pages.xray.outbound.testError" }}'
  668. });
  669. Vue.prototype.$message.error(msg.msg || '{{ i18n "pages.xray.outbound.testError" }}');
  670. }
  671. } catch (error) {
  672. this.$set(this.outboundTestStates[index], 'testing', false);
  673. this.$set(this.outboundTestStates[index], 'result', {
  674. success: false,
  675. error: error.message || '{{ i18n "pages.xray.outbound.testError" }}'
  676. });
  677. Vue.prototype.$message.error('{{ i18n "pages.xray.outbound.testError" }}: ' + error.message);
  678. }
  679. },
  680. addReverse() {
  681. reverseModal.show({
  682. title: '{{ i18n "pages.xray.outbound.addReverse"}}',
  683. okText: '{{ i18n "pages.xray.outbound.addReverse" }}',
  684. confirm: (reverse, rules) => {
  685. reverseModal.loading();
  686. if (reverse.tag.length > 0) {
  687. newTemplateSettings = this.templateSettings;
  688. if (newTemplateSettings.reverse == undefined) newTemplateSettings.reverse = {};
  689. if (newTemplateSettings.reverse[reverse.type + 's'] == undefined) newTemplateSettings.reverse[reverse.type + 's'] = [];
  690. newTemplateSettings.reverse[reverse.type + 's'].push({ tag: reverse.tag, domain: reverse.domain });
  691. this.templateSettings = newTemplateSettings;
  692. // Add related rules
  693. this.templateSettings.routing.rules.push(...rules);
  694. this.routingRuleSettings = JSON.stringify(this.templateSettings.routing.rules);
  695. }
  696. reverseModal.close();
  697. },
  698. isEdit: false
  699. });
  700. },
  701. editReverse(index) {
  702. if (this.reverseData[index].type == "bridge") {
  703. oldRules = this.templateSettings.routing.rules.filter(r => r.inboundTag && r.inboundTag[0] == this.reverseData[index].tag);
  704. } else {
  705. oldRules = this.templateSettings.routing.rules.filter(r => r.outboundTag && r.outboundTag == this.reverseData[index].tag);
  706. }
  707. reverseModal.show({
  708. title: '{{ i18n "pages.xray.outbound.editReverse"}} ' + (index + 1),
  709. reverse: this.reverseData[index],
  710. rules: oldRules,
  711. confirm: (reverse, rules) => {
  712. reverseModal.loading();
  713. if (reverse.tag.length > 0) {
  714. oldData = this.reverseData[index];
  715. newTemplateSettings = this.templateSettings;
  716. oldReverseIndex = newTemplateSettings.reverse[oldData.type + 's'].findIndex(rs => rs.tag == oldData.tag);
  717. oldRuleIndex0 = oldRules.length > 0 ? newTemplateSettings.routing.rules.findIndex(r => JSON.stringify(r) == JSON.stringify(oldRules[0])) : -1;
  718. oldRuleIndex1 = oldRules.length == 2 ? newTemplateSettings.routing.rules.findIndex(r => JSON.stringify(r) == JSON.stringify(oldRules[1])) : -1;
  719. if (oldData.type == reverse.type) {
  720. newTemplateSettings.reverse[oldData.type + 's'][oldReverseIndex] = { tag: reverse.tag, domain: reverse.domain };
  721. } else {
  722. newTemplateSettings.reverse[oldData.type + 's'].splice(oldReverseIndex, 1);
  723. // delete empty object
  724. if (newTemplateSettings.reverse[oldData.type + 's'].length == 0) Reflect.deleteProperty(newTemplateSettings.reverse, oldData.type + 's');
  725. // add other type of reverse if it is not exist
  726. if (!newTemplateSettings.reverse[reverse.type + 's']) newTemplateSettings.reverse[reverse.type + 's'] = [];
  727. newTemplateSettings.reverse[reverse.type + 's'].push({ tag: reverse.tag, domain: reverse.domain });
  728. }
  729. this.templateSettings = newTemplateSettings;
  730. // Adjust Rules
  731. newRules = this.templateSettings.routing.rules;
  732. oldRuleIndex0 != -1 ? newRules[oldRuleIndex0] = rules[0] : newRules.push(rules[0]);
  733. oldRuleIndex1 != -1 ? newRules[oldRuleIndex1] = rules[1] : newRules.push(rules[1]);
  734. this.routingRuleSettings = JSON.stringify(newRules);
  735. }
  736. reverseModal.close();
  737. },
  738. isEdit: true
  739. });
  740. },
  741. deleteReverse(index) {
  742. oldData = this.reverseData[index];
  743. newTemplateSettings = this.templateSettings;
  744. reverseTypeObj = newTemplateSettings.reverse[oldData.type + 's'];
  745. realIndex = reverseTypeObj.findIndex(r => r.tag == oldData.tag && r.domain == oldData.domain);
  746. newTemplateSettings.reverse[oldData.type + 's'].splice(realIndex, 1);
  747. // delete empty objects
  748. if (reverseTypeObj.length == 0) Reflect.deleteProperty(newTemplateSettings.reverse, oldData.type + 's');
  749. if (Object.keys(newTemplateSettings.reverse).length === 0) Reflect.deleteProperty(newTemplateSettings, 'reverse');
  750. // delete related routing rules
  751. newRules = newTemplateSettings.routing.rules;
  752. if (oldData.type == "bridge") {
  753. newRules = newTemplateSettings.routing.rules.filter(r => !(r.inboundTag && r.inboundTag.length == 1 && r.inboundTag[0] == oldData.tag));
  754. } else if (oldData.type == "portal") {
  755. newRules = newTemplateSettings.routing.rules.filter(r => r.outboundTag != oldData.tag);
  756. }
  757. newTemplateSettings.routing.rules = newRules;
  758. this.templateSettings = newTemplateSettings;
  759. },
  760. async refreshOutboundTraffic() {
  761. if (!this.refreshing) {
  762. this.refreshing = true;
  763. await this.getOutboundsTraffic();
  764. data = []
  765. if (this.templateSettings != null) {
  766. this.templateSettings.outbounds.forEach((o, index) => {
  767. data.push({ 'key': index, ...o });
  768. });
  769. }
  770. this.outboundData = data;
  771. this.refreshing = false;
  772. }
  773. },
  774. async resetOutboundTraffic(index) {
  775. let tag = "-alltags-";
  776. if (index >= 0) {
  777. tag = this.outboundData[index].tag ? this.outboundData[index].tag : ""
  778. }
  779. const msg = await HttpUtil.post("/panel/xray/resetOutboundsTraffic", { tag: tag });
  780. if (msg.success) {
  781. await this.refreshOutboundTraffic();
  782. }
  783. },
  784. addBalancer() {
  785. balancerModal.show({
  786. title: '{{ i18n "pages.xray.balancer.addBalancer"}}',
  787. okText: '{{ i18n "pages.xray.balancer.addBalancer"}}',
  788. balancerTags: this.balancersData.filter((o) => !ObjectUtil.isEmpty(o.tag)).map(obj => obj.tag),
  789. balancer: {
  790. tag: '',
  791. strategy: 'random',
  792. selector: [],
  793. fallbackTag: ''
  794. },
  795. confirm: (balancer) => {
  796. balancerModal.loading();
  797. newTemplateSettings = this.templateSettings;
  798. if (newTemplateSettings.routing.balancers == undefined) {
  799. newTemplateSettings.routing.balancers = [];
  800. }
  801. let tmpBalancer = {
  802. 'tag': balancer.tag,
  803. 'selector': balancer.selector,
  804. 'fallbackTag': balancer.fallbackTag
  805. };
  806. if (balancer.strategy && balancer.strategy != 'random') {
  807. tmpBalancer.strategy = {
  808. 'type': balancer.strategy
  809. };
  810. }
  811. newTemplateSettings.routing.balancers.push(tmpBalancer);
  812. this.templateSettings = newTemplateSettings;
  813. this.updateObservatorySelectors();
  814. balancerModal.close();
  815. this.changeObsCode();
  816. },
  817. isEdit: false
  818. });
  819. },
  820. editBalancer(index) {
  821. const oldTag = this.balancersData[index].tag;
  822. balancerModal.show({
  823. title: '{{ i18n "pages.xray.balancer.editBalancer"}}',
  824. okText: '{{ i18n "sure" }}',
  825. balancerTags: this.balancersData.filter((o) => !ObjectUtil.isEmpty(o.tag)).map(obj => obj.tag),
  826. balancer: this.balancersData[index],
  827. confirm: (balancer) => {
  828. balancerModal.loading();
  829. newTemplateSettings = this.templateSettings;
  830. let tmpBalancer = {
  831. 'tag': balancer.tag,
  832. 'selector': balancer.selector,
  833. 'fallbackTag': balancer.fallbackTag
  834. };
  835. // Remove old tag
  836. if (newTemplateSettings.observatory) {
  837. newTemplateSettings.observatory.subjectSelector = newTemplateSettings.observatory.subjectSelector.filter(s => s != oldTag);
  838. }
  839. if (newTemplateSettings.burstObservatory) {
  840. newTemplateSettings.burstObservatory.subjectSelector = newTemplateSettings.burstObservatory.subjectSelector.filter(s => s != oldTag);
  841. }
  842. if (balancer.strategy && balancer.strategy != 'random') {
  843. tmpBalancer.strategy = {
  844. 'type': balancer.strategy
  845. };
  846. }
  847. newTemplateSettings.routing.balancers[index] = tmpBalancer;
  848. // change edited tag if used in rule section
  849. if (oldTag != balancer.tag) {
  850. newTemplateSettings.routing.rules.forEach((rule) => {
  851. if (rule.balancerTag && rule.balancerTag == oldTag) {
  852. rule.balancerTag = balancer.tag;
  853. }
  854. });
  855. }
  856. this.templateSettings = newTemplateSettings;
  857. this.updateObservatorySelectors();
  858. balancerModal.close();
  859. this.changeObsCode();
  860. },
  861. isEdit: true
  862. });
  863. },
  864. updateObservatorySelectors() {
  865. newTemplateSettings = this.templateSettings;
  866. const leastPings = this.balancersData.filter((b) => b.strategy == 'leastPing');
  867. const leastLoads = this.balancersData.filter((b) =>
  868. b.strategy === 'leastLoad' ||
  869. b.strategy === 'roundRobin' ||
  870. b.strategy === 'random'
  871. );
  872. if (leastPings.length > 0) {
  873. if (!newTemplateSettings.observatory)
  874. newTemplateSettings.observatory = this.defaultObservatory;
  875. newTemplateSettings.observatory.subjectSelector = [];
  876. leastPings.forEach((b) => {
  877. b.selector.forEach((s) => {
  878. if (!newTemplateSettings.observatory.subjectSelector.includes(s))
  879. newTemplateSettings.observatory.subjectSelector.push(s);
  880. });
  881. });
  882. } else {
  883. delete newTemplateSettings.observatory
  884. }
  885. if (leastLoads.length > 0) {
  886. if (!newTemplateSettings.burstObservatory)
  887. newTemplateSettings.burstObservatory = this.defaultBurstObservatory;
  888. newTemplateSettings.burstObservatory.subjectSelector = [];
  889. leastLoads.forEach((b) => {
  890. b.selector.forEach((s) => {
  891. if (!newTemplateSettings.burstObservatory.subjectSelector.includes(s))
  892. newTemplateSettings.burstObservatory.subjectSelector.push(s);
  893. });
  894. });
  895. } else {
  896. delete newTemplateSettings.burstObservatory
  897. }
  898. this.templateSettings = newTemplateSettings;
  899. this.changeObsCode();
  900. },
  901. deleteBalancer(index) {
  902. newTemplateSettings = this.templateSettings;
  903. // Remove from balancers
  904. const removedBalancer = this.balancersData.splice(index, 1)[0];
  905. // Remove from settings
  906. let realIndex = newTemplateSettings.routing.balancers.findIndex((b) => b.tag === removedBalancer.tag);
  907. newTemplateSettings.routing.balancers.splice(realIndex, 1);
  908. // Update balancers property to an empty array if there are no more balancers
  909. if (newTemplateSettings.routing.balancers.length === 0) {
  910. delete newTemplateSettings.routing.balancers;
  911. }
  912. this.templateSettings = newTemplateSettings;
  913. this.updateObservatorySelectors();
  914. this.obsSettings = '';
  915. this.changeObsCode()
  916. },
  917. openDNSPresets() {
  918. dnsPresetsModal.show({
  919. title: '{{ i18n "pages.xray.dns.dnsPresetTitle" }}',
  920. selected: (selectedPreset) => {
  921. this.dnsServers = selectedPreset;
  922. dnsPresetsModal.close();
  923. }
  924. });
  925. },
  926. addDNSServer() {
  927. dnsModal.show({
  928. title: '{{ i18n "pages.xray.dns.add" }}',
  929. confirm: (dnsServer) => {
  930. dnsServers = this.dnsServers;
  931. dnsServers.push(dnsServer);
  932. this.dnsServers = dnsServers;
  933. dnsModal.close();
  934. },
  935. isEdit: false
  936. });
  937. },
  938. editDNSServer(index) {
  939. dnsModal.show({
  940. title: '{{ i18n "pages.xray.dns.edit" }} #' + (index + 1),
  941. dnsServer: this.dnsServers[index],
  942. confirm: (dnsServer) => {
  943. dnsServers = this.dnsServers;
  944. dnsServers[index] = dnsServer;
  945. this.dnsServers = dnsServers;
  946. dnsModal.close();
  947. },
  948. isEdit: true
  949. });
  950. },
  951. deleteDNSServer(index) {
  952. newDnsServers = this.dnsServers;
  953. newDnsServers.splice(index, 1);
  954. this.dnsServers = newDnsServers;
  955. },
  956. addFakedns() {
  957. fakednsModal.show({
  958. title: '{{ i18n "pages.xray.fakedns.add" }}',
  959. confirm: (item) => {
  960. fakeDns = this.fakeDns ?? [];
  961. fakeDns.push(item);
  962. this.fakeDns = fakeDns;
  963. fakednsModal.close();
  964. },
  965. isEdit: false
  966. });
  967. },
  968. editFakedns(index) {
  969. fakednsModal.show({
  970. title: '{{ i18n "pages.xray.fakedns.edit" }} #' + (index + 1),
  971. fakeDns: this.fakeDns[index],
  972. confirm: (item) => {
  973. fakeDns = this.fakeDns;
  974. fakeDns[index] = item;
  975. this.fakeDns = fakeDns;
  976. fakednsModal.close();
  977. },
  978. isEdit: true
  979. });
  980. },
  981. deleteFakedns(index) {
  982. fakeDns = this.fakeDns;
  983. fakeDns.splice(index, 1);
  984. this.fakeDns = fakeDns;
  985. },
  986. addRule() {
  987. ruleModal.show({
  988. title: '{{ i18n "pages.xray.rules.add"}}',
  989. okText: '{{ i18n "pages.xray.rules.add" }}',
  990. confirm: (rule) => {
  991. ruleModal.loading();
  992. if (JSON.stringify(rule).length > 3) {
  993. this.templateSettings.routing.rules.push(rule);
  994. this.routingRuleSettings = JSON.stringify(this.templateSettings.routing.rules);
  995. }
  996. ruleModal.close();
  997. },
  998. isEdit: false
  999. });
  1000. },
  1001. editRule(index) {
  1002. ruleModal.show({
  1003. title: '{{ i18n "pages.xray.rules.edit"}} ' + (index + 1),
  1004. rule: app.templateSettings.routing.rules[index],
  1005. confirm: (rule) => {
  1006. ruleModal.loading();
  1007. if (JSON.stringify(rule).length > 3) {
  1008. this.templateSettings.routing.rules[index] = rule;
  1009. this.routingRuleSettings = JSON.stringify(this.templateSettings.routing.rules);
  1010. }
  1011. ruleModal.close();
  1012. },
  1013. isEdit: true
  1014. });
  1015. },
  1016. replaceRule(old_index, new_index) {
  1017. rules = this.templateSettings.routing.rules;
  1018. if (new_index >= rules.length) rules.push(undefined);
  1019. rules.splice(new_index, 0, rules.splice(old_index, 1)[0]);
  1020. this.routingRuleSettings = JSON.stringify(rules);
  1021. },
  1022. deleteRule(index) {
  1023. rules = this.templateSettings.routing.rules;
  1024. rules.splice(index, 1);
  1025. this.routingRuleSettings = JSON.stringify(rules);
  1026. },
  1027. showWarp() {
  1028. warpModal.show();
  1029. }
  1030. },
  1031. async mounted() {
  1032. if (window.location.protocol !== "https:") {
  1033. this.showAlert = true;
  1034. }
  1035. await this.getXraySetting();
  1036. await this.getXrayResult();
  1037. await this.getOutboundsTraffic();
  1038. if (window.wsClient) {
  1039. window.wsClient.connect();
  1040. window.wsClient.on('outbounds', (payload) => {
  1041. if (payload) {
  1042. this.outboundsTraffic = payload;
  1043. this.$forceUpdate();
  1044. }
  1045. });
  1046. }
  1047. while (true) {
  1048. await PromiseUtil.sleep(800);
  1049. this.saveBtnDisable = this.oldXraySetting === this.xraySetting && this.oldOutboundTestUrl === this.outboundTestUrl;
  1050. }
  1051. },
  1052. computed: {
  1053. templateSettings: {
  1054. get: function () {
  1055. const parsedSettings = this.xraySetting ? JSON.parse(this.xraySetting) : null;
  1056. return parsedSettings;
  1057. },
  1058. set: function (newValue) {
  1059. if (newValue) {
  1060. this.xraySetting = JSON.stringify(newValue, null, 2);
  1061. }
  1062. },
  1063. },
  1064. inboundSettings: {
  1065. get: function () { return this.templateSettings ? JSON.stringify(this.templateSettings.inbounds, null, 2) : null; },
  1066. set: function (newValue) {
  1067. newTemplateSettings = this.templateSettings;
  1068. newTemplateSettings.inbounds = JSON.parse(newValue);
  1069. this.templateSettings = newTemplateSettings;
  1070. },
  1071. },
  1072. outboundSettings: {
  1073. get: function () { return this.templateSettings ? JSON.stringify(this.templateSettings.outbounds, null, 2) : null; },
  1074. set: function (newValue) {
  1075. newTemplateSettings = this.templateSettings;
  1076. newTemplateSettings.outbounds = JSON.parse(newValue);
  1077. this.templateSettings = newTemplateSettings;
  1078. },
  1079. },
  1080. outboundData: {
  1081. get: function () {
  1082. data = []
  1083. if (this.templateSettings != null) {
  1084. this.templateSettings.outbounds.forEach((o, index) => {
  1085. data.push({ 'key': index, ...o });
  1086. });
  1087. }
  1088. return data;
  1089. },
  1090. },
  1091. reverseData: {
  1092. get: function () {
  1093. data = []
  1094. if (this.templateSettings != null && this.templateSettings.reverse != null) {
  1095. if (this.templateSettings.reverse.bridges) {
  1096. this.templateSettings.reverse.bridges.forEach((o, index) => {
  1097. data.push({ 'key': index, 'type': 'bridge', ...o });
  1098. });
  1099. }
  1100. if (this.templateSettings.reverse.portals) {
  1101. this.templateSettings.reverse.portals.forEach((o, index) => {
  1102. data.push({ 'key': index, 'type': 'portal', ...o });
  1103. });
  1104. }
  1105. }
  1106. return data;
  1107. },
  1108. },
  1109. routingRuleSettings: {
  1110. get: function () { return this.templateSettings ? JSON.stringify(this.templateSettings.routing.rules, null, 2) : null; },
  1111. set: function (newValue) {
  1112. newTemplateSettings = this.templateSettings;
  1113. newTemplateSettings.routing.rules = JSON.parse(newValue);
  1114. this.templateSettings = newTemplateSettings;
  1115. },
  1116. },
  1117. routingRuleData: {
  1118. get: function () {
  1119. data = [];
  1120. if (this.templateSettings != null) {
  1121. this.templateSettings.routing.rules.forEach((r, index) => {
  1122. data.push({ 'key': index, ...r });
  1123. });
  1124. // Make rules readable
  1125. data.forEach(r => {
  1126. if (r.domain) r.domain = r.domain.join(',')
  1127. if (r.ip) r.ip = r.ip.join(',')
  1128. if (r.source) r.source = r.source.join(',');
  1129. if (r.user) r.user = r.user.join(',')
  1130. if (r.inboundTag) r.inboundTag = r.inboundTag.join(',')
  1131. if (r.protocol) r.protocol = r.protocol.join(',')
  1132. if (r.attrs) r.attrs = JSON.stringify(r.attrs, null, 2)
  1133. });
  1134. }
  1135. return data;
  1136. }
  1137. },
  1138. balancersData: {
  1139. get: function () {
  1140. data = []
  1141. if (this.templateSettings != null && this.templateSettings.routing != null && this.templateSettings.routing.balancers != null) {
  1142. this.templateSettings.routing.balancers.forEach((o, index) => {
  1143. data.push({
  1144. 'key': index,
  1145. 'tag': o.tag ? o.tag : "",
  1146. 'strategy': o.strategy?.type ?? "random",
  1147. 'selector': o.selector ? o.selector : [],
  1148. 'fallbackTag': o.fallbackTag ?? '',
  1149. });
  1150. });
  1151. }
  1152. return data;
  1153. }
  1154. },
  1155. observatory: {
  1156. get: function () {
  1157. return this.templateSettings?.observatory ? JSON.stringify(this.templateSettings.observatory, null, 2) : null;
  1158. },
  1159. set: function (newValue) {
  1160. newTemplateSettings = this.templateSettings;
  1161. newTemplateSettings.observatory = JSON.parse(newValue);
  1162. this.templateSettings = newTemplateSettings;
  1163. },
  1164. },
  1165. burstObservatory: {
  1166. get: function () {
  1167. return this.templateSettings?.burstObservatory ? JSON.stringify(this.templateSettings.burstObservatory, null, 2) : null;
  1168. },
  1169. set: function (newValue) {
  1170. newTemplateSettings = this.templateSettings;
  1171. newTemplateSettings.burstObservatory = JSON.parse(newValue);
  1172. this.templateSettings = newTemplateSettings;
  1173. },
  1174. },
  1175. observatoryEnable: function () { return this.templateSettings != null && this.templateSettings.observatory != undefined },
  1176. burstObservatoryEnable: function () { return this.templateSettings != null && this.templateSettings.burstObservatory != undefined },
  1177. freedomStrategy: {
  1178. get: function () {
  1179. if (!this.templateSettings) return "AsIs";
  1180. freedomOutbound = this.templateSettings.outbounds.find((o) => o.protocol === "freedom" && o.tag == "direct");
  1181. if (!freedomOutbound) return "AsIs";
  1182. if (!freedomOutbound.settings || !freedomOutbound.settings.domainStrategy) return "AsIs";
  1183. return freedomOutbound.settings.domainStrategy;
  1184. },
  1185. set: function (newValue) {
  1186. newTemplateSettings = this.templateSettings;
  1187. freedomOutboundIndex = newTemplateSettings.outbounds.findIndex((o) => o.protocol === "freedom" && o.tag == "direct");
  1188. if (freedomOutboundIndex == -1) {
  1189. newTemplateSettings.outbounds.push({ protocol: "freedom", tag: "direct", settings: { "domainStrategy": newValue } });
  1190. } else if (!newTemplateSettings.outbounds[freedomOutboundIndex].settings) {
  1191. newTemplateSettings.outbounds[freedomOutboundIndex].settings = { "domainStrategy": newValue };
  1192. } else {
  1193. newTemplateSettings.outbounds[freedomOutboundIndex].settings.domainStrategy = newValue;
  1194. }
  1195. this.templateSettings = newTemplateSettings;
  1196. }
  1197. },
  1198. routingStrategy: {
  1199. get: function () {
  1200. if (!this.templateSettings || !this.templateSettings.routing || !this.templateSettings.routing.domainStrategy) return "AsIs";
  1201. return this.templateSettings.routing.domainStrategy;
  1202. },
  1203. set: function (newValue) {
  1204. newTemplateSettings = this.templateSettings;
  1205. newTemplateSettings.routing.domainStrategy = newValue;
  1206. this.templateSettings = newTemplateSettings;
  1207. }
  1208. },
  1209. logLevel: {
  1210. get: function () {
  1211. if (!this.templateSettings || !this.templateSettings.log || !this.templateSettings.log.loglevel) return "warning";
  1212. return this.templateSettings.log.loglevel;
  1213. },
  1214. set: function (newValue) {
  1215. newTemplateSettings = this.templateSettings;
  1216. newTemplateSettings.log.loglevel = newValue;
  1217. this.templateSettings = newTemplateSettings;
  1218. }
  1219. },
  1220. accessLog: {
  1221. get: function () {
  1222. if (!this.templateSettings || !this.templateSettings.log || !this.templateSettings.log.access) return "";
  1223. return this.templateSettings.log.access;
  1224. },
  1225. set: function (newValue) {
  1226. newTemplateSettings = this.templateSettings;
  1227. newTemplateSettings.log.access = newValue;
  1228. this.templateSettings = newTemplateSettings;
  1229. }
  1230. },
  1231. errorLog: {
  1232. get: function () {
  1233. if (!this.templateSettings || !this.templateSettings.log || !this.templateSettings.log.error) return "";
  1234. return this.templateSettings.log.error;
  1235. },
  1236. set: function (newValue) {
  1237. newTemplateSettings = this.templateSettings;
  1238. newTemplateSettings.log.error = newValue;
  1239. this.templateSettings = newTemplateSettings;
  1240. }
  1241. },
  1242. dnslog: {
  1243. get: function () {
  1244. if (!this.templateSettings || !this.templateSettings.log || !this.templateSettings.log.dnsLog) return false;
  1245. return this.templateSettings.log.dnsLog;
  1246. },
  1247. set: function (newValue) {
  1248. newTemplateSettings = this.templateSettings;
  1249. newTemplateSettings.log.dnsLog = newValue;
  1250. this.templateSettings = newTemplateSettings;
  1251. }
  1252. },
  1253. statsInboundUplink: {
  1254. get: function () {
  1255. if (!this.templateSettings || !this.templateSettings.policy.system || !this.templateSettings.policy.system.statsInboundUplink) return false;
  1256. return this.templateSettings.policy.system.statsInboundUplink;
  1257. },
  1258. set: function (newValue) {
  1259. newTemplateSettings = this.templateSettings;
  1260. newTemplateSettings.policy.system.statsInboundUplink = newValue;
  1261. this.templateSettings = newTemplateSettings;
  1262. }
  1263. },
  1264. statsInboundDownlink: {
  1265. get: function () {
  1266. if (!this.templateSettings || !this.templateSettings.policy.system || !this.templateSettings.policy.system.statsInboundDownlink) return false;
  1267. return this.templateSettings.policy.system.statsInboundDownlink;
  1268. },
  1269. set: function (newValue) {
  1270. newTemplateSettings = this.templateSettings;
  1271. newTemplateSettings.policy.system.statsInboundDownlink = newValue;
  1272. this.templateSettings = newTemplateSettings;
  1273. }
  1274. },
  1275. statsOutboundUplink: {
  1276. get: function () {
  1277. if (!this.templateSettings || !this.templateSettings.policy.system || !this.templateSettings.policy.system.statsOutboundUplink) return false;
  1278. return this.templateSettings.policy.system.statsOutboundUplink;
  1279. },
  1280. set: function (newValue) {
  1281. newTemplateSettings = this.templateSettings;
  1282. newTemplateSettings.policy.system.statsOutboundUplink = newValue;
  1283. this.templateSettings = newTemplateSettings;
  1284. }
  1285. },
  1286. statsOutboundDownlink: {
  1287. get: function () {
  1288. if (!this.templateSettings || !this.templateSettings.policy.system || !this.templateSettings.policy.system.statsOutboundDownlink) return false;
  1289. return this.templateSettings.policy.system.statsOutboundDownlink;
  1290. },
  1291. set: function (newValue) {
  1292. newTemplateSettings = this.templateSettings;
  1293. newTemplateSettings.policy.system.statsOutboundDownlink = newValue;
  1294. this.templateSettings = newTemplateSettings;
  1295. }
  1296. },
  1297. maskAddressLog: {
  1298. get: function () {
  1299. if (!this.templateSettings || !this.templateSettings.log || !this.templateSettings.log.maskAddress) return "";
  1300. return this.templateSettings.log.maskAddress;
  1301. },
  1302. set: function (newValue) {
  1303. newTemplateSettings = this.templateSettings;
  1304. newTemplateSettings.log.maskAddress = newValue;
  1305. this.templateSettings = newTemplateSettings;
  1306. }
  1307. },
  1308. blockedIPs: {
  1309. get: function () {
  1310. return this.templateRuleGetter({ outboundTag: "blocked", property: "ip" });
  1311. },
  1312. set: function (newValue) {
  1313. this.templateRuleSetter({ outboundTag: "blocked", property: "ip", data: newValue });
  1314. }
  1315. },
  1316. blockedDomains: {
  1317. get: function () {
  1318. return this.templateRuleGetter({ outboundTag: "blocked", property: "domain" });
  1319. },
  1320. set: function (newValue) {
  1321. this.templateRuleSetter({ outboundTag: "blocked", property: "domain", data: newValue });
  1322. }
  1323. },
  1324. blockedProtocols: {
  1325. get: function () {
  1326. return this.templateRuleGetter({ outboundTag: "blocked", property: "protocol" });
  1327. },
  1328. set: function (newValue) {
  1329. this.templateRuleSetter({ outboundTag: "blocked", property: "protocol", data: newValue });
  1330. }
  1331. },
  1332. directIPs: {
  1333. get: function () {
  1334. return this.templateRuleGetter({ outboundTag: "direct", property: "ip" });
  1335. },
  1336. set: function (newValue) {
  1337. this.templateRuleSetter({ outboundTag: "direct", property: "ip", data: newValue });
  1338. this.syncRulesWithOutbound("direct", this.directSettings);
  1339. }
  1340. },
  1341. directDomains: {
  1342. get: function () {
  1343. return this.templateRuleGetter({ outboundTag: "direct", property: "domain" });
  1344. },
  1345. set: function (newValue) {
  1346. this.templateRuleSetter({ outboundTag: "direct", property: "domain", data: newValue });
  1347. this.syncRulesWithOutbound("direct", this.directSettings);
  1348. }
  1349. },
  1350. ipv4Domains: {
  1351. get: function () {
  1352. return this.templateRuleGetter({ outboundTag: "IPv4", property: "domain" });
  1353. },
  1354. set: function (newValue) {
  1355. this.templateRuleSetter({ outboundTag: "IPv4", property: "domain", data: newValue });
  1356. this.syncRulesWithOutbound("IPv4", this.ipv4Settings);
  1357. }
  1358. },
  1359. warpDomains: {
  1360. get: function () {
  1361. return this.templateRuleGetter({ outboundTag: "warp", property: "domain" });
  1362. },
  1363. set: function (newValue) {
  1364. this.templateRuleSetter({ outboundTag: "warp", property: "domain", data: newValue });
  1365. }
  1366. },
  1367. torrentSettings: {
  1368. get: function () {
  1369. return ArrayUtils.doAllItemsExist(this.settingsData.protocols.bittorrent, this.blockedProtocols);
  1370. },
  1371. set: function (newValue) {
  1372. if (newValue) {
  1373. this.blockedProtocols = [...this.blockedProtocols, ...this.settingsData.protocols.bittorrent];
  1374. } else {
  1375. this.blockedProtocols = this.blockedProtocols.filter(data => !this.settingsData.protocols.bittorrent.includes(data));
  1376. }
  1377. },
  1378. },
  1379. WarpExist: {
  1380. get: function () {
  1381. return this.templateSettings ? this.templateSettings.outbounds.findIndex((o) => o.tag == "warp") >= 0 : false;
  1382. },
  1383. },
  1384. enableDNS: {
  1385. get: function () {
  1386. return this.templateSettings ? this.templateSettings.dns != null : false;
  1387. },
  1388. set: function (newValue) {
  1389. newTemplateSettings = this.templateSettings;
  1390. if (newValue) {
  1391. newTemplateSettings.dns = {
  1392. servers: [],
  1393. queryStrategy: "UseIP",
  1394. tag: "dns_inbound",
  1395. enableParallelQuery: false
  1396. };
  1397. newTemplateSettings.fakedns = null;
  1398. } else {
  1399. delete newTemplateSettings.dns;
  1400. delete newTemplateSettings.fakedns;
  1401. }
  1402. this.templateSettings = newTemplateSettings;
  1403. }
  1404. },
  1405. dnsTag: {
  1406. get: function () {
  1407. return this.enableDNS ? this.templateSettings.dns.tag : "";
  1408. },
  1409. set: function (newValue) {
  1410. newTemplateSettings = this.templateSettings;
  1411. newTemplateSettings.dns.tag = newValue;
  1412. this.templateSettings = newTemplateSettings;
  1413. }
  1414. },
  1415. dnsClientIp: {
  1416. get: function () {
  1417. return this.enableDNS ? this.templateSettings.dns.clientIp : null;
  1418. },
  1419. set: function (newValue) {
  1420. newTemplateSettings = this.templateSettings;
  1421. if (newValue) {
  1422. newTemplateSettings.dns.clientIp = newValue;
  1423. } else {
  1424. delete newTemplateSettings.dns.clientIp;
  1425. }
  1426. this.templateSettings = newTemplateSettings;
  1427. }
  1428. },
  1429. dnsDisableCache: {
  1430. get: function () {
  1431. return this.enableDNS ? this.templateSettings.dns.disableCache : false;
  1432. },
  1433. set: function (newValue) {
  1434. newTemplateSettings = this.templateSettings;
  1435. if (newValue) {
  1436. newTemplateSettings.dns.disableCache = newValue;
  1437. } else {
  1438. delete newTemplateSettings.dns.disableCache
  1439. }
  1440. this.templateSettings = newTemplateSettings;
  1441. }
  1442. },
  1443. dnsDisableFallback: {
  1444. get: function () {
  1445. return this.enableDNS ? this.templateSettings.dns.disableFallback : false;
  1446. },
  1447. set: function (newValue) {
  1448. newTemplateSettings = this.templateSettings;
  1449. if (newValue) {
  1450. newTemplateSettings.dns.disableFallback = newValue;
  1451. } else {
  1452. delete newTemplateSettings.dns.disableFallback
  1453. }
  1454. this.templateSettings = newTemplateSettings;
  1455. }
  1456. },
  1457. dnsDisableFallbackIfMatch: {
  1458. get: function () {
  1459. return this.enableDNS ? this.templateSettings.dns.disableFallbackIfMatch : false;
  1460. },
  1461. set: function (newValue) {
  1462. newTemplateSettings = this.templateSettings;
  1463. if (newValue) {
  1464. newTemplateSettings.dns.disableFallbackIfMatch = newValue;
  1465. } else {
  1466. delete newTemplateSettings.dns.disableFallbackIfMatch
  1467. }
  1468. this.templateSettings = newTemplateSettings;
  1469. }
  1470. },
  1471. dnsEnableParallelQuery: {
  1472. get: function () {
  1473. return this.enableDNS ? (this.templateSettings.dns.enableParallelQuery || false) : false;
  1474. },
  1475. set: function (newValue) {
  1476. newTemplateSettings = this.templateSettings;
  1477. if (newValue) {
  1478. newTemplateSettings.dns.enableParallelQuery = newValue;
  1479. } else {
  1480. delete newTemplateSettings.dns.enableParallelQuery
  1481. }
  1482. this.templateSettings = newTemplateSettings;
  1483. }
  1484. },
  1485. dnsUseSystemHosts: {
  1486. get: function () {
  1487. return this.enableDNS ? this.templateSettings.dns.useSystemHosts : false;
  1488. },
  1489. set: function (newValue) {
  1490. newTemplateSettings = this.templateSettings;
  1491. if (newValue) {
  1492. newTemplateSettings.dns.useSystemHosts = newValue;
  1493. } else {
  1494. delete newTemplateSettings.dns.useSystemHosts
  1495. }
  1496. this.templateSettings = newTemplateSettings;
  1497. }
  1498. },
  1499. dnsStrategy: {
  1500. get: function () {
  1501. return this.enableDNS ? this.templateSettings.dns.queryStrategy : null;
  1502. },
  1503. set: function (newValue) {
  1504. newTemplateSettings = this.templateSettings;
  1505. newTemplateSettings.dns.queryStrategy = newValue;
  1506. this.templateSettings = newTemplateSettings;
  1507. }
  1508. },
  1509. dnsServers: {
  1510. get: function () { return this.enableDNS ? this.templateSettings.dns.servers : []; },
  1511. set: function (newValue) {
  1512. newTemplateSettings = this.templateSettings;
  1513. newTemplateSettings.dns.servers = newValue;
  1514. this.templateSettings = newTemplateSettings;
  1515. }
  1516. },
  1517. fakeDns: {
  1518. get: function () { return this.templateSettings && this.templateSettings.fakedns ? this.templateSettings.fakedns : []; },
  1519. set: function (newValue) {
  1520. newTemplateSettings = this.templateSettings;
  1521. if (this.enableDNS) {
  1522. newTemplateSettings.fakedns = newValue.length > 0 ? newValue : null;
  1523. } else {
  1524. delete newTemplateSettings.fakedns;
  1525. }
  1526. this.templateSettings = newTemplateSettings;
  1527. }
  1528. }
  1529. },
  1530. });
  1531. </script>
  1532. {{ template "page/body_end" .}}