xray.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  1. const Protocols = {
  2. VMESS: 'vmess',
  3. VLESS: 'vless',
  4. TROJAN: 'trojan',
  5. SHADOWSOCKS: 'shadowsocks',
  6. DOKODEMO: 'dokodemo-door',
  7. MTPROTO: 'mtproto',
  8. SOCKS: 'socks',
  9. HTTP: 'http',
  10. };
  11. const VmessMethods = {
  12. AES_128_GCM: 'aes-128-gcm',
  13. CHACHA20_POLY1305: 'chacha20-poly1305',
  14. AUTO: 'auto',
  15. NONE: 'none',
  16. };
  17. const SSMethods = {
  18. // AES_256_CFB: 'aes-256-cfb',
  19. // AES_128_CFB: 'aes-128-cfb',
  20. // CHACHA20: 'chacha20',
  21. // CHACHA20_IETF: 'chacha20-ietf',
  22. CHACHA20_POLY1305: 'chacha20-poly1305',
  23. AES_256_GCM: 'aes-256-gcm',
  24. AES_128_GCM: 'aes-128-gcm',
  25. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  26. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  27. BLAKE3_CHACHA20_POLY1305: '2022-blake3-chacha20-poly1305',
  28. };
  29. const RULE_IP = {
  30. PRIVATE: 'geoip:private',
  31. CN: 'geoip:cn',
  32. };
  33. const RULE_DOMAIN = {
  34. ADS: 'geosite:category-ads',
  35. ADS_ALL: 'geosite:category-ads-all',
  36. CN: 'geosite:cn',
  37. GOOGLE: 'geosite:google',
  38. FACEBOOK: 'geosite:facebook',
  39. SPEEDTEST: 'geosite:speedtest',
  40. };
  41. const XTLS_FLOW_CONTROL = {
  42. ORIGIN: "xtls-rprx-origin",
  43. DIRECT: "xtls-rprx-direct",
  44. };
  45. const TLS_FLOW_CONTROL = {
  46. VISION: "xtls-rprx-vision",
  47. };
  48. const TLS_VERSION_OPTION = {
  49. TLS10: "1.0",
  50. TLS11: "1.1",
  51. TLS12: "1.2",
  52. TLS13: "1.3",
  53. }
  54. const TLS_CIPHER_OPTION = {
  55. RSA_AES_128_CBC: "TLS_RSA_WITH_AES_128_CBC_SHA",
  56. RSA_AES_256_CBC: "TLS_RSA_WITH_AES_256_CBC_SHA",
  57. RSA_AES_128_GCM: "TLS_RSA_WITH_AES_128_GCM_SHA256",
  58. RSA_AES_256_GCM: "TLS_RSA_WITH_AES_256_GCM_SHA384",
  59. AES_128_GCM: "TLS_AES_128_GCM_SHA256",
  60. AES_256_GCM: "TLS_AES_256_GCM_SHA384",
  61. CHACHA20_POLY1305: "TLS_CHACHA20_POLY1305_SHA256",
  62. ECDHE_ECDSA_AES_128_CBC: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
  63. ECDHE_ECDSA_AES_256_CBC: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
  64. ECDHE_RSA_AES_128_CBC: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
  65. ECDHE_RSA_AES_256_CBC: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
  66. ECDHE_ECDSA_AES_128_GCM: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
  67. ECDHE_ECDSA_AES_256_GCM: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  68. ECDHE_RSA_AES_128_GCM: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  69. ECDHE_RSA_AES_256_GCM: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
  70. ECDHE_ECDSA_CHACHA20_POLY1305: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
  71. ECDHE_RSA_CHACHA20_POLY1305: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
  72. };
  73. const UTLS_FINGERPRINT = {
  74. UTLS_CHROME: "chrome",
  75. UTLS_FIREFOX: "firefox",
  76. UTLS_SAFARI: "safari",
  77. UTLS_IOS: "ios",
  78. UTLS_android: "android",
  79. UTLS_EDGE: "edge",
  80. UTLS_360: "360",
  81. UTLS_QQ: "qq",
  82. UTLS_RANDOM: "random",
  83. UTLS_RANDOMIZED: "randomized",
  84. };
  85. Object.freeze(Protocols);
  86. Object.freeze(VmessMethods);
  87. Object.freeze(SSMethods);
  88. Object.freeze(RULE_IP);
  89. Object.freeze(RULE_DOMAIN);
  90. Object.freeze(XTLS_FLOW_CONTROL);
  91. Object.freeze(TLS_FLOW_CONTROL);
  92. Object.freeze(TLS_VERSION_OPTION);
  93. Object.freeze(TLS_CIPHER_OPTION);
  94. Object.freeze(UTLS_FINGERPRINT);
  95. class XrayCommonClass {
  96. static toJsonArray(arr) {
  97. return arr.map(obj => obj.toJson());
  98. }
  99. static fromJson() {
  100. return new XrayCommonClass();
  101. }
  102. toJson() {
  103. return this;
  104. }
  105. toString(format=true) {
  106. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  107. }
  108. static toHeaders(v2Headers) {
  109. let newHeaders = [];
  110. if (v2Headers) {
  111. Object.keys(v2Headers).forEach(key => {
  112. let values = v2Headers[key];
  113. if (typeof(values) === 'string') {
  114. newHeaders.push({ name: key, value: values });
  115. } else {
  116. for (let i = 0; i < values.length; ++i) {
  117. newHeaders.push({ name: key, value: values[i] });
  118. }
  119. }
  120. });
  121. }
  122. return newHeaders;
  123. }
  124. static toV2Headers(headers, arr=true) {
  125. let v2Headers = {};
  126. for (let i = 0; i < headers.length; ++i) {
  127. let name = headers[i].name;
  128. let value = headers[i].value;
  129. if (ObjectUtil.isEmpty(name) || ObjectUtil.isEmpty(value)) {
  130. continue;
  131. }
  132. if (!(name in v2Headers)) {
  133. v2Headers[name] = arr ? [value] : value;
  134. } else {
  135. if (arr) {
  136. v2Headers[name].push(value);
  137. } else {
  138. v2Headers[name] = value;
  139. }
  140. }
  141. }
  142. return v2Headers;
  143. }
  144. }
  145. class TcpStreamSettings extends XrayCommonClass {
  146. constructor(
  147. type = 'none',
  148. acceptProxyProtocol = false,
  149. request = new TcpStreamSettings.TcpRequest(),
  150. response = new TcpStreamSettings.TcpResponse(),
  151. ) {
  152. super();
  153. this.type = type;
  154. this.request = request;
  155. this.response = response;
  156. this.acceptProxyProtocol = acceptProxyProtocol;
  157. }
  158. static fromJson(json = {}) {
  159. let header = json.header;
  160. if (!header) {
  161. header = {};
  162. }
  163. return new TcpStreamSettings(
  164. header.type,
  165. json.acceptProxyProtocol,
  166. TcpStreamSettings.TcpRequest.fromJson(header.request),
  167. TcpStreamSettings.TcpResponse.fromJson(header.response),
  168. );
  169. }
  170. toJson() {
  171. return {
  172. header: {
  173. type: this.type,
  174. request: this.type === 'http' ? this.request.toJson() : undefined,
  175. response: this.type === 'http' ? this.response.toJson() : undefined,
  176. },
  177. acceptProxyProtocol: this.acceptProxyProtocol,
  178. };
  179. }
  180. }
  181. TcpStreamSettings.TcpRequest = class extends XrayCommonClass {
  182. constructor(version = '1.1',
  183. method = 'GET',
  184. path = ['/'],
  185. headers = [],
  186. ) {
  187. super();
  188. this.version = version;
  189. this.method = method;
  190. this.path = path.length === 0 ? ['/'] : path;
  191. this.headers = headers;
  192. }
  193. addPath(path) {
  194. this.path.push(path);
  195. }
  196. removePath(index) {
  197. this.path.splice(index, 1);
  198. }
  199. addHeader(name, value) {
  200. this.headers.push({ name: name, value: value });
  201. }
  202. getHeader(name) {
  203. for (const header of this.headers) {
  204. if (header.name.toLowerCase() === name.toLowerCase()) {
  205. return header.value;
  206. }
  207. }
  208. return null;
  209. }
  210. removeHeader(index) {
  211. this.headers.splice(index, 1);
  212. }
  213. static fromJson(json = {}) {
  214. return new TcpStreamSettings.TcpRequest(
  215. json.version,
  216. json.method,
  217. json.path,
  218. XrayCommonClass.toHeaders(json.headers),
  219. );
  220. }
  221. toJson() {
  222. return {
  223. method: this.method,
  224. path: ObjectUtil.clone(this.path),
  225. headers: XrayCommonClass.toV2Headers(this.headers),
  226. };
  227. }
  228. };
  229. TcpStreamSettings.TcpResponse = class extends XrayCommonClass {
  230. constructor(version = '1.1',
  231. status = '200',
  232. reason = 'OK',
  233. headers = [],
  234. ) {
  235. super();
  236. this.version = version;
  237. this.status = status;
  238. this.reason = reason;
  239. this.headers = headers;
  240. }
  241. addHeader(name, value) {
  242. this.headers.push({ name: name, value: value });
  243. }
  244. removeHeader(index) {
  245. this.headers.splice(index, 1);
  246. }
  247. static fromJson(json = {}) {
  248. return new TcpStreamSettings.TcpResponse(
  249. json.version,
  250. json.status,
  251. json.reason,
  252. XrayCommonClass.toHeaders(json.headers),
  253. );
  254. }
  255. toJson() {
  256. return {
  257. version: this.version,
  258. status: this.status,
  259. reason: this.reason,
  260. headers: XrayCommonClass.toV2Headers(this.headers),
  261. };
  262. }
  263. };
  264. class KcpStreamSettings extends XrayCommonClass {
  265. constructor(mtu=1350, tti=20,
  266. uplinkCapacity=5,
  267. downlinkCapacity=20,
  268. congestion=false,
  269. readBufferSize=2,
  270. writeBufferSize=2,
  271. type='none',
  272. seed=RandomUtil.randomSeq(10),
  273. ) {
  274. super();
  275. this.mtu = mtu;
  276. this.tti = tti;
  277. this.upCap = uplinkCapacity;
  278. this.downCap = downlinkCapacity;
  279. this.congestion = congestion;
  280. this.readBuffer = readBufferSize;
  281. this.writeBuffer = writeBufferSize;
  282. this.type = type;
  283. this.seed = seed;
  284. }
  285. static fromJson(json={}) {
  286. return new KcpStreamSettings(
  287. json.mtu,
  288. json.tti,
  289. json.uplinkCapacity,
  290. json.downlinkCapacity,
  291. json.congestion,
  292. json.readBufferSize,
  293. json.writeBufferSize,
  294. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  295. json.seed,
  296. );
  297. }
  298. toJson() {
  299. return {
  300. mtu: this.mtu,
  301. tti: this.tti,
  302. uplinkCapacity: this.upCap,
  303. downlinkCapacity: this.downCap,
  304. congestion: this.congestion,
  305. readBufferSize: this.readBuffer,
  306. writeBufferSize: this.writeBuffer,
  307. header: {
  308. type: this.type,
  309. },
  310. seed: this.seed,
  311. };
  312. }
  313. }
  314. class WsStreamSettings extends XrayCommonClass {
  315. constructor(acceptProxyProtocol=false, path='/', headers=[]) {
  316. super();
  317. this.acceptProxyProtocol = acceptProxyProtocol;
  318. this.path = path;
  319. this.headers = headers;
  320. }
  321. addHeader(name, value) {
  322. this.headers.push({ name: name, value: value });
  323. }
  324. getHeader(name) {
  325. for (const header of this.headers) {
  326. if (header.name.toLowerCase() === name.toLowerCase()) {
  327. return header.value;
  328. }
  329. }
  330. return null;
  331. }
  332. removeHeader(index) {
  333. this.headers.splice(index, 1);
  334. }
  335. static fromJson(json={}) {
  336. return new WsStreamSettings(
  337. json.acceptProxyProtocol,
  338. json.path,
  339. XrayCommonClass.toHeaders(json.headers),
  340. );
  341. }
  342. toJson() {
  343. return {
  344. acceptProxyProtocol: this.acceptProxyProtocol,
  345. path: this.path,
  346. headers: XrayCommonClass.toV2Headers(this.headers, false),
  347. };
  348. }
  349. }
  350. class HttpStreamSettings extends XrayCommonClass {
  351. constructor(path='/', host=['']) {
  352. super();
  353. this.path = path;
  354. this.host = host.length === 0 ? [''] : host;
  355. }
  356. addHost(host) {
  357. this.host.push(host);
  358. }
  359. removeHost(index) {
  360. this.host.splice(index, 1);
  361. }
  362. static fromJson(json={}) {
  363. return new HttpStreamSettings(json.path, json.host);
  364. }
  365. toJson() {
  366. let host = [];
  367. for (let i = 0; i < this.host.length; ++i) {
  368. if (!ObjectUtil.isEmpty(this.host[i])) {
  369. host.push(this.host[i]);
  370. }
  371. }
  372. return {
  373. path: this.path,
  374. host: host,
  375. }
  376. }
  377. }
  378. class QuicStreamSettings extends XrayCommonClass {
  379. constructor(security=VmessMethods.NONE,
  380. key='', type='none') {
  381. super();
  382. this.security = security;
  383. this.key = key;
  384. this.type = type;
  385. }
  386. static fromJson(json={}) {
  387. return new QuicStreamSettings(
  388. json.security,
  389. json.key,
  390. json.header ? json.header.type : 'none',
  391. );
  392. }
  393. toJson() {
  394. return {
  395. security: this.security,
  396. key: this.key,
  397. header: {
  398. type: this.type,
  399. }
  400. }
  401. }
  402. }
  403. class GrpcStreamSettings extends XrayCommonClass {
  404. constructor(serviceName="") {
  405. super();
  406. this.serviceName = serviceName;
  407. }
  408. static fromJson(json={}) {
  409. return new GrpcStreamSettings(json.serviceName);
  410. }
  411. toJson() {
  412. return {
  413. serviceName: this.serviceName,
  414. }
  415. }
  416. }
  417. class TlsStreamSettings extends XrayCommonClass {
  418. constructor(serverName = '', minVersion = TLS_VERSION_OPTION.TLS12, maxVersion = TLS_VERSION_OPTION.TLS13,
  419. cipherSuites = '',
  420. certificates = [new TlsStreamSettings.Cert()], alpn = ["h2", "http/1.1"]) {
  421. super();
  422. this.server = serverName;
  423. this.minVersion = minVersion;
  424. this.maxVersion = maxVersion;
  425. this.cipherSuites = cipherSuites;
  426. this.certs = certificates;
  427. this.alpn = alpn;
  428. }
  429. addCert(cert) {
  430. this.certs.push(cert);
  431. }
  432. removeCert(index) {
  433. this.certs.splice(index, 1);
  434. }
  435. static fromJson(json={}) {
  436. let certs;
  437. if (!ObjectUtil.isEmpty(json.certificates)) {
  438. certs = json.certificates.map(cert => TlsStreamSettings.Cert.fromJson(cert));
  439. }
  440. return new TlsStreamSettings(
  441. json.serverName,
  442. json.minVersion,
  443. json.maxVersion,
  444. json.cipherSuites,
  445. certs,
  446. json.alpn,
  447. );
  448. }
  449. toJson() {
  450. return {
  451. serverName: this.server,
  452. minVersion: this.minVersion,
  453. maxVersion: this.maxVersion,
  454. cipherSuites: this.cipherSuites,
  455. certificates: TlsStreamSettings.toJsonArray(this.certs),
  456. alpn: this.alpn
  457. };
  458. }
  459. }
  460. TlsStreamSettings.Cert = class extends XrayCommonClass {
  461. constructor(useFile=true, certificateFile='', keyFile='', certificate='', key='') {
  462. super();
  463. this.useFile = useFile;
  464. this.certFile = certificateFile;
  465. this.keyFile = keyFile;
  466. this.cert = certificate instanceof Array ? certificate.join('\n') : certificate;
  467. this.key = key instanceof Array ? key.join('\n') : key;
  468. }
  469. static fromJson(json={}) {
  470. if ('certificateFile' in json && 'keyFile' in json) {
  471. return new TlsStreamSettings.Cert(
  472. true,
  473. json.certificateFile,
  474. json.keyFile,
  475. );
  476. } else {
  477. return new TlsStreamSettings.Cert(
  478. false, '', '',
  479. json.certificate.join('\n'),
  480. json.key.join('\n'),
  481. );
  482. }
  483. }
  484. toJson() {
  485. if (this.useFile) {
  486. return {
  487. certificateFile: this.certFile,
  488. keyFile: this.keyFile,
  489. };
  490. } else {
  491. return {
  492. certificate: this.cert.split('\n'),
  493. key: this.key.split('\n'),
  494. };
  495. }
  496. }
  497. };
  498. class StreamSettings extends XrayCommonClass {
  499. constructor(network='tcp',
  500. security='none',
  501. tlsSettings=new TlsStreamSettings(),
  502. tcpSettings=new TcpStreamSettings(),
  503. kcpSettings=new KcpStreamSettings(),
  504. wsSettings=new WsStreamSettings(),
  505. httpSettings=new HttpStreamSettings(),
  506. quicSettings=new QuicStreamSettings(),
  507. grpcSettings=new GrpcStreamSettings(),
  508. ) {
  509. super();
  510. this.network = network;
  511. this.security = security;
  512. this.tls = tlsSettings;
  513. this.tcp = tcpSettings;
  514. this.kcp = kcpSettings;
  515. this.ws = wsSettings;
  516. this.http = httpSettings;
  517. this.quic = quicSettings;
  518. this.grpc = grpcSettings;
  519. }
  520. get isTls() {
  521. return this.security === 'tls';
  522. }
  523. set isTls(isTls) {
  524. if (isTls) {
  525. this.security = 'tls';
  526. } else {
  527. this.security = 'none';
  528. }
  529. }
  530. get isXTls() {
  531. return this.security === "xtls";
  532. }
  533. set isXTls(isXTls) {
  534. if (isXTls) {
  535. this.security = 'xtls';
  536. } else {
  537. this.security = 'none';
  538. }
  539. }
  540. static fromJson(json={}) {
  541. let tls;
  542. if (json.security === "xtls") {
  543. tls = TlsStreamSettings.fromJson(json.xtlsSettings);
  544. } else {
  545. tls = TlsStreamSettings.fromJson(json.tlsSettings);
  546. }
  547. return new StreamSettings(
  548. json.network,
  549. json.security,
  550. tls,
  551. TcpStreamSettings.fromJson(json.tcpSettings),
  552. KcpStreamSettings.fromJson(json.kcpSettings),
  553. WsStreamSettings.fromJson(json.wsSettings),
  554. HttpStreamSettings.fromJson(json.httpSettings),
  555. QuicStreamSettings.fromJson(json.quicSettings),
  556. GrpcStreamSettings.fromJson(json.grpcSettings),
  557. );
  558. }
  559. toJson() {
  560. const network = this.network;
  561. return {
  562. network: network,
  563. security: this.security,
  564. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  565. xtlsSettings: this.isXTls ? this.tls.toJson() : undefined,
  566. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  567. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  568. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  569. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  570. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  571. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  572. };
  573. }
  574. }
  575. class Sniffing extends XrayCommonClass {
  576. constructor(enabled=true, destOverride=['http', 'tls']) {
  577. super();
  578. this.enabled = enabled;
  579. this.destOverride = destOverride;
  580. }
  581. static fromJson(json={}) {
  582. let destOverride = ObjectUtil.clone(json.destOverride);
  583. if (!ObjectUtil.isEmpty(destOverride) && !ObjectUtil.isArrEmpty(destOverride)) {
  584. if (ObjectUtil.isEmpty(destOverride[0])) {
  585. destOverride = ['http', 'tls'];
  586. }
  587. }
  588. return new Sniffing(
  589. !!json.enabled,
  590. destOverride,
  591. );
  592. }
  593. }
  594. class Inbound extends XrayCommonClass {
  595. constructor(port=RandomUtil.randomIntRange(10000, 60000),
  596. listen='',
  597. protocol=Protocols.VMESS,
  598. settings=null,
  599. streamSettings=new StreamSettings(),
  600. tag='',
  601. sniffing=new Sniffing(),
  602. clientStats='',
  603. ) {
  604. super();
  605. this.port = port;
  606. this.listen = listen;
  607. this._protocol = protocol;
  608. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  609. this.stream = streamSettings;
  610. this.tag = tag;
  611. this.sniffing = sniffing;
  612. this.clientStats = clientStats;
  613. }
  614. getClientStats() {
  615. return this.clientStats;
  616. }
  617. get protocol() {
  618. return this._protocol;
  619. }
  620. set protocol(protocol) {
  621. this._protocol = protocol;
  622. this.settings = Inbound.Settings.getSettings(protocol);
  623. if (protocol === Protocols.TROJAN) {
  624. this.tls = false;
  625. }
  626. }
  627. get tls() {
  628. return this.stream.security === 'tls';
  629. }
  630. set tls(isTls) {
  631. if (isTls) {
  632. this.stream.security = 'tls';
  633. } else {
  634. this.stream.security = 'none';
  635. }
  636. }
  637. get xtls() {
  638. return this.stream.security === 'xtls';
  639. }
  640. set xtls(isXTls) {
  641. if (isXTls) {
  642. this.stream.security = 'xtls';
  643. } else {
  644. this.stream.security = 'none';
  645. }
  646. }
  647. get network() {
  648. return this.stream.network;
  649. }
  650. set network(network) {
  651. this.stream.network = network;
  652. }
  653. get isTcp() {
  654. return this.network === "tcp";
  655. }
  656. get isWs() {
  657. return this.network === "ws";
  658. }
  659. get isKcp() {
  660. return this.network === "kcp";
  661. }
  662. get isQuic() {
  663. return this.network === "quic"
  664. }
  665. get isGrpc() {
  666. return this.network === "grpc";
  667. }
  668. get isH2() {
  669. return this.network === "http";
  670. }
  671. isInboundEmpty() {
  672. if (this.protocol == Protocols.VMESS && this.settings.vmesses.length == 0) {
  673. return true;
  674. } else if (this.protocol == Protocols.VLESS && this.settings.vlesses.length == 0) {
  675. return true;
  676. } else if (this.protocol == Protocols.TROJAN && this.settings.trojans.length == 0) {
  677. return true;
  678. } else {
  679. return false;
  680. }
  681. }
  682. // VMess & VLess
  683. get uuid() {
  684. switch (this.protocol) {
  685. case Protocols.VMESS:
  686. return this.settings.vmesses[0].id;
  687. case Protocols.VLESS:
  688. return this.settings.vlesses[0].id;
  689. default:
  690. return "";
  691. }
  692. }
  693. // VLess & Trojan
  694. get flow() {
  695. switch (this.protocol) {
  696. case Protocols.VLESS:
  697. return this.settings.vlesses[0].flow;
  698. case Protocols.TROJAN:
  699. return this.settings.trojans[0].flow;
  700. default:
  701. return "";
  702. }
  703. }
  704. // VMess
  705. get alterId() {
  706. switch (this.protocol) {
  707. case Protocols.VMESS:
  708. return this.settings.vmesses[0].alterId;
  709. default:
  710. return "";
  711. }
  712. }
  713. // Socks & HTTP
  714. get username() {
  715. switch (this.protocol) {
  716. case Protocols.SOCKS:
  717. case Protocols.HTTP:
  718. return this.settings.accounts[0].user;
  719. default:
  720. return "";
  721. }
  722. }
  723. // Trojan & Shadowsocks & Socks & HTTP
  724. get password() {
  725. switch (this.protocol) {
  726. case Protocols.TROJAN:
  727. return this.settings.trojans[0].password;
  728. case Protocols.SHADOWSOCKS:
  729. return this.settings.password;
  730. case Protocols.SOCKS:
  731. case Protocols.HTTP:
  732. return this.settings.accounts[0].pass;
  733. default:
  734. return "";
  735. }
  736. }
  737. // Shadowsocks
  738. get method() {
  739. switch (this.protocol) {
  740. case Protocols.SHADOWSOCKS:
  741. return this.settings.method;
  742. default:
  743. return "";
  744. }
  745. }
  746. get serverName() {
  747. if (this.stream.isTls || this.stream.isXTls) {
  748. return this.stream.tls.server;
  749. }
  750. return "";
  751. }
  752. get host() {
  753. if (this.isTcp) {
  754. return this.stream.tcp.request.getHeader("Host");
  755. } else if (this.isWs) {
  756. return this.stream.ws.getHeader("Host");
  757. } else if (this.isH2) {
  758. return this.stream.http.host[0];
  759. }
  760. return null;
  761. }
  762. get path() {
  763. if (this.isTcp) {
  764. return this.stream.tcp.request.path[0];
  765. } else if (this.isWs) {
  766. return this.stream.ws.path;
  767. } else if (this.isH2) {
  768. return this.stream.http.path[0];
  769. }
  770. return null;
  771. }
  772. get quicSecurity() {
  773. return this.stream.quic.security;
  774. }
  775. get quicKey() {
  776. return this.stream.quic.key;
  777. }
  778. get quicType() {
  779. return this.stream.quic.type;
  780. }
  781. get kcpType() {
  782. return this.stream.kcp.type;
  783. }
  784. get kcpSeed() {
  785. return this.stream.kcp.seed;
  786. }
  787. get serviceName() {
  788. return this.stream.grpc.serviceName;
  789. }
  790. isExpiry(index) {
  791. switch (this.protocol) {
  792. case Protocols.VMESS:
  793. if(this.settings.vmesses[index]._expiryTime != null)
  794. return this.settings.vmesses[index]._expiryTime < new Date().getTime();
  795. return false
  796. case Protocols.VLESS:
  797. if(this.settings.vlesses[index]._expiryTime != null)
  798. return this.settings.vlesses[index]._expiryTime < new Date().getTime();
  799. return false
  800. case Protocols.TROJAN:
  801. if(this.settings.trojans[index]._expiryTime != null)
  802. return this.settings.trojans[index]._expiryTime < new Date().getTime();
  803. return false
  804. default:
  805. return false;
  806. }
  807. }
  808. canEnableTls() {
  809. switch (this.protocol) {
  810. case Protocols.VMESS:
  811. case Protocols.VLESS:
  812. case Protocols.TROJAN:
  813. case Protocols.SHADOWSOCKS:
  814. break;
  815. default:
  816. return false;
  817. }
  818. switch (this.network) {
  819. case "tcp":
  820. case "ws":
  821. case "http":
  822. case "quic":
  823. case "grpc":
  824. return true;
  825. default:
  826. return false;
  827. }
  828. }
  829. //this is used for xtls-rprx-vison
  830. canEnableTlsFlow() {
  831. if ((this.stream.security === 'tls') && (this.network === "tcp")) {
  832. switch (this.protocol) {
  833. case Protocols.VLESS:
  834. return true;
  835. default:
  836. return false;
  837. }
  838. }
  839. return false;
  840. }
  841. canSetTls() {
  842. return this.canEnableTls();
  843. }
  844. canEnableXTls() {
  845. switch (this.protocol) {
  846. case Protocols.VLESS:
  847. case Protocols.TROJAN:
  848. break;
  849. default:
  850. return false;
  851. }
  852. return this.network === "tcp";
  853. }
  854. canEnableStream() {
  855. switch (this.protocol) {
  856. case Protocols.VMESS:
  857. case Protocols.VLESS:
  858. case Protocols.TROJAN:
  859. case Protocols.SHADOWSOCKS:
  860. return true;
  861. default:
  862. return false;
  863. }
  864. }
  865. canSniffing() {
  866. switch (this.protocol) {
  867. case Protocols.VMESS:
  868. case Protocols.VLESS:
  869. case Protocols.TROJAN:
  870. case Protocols.SHADOWSOCKS:
  871. return true;
  872. default:
  873. return false;
  874. }
  875. }
  876. reset() {
  877. this.port = RandomUtil.randomIntRange(10000, 60000);
  878. this.listen = '';
  879. this.protocol = Protocols.VMESS;
  880. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  881. this.stream = new StreamSettings();
  882. this.tag = '';
  883. this.sniffing = new Sniffing();
  884. }
  885. genVmessLink(address='', remark='', clientIndex=0) {
  886. if (this.protocol !== Protocols.VMESS) {
  887. return '';
  888. }
  889. let network = this.stream.network;
  890. let type = 'none';
  891. let host = '';
  892. let path = '';
  893. if (network === 'tcp') {
  894. let tcp = this.stream.tcp;
  895. type = tcp.type;
  896. if (type === 'http') {
  897. let request = tcp.request;
  898. path = request.path.join(',');
  899. let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  900. if (index >= 0) {
  901. host = request.headers[index].value;
  902. }
  903. }
  904. } else if (network === 'kcp') {
  905. let kcp = this.stream.kcp;
  906. type = kcp.type;
  907. path = kcp.seed;
  908. } else if (network === 'ws') {
  909. let ws = this.stream.ws;
  910. path = ws.path;
  911. let index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  912. if (index >= 0) {
  913. host = ws.headers[index].value;
  914. }
  915. } else if (network === 'http') {
  916. network = 'h2';
  917. path = this.stream.http.path;
  918. host = this.stream.http.host.join(',');
  919. } else if (network === 'quic') {
  920. type = this.stream.quic.type;
  921. host = this.stream.quic.security;
  922. path = this.stream.quic.key;
  923. } else if (network === 'grpc') {
  924. path = this.stream.grpc.serviceName;
  925. }
  926. if (this.stream.security === 'tls') {
  927. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  928. address = this.stream.tls.server;
  929. }
  930. }
  931. let obj = {
  932. v: '2',
  933. ps: remark,
  934. add: address,
  935. port: this.port,
  936. id: this.settings.vmesses[clientIndex].id,
  937. aid: this.settings.vmesses[clientIndex].alterId,
  938. net: network,
  939. type: type,
  940. host: host,
  941. path: path,
  942. tls: this.stream.security,
  943. };
  944. return 'vmess://' + base64(JSON.stringify(obj, null, 2));
  945. }
  946. genVLESSLink(address = '', remark='', clientIndex=0) {
  947. const settings = this.settings;
  948. const uuid = settings.vlesses[clientIndex].id;
  949. const port = this.port;
  950. const type = this.stream.network;
  951. const params = new Map();
  952. params.set("type", this.stream.network);
  953. if (this.xtls) {
  954. params.set("security", "xtls");
  955. } else {
  956. params.set("security", this.stream.security);
  957. }
  958. switch (type) {
  959. case "tcp":
  960. const tcp = this.stream.tcp;
  961. if (tcp.type === 'http') {
  962. const request = tcp.request;
  963. params.set("path", request.path.join(','));
  964. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  965. if (index >= 0) {
  966. const host = request.headers[index].value;
  967. params.set("host", host);
  968. }
  969. params.set("headerType", 'http');
  970. }
  971. break;
  972. case "kcp":
  973. const kcp = this.stream.kcp;
  974. params.set("headerType", kcp.type);
  975. params.set("seed", kcp.seed);
  976. break;
  977. case "ws":
  978. const ws = this.stream.ws;
  979. params.set("path", ws.path);
  980. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  981. if (index >= 0) {
  982. const host = ws.headers[index].value;
  983. params.set("host", host);
  984. }
  985. break;
  986. case "http":
  987. const http = this.stream.http;
  988. params.set("path", http.path);
  989. params.set("host", http.host);
  990. break;
  991. case "quic":
  992. const quic = this.stream.quic;
  993. params.set("quicSecurity", quic.security);
  994. params.set("key", quic.key);
  995. params.set("headerType", quic.type);
  996. break;
  997. case "grpc":
  998. const grpc = this.stream.grpc;
  999. params.set("serviceName", grpc.serviceName);
  1000. break;
  1001. }
  1002. if (this.stream.security === 'tls') {
  1003. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1004. address = this.stream.tls.server;
  1005. params.set("sni", address);
  1006. }
  1007. if (this.settings.vlesses[clientIndex].flow === "xtls-rprx-vision") {
  1008. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1009. }
  1010. params.set("fp", this.settings.vlesses[clientIndex].fingerprint);
  1011. }
  1012. if (this.xtls) {
  1013. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1014. }
  1015. const link = `vless://${uuid}@${address}:${port}`;
  1016. const url = new URL(link);
  1017. for (const [key, value] of params) {
  1018. url.searchParams.set(key, value)
  1019. }
  1020. url.hash = encodeURIComponent(remark);
  1021. return url.toString();
  1022. }
  1023. genSSLink(address = '', remark = '',clientIndex) {
  1024. let settings = this.settings;
  1025. const server = this.stream.tls.server;
  1026. if (!ObjectUtil.isEmpty(server)) {
  1027. address = server;
  1028. }
  1029. if (settings.method == SSMethods.BLAKE3_AES_128_GCM || settings.method == SSMethods.BLAKE3_AES_256_GCM || settings.method == SSMethods.BLAKE3_CHACHA20_POLY1305) {
  1030. return `ss://${settings.method}:${settings.password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1031. } else {
  1032. return 'ss://' + safeBase64(settings.method + ':' + settings.password + '@' + address + ':' + this.port)
  1033. + '#' + encodeURIComponent(remark);
  1034. }
  1035. }
  1036. genTrojanLink(address = '', remark = '', clientIndex = 0) {
  1037. let settings = this.settings;
  1038. const port = this.port;
  1039. const type = this.stream.network;
  1040. const params = new Map();
  1041. params.set("type", this.stream.network);
  1042. if (this.xtls) {
  1043. params.set("security", "xtls");
  1044. } else {
  1045. params.set("security", this.stream.security);
  1046. }
  1047. switch (type) {
  1048. case "tcp":
  1049. const tcp = this.stream.tcp;
  1050. if (tcp.type === 'http') {
  1051. const request = tcp.request;
  1052. params.set("path", request.path.join(','));
  1053. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1054. if (index >= 0) {
  1055. const host = request.headers[index].value;
  1056. params.set("host", host);
  1057. }
  1058. params.set("headerType", 'http');
  1059. }
  1060. break;
  1061. case "kcp":
  1062. const kcp = this.stream.kcp;
  1063. params.set("headerType", kcp.type);
  1064. params.set("seed", kcp.seed);
  1065. break;
  1066. case "ws":
  1067. const ws = this.stream.ws;
  1068. params.set("path", ws.path);
  1069. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1070. if (index >= 0) {
  1071. const host = ws.headers[index].value;
  1072. params.set("host", host);
  1073. }
  1074. break;
  1075. case "http":
  1076. const http = this.stream.http;
  1077. params.set("path", http.path);
  1078. params.set("host", http.host);
  1079. break;
  1080. case "quic":
  1081. const quic = this.stream.quic;
  1082. params.set("quicSecurity", quic.security);
  1083. params.set("key", quic.key);
  1084. params.set("headerType", quic.type);
  1085. break;
  1086. case "grpc":
  1087. const grpc = this.stream.grpc;
  1088. params.set("serviceName", grpc.serviceName);
  1089. break;
  1090. }
  1091. if (this.stream.security === 'tls') {
  1092. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1093. address = this.stream.tls.server;
  1094. params.set("sni", address);
  1095. }
  1096. params.set("flow", this.settings.trojans[clientIndex].flow);
  1097. }
  1098. if (this.xtls) {
  1099. params.set("flow", this.settings.trojans[clientIndex].flow);
  1100. }
  1101. const link = `trojan://${settings.trojans[clientIndex].password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1102. const url = new URL(link);
  1103. for (const [key, value] of params) {
  1104. url.searchParams.set(key, value)
  1105. }
  1106. url.hash = encodeURIComponent(remark);
  1107. return url.toString();
  1108. }
  1109. genLink(address='', remark='', clientIndex=0) {
  1110. switch (this.protocol) {
  1111. case Protocols.VMESS:
  1112. if (this.settings.vmesses[clientIndex].email != ""){
  1113. remark += '-' + this.settings.vmesses[clientIndex].email
  1114. }
  1115. return this.genVmessLink(address, remark, clientIndex);
  1116. case Protocols.VLESS:
  1117. if (this.settings.vlesses[clientIndex].email != ""){
  1118. remark += '-' + this.settings.vlesses[clientIndex].email
  1119. }
  1120. return this.genVLESSLink(address, remark, clientIndex);
  1121. case Protocols.SHADOWSOCKS: return this.genSSLink(address, remark);
  1122. case Protocols.TROJAN:
  1123. if (this.settings.trojans[clientIndex].email != ""){
  1124. remark += '-' + this.settings.trojans[clientIndex].email
  1125. }
  1126. return this.genTrojanLink(address, remark, clientIndex);
  1127. default: return '';
  1128. }
  1129. }
  1130. genInboundLinks(address = '', remark = '') {
  1131. let link = '';
  1132. JSON.parse(this.settings)
  1133. switch (this.protocol) {
  1134. case Protocols.VMESS:
  1135. case Protocols.VLESS:
  1136. case Protocols.TROJAN:
  1137. JSON.parse(this.settings).clients.forEach((client,index) => {
  1138. link += this.genLink(address, remark, index) + '\r\n';
  1139. });
  1140. return link;
  1141. case Protocols.SHADOWSOCKS:
  1142. return (this.genSSLink(address, remark) + '\r\n');
  1143. default: return '';
  1144. }
  1145. }
  1146. static fromJson(json={}) {
  1147. return new Inbound(
  1148. json.port,
  1149. json.listen,
  1150. json.protocol,
  1151. Inbound.Settings.fromJson(json.protocol, json.settings),
  1152. StreamSettings.fromJson(json.streamSettings),
  1153. json.tag,
  1154. Sniffing.fromJson(json.sniffing),
  1155. json.clientStats
  1156. )
  1157. }
  1158. toJson() {
  1159. let streamSettings;
  1160. if (this.canEnableStream() || this.protocol === Protocols.TROJAN) {
  1161. streamSettings = this.stream.toJson();
  1162. }
  1163. return {
  1164. port: this.port,
  1165. listen: this.listen,
  1166. protocol: this.protocol,
  1167. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1168. streamSettings: streamSettings,
  1169. tag: this.tag,
  1170. sniffing: this.sniffing.toJson(),
  1171. clientStats: this.clientStats
  1172. };
  1173. }
  1174. }
  1175. Inbound.Settings = class extends XrayCommonClass {
  1176. constructor(protocol) {
  1177. super();
  1178. this.protocol = protocol;
  1179. }
  1180. static getSettings(protocol) {
  1181. switch (protocol) {
  1182. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1183. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1184. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1185. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1186. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1187. case Protocols.MTPROTO: return new Inbound.MtprotoSettings(protocol);
  1188. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1189. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1190. default: return null;
  1191. }
  1192. }
  1193. static fromJson(protocol, json) {
  1194. switch (protocol) {
  1195. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1196. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1197. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1198. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1199. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1200. case Protocols.MTPROTO: return Inbound.MtprotoSettings.fromJson(json);
  1201. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1202. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1203. default: return null;
  1204. }
  1205. }
  1206. toJson() {
  1207. return {};
  1208. }
  1209. };
  1210. Inbound.VmessSettings = class extends Inbound.Settings {
  1211. constructor(protocol,
  1212. vmesses=[new Inbound.VmessSettings.Vmess()],
  1213. disableInsecureEncryption=false) {
  1214. super(protocol);
  1215. this.vmesses = vmesses;
  1216. this.disableInsecure = disableInsecureEncryption;
  1217. }
  1218. indexOfVmessById(id) {
  1219. return this.vmesses.findIndex(vmess => vmess.id === id);
  1220. }
  1221. addVmess(vmess) {
  1222. if (this.indexOfVmessById(vmess.id) >= 0) {
  1223. return false;
  1224. }
  1225. this.vmesses.push(vmess);
  1226. }
  1227. delVmess(vmess) {
  1228. const i = this.indexOfVmessById(vmess.id);
  1229. if (i >= 0) {
  1230. this.vmesses.splice(i, 1);
  1231. }
  1232. }
  1233. static fromJson(json={}) {
  1234. return new Inbound.VmessSettings(
  1235. Protocols.VMESS,
  1236. json.clients.map(client => Inbound.VmessSettings.Vmess.fromJson(client)),
  1237. ObjectUtil.isEmpty(json.disableInsecureEncryption) ? false : json.disableInsecureEncryption,
  1238. );
  1239. }
  1240. toJson() {
  1241. return {
  1242. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1243. disableInsecureEncryption: this.disableInsecure,
  1244. };
  1245. }
  1246. };
  1247. Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
  1248. constructor(id=RandomUtil.randomUUID(), alterId=0, email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime='') {
  1249. super();
  1250. this.id = id;
  1251. this.alterId = alterId;
  1252. this.email = email;
  1253. this.limitIp = limitIp;
  1254. this.totalGB = totalGB;
  1255. this.expiryTime = expiryTime;
  1256. }
  1257. static fromJson(json={}) {
  1258. return new Inbound.VmessSettings.Vmess(
  1259. json.id,
  1260. json.alterId,
  1261. json.email,
  1262. json.limitIp,
  1263. json.totalGB,
  1264. json.expiryTime,
  1265. );
  1266. }
  1267. get _expiryTime() {
  1268. if (this.expiryTime === 0 || this.expiryTime === "") {
  1269. return null;
  1270. }
  1271. return moment(this.expiryTime);
  1272. }
  1273. set _expiryTime(t) {
  1274. if (t == null || t === "") {
  1275. this.expiryTime = 0;
  1276. } else {
  1277. this.expiryTime = t.valueOf();
  1278. }
  1279. }
  1280. get _totalGB() {
  1281. return toFixed(this.totalGB / ONE_GB, 2);
  1282. }
  1283. set _totalGB(gb) {
  1284. this.totalGB = toFixed(gb * ONE_GB, 0);
  1285. }
  1286. };
  1287. Inbound.VLESSSettings = class extends Inbound.Settings {
  1288. constructor(protocol,
  1289. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1290. decryption='none',
  1291. fallbacks=[],) {
  1292. super(protocol);
  1293. this.vlesses = vlesses;
  1294. this.decryption = decryption;
  1295. this.fallbacks = fallbacks;
  1296. }
  1297. addFallback() {
  1298. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1299. }
  1300. delFallback(index) {
  1301. this.fallbacks.splice(index, 1);
  1302. }
  1303. static fromJson(json={}) {
  1304. return new Inbound.VLESSSettings(
  1305. Protocols.VLESS,
  1306. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1307. json.decryption,
  1308. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1309. );
  1310. }
  1311. toJson() {
  1312. return {
  1313. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1314. decryption: this.decryption,
  1315. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1316. };
  1317. }
  1318. };
  1319. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1320. constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, fingerprint = UTLS_FINGERPRINT.UTLS_CHROME, expiryTime='') {
  1321. super();
  1322. this.id = id;
  1323. this.flow = flow;
  1324. this.email = email;
  1325. this.limitIp = limitIp;
  1326. this.totalGB = totalGB;
  1327. this.fingerprint = fingerprint;
  1328. this.expiryTime = expiryTime;
  1329. }
  1330. static fromJson(json={}) {
  1331. return new Inbound.VLESSSettings.VLESS(
  1332. json.id,
  1333. json.flow,
  1334. json.email,
  1335. json.limitIp,
  1336. json.totalGB,
  1337. json.fingerprint,
  1338. json.expiryTime,
  1339. );
  1340. }
  1341. get _expiryTime() {
  1342. if (this.expiryTime === 0 || this.expiryTime === "") {
  1343. return null;
  1344. }
  1345. return moment(this.expiryTime);
  1346. }
  1347. set _expiryTime(t) {
  1348. if (t == null || t === "") {
  1349. this.expiryTime = 0;
  1350. } else {
  1351. this.expiryTime = t.valueOf();
  1352. }
  1353. }
  1354. get _totalGB() {
  1355. return toFixed(this.totalGB / ONE_GB, 2);
  1356. }
  1357. set _totalGB(gb) {
  1358. this.totalGB = toFixed(gb * ONE_GB, 0);
  1359. }
  1360. };
  1361. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1362. constructor(name="", alpn='', path='', dest='', xver=0) {
  1363. super();
  1364. this.name = name;
  1365. this.alpn = alpn;
  1366. this.path = path;
  1367. this.dest = dest;
  1368. this.xver = xver;
  1369. }
  1370. toJson() {
  1371. let xver = this.xver;
  1372. if (!Number.isInteger(xver)) {
  1373. xver = 0;
  1374. }
  1375. return {
  1376. name: this.name,
  1377. alpn: this.alpn,
  1378. path: this.path,
  1379. dest: this.dest,
  1380. xver: xver,
  1381. }
  1382. }
  1383. static fromJson(json=[]) {
  1384. const fallbacks = [];
  1385. for (let fallback of json) {
  1386. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1387. fallback.name,
  1388. fallback.alpn,
  1389. fallback.path,
  1390. fallback.dest,
  1391. fallback.xver,
  1392. ))
  1393. }
  1394. return fallbacks;
  1395. }
  1396. };
  1397. Inbound.TrojanSettings = class extends Inbound.Settings {
  1398. constructor(protocol,
  1399. trojans=[new Inbound.TrojanSettings.Trojan()],
  1400. fallbacks=[],) {
  1401. super(protocol);
  1402. this.trojans = trojans;
  1403. this.fallbacks = fallbacks;
  1404. }
  1405. addTrojanFallback() {
  1406. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1407. }
  1408. delTrojanFallback(index) {
  1409. this.fallbacks.splice(index, 1);
  1410. }
  1411. static fromJson(json={}) {
  1412. return new Inbound.TrojanSettings(
  1413. Protocols.TROJAN,
  1414. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1415. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1416. }
  1417. toJson() {
  1418. return {
  1419. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1420. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
  1421. };
  1422. }
  1423. };
  1424. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1425. constructor(password=RandomUtil.randomSeq(10), flow ='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime='') {
  1426. super();
  1427. this.password = password;
  1428. this.flow = flow;
  1429. this.email = email;
  1430. this.limitIp = limitIp;
  1431. this.totalGB = totalGB;
  1432. this.expiryTime = expiryTime;
  1433. }
  1434. toJson() {
  1435. return {
  1436. password: this.password,
  1437. flow: this.flow,
  1438. email: this.email,
  1439. limitIp: this.limitIp,
  1440. totalGB: this.totalGB,
  1441. expiryTime: this.expiryTime,
  1442. };
  1443. }
  1444. static fromJson(json={}) {
  1445. return new Inbound.TrojanSettings.Trojan(
  1446. json.password,
  1447. json.flow,
  1448. json.email,
  1449. json.limitIp,
  1450. json.totalGB,
  1451. json.expiryTime,
  1452. );
  1453. }
  1454. get _expiryTime() {
  1455. if (this.expiryTime === 0 || this.expiryTime === "") {
  1456. return null;
  1457. }
  1458. return moment(this.expiryTime);
  1459. }
  1460. set _expiryTime(t) {
  1461. if (t == null || t === "") {
  1462. this.expiryTime = 0;
  1463. } else {
  1464. this.expiryTime = t.valueOf();
  1465. }
  1466. }
  1467. get _totalGB() {
  1468. return toFixed(this.totalGB / ONE_GB, 2);
  1469. }
  1470. set _totalGB(gb) {
  1471. this.totalGB = toFixed(gb * ONE_GB, 0);
  1472. }
  1473. };
  1474. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1475. constructor(name="", alpn='', path='', dest='', xver=0) {
  1476. super();
  1477. this.name = name;
  1478. this.alpn = alpn;
  1479. this.path = path;
  1480. this.dest = dest;
  1481. this.xver = xver;
  1482. }
  1483. toJson() {
  1484. let xver = this.xver;
  1485. if (!Number.isInteger(xver)) {
  1486. xver = 0;
  1487. }
  1488. return {
  1489. name: this.name,
  1490. alpn: this.alpn,
  1491. path: this.path,
  1492. dest: this.dest,
  1493. xver: xver,
  1494. }
  1495. }
  1496. static fromJson(json=[]) {
  1497. const fallbacks = [];
  1498. for (let fallback of json) {
  1499. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1500. fallback.name,
  1501. fallback.alpn,
  1502. fallback.path,
  1503. fallback.dest,
  1504. fallback.xver,
  1505. ))
  1506. }
  1507. return fallbacks;
  1508. }
  1509. };
  1510. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1511. constructor(protocol,
  1512. method = SSMethods.BLAKE3_AES_256_GCM,
  1513. password = RandomUtil.randomSeq(44),
  1514. network = 'tcp,udp'
  1515. ) {
  1516. super(protocol);
  1517. this.method = method;
  1518. this.password = password;
  1519. this.network = network;
  1520. }
  1521. static fromJson(json = {}) {
  1522. return new Inbound.ShadowsocksSettings(
  1523. Protocols.SHADOWSOCKS,
  1524. json.method,
  1525. json.password,
  1526. json.network,
  1527. );
  1528. }
  1529. toJson() {
  1530. return {
  1531. method: this.method,
  1532. password: this.password,
  1533. network: this.network,
  1534. };
  1535. }
  1536. };
  1537. Inbound.DokodemoSettings = class extends Inbound.Settings {
  1538. constructor(protocol, address, port, network='tcp,udp') {
  1539. super(protocol);
  1540. this.address = address;
  1541. this.port = port;
  1542. this.network = network;
  1543. }
  1544. static fromJson(json={}) {
  1545. return new Inbound.DokodemoSettings(
  1546. Protocols.DOKODEMO,
  1547. json.address,
  1548. json.port,
  1549. json.network,
  1550. );
  1551. }
  1552. toJson() {
  1553. return {
  1554. address: this.address,
  1555. port: this.port,
  1556. network: this.network,
  1557. };
  1558. }
  1559. };
  1560. Inbound.MtprotoSettings = class extends Inbound.Settings {
  1561. constructor(protocol, users=[new Inbound.MtprotoSettings.MtUser()]) {
  1562. super(protocol);
  1563. this.users = users;
  1564. }
  1565. static fromJson(json={}) {
  1566. return new Inbound.MtprotoSettings(
  1567. Protocols.MTPROTO,
  1568. json.users.map(user => Inbound.MtprotoSettings.MtUser.fromJson(user)),
  1569. );
  1570. }
  1571. toJson() {
  1572. return {
  1573. users: XrayCommonClass.toJsonArray(this.users),
  1574. };
  1575. }
  1576. };
  1577. Inbound.MtprotoSettings.MtUser = class extends XrayCommonClass {
  1578. constructor(secret=RandomUtil.randomMTSecret()) {
  1579. super();
  1580. this.secret = secret;
  1581. }
  1582. static fromJson(json={}) {
  1583. return new Inbound.MtprotoSettings.MtUser(json.secret);
  1584. }
  1585. };
  1586. Inbound.SocksSettings = class extends Inbound.Settings {
  1587. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  1588. super(protocol);
  1589. this.auth = auth;
  1590. this.accounts = accounts;
  1591. this.udp = udp;
  1592. this.ip = ip;
  1593. }
  1594. addAccount(account) {
  1595. this.accounts.push(account);
  1596. }
  1597. delAccount(index) {
  1598. this.accounts.splice(index, 1);
  1599. }
  1600. static fromJson(json={}) {
  1601. let accounts;
  1602. if (json.auth === 'password') {
  1603. accounts = json.accounts.map(
  1604. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  1605. )
  1606. }
  1607. return new Inbound.SocksSettings(
  1608. Protocols.SOCKS,
  1609. json.auth,
  1610. accounts,
  1611. json.udp,
  1612. json.ip,
  1613. );
  1614. }
  1615. toJson() {
  1616. return {
  1617. auth: this.auth,
  1618. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  1619. udp: this.udp,
  1620. ip: this.ip,
  1621. };
  1622. }
  1623. };
  1624. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  1625. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1626. super();
  1627. this.user = user;
  1628. this.pass = pass;
  1629. }
  1630. static fromJson(json={}) {
  1631. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  1632. }
  1633. };
  1634. Inbound.HttpSettings = class extends Inbound.Settings {
  1635. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  1636. super(protocol);
  1637. this.accounts = accounts;
  1638. }
  1639. addAccount(account) {
  1640. this.accounts.push(account);
  1641. }
  1642. delAccount(index) {
  1643. this.accounts.splice(index, 1);
  1644. }
  1645. static fromJson(json={}) {
  1646. return new Inbound.HttpSettings(
  1647. Protocols.HTTP,
  1648. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  1649. );
  1650. }
  1651. toJson() {
  1652. return {
  1653. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  1654. };
  1655. }
  1656. };
  1657. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  1658. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1659. super();
  1660. this.user = user;
  1661. this.pass = pass;
  1662. }
  1663. static fromJson(json={}) {
  1664. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  1665. }
  1666. };