xray.js 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  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=[ALPN_OPTION.H2,ALPN_OPTION.HTTP1],
  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(
  538. show = false,xver = 0,
  539. fingerprint = UTLS_FINGERPRINT.UTLS_FIREFOX,
  540. dest = 'yahoo.com:443',
  541. serverNames = 'yahoo.com,www.yahoo.com',
  542. privateKey = RandomUtil.randomX25519PrivateKey(),
  543. publicKey = '',
  544. minClient = '',
  545. maxClient = '',
  546. maxTimediff = 0,
  547. shortIds = RandomUtil.randowShortId()
  548. )
  549. {
  550. super();
  551. this.show = show;
  552. this.xver = xver;
  553. this.fingerprint = fingerprint;
  554. this.dest = dest;
  555. this.serverNames = serverNames instanceof Array ? serverNames.join(",") : serverNames;
  556. this.privateKey = privateKey;
  557. this.publicKey = RandomUtil.randomX25519PublicKey(this.privateKey);
  558. this.minClient = minClient;
  559. this.maxClient = maxClient;
  560. this.maxTimediff = maxTimediff;
  561. this.shortIds = shortIds instanceof Array ? shortIds.join(",") : shortIds;
  562. }
  563. static fromJson(json = {}) {
  564. return new RealityStreamSettings(
  565. json.show,
  566. json.xver,
  567. json.fingerprint,
  568. json.dest,
  569. json.serverNames,
  570. json.privateKey,
  571. json.publicKey,
  572. json.minClient,
  573. json.maxClient,
  574. json.maxTimediff,
  575. json.shortIds
  576. );
  577. }
  578. toJson() {
  579. return {
  580. show: this.show,
  581. xver: this.xver,
  582. fingerprint: this.fingerprint,
  583. dest: this.dest,
  584. serverNames: this.serverNames.split(/,|,|\s+/),
  585. privateKey: this.privateKey,
  586. publicKey: this.publicKey,
  587. minClient: this.minClient,
  588. maxClient: this.maxClient,
  589. maxTimediff: this.maxTimediff,
  590. shortIds: this.shortIds.split(/,|,|\s+/)
  591. };
  592. }
  593. }
  594. class StreamSettings extends XrayCommonClass {
  595. constructor(network='tcp',
  596. security='none',
  597. tlsSettings=new TlsStreamSettings(),
  598. realitySettings = new RealityStreamSettings(),
  599. tcpSettings=new TcpStreamSettings(),
  600. kcpSettings=new KcpStreamSettings(),
  601. wsSettings=new WsStreamSettings(),
  602. httpSettings=new HttpStreamSettings(),
  603. quicSettings=new QuicStreamSettings(),
  604. grpcSettings=new GrpcStreamSettings(),
  605. ) {
  606. super();
  607. this.network = network;
  608. this.security = security;
  609. this.tls = tlsSettings;
  610. this.reality = realitySettings;
  611. this.tcp = tcpSettings;
  612. this.kcp = kcpSettings;
  613. this.ws = wsSettings;
  614. this.http = httpSettings;
  615. this.quic = quicSettings;
  616. this.grpc = grpcSettings;
  617. }
  618. get isTls() {
  619. return this.security === 'tls';
  620. }
  621. set isTls(isTls) {
  622. if (isTls) {
  623. this.security = 'tls';
  624. } else {
  625. this.security = 'none';
  626. }
  627. }
  628. get isXTLS() {
  629. return this.security === "xtls";
  630. }
  631. set isXTLS(isXTLS) {
  632. if (isXTLS) {
  633. this.security = 'xtls';
  634. } else {
  635. this.security = 'none';
  636. }
  637. }
  638. //for Reality
  639. get isReality() {
  640. return this.security === "reality";
  641. }
  642. set isReality(isReality) {
  643. if (isReality) {
  644. this.security = "reality";
  645. } else {
  646. this.security = "none";
  647. }
  648. }
  649. static fromJson(json = {}) {
  650. let tls, reality;
  651. if (json.security === "xtls") {
  652. tls = TlsStreamSettings.fromJson(json.XTLSSettings);
  653. } else if (json.security === "tls") {
  654. tls = TlsStreamSettings.fromJson(json.tlsSettings);
  655. }
  656. if (json.security === "reality") {
  657. reality = RealityStreamSettings.fromJson(json.realitySettings)
  658. }
  659. return new StreamSettings(
  660. json.network,
  661. json.security,
  662. tls,
  663. reality,
  664. TcpStreamSettings.fromJson(json.tcpSettings),
  665. KcpStreamSettings.fromJson(json.kcpSettings),
  666. WsStreamSettings.fromJson(json.wsSettings),
  667. HttpStreamSettings.fromJson(json.httpSettings),
  668. QuicStreamSettings.fromJson(json.quicSettings),
  669. GrpcStreamSettings.fromJson(json.grpcSettings),
  670. );
  671. }
  672. toJson() {
  673. const network = this.network;
  674. return {
  675. network: network,
  676. security: this.security,
  677. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  678. XTLSSettings: this.isXTLS ? this.tls.toJson() : undefined,
  679. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  680. realitySettings: this.isReality ? this.reality.toJson() : undefined,
  681. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  682. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  683. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  684. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  685. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  686. };
  687. }
  688. }
  689. class Sniffing extends XrayCommonClass {
  690. constructor(enabled=true, destOverride=['http', 'tls']) {
  691. super();
  692. this.enabled = enabled;
  693. this.destOverride = destOverride;
  694. }
  695. static fromJson(json={}) {
  696. let destOverride = ObjectUtil.clone(json.destOverride);
  697. if (!ObjectUtil.isEmpty(destOverride) && !ObjectUtil.isArrEmpty(destOverride)) {
  698. if (ObjectUtil.isEmpty(destOverride[0])) {
  699. destOverride = ['http', 'tls'];
  700. }
  701. }
  702. return new Sniffing(
  703. !!json.enabled,
  704. destOverride,
  705. );
  706. }
  707. }
  708. class Inbound extends XrayCommonClass {
  709. constructor(port=RandomUtil.randomIntRange(10000, 60000),
  710. listen='',
  711. protocol=Protocols.VLESS,
  712. settings=null,
  713. streamSettings=new StreamSettings(),
  714. tag='',
  715. sniffing=new Sniffing(),
  716. clientStats='',
  717. ) {
  718. super();
  719. this.port = port;
  720. this.listen = listen;
  721. this._protocol = protocol;
  722. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  723. this.stream = streamSettings;
  724. this.tag = tag;
  725. this.sniffing = sniffing;
  726. this.clientStats = clientStats;
  727. }
  728. getClientStats() {
  729. return this.clientStats;
  730. }
  731. get protocol() {
  732. return this._protocol;
  733. }
  734. set protocol(protocol) {
  735. this._protocol = protocol;
  736. this.settings = Inbound.Settings.getSettings(protocol);
  737. if (protocol === Protocols.TROJAN) {
  738. this.tls = true;
  739. }
  740. }
  741. get tls() {
  742. return this.stream.security === 'tls';
  743. }
  744. set tls(isTls) {
  745. if (isTls) {
  746. this.xtls = false;
  747. this.reality = false;
  748. this.stream.security = 'tls';
  749. } else {
  750. this.stream.security = 'none';
  751. }
  752. }
  753. get XTLS() {
  754. return this.stream.security === 'xtls';
  755. }
  756. set XTLS(isXTLS) {
  757. if (isXTLS) {
  758. this.xtls = false;
  759. this.reality = false;
  760. this.stream.security = 'xtls';
  761. } else {
  762. this.stream.security = 'none';
  763. }
  764. }
  765. //for Reality
  766. get reality() {
  767. if (this.stream.security === "reality") {
  768. return this.network === "tcp" || this.network === "grpc" || this.network === "http";
  769. }
  770. return false;
  771. }
  772. set reality(isReality) {
  773. if (isReality) {
  774. this.tls = false;
  775. this.xtls = false;
  776. this.stream.security = "reality";
  777. } else {
  778. this.stream.security = "none";
  779. }
  780. }
  781. get network() {
  782. return this.stream.network;
  783. }
  784. set network(network) {
  785. this.stream.network = network;
  786. }
  787. get isTcp() {
  788. return this.network === "tcp";
  789. }
  790. get isWs() {
  791. return this.network === "ws";
  792. }
  793. get isKcp() {
  794. return this.network === "kcp";
  795. }
  796. get isQuic() {
  797. return this.network === "quic"
  798. }
  799. get isGrpc() {
  800. return this.network === "grpc";
  801. }
  802. get isH2() {
  803. return this.network === "http";
  804. }
  805. // VMess & VLess
  806. get uuid() {
  807. switch (this.protocol) {
  808. case Protocols.VMESS:
  809. return this.settings.vmesses[0].id;
  810. case Protocols.VLESS:
  811. return this.settings.vlesses[0].id;
  812. default:
  813. return "";
  814. }
  815. }
  816. // VLess & Trojan
  817. get flow() {
  818. switch (this.protocol) {
  819. case Protocols.VLESS:
  820. return this.settings.vlesses[0].flow;
  821. case Protocols.TROJAN:
  822. return this.settings.trojans[0].flow;
  823. default:
  824. return "";
  825. }
  826. }
  827. // VMess
  828. get alterId() {
  829. switch (this.protocol) {
  830. case Protocols.VMESS:
  831. return this.settings.vmesses[0].alterId;
  832. default:
  833. return "";
  834. }
  835. }
  836. // Socks & HTTP
  837. get username() {
  838. switch (this.protocol) {
  839. case Protocols.SOCKS:
  840. case Protocols.HTTP:
  841. return this.settings.accounts[0].user;
  842. default:
  843. return "";
  844. }
  845. }
  846. // Trojan & Shadowsocks & Socks & HTTP
  847. get password() {
  848. switch (this.protocol) {
  849. case Protocols.TROJAN:
  850. return this.settings.trojans[0].password;
  851. case Protocols.SHADOWSOCKS:
  852. return this.settings.password;
  853. case Protocols.SOCKS:
  854. case Protocols.HTTP:
  855. return this.settings.accounts[0].pass;
  856. default:
  857. return "";
  858. }
  859. }
  860. // Shadowsocks
  861. get method() {
  862. switch (this.protocol) {
  863. case Protocols.SHADOWSOCKS:
  864. return this.settings.method;
  865. default:
  866. return "";
  867. }
  868. }
  869. get serverName() {
  870. if (this.stream.isTls || this.stream.isXTLS) {
  871. return this.stream.tls.server;
  872. }
  873. return "";
  874. }
  875. get host() {
  876. if (this.isTcp) {
  877. return this.stream.tcp.request.getHeader("Host");
  878. } else if (this.isWs) {
  879. return this.stream.ws.getHeader("Host");
  880. } else if (this.isH2) {
  881. return this.stream.http.host[0];
  882. }
  883. return null;
  884. }
  885. get path() {
  886. if (this.isTcp) {
  887. return this.stream.tcp.request.path[0];
  888. } else if (this.isWs) {
  889. return this.stream.ws.path;
  890. } else if (this.isH2) {
  891. return this.stream.http.path[0];
  892. }
  893. return null;
  894. }
  895. get quicSecurity() {
  896. return this.stream.quic.security;
  897. }
  898. get quicKey() {
  899. return this.stream.quic.key;
  900. }
  901. get quicType() {
  902. return this.stream.quic.type;
  903. }
  904. get kcpType() {
  905. return this.stream.kcp.type;
  906. }
  907. get kcpSeed() {
  908. return this.stream.kcp.seed;
  909. }
  910. get serviceName() {
  911. return this.stream.grpc.serviceName;
  912. }
  913. isExpiry(index) {
  914. switch (this.protocol) {
  915. case Protocols.VMESS:
  916. if(this.settings.vmesses[index].expiryTime > 0)
  917. return this.settings.vmesses[index].expiryTime < new Date().getTime();
  918. return false
  919. case Protocols.VLESS:
  920. if(this.settings.vlesses[index].expiryTime > 0)
  921. return this.settings.vlesses[index].expiryTime < new Date().getTime();
  922. return false
  923. case Protocols.TROJAN:
  924. if(this.settings.trojans[index].expiryTime > 0)
  925. return this.settings.trojans[index].expiryTime < new Date().getTime();
  926. return false
  927. default:
  928. return false;
  929. }
  930. }
  931. canEnableTls() {
  932. switch (this.protocol) {
  933. case Protocols.VMESS:
  934. case Protocols.VLESS:
  935. case Protocols.TROJAN:
  936. break;
  937. default:
  938. return false;
  939. }
  940. switch (this.network) {
  941. case "tcp":
  942. case "ws":
  943. case "http":
  944. case "quic":
  945. case "grpc":
  946. return true;
  947. default:
  948. return false;
  949. }
  950. }
  951. canEnableReality() {
  952. switch (this.protocol) {
  953. case Protocols.VLESS:
  954. case Protocols.TROJAN:
  955. break;
  956. default:
  957. return false;
  958. }
  959. return this.network === "tcp" || this.network === "grpc" || this.network === "http";
  960. }
  961. //this is used for xtls-rprx-vision
  962. canEnableTlsFlow() {
  963. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  964. switch (this.protocol) {
  965. case Protocols.VLESS:
  966. return true;
  967. default:
  968. return false;
  969. }
  970. }
  971. return false;
  972. }
  973. canSetTls() {
  974. return this.canEnableTls();
  975. }
  976. canEnableXTLS() {
  977. switch (this.protocol) {
  978. case Protocols.VLESS:
  979. case Protocols.TROJAN:
  980. break;
  981. default:
  982. return false;
  983. }
  984. return this.network === "tcp";
  985. }
  986. canEnableStream() {
  987. switch (this.protocol) {
  988. case Protocols.VMESS:
  989. case Protocols.VLESS:
  990. case Protocols.TROJAN:
  991. case Protocols.SHADOWSOCKS:
  992. return true;
  993. default:
  994. return false;
  995. }
  996. }
  997. canSniffing() {
  998. switch (this.protocol) {
  999. case Protocols.VMESS:
  1000. case Protocols.VLESS:
  1001. case Protocols.TROJAN:
  1002. case Protocols.SHADOWSOCKS:
  1003. return true;
  1004. default:
  1005. return false;
  1006. }
  1007. }
  1008. reset() {
  1009. this.port = RandomUtil.randomIntRange(10000, 60000);
  1010. this.listen = '';
  1011. this.protocol = Protocols.VMESS;
  1012. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1013. this.stream = new StreamSettings();
  1014. this.tag = '';
  1015. this.sniffing = new Sniffing();
  1016. }
  1017. genVmessLink(address='', remark='', clientIndex=0) {
  1018. if (this.protocol !== Protocols.VMESS) {
  1019. return '';
  1020. }
  1021. let network = this.stream.network;
  1022. let type = 'none';
  1023. let host = '';
  1024. let path = '';
  1025. if (network === 'tcp') {
  1026. let tcp = this.stream.tcp;
  1027. type = tcp.type;
  1028. if (type === 'http') {
  1029. let request = tcp.request;
  1030. path = request.path.join(',');
  1031. let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1032. if (index >= 0) {
  1033. host = request.headers[index].value;
  1034. }
  1035. }
  1036. } else if (network === 'kcp') {
  1037. let kcp = this.stream.kcp;
  1038. type = kcp.type;
  1039. path = kcp.seed;
  1040. } else if (network === 'ws') {
  1041. let ws = this.stream.ws;
  1042. path = ws.path;
  1043. let index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1044. if (index >= 0) {
  1045. host = ws.headers[index].value;
  1046. }
  1047. } else if (network === 'http') {
  1048. network = 'h2';
  1049. path = this.stream.http.path;
  1050. host = this.stream.http.host.join(',');
  1051. } else if (network === 'quic') {
  1052. type = this.stream.quic.type;
  1053. host = this.stream.quic.security;
  1054. path = this.stream.quic.key;
  1055. } else if (network === 'grpc') {
  1056. path = this.stream.grpc.serviceName;
  1057. }
  1058. if (this.stream.security === 'tls') {
  1059. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1060. address = this.stream.tls.server;
  1061. }
  1062. }
  1063. let obj = {
  1064. v: '2',
  1065. ps: remark,
  1066. add: address,
  1067. port: this.port,
  1068. id: this.settings.vmesses[clientIndex].id,
  1069. aid: this.settings.vmesses[clientIndex].alterId,
  1070. net: network,
  1071. type: type,
  1072. host: host,
  1073. path: path,
  1074. tls: this.stream.security,
  1075. sni: this.stream.tls.settings[0]['serverName'],
  1076. fp: this.stream.tls.settings[0]['fingerprint'],
  1077. alpn: this.stream.tls.alpn.join(','),
  1078. allowInsecure: this.stream.tls.settings[0].allowInsecure,
  1079. };
  1080. return 'vmess://' + base64(JSON.stringify(obj, null, 2));
  1081. }
  1082. genVLESSLink(address = '', remark='', clientIndex=0) {
  1083. const settings = this.settings;
  1084. const uuid = settings.vlesses[clientIndex].id;
  1085. const port = this.port;
  1086. const type = this.stream.network;
  1087. const params = new Map();
  1088. params.set("type", this.stream.network);
  1089. switch (type) {
  1090. case "tcp":
  1091. const tcp = this.stream.tcp;
  1092. if (tcp.type === 'http') {
  1093. const request = tcp.request;
  1094. params.set("path", request.path.join(','));
  1095. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1096. if (index >= 0) {
  1097. const host = request.headers[index].value;
  1098. params.set("host", host);
  1099. }
  1100. params.set("headerType", 'http');
  1101. }
  1102. break;
  1103. case "kcp":
  1104. const kcp = this.stream.kcp;
  1105. params.set("headerType", kcp.type);
  1106. params.set("seed", kcp.seed);
  1107. break;
  1108. case "ws":
  1109. const ws = this.stream.ws;
  1110. params.set("path", ws.path);
  1111. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1112. if (index >= 0) {
  1113. const host = ws.headers[index].value;
  1114. params.set("host", host);
  1115. }
  1116. break;
  1117. case "http":
  1118. const http = this.stream.http;
  1119. params.set("path", http.path);
  1120. params.set("host", http.host);
  1121. break;
  1122. case "quic":
  1123. const quic = this.stream.quic;
  1124. params.set("quicSecurity", quic.security);
  1125. params.set("key", quic.key);
  1126. params.set("headerType", quic.type);
  1127. break;
  1128. case "grpc":
  1129. const grpc = this.stream.grpc;
  1130. params.set("serviceName", grpc.serviceName);
  1131. break;
  1132. }
  1133. if (this.tls) {
  1134. params.set("security", "tls");
  1135. params.set("fp" , this.stream.tls.settings[0]['fingerprint']);
  1136. params.set("alpn", this.stream.tls.alpn);
  1137. if(this.stream.tls.settings[0].allowInsecure){
  1138. params.set("allowInsecure", "1");
  1139. }
  1140. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1141. address = this.stream.tls.server;
  1142. }
  1143. if (this.stream.tls.settings[0]['serverName'] !== ''){
  1144. params.set("sni", this.stream.tls.settings[0]['serverName']);
  1145. }
  1146. if (type === "tcp" && this.settings.vlesses[clientIndex].flow.length > 0) {
  1147. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1148. }
  1149. }
  1150. if (this.XTLS) {
  1151. params.set("security", "xtls");
  1152. params.set("alpn", this.stream.tls.alpn);
  1153. if(this.stream.tls.settings[0].allowInsecure){
  1154. params.set("allowInsecure", "1");
  1155. }
  1156. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1157. address = this.stream.tls.server;
  1158. }
  1159. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1160. }
  1161. if (this.reality) {
  1162. params.set("security", "reality");
  1163. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1164. params.set("sni", this.stream.reality.serverNames.split(/,|,|\s+/)[0]);
  1165. }
  1166. if (this.stream.reality.publicKey != "") {
  1167. //params.set("pbk", Ed25519.getPublicKey(this.stream.reality.privateKey));
  1168. params.set("pbk", this.stream.reality.publicKey);
  1169. }
  1170. if (this.stream.network === 'tcp') {
  1171. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1172. }
  1173. if (this.stream.reality.shortIds != "") {
  1174. params.set("sid", this.stream.reality.shortIds);
  1175. }
  1176. if (this.stream.reality.fingerprint != "") {
  1177. params.set("fp", this.stream.reality.fingerprint);
  1178. }
  1179. }
  1180. const link = `vless://${uuid}@${address}:${port}`;
  1181. const url = new URL(link);
  1182. for (const [key, value] of params) {
  1183. url.searchParams.set(key, value)
  1184. }
  1185. url.hash = encodeURIComponent(remark);
  1186. return url.toString();
  1187. }
  1188. genSSLink(address='', remark='') {
  1189. let settings = this.settings;
  1190. const server = this.stream.tls.server;
  1191. if (!ObjectUtil.isEmpty(server)) {
  1192. address = server;
  1193. }
  1194. return 'ss://' + safeBase64(settings.method + ':' + settings.password) + `@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1195. }
  1196. genTrojanLink(address = '', remark = '', clientIndex = 0) {
  1197. let settings = this.settings;
  1198. const port = this.port;
  1199. const type = this.stream.network;
  1200. const params = new Map();
  1201. params.set("type", this.stream.network);
  1202. switch (type) {
  1203. case "tcp":
  1204. const tcp = this.stream.tcp;
  1205. if (tcp.type === 'http') {
  1206. const request = tcp.request;
  1207. params.set("path", request.path.join(','));
  1208. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1209. if (index >= 0) {
  1210. const host = request.headers[index].value;
  1211. params.set("host", host);
  1212. }
  1213. params.set("headerType", 'http');
  1214. }
  1215. break;
  1216. case "kcp":
  1217. const kcp = this.stream.kcp;
  1218. params.set("headerType", kcp.type);
  1219. params.set("seed", kcp.seed);
  1220. break;
  1221. case "ws":
  1222. const ws = this.stream.ws;
  1223. params.set("path", ws.path);
  1224. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1225. if (index >= 0) {
  1226. const host = ws.headers[index].value;
  1227. params.set("host", host);
  1228. }
  1229. break;
  1230. case "http":
  1231. const http = this.stream.http;
  1232. params.set("path", http.path);
  1233. params.set("host", http.host);
  1234. break;
  1235. case "quic":
  1236. const quic = this.stream.quic;
  1237. params.set("quicSecurity", quic.security);
  1238. params.set("key", quic.key);
  1239. params.set("headerType", quic.type);
  1240. break;
  1241. case "grpc":
  1242. const grpc = this.stream.grpc;
  1243. params.set("serviceName", grpc.serviceName);
  1244. break;
  1245. }
  1246. if (this.tls) {
  1247. params.set("security", "tls");
  1248. params.set("fp" , this.stream.tls.settings[0]['fingerprint']);
  1249. params.set("alpn", this.stream.tls.alpn);
  1250. if(this.stream.tls.settings[0].allowInsecure){
  1251. params.set("allowInsecure", "1");
  1252. }
  1253. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1254. address = this.stream.tls.server;
  1255. }
  1256. if (this.stream.tls.settings[0]['serverName'] !== ''){
  1257. params.set("sni", this.stream.tls.settings[0]['serverName']);
  1258. }
  1259. }
  1260. if (this.reality) {
  1261. params.set("security", "reality");
  1262. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1263. params.set("sni", this.stream.reality.serverNames.split(/,|,|\s+/)[0]);
  1264. }
  1265. if (this.stream.reality.publicKey != "") {
  1266. //params.set("pbk", Ed25519.getPublicKey(this.stream.reality.privateKey));
  1267. params.set("pbk", this.stream.reality.publicKey);
  1268. }
  1269. if (this.stream.network === 'tcp') {
  1270. params.set("flow", this.settings.trojans[clientIndex].flow);
  1271. }
  1272. if (this.stream.reality.shortIds != "") {
  1273. params.set("sid", this.stream.reality.shortIds);
  1274. }
  1275. if (this.stream.reality.fingerprint != "") {
  1276. params.set("fp", this.stream.reality.fingerprint);
  1277. }
  1278. }
  1279. if (this.XTLS) {
  1280. params.set("security", "xtls");
  1281. params.set("alpn", this.stream.tls.alpn);
  1282. if(this.stream.tls.settings[0].allowInsecure){
  1283. params.set("allowInsecure", "1");
  1284. }
  1285. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1286. address = this.stream.tls.server;
  1287. }
  1288. params.set("flow", this.settings.trojans[clientIndex].flow);
  1289. }
  1290. const link = `trojan://${settings.trojans[clientIndex].password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1291. const url = new URL(link);
  1292. for (const [key, value] of params) {
  1293. url.searchParams.set(key, value)
  1294. }
  1295. url.hash = encodeURIComponent(remark);
  1296. return url.toString();
  1297. }
  1298. genLink(address='', remark='', clientIndex=0) {
  1299. switch (this.protocol) {
  1300. case Protocols.VMESS:
  1301. if (this.settings.vmesses[clientIndex].email != ""){
  1302. remark += '-' + this.settings.vmesses[clientIndex].email
  1303. }
  1304. return this.genVmessLink(address, remark, clientIndex);
  1305. case Protocols.VLESS:
  1306. if (this.settings.vlesses[clientIndex].email != ""){
  1307. remark += '-' + this.settings.vlesses[clientIndex].email
  1308. }
  1309. return this.genVLESSLink(address, remark, clientIndex);
  1310. case Protocols.SHADOWSOCKS: return this.genSSLink(address, remark);
  1311. case Protocols.TROJAN:
  1312. if (this.settings.trojans[clientIndex].email != ""){
  1313. remark += '-' + this.settings.trojans[clientIndex].email
  1314. }
  1315. return this.genTrojanLink(address, remark, clientIndex);
  1316. default: return '';
  1317. }
  1318. }
  1319. genInboundLinks(address = '', remark = '') {
  1320. let link = '';
  1321. switch (this.protocol) {
  1322. case Protocols.VMESS:
  1323. case Protocols.VLESS:
  1324. case Protocols.TROJAN:
  1325. JSON.parse(this.settings).clients.forEach((_,index) => {
  1326. link += this.genLink(address, remark, index) + '\r\n';
  1327. });
  1328. return link;
  1329. case Protocols.SHADOWSOCKS:
  1330. return (this.genSSLink(address, remark) + '\r\n');
  1331. default: return '';
  1332. }
  1333. }
  1334. static fromJson(json={}) {
  1335. return new Inbound(
  1336. json.port,
  1337. json.listen,
  1338. json.protocol,
  1339. Inbound.Settings.fromJson(json.protocol, json.settings),
  1340. StreamSettings.fromJson(json.streamSettings),
  1341. json.tag,
  1342. Sniffing.fromJson(json.sniffing),
  1343. json.clientStats
  1344. )
  1345. }
  1346. toJson() {
  1347. let streamSettings;
  1348. if (this.canEnableStream() || this.protocol === Protocols.TROJAN) {
  1349. streamSettings = this.stream.toJson();
  1350. }
  1351. return {
  1352. port: this.port,
  1353. listen: this.listen,
  1354. protocol: this.protocol,
  1355. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1356. streamSettings: streamSettings,
  1357. tag: this.tag,
  1358. sniffing: this.sniffing.toJson(),
  1359. clientStats: this.clientStats
  1360. };
  1361. }
  1362. }
  1363. Inbound.Settings = class extends XrayCommonClass {
  1364. constructor(protocol) {
  1365. super();
  1366. this.protocol = protocol;
  1367. }
  1368. static getSettings(protocol) {
  1369. switch (protocol) {
  1370. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1371. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1372. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1373. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1374. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1375. case Protocols.MTPROTO: return new Inbound.MtprotoSettings(protocol);
  1376. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1377. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1378. default: return null;
  1379. }
  1380. }
  1381. static fromJson(protocol, json) {
  1382. switch (protocol) {
  1383. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1384. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1385. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1386. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1387. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1388. case Protocols.MTPROTO: return Inbound.MtprotoSettings.fromJson(json);
  1389. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1390. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1391. default: return null;
  1392. }
  1393. }
  1394. toJson() {
  1395. return {};
  1396. }
  1397. };
  1398. Inbound.VmessSettings = class extends Inbound.Settings {
  1399. constructor(protocol,
  1400. vmesses=[new Inbound.VmessSettings.Vmess()],
  1401. disableInsecureEncryption=false) {
  1402. super(protocol);
  1403. this.vmesses = vmesses;
  1404. this.disableInsecure = disableInsecureEncryption;
  1405. }
  1406. indexOfVmessById(id) {
  1407. return this.vmesses.findIndex(vmess => vmess.id === id);
  1408. }
  1409. addVmess(vmess) {
  1410. if (this.indexOfVmessById(vmess.id) >= 0) {
  1411. return false;
  1412. }
  1413. this.vmesses.push(vmess);
  1414. }
  1415. delVmess(vmess) {
  1416. const i = this.indexOfVmessById(vmess.id);
  1417. if (i >= 0) {
  1418. this.vmesses.splice(i, 1);
  1419. }
  1420. }
  1421. static fromJson(json={}) {
  1422. return new Inbound.VmessSettings(
  1423. Protocols.VMESS,
  1424. json.clients.map(client => Inbound.VmessSettings.Vmess.fromJson(client)),
  1425. ObjectUtil.isEmpty(json.disableInsecureEncryption) ? false : json.disableInsecureEncryption,
  1426. );
  1427. }
  1428. toJson() {
  1429. return {
  1430. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1431. disableInsecureEncryption: this.disableInsecure,
  1432. };
  1433. }
  1434. };
  1435. Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
  1436. constructor(id=RandomUtil.randomUUID(), alterId=0, email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId='') {
  1437. super();
  1438. this.id = id;
  1439. this.alterId = alterId;
  1440. this.email = email;
  1441. this.limitIp = limitIp;
  1442. this.totalGB = totalGB;
  1443. this.expiryTime = expiryTime;
  1444. this.enable = enable;
  1445. this.tgId = tgId;
  1446. this.subId = subId;
  1447. }
  1448. static fromJson(json={}) {
  1449. return new Inbound.VmessSettings.Vmess(
  1450. json.id,
  1451. json.alterId,
  1452. json.email,
  1453. json.limitIp,
  1454. json.totalGB,
  1455. json.expiryTime,
  1456. json.enable,
  1457. json.tgId,
  1458. json.subId,
  1459. );
  1460. }
  1461. get _expiryTime() {
  1462. if (this.expiryTime === 0 || this.expiryTime === "") {
  1463. return null;
  1464. }
  1465. if (this.expiryTime < 0){
  1466. return this.expiryTime / -86400000;
  1467. }
  1468. return moment(this.expiryTime);
  1469. }
  1470. set _expiryTime(t) {
  1471. if (t == null || t === "") {
  1472. this.expiryTime = 0;
  1473. } else {
  1474. this.expiryTime = t.valueOf();
  1475. }
  1476. }
  1477. get _totalGB() {
  1478. return toFixed(this.totalGB / ONE_GB, 2);
  1479. }
  1480. set _totalGB(gb) {
  1481. this.totalGB = toFixed(gb * ONE_GB, 0);
  1482. }
  1483. };
  1484. Inbound.VLESSSettings = class extends Inbound.Settings {
  1485. constructor(protocol,
  1486. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1487. decryption='none',
  1488. fallbacks=[],) {
  1489. super(protocol);
  1490. this.vlesses = vlesses;
  1491. this.decryption = 'none'; // Using decryption is not implemented here
  1492. this.fallbacks = fallbacks;
  1493. }
  1494. addFallback() {
  1495. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1496. }
  1497. delFallback(index) {
  1498. this.fallbacks.splice(index, 1);
  1499. }
  1500. // decryption should be set to static value
  1501. static fromJson(json={}) {
  1502. return new Inbound.VLESSSettings(
  1503. Protocols.VLESS,
  1504. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1505. 'none',
  1506. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1507. );
  1508. }
  1509. toJson() {
  1510. return {
  1511. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1512. decryption: 'none',
  1513. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1514. };
  1515. }
  1516. };
  1517. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1518. constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId='') {
  1519. super();
  1520. this.id = id;
  1521. this.flow = flow;
  1522. this.email = email;
  1523. this.limitIp = limitIp;
  1524. this.totalGB = totalGB;
  1525. this.expiryTime = expiryTime;
  1526. this.enable = enable;
  1527. this.tgId = tgId;
  1528. this.subId = subId;
  1529. }
  1530. static fromJson(json={}) {
  1531. return new Inbound.VLESSSettings.VLESS(
  1532. json.id,
  1533. json.flow,
  1534. json.email,
  1535. json.limitIp,
  1536. json.totalGB,
  1537. json.expiryTime,
  1538. json.enable,
  1539. json.tgId,
  1540. json.subId,
  1541. );
  1542. }
  1543. get _expiryTime() {
  1544. if (this.expiryTime === 0 || this.expiryTime === "") {
  1545. return null;
  1546. }
  1547. if (this.expiryTime < 0){
  1548. return this.expiryTime / -86400000;
  1549. }
  1550. return moment(this.expiryTime);
  1551. }
  1552. set _expiryTime(t) {
  1553. if (t == null || t === "") {
  1554. this.expiryTime = 0;
  1555. } else {
  1556. this.expiryTime = t.valueOf();
  1557. }
  1558. }
  1559. get _totalGB() {
  1560. return toFixed(this.totalGB / ONE_GB, 2);
  1561. }
  1562. set _totalGB(gb) {
  1563. this.totalGB = toFixed(gb * ONE_GB, 0);
  1564. }
  1565. };
  1566. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1567. constructor(name="", alpn='', path='', dest='', xver=0) {
  1568. super();
  1569. this.name = name;
  1570. this.alpn = alpn;
  1571. this.path = path;
  1572. this.dest = dest;
  1573. this.xver = xver;
  1574. }
  1575. toJson() {
  1576. let xver = this.xver;
  1577. if (!Number.isInteger(xver)) {
  1578. xver = 0;
  1579. }
  1580. return {
  1581. name: this.name,
  1582. alpn: this.alpn,
  1583. path: this.path,
  1584. dest: this.dest,
  1585. xver: xver,
  1586. }
  1587. }
  1588. static fromJson(json=[]) {
  1589. const fallbacks = [];
  1590. for (let fallback of json) {
  1591. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1592. fallback.name,
  1593. fallback.alpn,
  1594. fallback.path,
  1595. fallback.dest,
  1596. fallback.xver,
  1597. ))
  1598. }
  1599. return fallbacks;
  1600. }
  1601. };
  1602. Inbound.TrojanSettings = class extends Inbound.Settings {
  1603. constructor(protocol,
  1604. trojans=[new Inbound.TrojanSettings.Trojan()],
  1605. fallbacks=[],) {
  1606. super(protocol);
  1607. this.trojans = trojans;
  1608. this.fallbacks = fallbacks;
  1609. }
  1610. addTrojanFallback() {
  1611. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1612. }
  1613. delTrojanFallback(index) {
  1614. this.fallbacks.splice(index, 1);
  1615. }
  1616. static fromJson(json={}) {
  1617. return new Inbound.TrojanSettings(
  1618. Protocols.TROJAN,
  1619. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1620. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1621. }
  1622. toJson() {
  1623. return {
  1624. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1625. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
  1626. };
  1627. }
  1628. };
  1629. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1630. constructor(password=RandomUtil.randomSeq(10), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId='') {
  1631. super();
  1632. this.password = password;
  1633. this.flow = flow;
  1634. this.email = email;
  1635. this.limitIp = limitIp;
  1636. this.totalGB = totalGB;
  1637. this.expiryTime = expiryTime;
  1638. this.enable = enable;
  1639. this.tgId = tgId;
  1640. this.subId = subId;
  1641. }
  1642. toJson() {
  1643. return {
  1644. password: this.password,
  1645. flow: this.flow,
  1646. email: this.email,
  1647. limitIp: this.limitIp,
  1648. totalGB: this.totalGB,
  1649. expiryTime: this.expiryTime,
  1650. enable: this.enable,
  1651. tgId: this.tgId,
  1652. subId: this.subId,
  1653. };
  1654. }
  1655. static fromJson(json = {}) {
  1656. return new Inbound.TrojanSettings.Trojan(
  1657. json.password,
  1658. json.flow,
  1659. json.email,
  1660. json.limitIp,
  1661. json.totalGB,
  1662. json.expiryTime,
  1663. json.enable,
  1664. json.tgId,
  1665. json.subId,
  1666. );
  1667. }
  1668. get _expiryTime() {
  1669. if (this.expiryTime === 0 || this.expiryTime === "") {
  1670. return null;
  1671. }
  1672. if (this.expiryTime < 0){
  1673. return this.expiryTime / -86400000;
  1674. }
  1675. return moment(this.expiryTime);
  1676. }
  1677. set _expiryTime(t) {
  1678. if (t == null || t === "") {
  1679. this.expiryTime = 0;
  1680. } else {
  1681. this.expiryTime = t.valueOf();
  1682. }
  1683. }
  1684. get _totalGB() {
  1685. return toFixed(this.totalGB / ONE_GB, 2);
  1686. }
  1687. set _totalGB(gb) {
  1688. this.totalGB = toFixed(gb * ONE_GB, 0);
  1689. }
  1690. };
  1691. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1692. constructor(name="", alpn='', path='', dest='', xver=0) {
  1693. super();
  1694. this.name = name;
  1695. this.alpn = alpn;
  1696. this.path = path;
  1697. this.dest = dest;
  1698. this.xver = xver;
  1699. }
  1700. toJson() {
  1701. let xver = this.xver;
  1702. if (!Number.isInteger(xver)) {
  1703. xver = 0;
  1704. }
  1705. return {
  1706. name: this.name,
  1707. alpn: this.alpn,
  1708. path: this.path,
  1709. dest: this.dest,
  1710. xver: xver,
  1711. }
  1712. }
  1713. static fromJson(json=[]) {
  1714. const fallbacks = [];
  1715. for (let fallback of json) {
  1716. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1717. fallback.name,
  1718. fallback.alpn,
  1719. fallback.path,
  1720. fallback.dest,
  1721. fallback.xver,
  1722. ))
  1723. }
  1724. return fallbacks;
  1725. }
  1726. };
  1727. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1728. constructor(protocol,
  1729. method=SSMethods.BLAKE3_AES_256_GCM,
  1730. password=RandomUtil.randomSeq(44),
  1731. network='tcp,udp'
  1732. ) {
  1733. super(protocol);
  1734. this.method = method;
  1735. this.password = password;
  1736. this.network = network;
  1737. }
  1738. static fromJson(json={}) {
  1739. return new Inbound.ShadowsocksSettings(
  1740. Protocols.SHADOWSOCKS,
  1741. json.method,
  1742. json.password,
  1743. json.network,
  1744. );
  1745. }
  1746. toJson() {
  1747. return {
  1748. method: this.method,
  1749. password: this.password,
  1750. network: this.network,
  1751. };
  1752. }
  1753. };
  1754. Inbound.DokodemoSettings = class extends Inbound.Settings {
  1755. constructor(protocol, address, port, network='tcp,udp', followRedirect=false) {
  1756. super(protocol);
  1757. this.address = address;
  1758. this.port = port;
  1759. this.network = network;
  1760. this.followRedirect = followRedirect;
  1761. }
  1762. static fromJson(json={}) {
  1763. return new Inbound.DokodemoSettings(
  1764. Protocols.DOKODEMO,
  1765. json.address,
  1766. json.port,
  1767. json.network,
  1768. json.followRedirect,
  1769. );
  1770. }
  1771. toJson() {
  1772. return {
  1773. address: this.address,
  1774. port: this.port,
  1775. network: this.network,
  1776. followRedirect: this.followRedirect,
  1777. };
  1778. }
  1779. };
  1780. Inbound.MtprotoSettings = class extends Inbound.Settings {
  1781. constructor(protocol, users=[new Inbound.MtprotoSettings.MtUser()]) {
  1782. super(protocol);
  1783. this.users = users;
  1784. }
  1785. static fromJson(json={}) {
  1786. return new Inbound.MtprotoSettings(
  1787. Protocols.MTPROTO,
  1788. json.users.map(user => Inbound.MtprotoSettings.MtUser.fromJson(user)),
  1789. );
  1790. }
  1791. toJson() {
  1792. return {
  1793. users: XrayCommonClass.toJsonArray(this.users),
  1794. };
  1795. }
  1796. };
  1797. Inbound.MtprotoSettings.MtUser = class extends XrayCommonClass {
  1798. constructor(secret=RandomUtil.randomMTSecret()) {
  1799. super();
  1800. this.secret = secret;
  1801. }
  1802. static fromJson(json={}) {
  1803. return new Inbound.MtprotoSettings.MtUser(json.secret);
  1804. }
  1805. };
  1806. Inbound.SocksSettings = class extends Inbound.Settings {
  1807. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  1808. super(protocol);
  1809. this.auth = auth;
  1810. this.accounts = accounts;
  1811. this.udp = udp;
  1812. this.ip = ip;
  1813. }
  1814. addAccount(account) {
  1815. this.accounts.push(account);
  1816. }
  1817. delAccount(index) {
  1818. this.accounts.splice(index, 1);
  1819. }
  1820. static fromJson(json={}) {
  1821. let accounts;
  1822. if (json.auth === 'password') {
  1823. accounts = json.accounts.map(
  1824. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  1825. )
  1826. }
  1827. return new Inbound.SocksSettings(
  1828. Protocols.SOCKS,
  1829. json.auth,
  1830. accounts,
  1831. json.udp,
  1832. json.ip,
  1833. );
  1834. }
  1835. toJson() {
  1836. return {
  1837. auth: this.auth,
  1838. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  1839. udp: this.udp,
  1840. ip: this.ip,
  1841. };
  1842. }
  1843. };
  1844. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  1845. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1846. super();
  1847. this.user = user;
  1848. this.pass = pass;
  1849. }
  1850. static fromJson(json={}) {
  1851. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  1852. }
  1853. };
  1854. Inbound.HttpSettings = class extends Inbound.Settings {
  1855. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  1856. super(protocol);
  1857. this.accounts = accounts;
  1858. }
  1859. addAccount(account) {
  1860. this.accounts.push(account);
  1861. }
  1862. delAccount(index) {
  1863. this.accounts.splice(index, 1);
  1864. }
  1865. static fromJson(json={}) {
  1866. return new Inbound.HttpSettings(
  1867. Protocols.HTTP,
  1868. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  1869. );
  1870. }
  1871. toJson() {
  1872. return {
  1873. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  1874. };
  1875. }
  1876. };
  1877. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  1878. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1879. super();
  1880. this.user = user;
  1881. this.pass = pass;
  1882. }
  1883. static fromJson(json={}) {
  1884. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  1885. }
  1886. };