xray.js 53 KB

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