1
0

xray.html 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  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 allOutboundsJSON = JSON.stringify(this.templateSettings.outbounds || []);
  644. const msg = await HttpUtil.post("/panel/xray/testOutbound", {
  645. outbound: outboundJSON,
  646. allOutbounds: allOutboundsJSON
  647. });
  648. // Update test state
  649. this.$set(this.outboundTestStates[index], 'testing', false);
  650. if (msg.success && msg.obj) {
  651. const result = msg.obj;
  652. this.$set(this.outboundTestStates[index], 'result', result);
  653. if (result.success) {
  654. Vue.prototype.$message.success(
  655. `{{ i18n "pages.xray.outbound.testSuccess" }}: ${result.delay}ms (${result.statusCode})`
  656. );
  657. } else {
  658. Vue.prototype.$message.error(
  659. `{{ i18n "pages.xray.outbound.testFailed" }}: ${result.error || 'Unknown error'}`
  660. );
  661. }
  662. } else {
  663. this.$set(this.outboundTestStates[index], 'result', {
  664. success: false,
  665. error: msg.msg || '{{ i18n "pages.xray.outbound.testError" }}'
  666. });
  667. Vue.prototype.$message.error(msg.msg || '{{ i18n "pages.xray.outbound.testError" }}');
  668. }
  669. } catch (error) {
  670. this.$set(this.outboundTestStates[index], 'testing', false);
  671. this.$set(this.outboundTestStates[index], 'result', {
  672. success: false,
  673. error: error.message || '{{ i18n "pages.xray.outbound.testError" }}'
  674. });
  675. Vue.prototype.$message.error('{{ i18n "pages.xray.outbound.testError" }}: ' + error.message);
  676. }
  677. },
  678. addReverse() {
  679. reverseModal.show({
  680. title: '{{ i18n "pages.xray.outbound.addReverse"}}',
  681. okText: '{{ i18n "pages.xray.outbound.addReverse" }}',
  682. confirm: (reverse, rules) => {
  683. reverseModal.loading();
  684. if (reverse.tag.length > 0) {
  685. newTemplateSettings = this.templateSettings;
  686. if (newTemplateSettings.reverse == undefined) newTemplateSettings.reverse = {};
  687. if (newTemplateSettings.reverse[reverse.type + 's'] == undefined) newTemplateSettings.reverse[reverse.type + 's'] = [];
  688. newTemplateSettings.reverse[reverse.type + 's'].push({ tag: reverse.tag, domain: reverse.domain });
  689. this.templateSettings = newTemplateSettings;
  690. // Add related rules
  691. this.templateSettings.routing.rules.push(...rules);
  692. this.routingRuleSettings = JSON.stringify(this.templateSettings.routing.rules);
  693. }
  694. reverseModal.close();
  695. },
  696. isEdit: false
  697. });
  698. },
  699. editReverse(index) {
  700. if (this.reverseData[index].type == "bridge") {
  701. oldRules = this.templateSettings.routing.rules.filter(r => r.inboundTag && r.inboundTag[0] == this.reverseData[index].tag);
  702. } else {
  703. oldRules = this.templateSettings.routing.rules.filter(r => r.outboundTag && r.outboundTag == this.reverseData[index].tag);
  704. }
  705. reverseModal.show({
  706. title: '{{ i18n "pages.xray.outbound.editReverse"}} ' + (index + 1),
  707. reverse: this.reverseData[index],
  708. rules: oldRules,
  709. confirm: (reverse, rules) => {
  710. reverseModal.loading();
  711. if (reverse.tag.length > 0) {
  712. oldData = this.reverseData[index];
  713. newTemplateSettings = this.templateSettings;
  714. oldReverseIndex = newTemplateSettings.reverse[oldData.type + 's'].findIndex(rs => rs.tag == oldData.tag);
  715. oldRuleIndex0 = oldRules.length > 0 ? newTemplateSettings.routing.rules.findIndex(r => JSON.stringify(r) == JSON.stringify(oldRules[0])) : -1;
  716. oldRuleIndex1 = oldRules.length == 2 ? newTemplateSettings.routing.rules.findIndex(r => JSON.stringify(r) == JSON.stringify(oldRules[1])) : -1;
  717. if (oldData.type == reverse.type) {
  718. newTemplateSettings.reverse[oldData.type + 's'][oldReverseIndex] = { tag: reverse.tag, domain: reverse.domain };
  719. } else {
  720. newTemplateSettings.reverse[oldData.type + 's'].splice(oldReverseIndex, 1);
  721. // delete empty object
  722. if (newTemplateSettings.reverse[oldData.type + 's'].length == 0) Reflect.deleteProperty(newTemplateSettings.reverse, oldData.type + 's');
  723. // add other type of reverse if it is not exist
  724. if (!newTemplateSettings.reverse[reverse.type + 's']) newTemplateSettings.reverse[reverse.type + 's'] = [];
  725. newTemplateSettings.reverse[reverse.type + 's'].push({ tag: reverse.tag, domain: reverse.domain });
  726. }
  727. this.templateSettings = newTemplateSettings;
  728. // Adjust Rules
  729. newRules = this.templateSettings.routing.rules;
  730. oldRuleIndex0 != -1 ? newRules[oldRuleIndex0] = rules[0] : newRules.push(rules[0]);
  731. oldRuleIndex1 != -1 ? newRules[oldRuleIndex1] = rules[1] : newRules.push(rules[1]);
  732. this.routingRuleSettings = JSON.stringify(newRules);
  733. }
  734. reverseModal.close();
  735. },
  736. isEdit: true
  737. });
  738. },
  739. deleteReverse(index) {
  740. oldData = this.reverseData[index];
  741. newTemplateSettings = this.templateSettings;
  742. reverseTypeObj = newTemplateSettings.reverse[oldData.type + 's'];
  743. realIndex = reverseTypeObj.findIndex(r => r.tag == oldData.tag && r.domain == oldData.domain);
  744. newTemplateSettings.reverse[oldData.type + 's'].splice(realIndex, 1);
  745. // delete empty objects
  746. if (reverseTypeObj.length == 0) Reflect.deleteProperty(newTemplateSettings.reverse, oldData.type + 's');
  747. if (Object.keys(newTemplateSettings.reverse).length === 0) Reflect.deleteProperty(newTemplateSettings, 'reverse');
  748. // delete related routing rules
  749. newRules = newTemplateSettings.routing.rules;
  750. if (oldData.type == "bridge") {
  751. newRules = newTemplateSettings.routing.rules.filter(r => !(r.inboundTag && r.inboundTag.length == 1 && r.inboundTag[0] == oldData.tag));
  752. } else if (oldData.type == "portal") {
  753. newRules = newTemplateSettings.routing.rules.filter(r => r.outboundTag != oldData.tag);
  754. }
  755. newTemplateSettings.routing.rules = newRules;
  756. this.templateSettings = newTemplateSettings;
  757. },
  758. async refreshOutboundTraffic() {
  759. if (!this.refreshing) {
  760. this.refreshing = true;
  761. await this.getOutboundsTraffic();
  762. data = []
  763. if (this.templateSettings != null) {
  764. this.templateSettings.outbounds.forEach((o, index) => {
  765. data.push({ 'key': index, ...o });
  766. });
  767. }
  768. this.outboundData = data;
  769. this.refreshing = false;
  770. }
  771. },
  772. async resetOutboundTraffic(index) {
  773. let tag = "-alltags-";
  774. if (index >= 0) {
  775. tag = this.outboundData[index].tag ? this.outboundData[index].tag : ""
  776. }
  777. const msg = await HttpUtil.post("/panel/xray/resetOutboundsTraffic", { tag: tag });
  778. if (msg.success) {
  779. await this.refreshOutboundTraffic();
  780. }
  781. },
  782. addBalancer() {
  783. balancerModal.show({
  784. title: '{{ i18n "pages.xray.balancer.addBalancer"}}',
  785. okText: '{{ i18n "pages.xray.balancer.addBalancer"}}',
  786. balancerTags: this.balancersData.filter((o) => !ObjectUtil.isEmpty(o.tag)).map(obj => obj.tag),
  787. balancer: {
  788. tag: '',
  789. strategy: 'random',
  790. selector: [],
  791. fallbackTag: ''
  792. },
  793. confirm: (balancer) => {
  794. balancerModal.loading();
  795. newTemplateSettings = this.templateSettings;
  796. if (newTemplateSettings.routing.balancers == undefined) {
  797. newTemplateSettings.routing.balancers = [];
  798. }
  799. let tmpBalancer = {
  800. 'tag': balancer.tag,
  801. 'selector': balancer.selector,
  802. 'fallbackTag': balancer.fallbackTag
  803. };
  804. if (balancer.strategy && balancer.strategy != 'random') {
  805. tmpBalancer.strategy = {
  806. 'type': balancer.strategy
  807. };
  808. }
  809. newTemplateSettings.routing.balancers.push(tmpBalancer);
  810. this.templateSettings = newTemplateSettings;
  811. this.updateObservatorySelectors();
  812. balancerModal.close();
  813. this.changeObsCode();
  814. },
  815. isEdit: false
  816. });
  817. },
  818. editBalancer(index) {
  819. const oldTag = this.balancersData[index].tag;
  820. balancerModal.show({
  821. title: '{{ i18n "pages.xray.balancer.editBalancer"}}',
  822. okText: '{{ i18n "sure" }}',
  823. balancerTags: this.balancersData.filter((o) => !ObjectUtil.isEmpty(o.tag)).map(obj => obj.tag),
  824. balancer: this.balancersData[index],
  825. confirm: (balancer) => {
  826. balancerModal.loading();
  827. newTemplateSettings = this.templateSettings;
  828. let tmpBalancer = {
  829. 'tag': balancer.tag,
  830. 'selector': balancer.selector,
  831. 'fallbackTag': balancer.fallbackTag
  832. };
  833. // Remove old tag
  834. if (newTemplateSettings.observatory) {
  835. newTemplateSettings.observatory.subjectSelector = newTemplateSettings.observatory.subjectSelector.filter(s => s != oldTag);
  836. }
  837. if (newTemplateSettings.burstObservatory) {
  838. newTemplateSettings.burstObservatory.subjectSelector = newTemplateSettings.burstObservatory.subjectSelector.filter(s => s != oldTag);
  839. }
  840. if (balancer.strategy && balancer.strategy != 'random') {
  841. tmpBalancer.strategy = {
  842. 'type': balancer.strategy
  843. };
  844. }
  845. newTemplateSettings.routing.balancers[index] = tmpBalancer;
  846. // change edited tag if used in rule section
  847. if (oldTag != balancer.tag) {
  848. newTemplateSettings.routing.rules.forEach((rule) => {
  849. if (rule.balancerTag && rule.balancerTag == oldTag) {
  850. rule.balancerTag = balancer.tag;
  851. }
  852. });
  853. }
  854. this.templateSettings = newTemplateSettings;
  855. this.updateObservatorySelectors();
  856. balancerModal.close();
  857. this.changeObsCode();
  858. },
  859. isEdit: true
  860. });
  861. },
  862. updateObservatorySelectors() {
  863. newTemplateSettings = this.templateSettings;
  864. const leastPings = this.balancersData.filter((b) => b.strategy == 'leastPing');
  865. const leastLoads = this.balancersData.filter((b) =>
  866. b.strategy === 'leastLoad' ||
  867. b.strategy === 'roundRobin' ||
  868. b.strategy === 'random'
  869. );
  870. if (leastPings.length > 0) {
  871. if (!newTemplateSettings.observatory)
  872. newTemplateSettings.observatory = this.defaultObservatory;
  873. newTemplateSettings.observatory.subjectSelector = [];
  874. leastPings.forEach((b) => {
  875. b.selector.forEach((s) => {
  876. if (!newTemplateSettings.observatory.subjectSelector.includes(s))
  877. newTemplateSettings.observatory.subjectSelector.push(s);
  878. });
  879. });
  880. } else {
  881. delete newTemplateSettings.observatory
  882. }
  883. if (leastLoads.length > 0) {
  884. if (!newTemplateSettings.burstObservatory)
  885. newTemplateSettings.burstObservatory = this.defaultBurstObservatory;
  886. newTemplateSettings.burstObservatory.subjectSelector = [];
  887. leastLoads.forEach((b) => {
  888. b.selector.forEach((s) => {
  889. if (!newTemplateSettings.burstObservatory.subjectSelector.includes(s))
  890. newTemplateSettings.burstObservatory.subjectSelector.push(s);
  891. });
  892. });
  893. } else {
  894. delete newTemplateSettings.burstObservatory
  895. }
  896. this.templateSettings = newTemplateSettings;
  897. this.changeObsCode();
  898. },
  899. deleteBalancer(index) {
  900. newTemplateSettings = this.templateSettings;
  901. // Remove from balancers
  902. const removedBalancer = this.balancersData.splice(index, 1)[0];
  903. // Remove from settings
  904. let realIndex = newTemplateSettings.routing.balancers.findIndex((b) => b.tag === removedBalancer.tag);
  905. newTemplateSettings.routing.balancers.splice(realIndex, 1);
  906. // Update balancers property to an empty array if there are no more balancers
  907. if (newTemplateSettings.routing.balancers.length === 0) {
  908. delete newTemplateSettings.routing.balancers;
  909. }
  910. this.templateSettings = newTemplateSettings;
  911. this.updateObservatorySelectors();
  912. this.obsSettings = '';
  913. this.changeObsCode()
  914. },
  915. openDNSPresets() {
  916. dnsPresetsModal.show({
  917. title: '{{ i18n "pages.xray.dns.dnsPresetTitle" }}',
  918. selected: (selectedPreset) => {
  919. this.dnsServers = selectedPreset;
  920. dnsPresetsModal.close();
  921. }
  922. });
  923. },
  924. addDNSServer() {
  925. dnsModal.show({
  926. title: '{{ i18n "pages.xray.dns.add" }}',
  927. confirm: (dnsServer) => {
  928. dnsServers = this.dnsServers;
  929. dnsServers.push(dnsServer);
  930. this.dnsServers = dnsServers;
  931. dnsModal.close();
  932. },
  933. isEdit: false
  934. });
  935. },
  936. editDNSServer(index) {
  937. dnsModal.show({
  938. title: '{{ i18n "pages.xray.dns.edit" }} #' + (index + 1),
  939. dnsServer: this.dnsServers[index],
  940. confirm: (dnsServer) => {
  941. dnsServers = this.dnsServers;
  942. dnsServers[index] = dnsServer;
  943. this.dnsServers = dnsServers;
  944. dnsModal.close();
  945. },
  946. isEdit: true
  947. });
  948. },
  949. deleteDNSServer(index) {
  950. newDnsServers = this.dnsServers;
  951. newDnsServers.splice(index, 1);
  952. this.dnsServers = newDnsServers;
  953. },
  954. addFakedns() {
  955. fakednsModal.show({
  956. title: '{{ i18n "pages.xray.fakedns.add" }}',
  957. confirm: (item) => {
  958. fakeDns = this.fakeDns ?? [];
  959. fakeDns.push(item);
  960. this.fakeDns = fakeDns;
  961. fakednsModal.close();
  962. },
  963. isEdit: false
  964. });
  965. },
  966. editFakedns(index) {
  967. fakednsModal.show({
  968. title: '{{ i18n "pages.xray.fakedns.edit" }} #' + (index + 1),
  969. fakeDns: this.fakeDns[index],
  970. confirm: (item) => {
  971. fakeDns = this.fakeDns;
  972. fakeDns[index] = item;
  973. this.fakeDns = fakeDns;
  974. fakednsModal.close();
  975. },
  976. isEdit: true
  977. });
  978. },
  979. deleteFakedns(index) {
  980. fakeDns = this.fakeDns;
  981. fakeDns.splice(index, 1);
  982. this.fakeDns = fakeDns;
  983. },
  984. addRule() {
  985. ruleModal.show({
  986. title: '{{ i18n "pages.xray.rules.add"}}',
  987. okText: '{{ i18n "pages.xray.rules.add" }}',
  988. confirm: (rule) => {
  989. ruleModal.loading();
  990. if (JSON.stringify(rule).length > 3) {
  991. this.templateSettings.routing.rules.push(rule);
  992. this.routingRuleSettings = JSON.stringify(this.templateSettings.routing.rules);
  993. }
  994. ruleModal.close();
  995. },
  996. isEdit: false
  997. });
  998. },
  999. editRule(index) {
  1000. ruleModal.show({
  1001. title: '{{ i18n "pages.xray.rules.edit"}} ' + (index + 1),
  1002. rule: app.templateSettings.routing.rules[index],
  1003. confirm: (rule) => {
  1004. ruleModal.loading();
  1005. if (JSON.stringify(rule).length > 3) {
  1006. this.templateSettings.routing.rules[index] = rule;
  1007. this.routingRuleSettings = JSON.stringify(this.templateSettings.routing.rules);
  1008. }
  1009. ruleModal.close();
  1010. },
  1011. isEdit: true
  1012. });
  1013. },
  1014. replaceRule(old_index, new_index) {
  1015. rules = this.templateSettings.routing.rules;
  1016. if (new_index >= rules.length) rules.push(undefined);
  1017. rules.splice(new_index, 0, rules.splice(old_index, 1)[0]);
  1018. this.routingRuleSettings = JSON.stringify(rules);
  1019. },
  1020. deleteRule(index) {
  1021. rules = this.templateSettings.routing.rules;
  1022. rules.splice(index, 1);
  1023. this.routingRuleSettings = JSON.stringify(rules);
  1024. },
  1025. showWarp() {
  1026. warpModal.show();
  1027. }
  1028. },
  1029. async mounted() {
  1030. if (window.location.protocol !== "https:") {
  1031. this.showAlert = true;
  1032. }
  1033. await this.getXraySetting();
  1034. await this.getXrayResult();
  1035. await this.getOutboundsTraffic();
  1036. if (window.wsClient) {
  1037. window.wsClient.connect();
  1038. window.wsClient.on('outbounds', (payload) => {
  1039. if (payload) {
  1040. this.outboundsTraffic = payload;
  1041. this.$forceUpdate();
  1042. }
  1043. });
  1044. }
  1045. while (true) {
  1046. await PromiseUtil.sleep(800);
  1047. this.saveBtnDisable = this.oldXraySetting === this.xraySetting && this.oldOutboundTestUrl === this.outboundTestUrl;
  1048. }
  1049. },
  1050. computed: {
  1051. templateSettings: {
  1052. get: function () {
  1053. const parsedSettings = this.xraySetting ? JSON.parse(this.xraySetting) : null;
  1054. return parsedSettings;
  1055. },
  1056. set: function (newValue) {
  1057. if (newValue) {
  1058. this.xraySetting = JSON.stringify(newValue, null, 2);
  1059. }
  1060. },
  1061. },
  1062. inboundSettings: {
  1063. get: function () { return this.templateSettings ? JSON.stringify(this.templateSettings.inbounds, null, 2) : null; },
  1064. set: function (newValue) {
  1065. newTemplateSettings = this.templateSettings;
  1066. newTemplateSettings.inbounds = JSON.parse(newValue);
  1067. this.templateSettings = newTemplateSettings;
  1068. },
  1069. },
  1070. outboundSettings: {
  1071. get: function () { return this.templateSettings ? JSON.stringify(this.templateSettings.outbounds, null, 2) : null; },
  1072. set: function (newValue) {
  1073. newTemplateSettings = this.templateSettings;
  1074. newTemplateSettings.outbounds = JSON.parse(newValue);
  1075. this.templateSettings = newTemplateSettings;
  1076. },
  1077. },
  1078. outboundData: {
  1079. get: function () {
  1080. data = []
  1081. if (this.templateSettings != null) {
  1082. this.templateSettings.outbounds.forEach((o, index) => {
  1083. data.push({ 'key': index, ...o });
  1084. });
  1085. }
  1086. return data;
  1087. },
  1088. },
  1089. reverseData: {
  1090. get: function () {
  1091. data = []
  1092. if (this.templateSettings != null && this.templateSettings.reverse != null) {
  1093. if (this.templateSettings.reverse.bridges) {
  1094. this.templateSettings.reverse.bridges.forEach((o, index) => {
  1095. data.push({ 'key': index, 'type': 'bridge', ...o });
  1096. });
  1097. }
  1098. if (this.templateSettings.reverse.portals) {
  1099. this.templateSettings.reverse.portals.forEach((o, index) => {
  1100. data.push({ 'key': index, 'type': 'portal', ...o });
  1101. });
  1102. }
  1103. }
  1104. return data;
  1105. },
  1106. },
  1107. routingRuleSettings: {
  1108. get: function () { return this.templateSettings ? JSON.stringify(this.templateSettings.routing.rules, null, 2) : null; },
  1109. set: function (newValue) {
  1110. newTemplateSettings = this.templateSettings;
  1111. newTemplateSettings.routing.rules = JSON.parse(newValue);
  1112. this.templateSettings = newTemplateSettings;
  1113. },
  1114. },
  1115. routingRuleData: {
  1116. get: function () {
  1117. data = [];
  1118. if (this.templateSettings != null) {
  1119. this.templateSettings.routing.rules.forEach((r, index) => {
  1120. data.push({ 'key': index, ...r });
  1121. });
  1122. // Make rules readable
  1123. data.forEach(r => {
  1124. if (r.domain) r.domain = r.domain.join(',')
  1125. if (r.ip) r.ip = r.ip.join(',')
  1126. if (r.source) r.source = r.source.join(',');
  1127. if (r.user) r.user = r.user.join(',')
  1128. if (r.inboundTag) r.inboundTag = r.inboundTag.join(',')
  1129. if (r.protocol) r.protocol = r.protocol.join(',')
  1130. if (r.attrs) r.attrs = JSON.stringify(r.attrs, null, 2)
  1131. });
  1132. }
  1133. return data;
  1134. }
  1135. },
  1136. balancersData: {
  1137. get: function () {
  1138. data = []
  1139. if (this.templateSettings != null && this.templateSettings.routing != null && this.templateSettings.routing.balancers != null) {
  1140. this.templateSettings.routing.balancers.forEach((o, index) => {
  1141. data.push({
  1142. 'key': index,
  1143. 'tag': o.tag ? o.tag : "",
  1144. 'strategy': o.strategy?.type ?? "random",
  1145. 'selector': o.selector ? o.selector : [],
  1146. 'fallbackTag': o.fallbackTag ?? '',
  1147. });
  1148. });
  1149. }
  1150. return data;
  1151. }
  1152. },
  1153. observatory: {
  1154. get: function () {
  1155. return this.templateSettings?.observatory ? JSON.stringify(this.templateSettings.observatory, null, 2) : null;
  1156. },
  1157. set: function (newValue) {
  1158. newTemplateSettings = this.templateSettings;
  1159. newTemplateSettings.observatory = JSON.parse(newValue);
  1160. this.templateSettings = newTemplateSettings;
  1161. },
  1162. },
  1163. burstObservatory: {
  1164. get: function () {
  1165. return this.templateSettings?.burstObservatory ? JSON.stringify(this.templateSettings.burstObservatory, null, 2) : null;
  1166. },
  1167. set: function (newValue) {
  1168. newTemplateSettings = this.templateSettings;
  1169. newTemplateSettings.burstObservatory = JSON.parse(newValue);
  1170. this.templateSettings = newTemplateSettings;
  1171. },
  1172. },
  1173. observatoryEnable: function () { return this.templateSettings != null && this.templateSettings.observatory != undefined },
  1174. burstObservatoryEnable: function () { return this.templateSettings != null && this.templateSettings.burstObservatory != undefined },
  1175. freedomStrategy: {
  1176. get: function () {
  1177. if (!this.templateSettings) return "AsIs";
  1178. freedomOutbound = this.templateSettings.outbounds.find((o) => o.protocol === "freedom" && o.tag == "direct");
  1179. if (!freedomOutbound) return "AsIs";
  1180. if (!freedomOutbound.settings || !freedomOutbound.settings.domainStrategy) return "AsIs";
  1181. return freedomOutbound.settings.domainStrategy;
  1182. },
  1183. set: function (newValue) {
  1184. newTemplateSettings = this.templateSettings;
  1185. freedomOutboundIndex = newTemplateSettings.outbounds.findIndex((o) => o.protocol === "freedom" && o.tag == "direct");
  1186. if (freedomOutboundIndex == -1) {
  1187. newTemplateSettings.outbounds.push({ protocol: "freedom", tag: "direct", settings: { "domainStrategy": newValue } });
  1188. } else if (!newTemplateSettings.outbounds[freedomOutboundIndex].settings) {
  1189. newTemplateSettings.outbounds[freedomOutboundIndex].settings = { "domainStrategy": newValue };
  1190. } else {
  1191. newTemplateSettings.outbounds[freedomOutboundIndex].settings.domainStrategy = newValue;
  1192. }
  1193. this.templateSettings = newTemplateSettings;
  1194. }
  1195. },
  1196. routingStrategy: {
  1197. get: function () {
  1198. if (!this.templateSettings || !this.templateSettings.routing || !this.templateSettings.routing.domainStrategy) return "AsIs";
  1199. return this.templateSettings.routing.domainStrategy;
  1200. },
  1201. set: function (newValue) {
  1202. newTemplateSettings = this.templateSettings;
  1203. newTemplateSettings.routing.domainStrategy = newValue;
  1204. this.templateSettings = newTemplateSettings;
  1205. }
  1206. },
  1207. logLevel: {
  1208. get: function () {
  1209. if (!this.templateSettings || !this.templateSettings.log || !this.templateSettings.log.loglevel) return "warning";
  1210. return this.templateSettings.log.loglevel;
  1211. },
  1212. set: function (newValue) {
  1213. newTemplateSettings = this.templateSettings;
  1214. newTemplateSettings.log.loglevel = newValue;
  1215. this.templateSettings = newTemplateSettings;
  1216. }
  1217. },
  1218. accessLog: {
  1219. get: function () {
  1220. if (!this.templateSettings || !this.templateSettings.log || !this.templateSettings.log.access) return "";
  1221. return this.templateSettings.log.access;
  1222. },
  1223. set: function (newValue) {
  1224. newTemplateSettings = this.templateSettings;
  1225. newTemplateSettings.log.access = newValue;
  1226. this.templateSettings = newTemplateSettings;
  1227. }
  1228. },
  1229. errorLog: {
  1230. get: function () {
  1231. if (!this.templateSettings || !this.templateSettings.log || !this.templateSettings.log.error) return "";
  1232. return this.templateSettings.log.error;
  1233. },
  1234. set: function (newValue) {
  1235. newTemplateSettings = this.templateSettings;
  1236. newTemplateSettings.log.error = newValue;
  1237. this.templateSettings = newTemplateSettings;
  1238. }
  1239. },
  1240. dnslog: {
  1241. get: function () {
  1242. if (!this.templateSettings || !this.templateSettings.log || !this.templateSettings.log.dnsLog) return false;
  1243. return this.templateSettings.log.dnsLog;
  1244. },
  1245. set: function (newValue) {
  1246. newTemplateSettings = this.templateSettings;
  1247. newTemplateSettings.log.dnsLog = newValue;
  1248. this.templateSettings = newTemplateSettings;
  1249. }
  1250. },
  1251. statsInboundUplink: {
  1252. get: function () {
  1253. if (!this.templateSettings || !this.templateSettings.policy.system || !this.templateSettings.policy.system.statsInboundUplink) return false;
  1254. return this.templateSettings.policy.system.statsInboundUplink;
  1255. },
  1256. set: function (newValue) {
  1257. newTemplateSettings = this.templateSettings;
  1258. newTemplateSettings.policy.system.statsInboundUplink = newValue;
  1259. this.templateSettings = newTemplateSettings;
  1260. }
  1261. },
  1262. statsInboundDownlink: {
  1263. get: function () {
  1264. if (!this.templateSettings || !this.templateSettings.policy.system || !this.templateSettings.policy.system.statsInboundDownlink) return false;
  1265. return this.templateSettings.policy.system.statsInboundDownlink;
  1266. },
  1267. set: function (newValue) {
  1268. newTemplateSettings = this.templateSettings;
  1269. newTemplateSettings.policy.system.statsInboundDownlink = newValue;
  1270. this.templateSettings = newTemplateSettings;
  1271. }
  1272. },
  1273. statsOutboundUplink: {
  1274. get: function () {
  1275. if (!this.templateSettings || !this.templateSettings.policy.system || !this.templateSettings.policy.system.statsOutboundUplink) return false;
  1276. return this.templateSettings.policy.system.statsOutboundUplink;
  1277. },
  1278. set: function (newValue) {
  1279. newTemplateSettings = this.templateSettings;
  1280. newTemplateSettings.policy.system.statsOutboundUplink = newValue;
  1281. this.templateSettings = newTemplateSettings;
  1282. }
  1283. },
  1284. statsOutboundDownlink: {
  1285. get: function () {
  1286. if (!this.templateSettings || !this.templateSettings.policy.system || !this.templateSettings.policy.system.statsOutboundDownlink) return false;
  1287. return this.templateSettings.policy.system.statsOutboundDownlink;
  1288. },
  1289. set: function (newValue) {
  1290. newTemplateSettings = this.templateSettings;
  1291. newTemplateSettings.policy.system.statsOutboundDownlink = newValue;
  1292. this.templateSettings = newTemplateSettings;
  1293. }
  1294. },
  1295. maskAddressLog: {
  1296. get: function () {
  1297. if (!this.templateSettings || !this.templateSettings.log || !this.templateSettings.log.maskAddress) return "";
  1298. return this.templateSettings.log.maskAddress;
  1299. },
  1300. set: function (newValue) {
  1301. newTemplateSettings = this.templateSettings;
  1302. newTemplateSettings.log.maskAddress = newValue;
  1303. this.templateSettings = newTemplateSettings;
  1304. }
  1305. },
  1306. blockedIPs: {
  1307. get: function () {
  1308. return this.templateRuleGetter({ outboundTag: "blocked", property: "ip" });
  1309. },
  1310. set: function (newValue) {
  1311. this.templateRuleSetter({ outboundTag: "blocked", property: "ip", data: newValue });
  1312. }
  1313. },
  1314. blockedDomains: {
  1315. get: function () {
  1316. return this.templateRuleGetter({ outboundTag: "blocked", property: "domain" });
  1317. },
  1318. set: function (newValue) {
  1319. this.templateRuleSetter({ outboundTag: "blocked", property: "domain", data: newValue });
  1320. }
  1321. },
  1322. blockedProtocols: {
  1323. get: function () {
  1324. return this.templateRuleGetter({ outboundTag: "blocked", property: "protocol" });
  1325. },
  1326. set: function (newValue) {
  1327. this.templateRuleSetter({ outboundTag: "blocked", property: "protocol", data: newValue });
  1328. }
  1329. },
  1330. directIPs: {
  1331. get: function () {
  1332. return this.templateRuleGetter({ outboundTag: "direct", property: "ip" });
  1333. },
  1334. set: function (newValue) {
  1335. this.templateRuleSetter({ outboundTag: "direct", property: "ip", data: newValue });
  1336. this.syncRulesWithOutbound("direct", this.directSettings);
  1337. }
  1338. },
  1339. directDomains: {
  1340. get: function () {
  1341. return this.templateRuleGetter({ outboundTag: "direct", property: "domain" });
  1342. },
  1343. set: function (newValue) {
  1344. this.templateRuleSetter({ outboundTag: "direct", property: "domain", data: newValue });
  1345. this.syncRulesWithOutbound("direct", this.directSettings);
  1346. }
  1347. },
  1348. ipv4Domains: {
  1349. get: function () {
  1350. return this.templateRuleGetter({ outboundTag: "IPv4", property: "domain" });
  1351. },
  1352. set: function (newValue) {
  1353. this.templateRuleSetter({ outboundTag: "IPv4", property: "domain", data: newValue });
  1354. this.syncRulesWithOutbound("IPv4", this.ipv4Settings);
  1355. }
  1356. },
  1357. warpDomains: {
  1358. get: function () {
  1359. return this.templateRuleGetter({ outboundTag: "warp", property: "domain" });
  1360. },
  1361. set: function (newValue) {
  1362. this.templateRuleSetter({ outboundTag: "warp", property: "domain", data: newValue });
  1363. }
  1364. },
  1365. torrentSettings: {
  1366. get: function () {
  1367. return ArrayUtils.doAllItemsExist(this.settingsData.protocols.bittorrent, this.blockedProtocols);
  1368. },
  1369. set: function (newValue) {
  1370. if (newValue) {
  1371. this.blockedProtocols = [...this.blockedProtocols, ...this.settingsData.protocols.bittorrent];
  1372. } else {
  1373. this.blockedProtocols = this.blockedProtocols.filter(data => !this.settingsData.protocols.bittorrent.includes(data));
  1374. }
  1375. },
  1376. },
  1377. WarpExist: {
  1378. get: function () {
  1379. return this.templateSettings ? this.templateSettings.outbounds.findIndex((o) => o.tag == "warp") >= 0 : false;
  1380. },
  1381. },
  1382. enableDNS: {
  1383. get: function () {
  1384. return this.templateSettings ? this.templateSettings.dns != null : false;
  1385. },
  1386. set: function (newValue) {
  1387. newTemplateSettings = this.templateSettings;
  1388. if (newValue) {
  1389. newTemplateSettings.dns = {
  1390. servers: [],
  1391. queryStrategy: "UseIP",
  1392. tag: "dns_inbound",
  1393. enableParallelQuery: false
  1394. };
  1395. newTemplateSettings.fakedns = null;
  1396. } else {
  1397. delete newTemplateSettings.dns;
  1398. delete newTemplateSettings.fakedns;
  1399. }
  1400. this.templateSettings = newTemplateSettings;
  1401. }
  1402. },
  1403. dnsTag: {
  1404. get: function () {
  1405. return this.enableDNS ? this.templateSettings.dns.tag : "";
  1406. },
  1407. set: function (newValue) {
  1408. newTemplateSettings = this.templateSettings;
  1409. newTemplateSettings.dns.tag = newValue;
  1410. this.templateSettings = newTemplateSettings;
  1411. }
  1412. },
  1413. dnsClientIp: {
  1414. get: function () {
  1415. return this.enableDNS ? this.templateSettings.dns.clientIp : null;
  1416. },
  1417. set: function (newValue) {
  1418. newTemplateSettings = this.templateSettings;
  1419. if (newValue) {
  1420. newTemplateSettings.dns.clientIp = newValue;
  1421. } else {
  1422. delete newTemplateSettings.dns.clientIp;
  1423. }
  1424. this.templateSettings = newTemplateSettings;
  1425. }
  1426. },
  1427. dnsDisableCache: {
  1428. get: function () {
  1429. return this.enableDNS ? this.templateSettings.dns.disableCache : false;
  1430. },
  1431. set: function (newValue) {
  1432. newTemplateSettings = this.templateSettings;
  1433. if (newValue) {
  1434. newTemplateSettings.dns.disableCache = newValue;
  1435. } else {
  1436. delete newTemplateSettings.dns.disableCache
  1437. }
  1438. this.templateSettings = newTemplateSettings;
  1439. }
  1440. },
  1441. dnsDisableFallback: {
  1442. get: function () {
  1443. return this.enableDNS ? this.templateSettings.dns.disableFallback : false;
  1444. },
  1445. set: function (newValue) {
  1446. newTemplateSettings = this.templateSettings;
  1447. if (newValue) {
  1448. newTemplateSettings.dns.disableFallback = newValue;
  1449. } else {
  1450. delete newTemplateSettings.dns.disableFallback
  1451. }
  1452. this.templateSettings = newTemplateSettings;
  1453. }
  1454. },
  1455. dnsDisableFallbackIfMatch: {
  1456. get: function () {
  1457. return this.enableDNS ? this.templateSettings.dns.disableFallbackIfMatch : false;
  1458. },
  1459. set: function (newValue) {
  1460. newTemplateSettings = this.templateSettings;
  1461. if (newValue) {
  1462. newTemplateSettings.dns.disableFallbackIfMatch = newValue;
  1463. } else {
  1464. delete newTemplateSettings.dns.disableFallbackIfMatch
  1465. }
  1466. this.templateSettings = newTemplateSettings;
  1467. }
  1468. },
  1469. dnsEnableParallelQuery: {
  1470. get: function () {
  1471. return this.enableDNS ? (this.templateSettings.dns.enableParallelQuery || false) : false;
  1472. },
  1473. set: function (newValue) {
  1474. newTemplateSettings = this.templateSettings;
  1475. if (newValue) {
  1476. newTemplateSettings.dns.enableParallelQuery = newValue;
  1477. } else {
  1478. delete newTemplateSettings.dns.enableParallelQuery
  1479. }
  1480. this.templateSettings = newTemplateSettings;
  1481. }
  1482. },
  1483. dnsUseSystemHosts: {
  1484. get: function () {
  1485. return this.enableDNS ? this.templateSettings.dns.useSystemHosts : false;
  1486. },
  1487. set: function (newValue) {
  1488. newTemplateSettings = this.templateSettings;
  1489. if (newValue) {
  1490. newTemplateSettings.dns.useSystemHosts = newValue;
  1491. } else {
  1492. delete newTemplateSettings.dns.useSystemHosts
  1493. }
  1494. this.templateSettings = newTemplateSettings;
  1495. }
  1496. },
  1497. dnsStrategy: {
  1498. get: function () {
  1499. return this.enableDNS ? this.templateSettings.dns.queryStrategy : null;
  1500. },
  1501. set: function (newValue) {
  1502. newTemplateSettings = this.templateSettings;
  1503. newTemplateSettings.dns.queryStrategy = newValue;
  1504. this.templateSettings = newTemplateSettings;
  1505. }
  1506. },
  1507. dnsServers: {
  1508. get: function () { return this.enableDNS ? this.templateSettings.dns.servers : []; },
  1509. set: function (newValue) {
  1510. newTemplateSettings = this.templateSettings;
  1511. newTemplateSettings.dns.servers = newValue;
  1512. this.templateSettings = newTemplateSettings;
  1513. }
  1514. },
  1515. fakeDns: {
  1516. get: function () { return this.templateSettings && this.templateSettings.fakedns ? this.templateSettings.fakedns : []; },
  1517. set: function (newValue) {
  1518. newTemplateSettings = this.templateSettings;
  1519. if (this.enableDNS) {
  1520. newTemplateSettings.fakedns = newValue.length > 0 ? newValue : null;
  1521. } else {
  1522. delete newTemplateSettings.fakedns;
  1523. }
  1524. this.templateSettings = newTemplateSettings;
  1525. }
  1526. }
  1527. },
  1528. });
  1529. </script>
  1530. {{ template "page/body_end" .}}