xray.js 59 KB

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