xray.html 69 KB

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