xray.js 55 KB

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