xray.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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. };
  26. const RULE_IP = {
  27. PRIVATE: 'geoip:private',
  28. CN: 'geoip:cn',
  29. };
  30. const RULE_DOMAIN = {
  31. ADS: 'geosite:category-ads',
  32. ADS_ALL: 'geosite:category-ads-all',
  33. CN: 'geosite:cn',
  34. GOOGLE: 'geosite:google',
  35. FACEBOOK: 'geosite:facebook',
  36. SPEEDTEST: 'geosite:speedtest',
  37. };
  38. const VLESS_FLOW = {
  39. ORIGIN: "xtls-rprx-origin",
  40. DIRECT: "xtls-rprx-direct",
  41. };
  42. Object.freeze(Protocols);
  43. Object.freeze(VmessMethods);
  44. Object.freeze(SSMethods);
  45. Object.freeze(RULE_IP);
  46. Object.freeze(RULE_DOMAIN);
  47. Object.freeze(VLESS_FLOW);
  48. class XrayCommonClass {
  49. static toJsonArray(arr) {
  50. return arr.map(obj => obj.toJson());
  51. }
  52. static fromJson() {
  53. return new XrayCommonClass();
  54. }
  55. toJson() {
  56. return this;
  57. }
  58. toString(format=true) {
  59. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  60. }
  61. static toHeaders(v2Headers) {
  62. let newHeaders = [];
  63. if (v2Headers) {
  64. Object.keys(v2Headers).forEach(key => {
  65. let values = v2Headers[key];
  66. if (typeof(values) === 'string') {
  67. newHeaders.push({ name: key, value: values });
  68. } else {
  69. for (let i = 0; i < values.length; ++i) {
  70. newHeaders.push({ name: key, value: values[i] });
  71. }
  72. }
  73. });
  74. }
  75. return newHeaders;
  76. }
  77. static toV2Headers(headers, arr=true) {
  78. let v2Headers = {};
  79. for (let i = 0; i < headers.length; ++i) {
  80. let name = headers[i].name;
  81. let value = headers[i].value;
  82. if (ObjectUtil.isEmpty(name) || ObjectUtil.isEmpty(value)) {
  83. continue;
  84. }
  85. if (!(name in v2Headers)) {
  86. v2Headers[name] = arr ? [value] : value;
  87. } else {
  88. if (arr) {
  89. v2Headers[name].push(value);
  90. } else {
  91. v2Headers[name] = value;
  92. }
  93. }
  94. }
  95. return v2Headers;
  96. }
  97. }
  98. class TcpStreamSettings extends XrayCommonClass {
  99. constructor(type='none',
  100. request=new TcpStreamSettings.TcpRequest(),
  101. response=new TcpStreamSettings.TcpResponse(),
  102. ) {
  103. super();
  104. this.type = type;
  105. this.request = request;
  106. this.response = response;
  107. }
  108. static fromJson(json={}) {
  109. let header = json.header;
  110. if (!header) {
  111. header = {};
  112. }
  113. return new TcpStreamSettings(
  114. header.type,
  115. TcpStreamSettings.TcpRequest.fromJson(header.request),
  116. TcpStreamSettings.TcpResponse.fromJson(header.response),
  117. );
  118. }
  119. toJson() {
  120. return {
  121. header: {
  122. type: this.type,
  123. request: this.type === 'http' ? this.request.toJson() : undefined,
  124. response: this.type === 'http' ? this.response.toJson() : undefined,
  125. },
  126. };
  127. }
  128. }
  129. TcpStreamSettings.TcpRequest = class extends XrayCommonClass {
  130. constructor(version='1.1',
  131. method='GET',
  132. path=['/'],
  133. headers=[],
  134. ) {
  135. super();
  136. this.version = version;
  137. this.method = method;
  138. this.path = path.length === 0 ? ['/'] : path;
  139. this.headers = headers;
  140. }
  141. addPath(path) {
  142. this.path.push(path);
  143. }
  144. removePath(index) {
  145. this.path.splice(index, 1);
  146. }
  147. addHeader(name, value) {
  148. this.headers.push({ name: name, value: value });
  149. }
  150. getHeader(name) {
  151. for (const header of this.headers) {
  152. if (header.name.toLowerCase() === name.toLowerCase()) {
  153. return header.value;
  154. }
  155. }
  156. return null;
  157. }
  158. removeHeader(index) {
  159. this.headers.splice(index, 1);
  160. }
  161. static fromJson(json={}) {
  162. return new TcpStreamSettings.TcpRequest(
  163. json.version,
  164. json.method,
  165. json.path,
  166. XrayCommonClass.toHeaders(json.headers),
  167. );
  168. }
  169. toJson() {
  170. return {
  171. method: this.method,
  172. path: ObjectUtil.clone(this.path),
  173. headers: XrayCommonClass.toV2Headers(this.headers),
  174. };
  175. }
  176. };
  177. TcpStreamSettings.TcpResponse = class extends XrayCommonClass {
  178. constructor(version='1.1',
  179. status='200',
  180. reason='OK',
  181. headers=[],
  182. ) {
  183. super();
  184. this.version = version;
  185. this.status = status;
  186. this.reason = reason;
  187. this.headers = headers;
  188. }
  189. addHeader(name, value) {
  190. this.headers.push({ name: name, value: value });
  191. }
  192. removeHeader(index) {
  193. this.headers.splice(index, 1);
  194. }
  195. static fromJson(json={}) {
  196. return new TcpStreamSettings.TcpResponse(
  197. json.version,
  198. json.status,
  199. json.reason,
  200. XrayCommonClass.toHeaders(json.headers),
  201. );
  202. }
  203. toJson() {
  204. return {
  205. version: this.version,
  206. status: this.status,
  207. reason: this.reason,
  208. headers: XrayCommonClass.toV2Headers(this.headers),
  209. };
  210. }
  211. };
  212. class KcpStreamSettings extends XrayCommonClass {
  213. constructor(mtu=1350, tti=20,
  214. uplinkCapacity=5,
  215. downlinkCapacity=20,
  216. congestion=false,
  217. readBufferSize=2,
  218. writeBufferSize=2,
  219. type='none',
  220. seed=RandomUtil.randomSeq(10),
  221. ) {
  222. super();
  223. this.mtu = mtu;
  224. this.tti = tti;
  225. this.upCap = uplinkCapacity;
  226. this.downCap = downlinkCapacity;
  227. this.congestion = congestion;
  228. this.readBuffer = readBufferSize;
  229. this.writeBuffer = writeBufferSize;
  230. this.type = type;
  231. this.seed = seed;
  232. }
  233. static fromJson(json={}) {
  234. return new KcpStreamSettings(
  235. json.mtu,
  236. json.tti,
  237. json.uplinkCapacity,
  238. json.downlinkCapacity,
  239. json.congestion,
  240. json.readBufferSize,
  241. json.writeBufferSize,
  242. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  243. json.seed,
  244. );
  245. }
  246. toJson() {
  247. return {
  248. mtu: this.mtu,
  249. tti: this.tti,
  250. uplinkCapacity: this.upCap,
  251. downlinkCapacity: this.downCap,
  252. congestion: this.congestion,
  253. readBufferSize: this.readBuffer,
  254. writeBufferSize: this.writeBuffer,
  255. header: {
  256. type: this.type,
  257. },
  258. seed: this.seed,
  259. };
  260. }
  261. }
  262. class WsStreamSettings extends XrayCommonClass {
  263. constructor(path='/', headers=[]) {
  264. super();
  265. this.path = path;
  266. this.headers = headers;
  267. }
  268. addHeader(name, value) {
  269. this.headers.push({ name: name, value: value });
  270. }
  271. getHeader(name) {
  272. for (const header of this.headers) {
  273. if (header.name.toLowerCase() === name.toLowerCase()) {
  274. return header.value;
  275. }
  276. }
  277. return null;
  278. }
  279. removeHeader(index) {
  280. this.headers.splice(index, 1);
  281. }
  282. static fromJson(json={}) {
  283. return new WsStreamSettings(
  284. json.path,
  285. XrayCommonClass.toHeaders(json.headers),
  286. );
  287. }
  288. toJson() {
  289. return {
  290. path: this.path,
  291. headers: XrayCommonClass.toV2Headers(this.headers, false),
  292. };
  293. }
  294. }
  295. class HttpStreamSettings extends XrayCommonClass {
  296. constructor(path='/', host=['']) {
  297. super();
  298. this.path = path;
  299. this.host = host.length === 0 ? [''] : host;
  300. }
  301. addHost(host) {
  302. this.host.push(host);
  303. }
  304. removeHost(index) {
  305. this.host.splice(index, 1);
  306. }
  307. static fromJson(json={}) {
  308. return new HttpStreamSettings(json.path, json.host);
  309. }
  310. toJson() {
  311. let host = [];
  312. for (let i = 0; i < this.host.length; ++i) {
  313. if (!ObjectUtil.isEmpty(this.host[i])) {
  314. host.push(this.host[i]);
  315. }
  316. }
  317. return {
  318. path: this.path,
  319. host: host,
  320. }
  321. }
  322. }
  323. class QuicStreamSettings extends XrayCommonClass {
  324. constructor(security=VmessMethods.NONE,
  325. key='', type='none') {
  326. super();
  327. this.security = security;
  328. this.key = key;
  329. this.type = type;
  330. }
  331. static fromJson(json={}) {
  332. return new QuicStreamSettings(
  333. json.security,
  334. json.key,
  335. json.header ? json.header.type : 'none',
  336. );
  337. }
  338. toJson() {
  339. return {
  340. security: this.security,
  341. key: this.key,
  342. header: {
  343. type: this.type,
  344. }
  345. }
  346. }
  347. }
  348. class GrpcStreamSettings extends XrayCommonClass {
  349. constructor(serviceName="") {
  350. super();
  351. this.serviceName = serviceName;
  352. }
  353. static fromJson(json={}) {
  354. return new GrpcStreamSettings(json.serviceName);
  355. }
  356. toJson() {
  357. return {
  358. serviceName: this.serviceName,
  359. }
  360. }
  361. }
  362. class TlsStreamSettings extends XrayCommonClass {
  363. constructor(serverName='',
  364. certificates=[new TlsStreamSettings.Cert()]) {
  365. super();
  366. this.server = serverName;
  367. this.certs = certificates;
  368. }
  369. addCert(cert) {
  370. this.certs.push(cert);
  371. }
  372. removeCert(index) {
  373. this.certs.splice(index, 1);
  374. }
  375. static fromJson(json={}) {
  376. let certs;
  377. if (!ObjectUtil.isEmpty(json.certificates)) {
  378. certs = json.certificates.map(cert => TlsStreamSettings.Cert.fromJson(cert));
  379. }
  380. return new TlsStreamSettings(
  381. json.serverName,
  382. certs,
  383. );
  384. }
  385. toJson() {
  386. return {
  387. serverName: this.server,
  388. certificates: TlsStreamSettings.toJsonArray(this.certs),
  389. };
  390. }
  391. }
  392. TlsStreamSettings.Cert = class extends XrayCommonClass {
  393. constructor(useFile=true, certificateFile='', keyFile='', certificate='', key='') {
  394. super();
  395. this.useFile = useFile;
  396. this.certFile = certificateFile;
  397. this.keyFile = keyFile;
  398. this.cert = certificate instanceof Array ? certificate.join('\n') : certificate;
  399. this.key = key instanceof Array ? key.join('\n') : key;
  400. }
  401. static fromJson(json={}) {
  402. if ('certificateFile' in json && 'keyFile' in json) {
  403. return new TlsStreamSettings.Cert(
  404. true,
  405. json.certificateFile,
  406. json.keyFile,
  407. );
  408. } else {
  409. return new TlsStreamSettings.Cert(
  410. false, '', '',
  411. json.certificate.join('\n'),
  412. json.key.join('\n'),
  413. );
  414. }
  415. }
  416. toJson() {
  417. if (this.useFile) {
  418. return {
  419. certificateFile: this.certFile,
  420. keyFile: this.keyFile,
  421. };
  422. } else {
  423. return {
  424. certificate: this.cert.split('\n'),
  425. key: this.key.split('\n'),
  426. };
  427. }
  428. }
  429. };
  430. class StreamSettings extends XrayCommonClass {
  431. constructor(network='tcp',
  432. security='none',
  433. tlsSettings=new TlsStreamSettings(),
  434. tcpSettings=new TcpStreamSettings(),
  435. kcpSettings=new KcpStreamSettings(),
  436. wsSettings=new WsStreamSettings(),
  437. httpSettings=new HttpStreamSettings(),
  438. quicSettings=new QuicStreamSettings(),
  439. grpcSettings=new GrpcStreamSettings(),
  440. ) {
  441. super();
  442. this.network = network;
  443. this.security = security;
  444. this.tls = tlsSettings;
  445. this.tcp = tcpSettings;
  446. this.kcp = kcpSettings;
  447. this.ws = wsSettings;
  448. this.http = httpSettings;
  449. this.quic = quicSettings;
  450. this.grpc = grpcSettings;
  451. }
  452. get isTls() {
  453. return this.security === 'tls';
  454. }
  455. set isTls(isTls) {
  456. if (isTls) {
  457. this.security = 'tls';
  458. } else {
  459. this.security = 'none';
  460. }
  461. }
  462. get isXTls() {
  463. return this.security === "xtls";
  464. }
  465. set isXTls(isXTls) {
  466. if (isXTls) {
  467. this.security = 'xtls';
  468. } else {
  469. this.security = 'none';
  470. }
  471. }
  472. static fromJson(json={}) {
  473. let tls;
  474. if (json.security === "xtls") {
  475. tls = TlsStreamSettings.fromJson(json.xtlsSettings);
  476. } else {
  477. tls = TlsStreamSettings.fromJson(json.tlsSettings);
  478. }
  479. return new StreamSettings(
  480. json.network,
  481. json.security,
  482. tls,
  483. TcpStreamSettings.fromJson(json.tcpSettings),
  484. KcpStreamSettings.fromJson(json.kcpSettings),
  485. WsStreamSettings.fromJson(json.wsSettings),
  486. HttpStreamSettings.fromJson(json.httpSettings),
  487. QuicStreamSettings.fromJson(json.quicSettings),
  488. GrpcStreamSettings.fromJson(json.grpcSettings),
  489. );
  490. }
  491. toJson() {
  492. const network = this.network;
  493. return {
  494. network: network,
  495. security: this.security,
  496. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  497. xtlsSettings: this.isXTls ? this.tls.toJson() : undefined,
  498. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  499. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  500. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  501. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  502. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  503. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  504. };
  505. }
  506. }
  507. class Sniffing extends XrayCommonClass {
  508. constructor(enabled=true, destOverride=['http', 'tls']) {
  509. super();
  510. this.enabled = enabled;
  511. this.destOverride = destOverride;
  512. }
  513. static fromJson(json={}) {
  514. let destOverride = ObjectUtil.clone(json.destOverride);
  515. if (!ObjectUtil.isEmpty(destOverride) && !ObjectUtil.isArrEmpty(destOverride)) {
  516. if (ObjectUtil.isEmpty(destOverride[0])) {
  517. destOverride = ['http', 'tls'];
  518. }
  519. }
  520. return new Sniffing(
  521. !!json.enabled,
  522. destOverride,
  523. );
  524. }
  525. }
  526. class Inbound extends XrayCommonClass {
  527. constructor(port=RandomUtil.randomIntRange(10000, 60000),
  528. listen='',
  529. protocol=Protocols.VMESS,
  530. settings=null,
  531. streamSettings=new StreamSettings(),
  532. tag='',
  533. sniffing=new Sniffing(),
  534. ) {
  535. super();
  536. this.port = port;
  537. this.listen = listen;
  538. this._protocol = protocol;
  539. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  540. this.stream = streamSettings;
  541. this.tag = tag;
  542. this.sniffing = sniffing;
  543. }
  544. get protocol() {
  545. return this._protocol;
  546. }
  547. set protocol(protocol) {
  548. this._protocol = protocol;
  549. this.settings = Inbound.Settings.getSettings(protocol);
  550. if (protocol === Protocols.TROJAN) {
  551. this.tls = true;
  552. }
  553. }
  554. get tls() {
  555. return this.stream.security === 'tls';
  556. }
  557. set tls(isTls) {
  558. if (isTls) {
  559. this.stream.security = 'tls';
  560. } else {
  561. if (this.protocol === Protocols.TROJAN) {
  562. this.xtls = true;
  563. } else {
  564. this.stream.security = 'none';
  565. }
  566. }
  567. }
  568. get xtls() {
  569. return this.stream.security === 'xtls';
  570. }
  571. set xtls(isXTls) {
  572. if (isXTls) {
  573. this.stream.security = 'xtls';
  574. } else {
  575. if (this.protocol === Protocols.TROJAN) {
  576. this.tls = true;
  577. } else {
  578. this.stream.security = 'none';
  579. }
  580. }
  581. }
  582. get network() {
  583. return this.stream.network;
  584. }
  585. set network(network) {
  586. this.stream.network = network;
  587. }
  588. get isTcp() {
  589. return this.network === "tcp";
  590. }
  591. get isWs() {
  592. return this.network === "ws";
  593. }
  594. get isKcp() {
  595. return this.network === "kcp";
  596. }
  597. get isQuic() {
  598. return this.network === "quic"
  599. }
  600. get isGrpc() {
  601. return this.network === "grpc";
  602. }
  603. get isH2() {
  604. return this.network === "http";
  605. }
  606. // VMess & VLess
  607. get uuid() {
  608. switch (this.protocol) {
  609. case Protocols.VMESS:
  610. return this.settings.vmesses[0].id;
  611. case Protocols.VLESS:
  612. return this.settings.vlesses[0].id;
  613. default:
  614. return "";
  615. }
  616. }
  617. // VLess
  618. get flow() {
  619. switch (this.protocol) {
  620. case Protocols.VLESS:
  621. return this.settings.vlesses[0].flow;
  622. default:
  623. return "";
  624. }
  625. }
  626. // VMess
  627. get alterId() {
  628. switch (this.protocol) {
  629. case Protocols.VMESS:
  630. return this.settings.vmesses[0].alterId;
  631. default:
  632. return "";
  633. }
  634. }
  635. // Socks & HTTP
  636. get username() {
  637. switch (this.protocol) {
  638. case Protocols.SOCKS:
  639. case Protocols.HTTP:
  640. return this.settings.accounts[0].user;
  641. default:
  642. return "";
  643. }
  644. }
  645. // Trojan & Shadowsocks & Socks & HTTP
  646. get password() {
  647. switch (this.protocol) {
  648. case Protocols.TROJAN:
  649. return this.settings.clients[0].password;
  650. case Protocols.SHADOWSOCKS:
  651. return this.settings.password;
  652. case Protocols.SOCKS:
  653. case Protocols.HTTP:
  654. return this.settings.accounts[0].pass;
  655. default:
  656. return "";
  657. }
  658. }
  659. // Shadowsocks
  660. get method() {
  661. switch (this.protocol) {
  662. case Protocols.SHADOWSOCKS:
  663. return this.settings.method;
  664. default:
  665. return "";
  666. }
  667. }
  668. get serverName() {
  669. if (this.stream.isTls || this.stream.isXTls) {
  670. return this.stream.tls.server;
  671. }
  672. return "";
  673. }
  674. get host() {
  675. if (this.isTcp) {
  676. return this.stream.tcp.request.getHeader("Host");
  677. } else if (this.isWs) {
  678. return this.stream.ws.getHeader("Host");
  679. } else if (this.isH2) {
  680. return this.stream.http.host[0];
  681. }
  682. return null;
  683. }
  684. get path() {
  685. if (this.isTcp) {
  686. return this.stream.tcp.request.path[0];
  687. } else if (this.isWs) {
  688. return this.stream.ws.path;
  689. } else if (this.isH2) {
  690. return this.stream.http.path[0];
  691. }
  692. return null;
  693. }
  694. get quicSecurity() {
  695. return this.stream.quic.security;
  696. }
  697. get quicKey() {
  698. return this.stream.quic.key;
  699. }
  700. get quicType() {
  701. return this.stream.quic.type;
  702. }
  703. get kcpType() {
  704. return this.stream.kcp.type;
  705. }
  706. get kcpSeed() {
  707. return this.stream.kcp.seed;
  708. }
  709. get serviceName() {
  710. return this.stream.grpc.serviceName;
  711. }
  712. canEnableTls() {
  713. switch (this.protocol) {
  714. case Protocols.VMESS:
  715. case Protocols.VLESS:
  716. case Protocols.TROJAN:
  717. case Protocols.SHADOWSOCKS:
  718. break;
  719. default:
  720. return false;
  721. }
  722. switch (this.network) {
  723. case "tcp":
  724. case "ws":
  725. case "http":
  726. case "quic":
  727. case "grpc":
  728. return true;
  729. default:
  730. return false;
  731. }
  732. }
  733. canSetTls() {
  734. return this.canEnableTls();
  735. }
  736. canEnableXTls() {
  737. switch (this.protocol) {
  738. case Protocols.VLESS:
  739. case Protocols.TROJAN:
  740. break;
  741. default:
  742. return false;
  743. }
  744. return this.network === "tcp";
  745. }
  746. canEnableStream() {
  747. switch (this.protocol) {
  748. case Protocols.VMESS:
  749. case Protocols.VLESS:
  750. case Protocols.SHADOWSOCKS:
  751. return true;
  752. default:
  753. return false;
  754. }
  755. }
  756. canSniffing() {
  757. switch (this.protocol) {
  758. case Protocols.VMESS:
  759. case Protocols.VLESS:
  760. case Protocols.TROJAN:
  761. case Protocols.SHADOWSOCKS:
  762. return true;
  763. default:
  764. return false;
  765. }
  766. }
  767. reset() {
  768. this.port = RandomUtil.randomIntRange(10000, 60000);
  769. this.listen = '';
  770. this.protocol = Protocols.VMESS;
  771. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  772. this.stream = new StreamSettings();
  773. this.tag = '';
  774. this.sniffing = new Sniffing();
  775. }
  776. genVmessLink(address='', remark='') {
  777. if (this.protocol !== Protocols.VMESS) {
  778. return '';
  779. }
  780. let network = this.stream.network;
  781. let type = 'none';
  782. let host = '';
  783. let path = '';
  784. if (network === 'tcp') {
  785. let tcp = this.stream.tcp;
  786. type = tcp.type;
  787. if (type === 'http') {
  788. let request = tcp.request;
  789. path = request.path.join(',');
  790. let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  791. if (index >= 0) {
  792. host = request.headers[index].value;
  793. }
  794. }
  795. } else if (network === 'kcp') {
  796. let kcp = this.stream.kcp;
  797. type = kcp.type;
  798. path = kcp.seed;
  799. } else if (network === 'ws') {
  800. let ws = this.stream.ws;
  801. path = ws.path;
  802. let index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  803. if (index >= 0) {
  804. host = ws.headers[index].value;
  805. }
  806. } else if (network === 'http') {
  807. network = 'h2';
  808. path = this.stream.http.path;
  809. host = this.stream.http.host.join(',');
  810. } else if (network === 'quic') {
  811. type = this.stream.quic.type;
  812. host = this.stream.quic.security;
  813. path = this.stream.quic.key;
  814. } else if (network === 'grpc') {
  815. path = this.stream.grpc.serviceName;
  816. }
  817. if (this.stream.security === 'tls') {
  818. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  819. address = this.stream.tls.server;
  820. }
  821. }
  822. let obj = {
  823. v: '2',
  824. ps: remark,
  825. add: address,
  826. port: this.port,
  827. id: this.settings.vmesses[0].id,
  828. aid: this.settings.vmesses[0].alterId,
  829. net: network,
  830. type: type,
  831. host: host,
  832. path: path,
  833. tls: this.stream.security,
  834. };
  835. return 'vmess://' + base64(JSON.stringify(obj, null, 2));
  836. }
  837. genVLESSLink(address = '', remark='') {
  838. const settings = this.settings;
  839. const uuid = settings.vlesses[0].id;
  840. const port = this.port;
  841. const type = this.stream.network;
  842. const params = new Map();
  843. params.set("type", this.stream.network);
  844. if (this.xtls) {
  845. params.set("security", "xtls");
  846. } else {
  847. params.set("security", this.stream.security);
  848. }
  849. switch (type) {
  850. case "tcp":
  851. const tcp = this.stream.tcp;
  852. if (tcp.type === 'http') {
  853. const request = tcp.request;
  854. params.set("path", request.path.join(','));
  855. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  856. if (index >= 0) {
  857. const host = request.headers[index].value;
  858. params.set("host", host);
  859. }
  860. }
  861. break;
  862. case "kcp":
  863. const kcp = this.stream.kcp;
  864. params.set("headerType", kcp.type);
  865. params.set("seed", kcp.seed);
  866. break;
  867. case "ws":
  868. const ws = this.stream.ws;
  869. params.set("path", ws.path);
  870. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  871. if (index >= 0) {
  872. const host = ws.headers[index].value;
  873. params.set("host", host);
  874. }
  875. break;
  876. case "http":
  877. const http = this.stream.http;
  878. params.set("path", http.path);
  879. params.set("host", http.host);
  880. break;
  881. case "quic":
  882. const quic = this.stream.quic;
  883. params.set("quicSecurity", quic.security);
  884. params.set("key", quic.key);
  885. params.set("headerType", quic.type);
  886. break;
  887. case "grpc":
  888. const grpc = this.stream.grpc;
  889. params.set("serviceName", grpc.serviceName);
  890. break;
  891. }
  892. if (this.stream.security === 'tls') {
  893. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  894. address = this.stream.tls.server;
  895. params.set("sni", address);
  896. }
  897. }
  898. if (this.xtls) {
  899. params.set("flow", this.settings.vlesses[0].flow);
  900. }
  901. for (const [key, value] of params) {
  902. switch (key) {
  903. case "host":
  904. case "path":
  905. case "seed":
  906. case "key":
  907. case "alpn":
  908. params.set(key, encodeURIComponent(value));
  909. break;
  910. }
  911. }
  912. const link = `vless://${uuid}@${address}:${port}`;
  913. const url = new URL(link);
  914. for (const [key, value] of params) {
  915. url.searchParams.set(key, value)
  916. }
  917. url.hash = encodeURIComponent(remark);
  918. return url.toString();
  919. }
  920. genSSLink(address='', remark='') {
  921. let settings = this.settings;
  922. const server = this.stream.tls.server;
  923. if (!ObjectUtil.isEmpty(server)) {
  924. address = server;
  925. }
  926. return 'ss://' + safeBase64(settings.method + ':' + settings.password + '@' + address + ':' + this.port)
  927. + '#' + encodeURIComponent(remark);
  928. }
  929. genTrojanLink(address='', remark='') {
  930. let settings = this.settings;
  931. return `trojan://${settings.clients[0].password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  932. }
  933. genLink(address='', remark='') {
  934. switch (this.protocol) {
  935. case Protocols.VMESS: return this.genVmessLink(address, remark);
  936. case Protocols.VLESS: return this.genVLESSLink(address, remark);
  937. case Protocols.SHADOWSOCKS: return this.genSSLink(address, remark);
  938. case Protocols.TROJAN: return this.genTrojanLink(address, remark);
  939. default: return '';
  940. }
  941. }
  942. static fromJson(json={}) {
  943. return new Inbound(
  944. json.port,
  945. json.listen,
  946. json.protocol,
  947. Inbound.Settings.fromJson(json.protocol, json.settings),
  948. StreamSettings.fromJson(json.streamSettings),
  949. json.tag,
  950. Sniffing.fromJson(json.sniffing),
  951. )
  952. }
  953. toJson() {
  954. let streamSettings;
  955. if (this.canEnableStream() || this.protocol === Protocols.TROJAN) {
  956. streamSettings = this.stream.toJson();
  957. }
  958. return {
  959. port: this.port,
  960. listen: this.listen,
  961. protocol: this.protocol,
  962. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  963. streamSettings: streamSettings,
  964. tag: this.tag,
  965. sniffing: this.sniffing.toJson(),
  966. };
  967. }
  968. }
  969. Inbound.Settings = class extends XrayCommonClass {
  970. constructor(protocol) {
  971. super();
  972. this.protocol = protocol;
  973. }
  974. static getSettings(protocol) {
  975. switch (protocol) {
  976. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  977. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  978. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  979. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  980. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  981. case Protocols.MTPROTO: return new Inbound.MtprotoSettings(protocol);
  982. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  983. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  984. default: return null;
  985. }
  986. }
  987. static fromJson(protocol, json) {
  988. switch (protocol) {
  989. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  990. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  991. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  992. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  993. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  994. case Protocols.MTPROTO: return Inbound.MtprotoSettings.fromJson(json);
  995. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  996. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  997. default: return null;
  998. }
  999. }
  1000. toJson() {
  1001. return {};
  1002. }
  1003. };
  1004. Inbound.VmessSettings = class extends Inbound.Settings {
  1005. constructor(protocol,
  1006. vmesses=[new Inbound.VmessSettings.Vmess()],
  1007. disableInsecureEncryption=false) {
  1008. super(protocol);
  1009. this.vmesses = vmesses;
  1010. this.disableInsecure = disableInsecureEncryption;
  1011. }
  1012. indexOfVmessById(id) {
  1013. return this.vmesses.findIndex(vmess => vmess.id === id);
  1014. }
  1015. addVmess(vmess) {
  1016. if (this.indexOfVmessById(vmess.id) >= 0) {
  1017. return false;
  1018. }
  1019. this.vmesses.push(vmess);
  1020. }
  1021. delVmess(vmess) {
  1022. const i = this.indexOfVmessById(vmess.id);
  1023. if (i >= 0) {
  1024. this.vmesses.splice(i, 1);
  1025. }
  1026. }
  1027. static fromJson(json={}) {
  1028. return new Inbound.VmessSettings(
  1029. Protocols.VMESS,
  1030. json.clients.map(client => Inbound.VmessSettings.Vmess.fromJson(client)),
  1031. ObjectUtil.isEmpty(json.disableInsecureEncryption) ? false : json.disableInsecureEncryption,
  1032. );
  1033. }
  1034. toJson() {
  1035. return {
  1036. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1037. disableInsecureEncryption: this.disableInsecure,
  1038. };
  1039. }
  1040. };
  1041. Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
  1042. constructor(id=RandomUtil.randomUUID(), alterId=64) {
  1043. super();
  1044. this.id = id;
  1045. this.alterId = alterId;
  1046. }
  1047. static fromJson(json={}) {
  1048. return new Inbound.VmessSettings.Vmess(
  1049. json.id,
  1050. json.alterId,
  1051. );
  1052. }
  1053. };
  1054. Inbound.VLESSSettings = class extends Inbound.Settings {
  1055. constructor(protocol,
  1056. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1057. decryption='none',
  1058. fallbacks=[],) {
  1059. super(protocol);
  1060. this.vlesses = vlesses;
  1061. this.decryption = decryption;
  1062. this.fallbacks = fallbacks;
  1063. }
  1064. addFallback() {
  1065. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1066. }
  1067. delFallback(index) {
  1068. this.fallbacks.splice(index, 1);
  1069. }
  1070. static fromJson(json={}) {
  1071. return new Inbound.VLESSSettings(
  1072. Protocols.VLESS,
  1073. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1074. json.decryption,
  1075. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1076. );
  1077. }
  1078. toJson() {
  1079. return {
  1080. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1081. decryption: this.decryption,
  1082. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1083. };
  1084. }
  1085. };
  1086. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1087. constructor(id=RandomUtil.randomUUID(), flow=VLESS_FLOW.DIRECT) {
  1088. super();
  1089. this.id = id;
  1090. this.flow = flow;
  1091. }
  1092. static fromJson(json={}) {
  1093. return new Inbound.VLESSSettings.VLESS(
  1094. json.id,
  1095. json.flow,
  1096. );
  1097. }
  1098. };
  1099. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1100. constructor(name="", alpn='', path='', dest='', xver=0) {
  1101. super();
  1102. this.name = name;
  1103. this.alpn = alpn;
  1104. this.path = path;
  1105. this.dest = dest;
  1106. this.xver = xver;
  1107. }
  1108. toJson() {
  1109. let xver = this.xver;
  1110. if (!Number.isInteger(xver)) {
  1111. xver = 0;
  1112. }
  1113. return {
  1114. name: this.name,
  1115. alpn: this.alpn,
  1116. path: this.path,
  1117. dest: this.dest,
  1118. xver: xver,
  1119. }
  1120. }
  1121. static fromJson(json=[]) {
  1122. const fallbacks = [];
  1123. for (let fallback of json) {
  1124. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1125. fallback.name,
  1126. fallback.alpn,
  1127. fallback.path,
  1128. fallback.dest,
  1129. fallback.xver,
  1130. ))
  1131. }
  1132. return fallbacks;
  1133. }
  1134. };
  1135. Inbound.TrojanSettings = class extends Inbound.Settings {
  1136. constructor(protocol, clients=[new Inbound.TrojanSettings.Client()]) {
  1137. super(protocol);
  1138. this.clients = clients;
  1139. }
  1140. toJson() {
  1141. return {
  1142. clients: Inbound.TrojanSettings.toJsonArray(this.clients),
  1143. };
  1144. }
  1145. static fromJson(json={}) {
  1146. const clients = [];
  1147. for (const c of json.clients) {
  1148. clients.push(Inbound.TrojanSettings.Client.fromJson(c));
  1149. }
  1150. return new Inbound.TrojanSettings(Protocols.TROJAN, clients);
  1151. }
  1152. };
  1153. Inbound.TrojanSettings.Client = class extends XrayCommonClass {
  1154. constructor(password=RandomUtil.randomSeq(10)) {
  1155. super();
  1156. this.password = password;
  1157. }
  1158. toJson() {
  1159. return {
  1160. password: this.password,
  1161. };
  1162. }
  1163. static fromJson(json={}) {
  1164. return new Inbound.TrojanSettings.Client(json.password);
  1165. }
  1166. };
  1167. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1168. constructor(protocol,
  1169. method=SSMethods.AES_256_GCM,
  1170. password=RandomUtil.randomSeq(10),
  1171. network='tcp,udp'
  1172. ) {
  1173. super(protocol);
  1174. this.method = method;
  1175. this.password = password;
  1176. this.network = network;
  1177. }
  1178. static fromJson(json={}) {
  1179. return new Inbound.ShadowsocksSettings(
  1180. Protocols.SHADOWSOCKS,
  1181. json.method,
  1182. json.password,
  1183. json.network,
  1184. );
  1185. }
  1186. toJson() {
  1187. return {
  1188. method: this.method,
  1189. password: this.password,
  1190. network: this.network,
  1191. };
  1192. }
  1193. };
  1194. Inbound.DokodemoSettings = class extends Inbound.Settings {
  1195. constructor(protocol, address, port, network='tcp,udp') {
  1196. super(protocol);
  1197. this.address = address;
  1198. this.port = port;
  1199. this.network = network;
  1200. }
  1201. static fromJson(json={}) {
  1202. return new Inbound.DokodemoSettings(
  1203. Protocols.DOKODEMO,
  1204. json.address,
  1205. json.port,
  1206. json.network,
  1207. );
  1208. }
  1209. toJson() {
  1210. return {
  1211. address: this.address,
  1212. port: this.port,
  1213. network: this.network,
  1214. };
  1215. }
  1216. };
  1217. Inbound.MtprotoSettings = class extends Inbound.Settings {
  1218. constructor(protocol, users=[new Inbound.MtprotoSettings.MtUser()]) {
  1219. super(protocol);
  1220. this.users = users;
  1221. }
  1222. static fromJson(json={}) {
  1223. return new Inbound.MtprotoSettings(
  1224. Protocols.MTPROTO,
  1225. json.users.map(user => Inbound.MtprotoSettings.MtUser.fromJson(user)),
  1226. );
  1227. }
  1228. toJson() {
  1229. return {
  1230. users: XrayCommonClass.toJsonArray(this.users),
  1231. };
  1232. }
  1233. };
  1234. Inbound.MtprotoSettings.MtUser = class extends XrayCommonClass {
  1235. constructor(secret=RandomUtil.randomMTSecret()) {
  1236. super();
  1237. this.secret = secret;
  1238. }
  1239. static fromJson(json={}) {
  1240. return new Inbound.MtprotoSettings.MtUser(json.secret);
  1241. }
  1242. };
  1243. Inbound.SocksSettings = class extends Inbound.Settings {
  1244. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  1245. super(protocol);
  1246. this.auth = auth;
  1247. this.accounts = accounts;
  1248. this.udp = udp;
  1249. this.ip = ip;
  1250. }
  1251. addAccount(account) {
  1252. this.accounts.push(account);
  1253. }
  1254. delAccount(index) {
  1255. this.accounts.splice(index, 1);
  1256. }
  1257. static fromJson(json={}) {
  1258. let accounts;
  1259. if (json.auth === 'password') {
  1260. accounts = json.accounts.map(
  1261. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  1262. )
  1263. }
  1264. return new Inbound.SocksSettings(
  1265. Protocols.SOCKS,
  1266. json.auth,
  1267. accounts,
  1268. json.udp,
  1269. json.ip,
  1270. );
  1271. }
  1272. toJson() {
  1273. return {
  1274. auth: this.auth,
  1275. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  1276. udp: this.udp,
  1277. ip: this.ip,
  1278. };
  1279. }
  1280. };
  1281. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  1282. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1283. super();
  1284. this.user = user;
  1285. this.pass = pass;
  1286. }
  1287. static fromJson(json={}) {
  1288. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  1289. }
  1290. };
  1291. Inbound.HttpSettings = class extends Inbound.Settings {
  1292. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  1293. super(protocol);
  1294. this.accounts = accounts;
  1295. }
  1296. addAccount(account) {
  1297. this.accounts.push(account);
  1298. }
  1299. delAccount(index) {
  1300. this.accounts.splice(index, 1);
  1301. }
  1302. static fromJson(json={}) {
  1303. return new Inbound.HttpSettings(
  1304. Protocols.HTTP,
  1305. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  1306. );
  1307. }
  1308. toJson() {
  1309. return {
  1310. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  1311. };
  1312. }
  1313. };
  1314. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  1315. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1316. super();
  1317. this.user = user;
  1318. this.pass = pass;
  1319. }
  1320. static fromJson(json={}) {
  1321. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  1322. }
  1323. };