1
0

xray.html 64 KB

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