xray.js 64 KB

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