xray.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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. isInboundEmpty() {
  708. if (this.protocol == Protocols.VMESS && this.settings.vmesses.length == 0) {
  709. return true;
  710. } else if (this.protocol == Protocols.VLESS && this.settings.vlesses.length == 0) {
  711. return true;
  712. } else if (this.protocol == Protocols.TROJAN && this.settings.trojans.length == 0) {
  713. return true;
  714. } else {
  715. return false;
  716. }
  717. }
  718. // VMess & VLess
  719. get uuid() {
  720. switch (this.protocol) {
  721. case Protocols.VMESS:
  722. return this.settings.vmesses[0].id;
  723. case Protocols.VLESS:
  724. return this.settings.vlesses[0].id;
  725. default:
  726. return "";
  727. }
  728. }
  729. // VLess & Trojan
  730. get flow() {
  731. switch (this.protocol) {
  732. case Protocols.VLESS:
  733. return this.settings.vlesses[0].flow;
  734. case Protocols.TROJAN:
  735. return this.settings.trojans[0].flow;
  736. default:
  737. return "";
  738. }
  739. }
  740. // VMess
  741. get alterId() {
  742. switch (this.protocol) {
  743. case Protocols.VMESS:
  744. return this.settings.vmesses[0].alterId;
  745. default:
  746. return "";
  747. }
  748. }
  749. // Socks & HTTP
  750. get username() {
  751. switch (this.protocol) {
  752. case Protocols.SOCKS:
  753. case Protocols.HTTP:
  754. return this.settings.accounts[0].user;
  755. default:
  756. return "";
  757. }
  758. }
  759. // Trojan & Shadowsocks & Socks & HTTP
  760. get password() {
  761. switch (this.protocol) {
  762. case Protocols.TROJAN:
  763. return this.settings.trojans[0].password;
  764. case Protocols.SHADOWSOCKS:
  765. return this.settings.password;
  766. case Protocols.SOCKS:
  767. case Protocols.HTTP:
  768. return this.settings.accounts[0].pass;
  769. default:
  770. return "";
  771. }
  772. }
  773. // Shadowsocks
  774. get method() {
  775. switch (this.protocol) {
  776. case Protocols.SHADOWSOCKS:
  777. return this.settings.method;
  778. default:
  779. return "";
  780. }
  781. }
  782. get serverName() {
  783. if (this.stream.isTls || this.stream.isXTLS) {
  784. return this.stream.tls.server;
  785. }
  786. return "";
  787. }
  788. get host() {
  789. if (this.isTcp) {
  790. return this.stream.tcp.request.getHeader("Host");
  791. } else if (this.isWs) {
  792. return this.stream.ws.getHeader("Host");
  793. } else if (this.isH2) {
  794. return this.stream.http.host[0];
  795. }
  796. return null;
  797. }
  798. get path() {
  799. if (this.isTcp) {
  800. return this.stream.tcp.request.path[0];
  801. } else if (this.isWs) {
  802. return this.stream.ws.path;
  803. } else if (this.isH2) {
  804. return this.stream.http.path[0];
  805. }
  806. return null;
  807. }
  808. get quicSecurity() {
  809. return this.stream.quic.security;
  810. }
  811. get quicKey() {
  812. return this.stream.quic.key;
  813. }
  814. get quicType() {
  815. return this.stream.quic.type;
  816. }
  817. get kcpType() {
  818. return this.stream.kcp.type;
  819. }
  820. get kcpSeed() {
  821. return this.stream.kcp.seed;
  822. }
  823. get serviceName() {
  824. return this.stream.grpc.serviceName;
  825. }
  826. isExpiry(index) {
  827. switch (this.protocol) {
  828. case Protocols.VMESS:
  829. if(this.settings.vmesses[index]._expiryTime != null)
  830. return this.settings.vmesses[index]._expiryTime < new Date().getTime();
  831. return false
  832. case Protocols.VLESS:
  833. if(this.settings.vlesses[index]._expiryTime != null)
  834. return this.settings.vlesses[index]._expiryTime < new Date().getTime();
  835. return false
  836. case Protocols.TROJAN:
  837. if(this.settings.trojans[index]._expiryTime != null)
  838. return this.settings.trojans[index]._expiryTime < new Date().getTime();
  839. return false
  840. default:
  841. return false;
  842. }
  843. }
  844. canEnableTls() {
  845. switch (this.protocol) {
  846. case Protocols.VMESS:
  847. case Protocols.VLESS:
  848. case Protocols.TROJAN:
  849. case Protocols.SHADOWSOCKS:
  850. break;
  851. default:
  852. return false;
  853. }
  854. switch (this.network) {
  855. case "tcp":
  856. case "ws":
  857. case "http":
  858. case "quic":
  859. case "grpc":
  860. return true;
  861. default:
  862. return false;
  863. }
  864. }
  865. //this is used for xtls-rprx-vision
  866. canEnableTlsFlow() {
  867. if ((this.stream.security === 'tls') && (this.network === "tcp")) {
  868. switch (this.protocol) {
  869. case Protocols.VLESS:
  870. return true;
  871. default:
  872. return false;
  873. }
  874. }
  875. return false;
  876. }
  877. canSetTls() {
  878. return this.canEnableTls();
  879. }
  880. canEnableXTLS() {
  881. switch (this.protocol) {
  882. case Protocols.VLESS:
  883. case Protocols.TROJAN:
  884. break;
  885. default:
  886. return false;
  887. }
  888. return this.network === "tcp";
  889. }
  890. canEnableStream() {
  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. canSniffing() {
  902. switch (this.protocol) {
  903. case Protocols.VMESS:
  904. case Protocols.VLESS:
  905. case Protocols.TROJAN:
  906. case Protocols.SHADOWSOCKS:
  907. return true;
  908. default:
  909. return false;
  910. }
  911. }
  912. reset() {
  913. this.port = RandomUtil.randomIntRange(10000, 60000);
  914. this.listen = '';
  915. this.protocol = Protocols.VMESS;
  916. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  917. this.stream = new StreamSettings();
  918. this.tag = '';
  919. this.sniffing = new Sniffing();
  920. }
  921. genVmessLink(address='', remark='', clientIndex=0) {
  922. if (this.protocol !== Protocols.VMESS) {
  923. return '';
  924. }
  925. let network = this.stream.network;
  926. let type = 'none';
  927. let host = '';
  928. let path = '';
  929. if (network === 'tcp') {
  930. let tcp = this.stream.tcp;
  931. type = tcp.type;
  932. if (type === 'http') {
  933. let request = tcp.request;
  934. path = request.path.join(',');
  935. let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  936. if (index >= 0) {
  937. host = request.headers[index].value;
  938. }
  939. }
  940. } else if (network === 'kcp') {
  941. let kcp = this.stream.kcp;
  942. type = kcp.type;
  943. path = kcp.seed;
  944. } else if (network === 'ws') {
  945. let ws = this.stream.ws;
  946. path = ws.path;
  947. let index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  948. if (index >= 0) {
  949. host = ws.headers[index].value;
  950. }
  951. } else if (network === 'http') {
  952. network = 'h2';
  953. path = this.stream.http.path;
  954. host = this.stream.http.host.join(',');
  955. } else if (network === 'quic') {
  956. type = this.stream.quic.type;
  957. host = this.stream.quic.security;
  958. path = this.stream.quic.key;
  959. } else if (network === 'grpc') {
  960. path = this.stream.grpc.serviceName;
  961. }
  962. if (this.stream.security === 'tls') {
  963. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  964. address = this.stream.tls.server;
  965. }
  966. }
  967. let obj = {
  968. v: '2',
  969. ps: remark,
  970. add: address,
  971. port: this.port,
  972. id: this.settings.vmesses[clientIndex].id,
  973. aid: this.settings.vmesses[clientIndex].alterId,
  974. net: network,
  975. type: type,
  976. host: host,
  977. path: path,
  978. tls: this.stream.security,
  979. sni: this.stream.tls.settings[0]['serverName'],
  980. fp: this.stream.tls.settings[0]['fingerprint'],
  981. alpn: this.stream.tls.alpn[0],
  982. };
  983. return 'vmess://' + base64(JSON.stringify(obj, null, 2));
  984. }
  985. genVLESSLink(address = '', remark='', clientIndex=0) {
  986. const settings = this.settings;
  987. const uuid = settings.vlesses[clientIndex].id;
  988. const port = this.port;
  989. const type = this.stream.network;
  990. const params = new Map();
  991. params.set("type", this.stream.network);
  992. params.set("security", this.stream.security);
  993. if (this.XTLS) {
  994. params.set("security", "xtls");
  995. } else {
  996. params.set("security", this.stream.security);
  997. }
  998. switch (type) {
  999. case "tcp":
  1000. const tcp = this.stream.tcp;
  1001. if (tcp.type === 'http') {
  1002. const request = tcp.request;
  1003. params.set("path", request.path.join(','));
  1004. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1005. if (index >= 0) {
  1006. const host = request.headers[index].value;
  1007. params.set("host", host);
  1008. }
  1009. params.set("headerType", 'http');
  1010. }
  1011. break;
  1012. case "kcp":
  1013. const kcp = this.stream.kcp;
  1014. params.set("headerType", kcp.type);
  1015. params.set("seed", kcp.seed);
  1016. break;
  1017. case "ws":
  1018. const ws = this.stream.ws;
  1019. params.set("path", ws.path);
  1020. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1021. if (index >= 0) {
  1022. const host = ws.headers[index].value;
  1023. params.set("host", host);
  1024. }
  1025. break;
  1026. case "http":
  1027. const http = this.stream.http;
  1028. params.set("path", http.path);
  1029. params.set("host", http.host);
  1030. break;
  1031. case "quic":
  1032. const quic = this.stream.quic;
  1033. params.set("quicSecurity", quic.security);
  1034. params.set("key", quic.key);
  1035. params.set("headerType", quic.type);
  1036. break;
  1037. case "grpc":
  1038. const grpc = this.stream.grpc;
  1039. params.set("serviceName", grpc.serviceName);
  1040. break;
  1041. }
  1042. if (this.stream.security === 'tls') {
  1043. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1044. address = this.stream.tls.server;
  1045. params.set("fp" , this.stream.tls.settings[0]['fingerprint']);
  1046. params.set("alpn", this.stream.tls.alpn[0]);
  1047. if (this.stream.tls.settings[0]['serverName'] !== ''){
  1048. params.set("sni", this.stream.tls.settings[0]['serverName']);
  1049. }
  1050. else{
  1051. params.set("sni", address);
  1052. }
  1053. if (type === "tcp") {
  1054. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1055. }
  1056. }
  1057. }
  1058. if (this.stream.security === 'xtls') {
  1059. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1060. address = this.stream.tls.server;
  1061. if (this.stream.tls.settings[0]['serverName'] !== ''){
  1062. params.set("sni", this.stream.tls.settings[0]['serverName']);
  1063. }
  1064. else{
  1065. params.set("sni", address);
  1066. }
  1067. if (type === "tcp") {
  1068. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1069. }
  1070. }
  1071. }
  1072. const link = `vless://${uuid}@${address}:${port}`;
  1073. const url = new URL(link);
  1074. for (const [key, value] of params) {
  1075. url.searchParams.set(key, value)
  1076. }
  1077. url.hash = encodeURIComponent(remark);
  1078. return url.toString();
  1079. }
  1080. genSSLink(address = '', remark = '') {
  1081. let settings = this.settings;
  1082. const server = this.stream.tls.server;
  1083. if (!ObjectUtil.isEmpty(server)) {
  1084. address = server;
  1085. }
  1086. if (settings.method == SSMethods.BLAKE3_AES_128_GCM || settings.method == SSMethods.BLAKE3_AES_256_GCM || settings.method == SSMethods.BLAKE3_CHACHA20_POLY1305) {
  1087. return `ss://${settings.method}:${settings.password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1088. } else {
  1089. return 'ss://' + safeBase64(settings.method + ':' + settings.password + '@' + address + ':' + this.port)
  1090. + '#' + encodeURIComponent(remark);
  1091. }
  1092. }
  1093. genTrojanLink(address = '', remark = '', clientIndex = 0) {
  1094. let settings = this.settings;
  1095. const port = this.port;
  1096. const type = this.stream.network;
  1097. const params = new Map();
  1098. switch (type) {
  1099. case "tcp":
  1100. const tcp = this.stream.tcp;
  1101. if (tcp.type === 'http') {
  1102. const request = tcp.request;
  1103. params.set("path", request.path.join(','));
  1104. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1105. if (index >= 0) {
  1106. const host = request.headers[index].value;
  1107. params.set("host", host);
  1108. }
  1109. params.set("headerType", 'http');
  1110. }
  1111. break;
  1112. case "kcp":
  1113. const kcp = this.stream.kcp;
  1114. params.set("headerType", kcp.type);
  1115. params.set("seed", kcp.seed);
  1116. break;
  1117. case "ws":
  1118. const ws = this.stream.ws;
  1119. params.set("path", ws.path);
  1120. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1121. if (index >= 0) {
  1122. const host = ws.headers[index].value;
  1123. params.set("host", host);
  1124. }
  1125. break;
  1126. case "http":
  1127. const http = this.stream.http;
  1128. params.set("path", http.path);
  1129. params.set("host", http.host);
  1130. break;
  1131. case "quic":
  1132. const quic = this.stream.quic;
  1133. params.set("quicSecurity", quic.security);
  1134. params.set("key", quic.key);
  1135. params.set("headerType", quic.type);
  1136. break;
  1137. case "grpc":
  1138. const grpc = this.stream.grpc;
  1139. params.set("serviceName", grpc.serviceName);
  1140. break;
  1141. }
  1142. if (this.stream.security === 'tls') {
  1143. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1144. address = this.stream.tls.server;
  1145. params.set("fp" , this.stream.tls.settings[0]['fingerprint']);
  1146. params.set("alpn", this.stream.tls.alpn[0]);
  1147. if (this.stream.tls.settings[0]['serverName'] !== ''){
  1148. params.set("sni", this.stream.tls.settings[0]['serverName']);
  1149. }
  1150. else{
  1151. params.set("sni", address);
  1152. }
  1153. }
  1154. }
  1155. if (this.stream.security === 'xtls') {
  1156. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1157. address = this.stream.tls.server;
  1158. if (this.stream.tls.settings[0]['serverName'] !== ''){
  1159. params.set("sni", this.stream.tls.settings[0]['serverName']);
  1160. }
  1161. else{
  1162. params.set("sni", address);
  1163. }
  1164. if (type === "tcp") {
  1165. params.set("flow", this.settings.trojans[clientIndex].flow);
  1166. }
  1167. }
  1168. }
  1169. const link = `trojan://${settings.trojans[clientIndex].password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1170. const url = new URL(link);
  1171. for (const [key, value] of params) {
  1172. url.searchParams.set(key, value)
  1173. }
  1174. url.hash = encodeURIComponent(remark);
  1175. return url.toString();
  1176. }
  1177. genLink(address='', remark='', clientIndex=0) {
  1178. switch (this.protocol) {
  1179. case Protocols.VMESS:
  1180. if (this.settings.vmesses[clientIndex].email != ""){
  1181. remark += '-' + this.settings.vmesses[clientIndex].email
  1182. }
  1183. return this.genVmessLink(address, remark, clientIndex);
  1184. case Protocols.VLESS:
  1185. if (this.settings.vlesses[clientIndex].email != ""){
  1186. remark += '-' + this.settings.vlesses[clientIndex].email
  1187. }
  1188. return this.genVLESSLink(address, remark, clientIndex);
  1189. case Protocols.SHADOWSOCKS: return this.genSSLink(address, remark);
  1190. case Protocols.TROJAN:
  1191. if (this.settings.trojans[clientIndex].email != ""){
  1192. remark += '-' + this.settings.trojans[clientIndex].email
  1193. }
  1194. return this.genTrojanLink(address, remark, clientIndex);
  1195. default: return '';
  1196. }
  1197. }
  1198. genInboundLinks(address = '', remark = '') {
  1199. let link = '';
  1200. switch (this.protocol) {
  1201. case Protocols.VMESS:
  1202. case Protocols.VLESS:
  1203. case Protocols.TROJAN:
  1204. JSON.parse(this.settings).clients.forEach((_,index) => {
  1205. link += this.genLink(address, remark, index) + '\r\n';
  1206. });
  1207. return link;
  1208. case Protocols.SHADOWSOCKS:
  1209. return (this.genSSLink(address, remark) + '\r\n');
  1210. default: return '';
  1211. }
  1212. }
  1213. static fromJson(json={}) {
  1214. return new Inbound(
  1215. json.port,
  1216. json.listen,
  1217. json.protocol,
  1218. Inbound.Settings.fromJson(json.protocol, json.settings),
  1219. StreamSettings.fromJson(json.streamSettings),
  1220. json.tag,
  1221. Sniffing.fromJson(json.sniffing),
  1222. json.clientStats
  1223. )
  1224. }
  1225. toJson() {
  1226. let streamSettings;
  1227. if (this.canEnableStream() || this.protocol === Protocols.TROJAN) {
  1228. streamSettings = this.stream.toJson();
  1229. }
  1230. return {
  1231. port: this.port,
  1232. listen: this.listen,
  1233. protocol: this.protocol,
  1234. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1235. streamSettings: streamSettings,
  1236. tag: this.tag,
  1237. sniffing: this.sniffing.toJson(),
  1238. clientStats: this.clientStats
  1239. };
  1240. }
  1241. }
  1242. Inbound.Settings = class extends XrayCommonClass {
  1243. constructor(protocol) {
  1244. super();
  1245. this.protocol = protocol;
  1246. }
  1247. static getSettings(protocol) {
  1248. switch (protocol) {
  1249. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1250. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1251. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1252. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1253. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1254. case Protocols.MTPROTO: return new Inbound.MtprotoSettings(protocol);
  1255. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1256. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1257. default: return null;
  1258. }
  1259. }
  1260. static fromJson(protocol, json) {
  1261. switch (protocol) {
  1262. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1263. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1264. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1265. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1266. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1267. case Protocols.MTPROTO: return Inbound.MtprotoSettings.fromJson(json);
  1268. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1269. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1270. default: return null;
  1271. }
  1272. }
  1273. toJson() {
  1274. return {};
  1275. }
  1276. };
  1277. Inbound.VmessSettings = class extends Inbound.Settings {
  1278. constructor(protocol,
  1279. vmesses=[new Inbound.VmessSettings.Vmess()],
  1280. disableInsecureEncryption=false) {
  1281. super(protocol);
  1282. this.vmesses = vmesses;
  1283. this.disableInsecure = disableInsecureEncryption;
  1284. }
  1285. indexOfVmessById(id) {
  1286. return this.vmesses.findIndex(vmess => vmess.id === id);
  1287. }
  1288. addVmess(vmess) {
  1289. if (this.indexOfVmessById(vmess.id) >= 0) {
  1290. return false;
  1291. }
  1292. this.vmesses.push(vmess);
  1293. }
  1294. delVmess(vmess) {
  1295. const i = this.indexOfVmessById(vmess.id);
  1296. if (i >= 0) {
  1297. this.vmesses.splice(i, 1);
  1298. }
  1299. }
  1300. static fromJson(json={}) {
  1301. return new Inbound.VmessSettings(
  1302. Protocols.VMESS,
  1303. json.clients.map(client => Inbound.VmessSettings.Vmess.fromJson(client)),
  1304. ObjectUtil.isEmpty(json.disableInsecureEncryption) ? false : json.disableInsecureEncryption,
  1305. );
  1306. }
  1307. toJson() {
  1308. return {
  1309. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1310. disableInsecureEncryption: this.disableInsecure,
  1311. };
  1312. }
  1313. };
  1314. Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
  1315. constructor(id=RandomUtil.randomUUID(), alterId=0, email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime='') {
  1316. super();
  1317. this.id = id;
  1318. this.alterId = alterId;
  1319. this.email = email;
  1320. this.limitIp = limitIp;
  1321. this.totalGB = totalGB;
  1322. this.expiryTime = expiryTime;
  1323. }
  1324. static fromJson(json={}) {
  1325. return new Inbound.VmessSettings.Vmess(
  1326. json.id,
  1327. json.alterId,
  1328. json.email,
  1329. json.limitIp,
  1330. json.totalGB,
  1331. json.expiryTime,
  1332. );
  1333. }
  1334. get _expiryTime() {
  1335. if (this.expiryTime === 0 || this.expiryTime === "") {
  1336. return null;
  1337. }
  1338. return moment(this.expiryTime);
  1339. }
  1340. set _expiryTime(t) {
  1341. if (t == null || t === "") {
  1342. this.expiryTime = 0;
  1343. } else {
  1344. this.expiryTime = t.valueOf();
  1345. }
  1346. }
  1347. get _totalGB() {
  1348. return toFixed(this.totalGB / ONE_GB, 2);
  1349. }
  1350. set _totalGB(gb) {
  1351. this.totalGB = toFixed(gb * ONE_GB, 0);
  1352. }
  1353. };
  1354. Inbound.VLESSSettings = class extends Inbound.Settings {
  1355. constructor(protocol,
  1356. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1357. decryption='none',
  1358. fallbacks=[],) {
  1359. super(protocol);
  1360. this.vlesses = vlesses;
  1361. this.decryption = 'none';
  1362. this.fallbacks = fallbacks;
  1363. }
  1364. addFallback() {
  1365. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1366. }
  1367. delFallback(index) {
  1368. this.fallbacks.splice(index, 1);
  1369. }
  1370. static fromJson(json={}) {
  1371. return new Inbound.VLESSSettings(
  1372. Protocols.VLESS,
  1373. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1374. 'none',
  1375. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1376. );
  1377. }
  1378. toJson() {
  1379. return {
  1380. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1381. decryption: this.decryption,
  1382. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1383. };
  1384. }
  1385. };
  1386. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1387. constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime='') {
  1388. super();
  1389. this.id = id;
  1390. this.flow = flow;
  1391. this.email = email;
  1392. this.limitIp = limitIp;
  1393. this.totalGB = totalGB;
  1394. this.expiryTime = expiryTime;
  1395. }
  1396. static fromJson(json={}) {
  1397. return new Inbound.VLESSSettings.VLESS(
  1398. json.id,
  1399. json.flow,
  1400. json.email,
  1401. json.limitIp,
  1402. json.totalGB,
  1403. json.expiryTime,
  1404. );
  1405. }
  1406. get _expiryTime() {
  1407. if (this.expiryTime === 0 || this.expiryTime === "") {
  1408. return null;
  1409. }
  1410. return moment(this.expiryTime);
  1411. }
  1412. set _expiryTime(t) {
  1413. if (t == null || t === "") {
  1414. this.expiryTime = 0;
  1415. } else {
  1416. this.expiryTime = t.valueOf();
  1417. }
  1418. }
  1419. get _totalGB() {
  1420. return toFixed(this.totalGB / ONE_GB, 2);
  1421. }
  1422. set _totalGB(gb) {
  1423. this.totalGB = toFixed(gb * ONE_GB, 0);
  1424. }
  1425. };
  1426. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1427. constructor(name="", alpn='', path='', dest='', xver=0) {
  1428. super();
  1429. this.name = name;
  1430. this.alpn = alpn;
  1431. this.path = path;
  1432. this.dest = dest;
  1433. this.xver = xver;
  1434. }
  1435. toJson() {
  1436. let xver = this.xver;
  1437. if (!Number.isInteger(xver)) {
  1438. xver = 0;
  1439. }
  1440. return {
  1441. name: this.name,
  1442. alpn: this.alpn,
  1443. path: this.path,
  1444. dest: this.dest,
  1445. xver: xver,
  1446. }
  1447. }
  1448. static fromJson(json=[]) {
  1449. const fallbacks = [];
  1450. for (let fallback of json) {
  1451. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1452. fallback.name,
  1453. fallback.alpn,
  1454. fallback.path,
  1455. fallback.dest,
  1456. fallback.xver,
  1457. ))
  1458. }
  1459. return fallbacks;
  1460. }
  1461. };
  1462. Inbound.TrojanSettings = class extends Inbound.Settings {
  1463. constructor(protocol,
  1464. trojans=[new Inbound.TrojanSettings.Trojan()],
  1465. fallbacks=[],) {
  1466. super(protocol);
  1467. this.trojans = trojans;
  1468. this.fallbacks = fallbacks;
  1469. }
  1470. addTrojanFallback() {
  1471. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1472. }
  1473. delTrojanFallback(index) {
  1474. this.fallbacks.splice(index, 1);
  1475. }
  1476. static fromJson(json={}) {
  1477. return new Inbound.TrojanSettings(
  1478. Protocols.TROJAN,
  1479. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1480. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1481. }
  1482. toJson() {
  1483. return {
  1484. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1485. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
  1486. };
  1487. }
  1488. };
  1489. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1490. constructor(password=RandomUtil.randomSeq(10), flow ='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime='') {
  1491. super();
  1492. this.password = password;
  1493. this.flow = flow;
  1494. this.email = email;
  1495. this.limitIp = limitIp;
  1496. this.totalGB = totalGB;
  1497. this.expiryTime = expiryTime;
  1498. }
  1499. toJson() {
  1500. return {
  1501. password: this.password,
  1502. flow: this.flow,
  1503. email: this.email,
  1504. limitIp: this.limitIp,
  1505. totalGB: this.totalGB,
  1506. expiryTime: this.expiryTime,
  1507. };
  1508. }
  1509. static fromJson(json={}) {
  1510. return new Inbound.TrojanSettings.Trojan(
  1511. json.password,
  1512. json.flow,
  1513. json.email,
  1514. json.limitIp,
  1515. json.totalGB,
  1516. json.expiryTime,
  1517. );
  1518. }
  1519. get _expiryTime() {
  1520. if (this.expiryTime === 0 || this.expiryTime === "") {
  1521. return null;
  1522. }
  1523. return moment(this.expiryTime);
  1524. }
  1525. set _expiryTime(t) {
  1526. if (t == null || t === "") {
  1527. this.expiryTime = 0;
  1528. } else {
  1529. this.expiryTime = t.valueOf();
  1530. }
  1531. }
  1532. get _totalGB() {
  1533. return toFixed(this.totalGB / ONE_GB, 2);
  1534. }
  1535. set _totalGB(gb) {
  1536. this.totalGB = toFixed(gb * ONE_GB, 0);
  1537. }
  1538. };
  1539. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1540. constructor(name="", alpn='', path='', dest='', xver=0) {
  1541. super();
  1542. this.name = name;
  1543. this.alpn = alpn;
  1544. this.path = path;
  1545. this.dest = dest;
  1546. this.xver = xver;
  1547. }
  1548. toJson() {
  1549. let xver = this.xver;
  1550. if (!Number.isInteger(xver)) {
  1551. xver = 0;
  1552. }
  1553. return {
  1554. name: this.name,
  1555. alpn: this.alpn,
  1556. path: this.path,
  1557. dest: this.dest,
  1558. xver: xver,
  1559. }
  1560. }
  1561. static fromJson(json=[]) {
  1562. const fallbacks = [];
  1563. for (let fallback of json) {
  1564. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1565. fallback.name,
  1566. fallback.alpn,
  1567. fallback.path,
  1568. fallback.dest,
  1569. fallback.xver,
  1570. ))
  1571. }
  1572. return fallbacks;
  1573. }
  1574. };
  1575. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1576. constructor(protocol,
  1577. method = SSMethods.BLAKE3_AES_256_GCM,
  1578. password = RandomUtil.randomSeq(44),
  1579. network = 'tcp,udp'
  1580. ) {
  1581. super(protocol);
  1582. this.method = method;
  1583. this.password = password;
  1584. this.network = network;
  1585. }
  1586. static fromJson(json = {}) {
  1587. return new Inbound.ShadowsocksSettings(
  1588. Protocols.SHADOWSOCKS,
  1589. json.method,
  1590. json.password,
  1591. json.network,
  1592. );
  1593. }
  1594. toJson() {
  1595. return {
  1596. method: this.method,
  1597. password: this.password,
  1598. network: this.network,
  1599. };
  1600. }
  1601. };
  1602. Inbound.DokodemoSettings = class extends Inbound.Settings {
  1603. constructor(protocol, address, port, network='tcp,udp') {
  1604. super(protocol);
  1605. this.address = address;
  1606. this.port = port;
  1607. this.network = network;
  1608. }
  1609. static fromJson(json={}) {
  1610. return new Inbound.DokodemoSettings(
  1611. Protocols.DOKODEMO,
  1612. json.address,
  1613. json.port,
  1614. json.network,
  1615. );
  1616. }
  1617. toJson() {
  1618. return {
  1619. address: this.address,
  1620. port: this.port,
  1621. network: this.network,
  1622. };
  1623. }
  1624. };
  1625. Inbound.MtprotoSettings = class extends Inbound.Settings {
  1626. constructor(protocol, users=[new Inbound.MtprotoSettings.MtUser()]) {
  1627. super(protocol);
  1628. this.users = users;
  1629. }
  1630. static fromJson(json={}) {
  1631. return new Inbound.MtprotoSettings(
  1632. Protocols.MTPROTO,
  1633. json.users.map(user => Inbound.MtprotoSettings.MtUser.fromJson(user)),
  1634. );
  1635. }
  1636. toJson() {
  1637. return {
  1638. users: XrayCommonClass.toJsonArray(this.users),
  1639. };
  1640. }
  1641. };
  1642. Inbound.MtprotoSettings.MtUser = class extends XrayCommonClass {
  1643. constructor(secret=RandomUtil.randomMTSecret()) {
  1644. super();
  1645. this.secret = secret;
  1646. }
  1647. static fromJson(json={}) {
  1648. return new Inbound.MtprotoSettings.MtUser(json.secret);
  1649. }
  1650. };
  1651. Inbound.SocksSettings = class extends Inbound.Settings {
  1652. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  1653. super(protocol);
  1654. this.auth = auth;
  1655. this.accounts = accounts;
  1656. this.udp = udp;
  1657. this.ip = ip;
  1658. }
  1659. addAccount(account) {
  1660. this.accounts.push(account);
  1661. }
  1662. delAccount(index) {
  1663. this.accounts.splice(index, 1);
  1664. }
  1665. static fromJson(json={}) {
  1666. let accounts;
  1667. if (json.auth === 'password') {
  1668. accounts = json.accounts.map(
  1669. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  1670. )
  1671. }
  1672. return new Inbound.SocksSettings(
  1673. Protocols.SOCKS,
  1674. json.auth,
  1675. accounts,
  1676. json.udp,
  1677. json.ip,
  1678. );
  1679. }
  1680. toJson() {
  1681. return {
  1682. auth: this.auth,
  1683. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  1684. udp: this.udp,
  1685. ip: this.ip,
  1686. };
  1687. }
  1688. };
  1689. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  1690. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1691. super();
  1692. this.user = user;
  1693. this.pass = pass;
  1694. }
  1695. static fromJson(json={}) {
  1696. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  1697. }
  1698. };
  1699. Inbound.HttpSettings = class extends Inbound.Settings {
  1700. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  1701. super(protocol);
  1702. this.accounts = accounts;
  1703. }
  1704. addAccount(account) {
  1705. this.accounts.push(account);
  1706. }
  1707. delAccount(index) {
  1708. this.accounts.splice(index, 1);
  1709. }
  1710. static fromJson(json={}) {
  1711. return new Inbound.HttpSettings(
  1712. Protocols.HTTP,
  1713. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  1714. );
  1715. }
  1716. toJson() {
  1717. return {
  1718. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  1719. };
  1720. }
  1721. };
  1722. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  1723. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1724. super();
  1725. this.user = user;
  1726. this.pass = pass;
  1727. }
  1728. static fromJson(json={}) {
  1729. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  1730. }
  1731. };