xray.html 59 KB

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