xray.js 54 KB

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