xray.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. const Protocols = {
  2. VMESS: 'vmess',
  3. VLESS: 'vless',
  4. TROJAN: 'trojan',
  5. SHADOWSOCKS: 'shadowsocks',
  6. DOKODEMO: 'dokodemo-door',
  7. MTPROTO: 'mtproto',
  8. SOCKS: 'socks',
  9. HTTP: 'http',
  10. };
  11. const VmessMethods = {
  12. AES_128_GCM: 'aes-128-gcm',
  13. CHACHA20_POLY1305: 'chacha20-poly1305',
  14. AUTO: 'auto',
  15. NONE: 'none',
  16. };
  17. const SSMethods = {
  18. // AES_256_CFB: 'aes-256-cfb',
  19. // AES_128_CFB: 'aes-128-cfb',
  20. // CHACHA20: 'chacha20',
  21. // CHACHA20_IETF: 'chacha20-ietf',
  22. CHACHA20_POLY1305: 'chacha20-poly1305',
  23. AES_256_GCM: 'aes-256-gcm',
  24. AES_128_GCM: 'aes-128-gcm',
  25. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  26. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  27. BLAKE3_CHACHA20_POLY1305: '2022-blake3-chacha20-poly1305',
  28. };
  29. const RULE_IP = {
  30. PRIVATE: 'geoip:private',
  31. CN: 'geoip:cn',
  32. };
  33. const RULE_DOMAIN = {
  34. ADS: 'geosite:category-ads',
  35. ADS_ALL: 'geosite:category-ads-all',
  36. CN: 'geosite:cn',
  37. GOOGLE: 'geosite:google',
  38. FACEBOOK: 'geosite:facebook',
  39. SPEEDTEST: 'geosite:speedtest',
  40. };
  41. const XTLS_FLOW_CONTROL = {
  42. ORIGIN: "xtls-rprx-origin",
  43. DIRECT: "xtls-rprx-direct",
  44. };
  45. const TLS_FLOW_CONTROL = {
  46. VISION: "xtls-rprx-vision",
  47. };
  48. const TLS_VERSION_OPTION = {
  49. TLS10: "1.0",
  50. TLS11: "1.1",
  51. TLS12: "1.2",
  52. TLS13: "1.3",
  53. };
  54. const TLS_CIPHER_OPTION = {
  55. RSA_AES_128_CBC: "TLS_RSA_WITH_AES_128_CBC_SHA",
  56. RSA_AES_256_CBC: "TLS_RSA_WITH_AES_256_CBC_SHA",
  57. RSA_AES_128_GCM: "TLS_RSA_WITH_AES_128_GCM_SHA256",
  58. RSA_AES_256_GCM: "TLS_RSA_WITH_AES_256_GCM_SHA384",
  59. AES_128_GCM: "TLS_AES_128_GCM_SHA256",
  60. AES_256_GCM: "TLS_AES_256_GCM_SHA384",
  61. CHACHA20_POLY1305: "TLS_CHACHA20_POLY1305_SHA256",
  62. ECDHE_ECDSA_AES_128_CBC: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
  63. ECDHE_ECDSA_AES_256_CBC: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
  64. ECDHE_RSA_AES_128_CBC: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
  65. ECDHE_RSA_AES_256_CBC: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
  66. ECDHE_ECDSA_AES_128_GCM: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
  67. ECDHE_ECDSA_AES_256_GCM: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  68. ECDHE_RSA_AES_128_GCM: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  69. ECDHE_RSA_AES_256_GCM: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
  70. ECDHE_ECDSA_CHACHA20_POLY1305: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
  71. ECDHE_RSA_CHACHA20_POLY1305: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
  72. };
  73. const UTLS_FINGERPRINT = {
  74. UTLS_CHROME: "chrome",
  75. UTLS_FIREFOX: "firefox",
  76. UTLS_SAFARI: "safari",
  77. UTLS_IOS: "ios",
  78. UTLS_android: "android",
  79. UTLS_EDGE: "edge",
  80. UTLS_360: "360",
  81. UTLS_QQ: "qq",
  82. UTLS_RANDOM: "random",
  83. UTLS_RANDOMIZED: "randomized",
  84. };
  85. const ALPN_OPTION = {
  86. H2: "h2",
  87. HTTP1: "http/1.1",
  88. BOTH: "h2,http/1.1",
  89. };
  90. Object.freeze(Protocols);
  91. Object.freeze(VmessMethods);
  92. Object.freeze(SSMethods);
  93. Object.freeze(RULE_IP);
  94. Object.freeze(RULE_DOMAIN);
  95. Object.freeze(XTLS_FLOW_CONTROL);
  96. Object.freeze(TLS_FLOW_CONTROL);
  97. Object.freeze(TLS_VERSION_OPTION);
  98. Object.freeze(TLS_CIPHER_OPTION);
  99. Object.freeze(UTLS_FINGERPRINT);
  100. Object.freeze(ALPN_OPTION);
  101. class XrayCommonClass {
  102. static toJsonArray(arr) {
  103. return arr.map(obj => obj.toJson());
  104. }
  105. static fromJson() {
  106. return new XrayCommonClass();
  107. }
  108. toJson() {
  109. return this;
  110. }
  111. toString(format=true) {
  112. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  113. }
  114. static toHeaders(v2Headers) {
  115. let newHeaders = [];
  116. if (v2Headers) {
  117. Object.keys(v2Headers).forEach(key => {
  118. let values = v2Headers[key];
  119. if (typeof(values) === 'string') {
  120. newHeaders.push({ name: key, value: values });
  121. } else {
  122. for (let i = 0; i < values.length; ++i) {
  123. newHeaders.push({ name: key, value: values[i] });
  124. }
  125. }
  126. });
  127. }
  128. return newHeaders;
  129. }
  130. static toV2Headers(headers, arr=true) {
  131. let v2Headers = {};
  132. for (let i = 0; i < headers.length; ++i) {
  133. let name = headers[i].name;
  134. let value = headers[i].value;
  135. if (ObjectUtil.isEmpty(name) || ObjectUtil.isEmpty(value)) {
  136. continue;
  137. }
  138. if (!(name in v2Headers)) {
  139. v2Headers[name] = arr ? [value] : value;
  140. } else {
  141. if (arr) {
  142. v2Headers[name].push(value);
  143. } else {
  144. v2Headers[name] = value;
  145. }
  146. }
  147. }
  148. return v2Headers;
  149. }
  150. }
  151. class TcpStreamSettings extends XrayCommonClass {
  152. constructor(
  153. type = 'none',
  154. acceptProxyProtocol = false,
  155. request = new TcpStreamSettings.TcpRequest(),
  156. response = new TcpStreamSettings.TcpResponse(),
  157. ) {
  158. super();
  159. this.type = type;
  160. this.request = request;
  161. this.response = response;
  162. this.acceptProxyProtocol = acceptProxyProtocol;
  163. }
  164. static fromJson(json = {}) {
  165. let header = json.header;
  166. if (!header) {
  167. header = {};
  168. }
  169. return new TcpStreamSettings(
  170. header.type,
  171. json.acceptProxyProtocol,
  172. TcpStreamSettings.TcpRequest.fromJson(header.request),
  173. TcpStreamSettings.TcpResponse.fromJson(header.response),
  174. );
  175. }
  176. toJson() {
  177. return {
  178. header: {
  179. type: this.type,
  180. request: this.type === 'http' ? this.request.toJson() : undefined,
  181. response: this.type === 'http' ? this.response.toJson() : undefined,
  182. },
  183. acceptProxyProtocol: this.acceptProxyProtocol,
  184. };
  185. }
  186. }
  187. TcpStreamSettings.TcpRequest = class extends XrayCommonClass {
  188. constructor(version = '1.1',
  189. method = 'GET',
  190. path = ['/'],
  191. headers = [],
  192. ) {
  193. super();
  194. this.version = version;
  195. this.method = method;
  196. this.path = path.length === 0 ? ['/'] : path;
  197. this.headers = headers;
  198. }
  199. addPath(path) {
  200. this.path.push(path);
  201. }
  202. removePath(index) {
  203. this.path.splice(index, 1);
  204. }
  205. addHeader(name, value) {
  206. this.headers.push({ name: name, value: value });
  207. }
  208. getHeader(name) {
  209. for (const header of this.headers) {
  210. if (header.name.toLowerCase() === name.toLowerCase()) {
  211. return header.value;
  212. }
  213. }
  214. return null;
  215. }
  216. removeHeader(index) {
  217. this.headers.splice(index, 1);
  218. }
  219. static fromJson(json = {}) {
  220. return new TcpStreamSettings.TcpRequest(
  221. json.version,
  222. json.method,
  223. json.path,
  224. XrayCommonClass.toHeaders(json.headers),
  225. );
  226. }
  227. toJson() {
  228. return {
  229. method: this.method,
  230. path: ObjectUtil.clone(this.path),
  231. headers: XrayCommonClass.toV2Headers(this.headers),
  232. };
  233. }
  234. };
  235. TcpStreamSettings.TcpResponse = class extends XrayCommonClass {
  236. constructor(version = '1.1',
  237. status = '200',
  238. reason = 'OK',
  239. headers = [],
  240. ) {
  241. super();
  242. this.version = version;
  243. this.status = status;
  244. this.reason = reason;
  245. this.headers = headers;
  246. }
  247. addHeader(name, value) {
  248. this.headers.push({ name: name, value: value });
  249. }
  250. removeHeader(index) {
  251. this.headers.splice(index, 1);
  252. }
  253. static fromJson(json = {}) {
  254. return new TcpStreamSettings.TcpResponse(
  255. json.version,
  256. json.status,
  257. json.reason,
  258. XrayCommonClass.toHeaders(json.headers),
  259. );
  260. }
  261. toJson() {
  262. return {
  263. version: this.version,
  264. status: this.status,
  265. reason: this.reason,
  266. headers: XrayCommonClass.toV2Headers(this.headers),
  267. };
  268. }
  269. };
  270. class KcpStreamSettings extends XrayCommonClass {
  271. constructor(mtu=1350, tti=20,
  272. uplinkCapacity=5,
  273. downlinkCapacity=20,
  274. congestion=false,
  275. readBufferSize=2,
  276. writeBufferSize=2,
  277. type='none',
  278. seed=RandomUtil.randomSeq(10),
  279. ) {
  280. super();
  281. this.mtu = mtu;
  282. this.tti = tti;
  283. this.upCap = uplinkCapacity;
  284. this.downCap = downlinkCapacity;
  285. this.congestion = congestion;
  286. this.readBuffer = readBufferSize;
  287. this.writeBuffer = writeBufferSize;
  288. this.type = type;
  289. this.seed = seed;
  290. }
  291. static fromJson(json={}) {
  292. return new KcpStreamSettings(
  293. json.mtu,
  294. json.tti,
  295. json.uplinkCapacity,
  296. json.downlinkCapacity,
  297. json.congestion,
  298. json.readBufferSize,
  299. json.writeBufferSize,
  300. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  301. json.seed,
  302. );
  303. }
  304. toJson() {
  305. return {
  306. mtu: this.mtu,
  307. tti: this.tti,
  308. uplinkCapacity: this.upCap,
  309. downlinkCapacity: this.downCap,
  310. congestion: this.congestion,
  311. readBufferSize: this.readBuffer,
  312. writeBufferSize: this.writeBuffer,
  313. header: {
  314. type: this.type,
  315. },
  316. seed: this.seed,
  317. };
  318. }
  319. }
  320. class WsStreamSettings extends XrayCommonClass {
  321. constructor(acceptProxyProtocol=false, path='/', headers=[]) {
  322. super();
  323. this.acceptProxyProtocol = acceptProxyProtocol;
  324. this.path = path;
  325. this.headers = headers;
  326. }
  327. addHeader(name, value) {
  328. this.headers.push({ name: name, value: value });
  329. }
  330. getHeader(name) {
  331. for (const header of this.headers) {
  332. if (header.name.toLowerCase() === name.toLowerCase()) {
  333. return header.value;
  334. }
  335. }
  336. return null;
  337. }
  338. removeHeader(index) {
  339. this.headers.splice(index, 1);
  340. }
  341. static fromJson(json={}) {
  342. return new WsStreamSettings(
  343. json.acceptProxyProtocol,
  344. json.path,
  345. XrayCommonClass.toHeaders(json.headers),
  346. );
  347. }
  348. toJson() {
  349. return {
  350. acceptProxyProtocol: this.acceptProxyProtocol,
  351. path: this.path,
  352. headers: XrayCommonClass.toV2Headers(this.headers, false),
  353. };
  354. }
  355. }
  356. class HttpStreamSettings extends XrayCommonClass {
  357. constructor(path='/', host=['']) {
  358. super();
  359. this.path = path;
  360. this.host = host.length === 0 ? [''] : host;
  361. }
  362. addHost(host) {
  363. this.host.push(host);
  364. }
  365. removeHost(index) {
  366. this.host.splice(index, 1);
  367. }
  368. static fromJson(json={}) {
  369. return new HttpStreamSettings(json.path, json.host);
  370. }
  371. toJson() {
  372. let host = [];
  373. for (let i = 0; i < this.host.length; ++i) {
  374. if (!ObjectUtil.isEmpty(this.host[i])) {
  375. host.push(this.host[i]);
  376. }
  377. }
  378. return {
  379. path: this.path,
  380. host: host,
  381. }
  382. }
  383. }
  384. class QuicStreamSettings extends XrayCommonClass {
  385. constructor(security=VmessMethods.NONE,
  386. key='', type='none') {
  387. super();
  388. this.security = security;
  389. this.key = key;
  390. this.type = type;
  391. }
  392. static fromJson(json={}) {
  393. return new QuicStreamSettings(
  394. json.security,
  395. json.key,
  396. json.header ? json.header.type : 'none',
  397. );
  398. }
  399. toJson() {
  400. return {
  401. security: this.security,
  402. key: this.key,
  403. header: {
  404. type: this.type,
  405. }
  406. }
  407. }
  408. }
  409. class GrpcStreamSettings extends XrayCommonClass {
  410. constructor(serviceName="") {
  411. super();
  412. this.serviceName = serviceName;
  413. }
  414. static fromJson(json={}) {
  415. return new GrpcStreamSettings(json.serviceName);
  416. }
  417. toJson() {
  418. return {
  419. serviceName: this.serviceName,
  420. }
  421. }
  422. }
  423. class TlsStreamSettings extends XrayCommonClass {
  424. constructor(serverName = '', minVersion = TLS_VERSION_OPTION.TLS12, maxVersion = TLS_VERSION_OPTION.TLS13,
  425. cipherSuites = '',
  426. certificates = [new TlsStreamSettings.Cert()], alpn=[''] ,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(insecure = false, fingerprint = '', serverName = '') {
  514. super();
  515. this.inSecure = insecure;
  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.inSecure,
  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 = false;
  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 != null)
  819. return this.settings.vmesses[index]._expiryTime < new Date().getTime();
  820. return false
  821. case Protocols.VLESS:
  822. if(this.settings.vlesses[index]._expiryTime != null)
  823. return this.settings.vlesses[index]._expiryTime < new Date().getTime();
  824. return false
  825. case Protocols.TROJAN:
  826. if(this.settings.trojans[index]._expiryTime != null)
  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. case Protocols.SHADOWSOCKS:
  839. break;
  840. default:
  841. return false;
  842. }
  843. switch (this.network) {
  844. case "tcp":
  845. case "ws":
  846. case "http":
  847. case "quic":
  848. case "grpc":
  849. return true;
  850. default:
  851. return false;
  852. }
  853. }
  854. //this is used for xtls-rprx-vision
  855. canEnableTlsFlow() {
  856. if ((this.stream.security === 'tls') && (this.network === "tcp")) {
  857. switch (this.protocol) {
  858. case Protocols.VLESS:
  859. return true;
  860. default:
  861. return false;
  862. }
  863. }
  864. return false;
  865. }
  866. canSetTls() {
  867. return this.canEnableTls();
  868. }
  869. canEnableXTLS() {
  870. switch (this.protocol) {
  871. case Protocols.VLESS:
  872. case Protocols.TROJAN:
  873. break;
  874. default:
  875. return false;
  876. }
  877. return this.network === "tcp";
  878. }
  879. canEnableStream() {
  880. switch (this.protocol) {
  881. case Protocols.VMESS:
  882. case Protocols.VLESS:
  883. case Protocols.TROJAN:
  884. case Protocols.SHADOWSOCKS:
  885. return true;
  886. default:
  887. return false;
  888. }
  889. }
  890. canSniffing() {
  891. switch (this.protocol) {
  892. case Protocols.VMESS:
  893. case Protocols.VLESS:
  894. case Protocols.TROJAN:
  895. case Protocols.SHADOWSOCKS:
  896. return true;
  897. default:
  898. return false;
  899. }
  900. }
  901. reset() {
  902. this.port = RandomUtil.randomIntRange(10000, 60000);
  903. this.listen = '';
  904. this.protocol = Protocols.VMESS;
  905. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  906. this.stream = new StreamSettings();
  907. this.tag = '';
  908. this.sniffing = new Sniffing();
  909. }
  910. genVmessLink(address='', remark='', clientIndex=0) {
  911. if (this.protocol !== Protocols.VMESS) {
  912. return '';
  913. }
  914. let network = this.stream.network;
  915. let type = 'none';
  916. let host = '';
  917. let path = '';
  918. if (network === 'tcp') {
  919. let tcp = this.stream.tcp;
  920. type = tcp.type;
  921. if (type === 'http') {
  922. let request = tcp.request;
  923. path = request.path.join(',');
  924. let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  925. if (index >= 0) {
  926. host = request.headers[index].value;
  927. }
  928. }
  929. } else if (network === 'kcp') {
  930. let kcp = this.stream.kcp;
  931. type = kcp.type;
  932. path = kcp.seed;
  933. } else if (network === 'ws') {
  934. let ws = this.stream.ws;
  935. path = ws.path;
  936. let index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  937. if (index >= 0) {
  938. host = ws.headers[index].value;
  939. }
  940. } else if (network === 'http') {
  941. network = 'h2';
  942. path = this.stream.http.path;
  943. host = this.stream.http.host.join(',');
  944. } else if (network === 'quic') {
  945. type = this.stream.quic.type;
  946. host = this.stream.quic.security;
  947. path = this.stream.quic.key;
  948. } else if (network === 'grpc') {
  949. path = this.stream.grpc.serviceName;
  950. }
  951. if (this.stream.security === 'tls') {
  952. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  953. address = this.stream.tls.server;
  954. }
  955. }
  956. let obj = {
  957. v: '2',
  958. ps: remark,
  959. add: address,
  960. port: this.port,
  961. id: this.settings.vmesses[clientIndex].id,
  962. aid: this.settings.vmesses[clientIndex].alterId,
  963. net: network,
  964. type: type,
  965. host: host,
  966. path: path,
  967. tls: this.stream.security,
  968. sni: this.stream.tls.settings[0]['serverName'],
  969. fp: this.stream.tls.settings[0]['fingerprint'],
  970. alpn: this.stream.tls.alpn[0],
  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. params.set("security", this.stream.security);
  982. if (this.XTLS) {
  983. params.set("security", "xtls");
  984. } else {
  985. params.set("security", this.stream.security);
  986. }
  987. switch (type) {
  988. case "tcp":
  989. const tcp = this.stream.tcp;
  990. if (tcp.type === 'http') {
  991. const request = tcp.request;
  992. params.set("path", request.path.join(','));
  993. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  994. if (index >= 0) {
  995. const host = request.headers[index].value;
  996. params.set("host", host);
  997. }
  998. params.set("headerType", 'http');
  999. }
  1000. break;
  1001. case "kcp":
  1002. const kcp = this.stream.kcp;
  1003. params.set("headerType", kcp.type);
  1004. params.set("seed", kcp.seed);
  1005. break;
  1006. case "ws":
  1007. const ws = this.stream.ws;
  1008. params.set("path", ws.path);
  1009. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1010. if (index >= 0) {
  1011. const host = ws.headers[index].value;
  1012. params.set("host", host);
  1013. }
  1014. break;
  1015. case "http":
  1016. const http = this.stream.http;
  1017. params.set("path", http.path);
  1018. params.set("host", http.host);
  1019. break;
  1020. case "quic":
  1021. const quic = this.stream.quic;
  1022. params.set("quicSecurity", quic.security);
  1023. params.set("key", quic.key);
  1024. params.set("headerType", quic.type);
  1025. break;
  1026. case "grpc":
  1027. const grpc = this.stream.grpc;
  1028. params.set("serviceName", grpc.serviceName);
  1029. break;
  1030. }
  1031. if (this.stream.security === 'tls') {
  1032. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1033. address = this.stream.tls.server;
  1034. params.set("fp" , this.stream.tls.settings[0]['fingerprint']);
  1035. params.set("alpn", this.stream.tls.alpn[0]);
  1036. if (this.stream.tls.settings[0]['serverName'] !== ''){
  1037. params.set("sni", this.stream.tls.settings[0]['serverName']);
  1038. }
  1039. else{
  1040. params.set("sni", address);
  1041. }
  1042. if (type === "tcp" && this.settings.vlesses[clientIndex].flow.length > 0) {
  1043. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1044. }
  1045. }
  1046. }
  1047. if (this.xtls) {
  1048. if (this.stream.security === 'xtls') {
  1049. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1050. address = this.stream.tls.server;
  1051. if (type === "tcp") {
  1052. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1053. }
  1054. }
  1055. }
  1056. }
  1057. const link = `vless://${uuid}@${address}:${port}`;
  1058. const url = new URL(link);
  1059. for (const [key, value] of params) {
  1060. url.searchParams.set(key, value)
  1061. }
  1062. url.hash = encodeURIComponent(remark);
  1063. return url.toString();
  1064. }
  1065. genSSLink(address = '', remark = '') {
  1066. let settings = this.settings;
  1067. const server = this.stream.tls.server;
  1068. if (!ObjectUtil.isEmpty(server)) {
  1069. address = server;
  1070. }
  1071. if (settings.method == SSMethods.BLAKE3_AES_128_GCM || settings.method == SSMethods.BLAKE3_AES_256_GCM || settings.method == SSMethods.BLAKE3_CHACHA20_POLY1305) {
  1072. return `ss://${settings.method}:${settings.password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1073. } else {
  1074. return 'ss://' + safeBase64(settings.method + ':' + settings.password + '@' + address + ':' + this.port)
  1075. + '#' + encodeURIComponent(remark);
  1076. }
  1077. }
  1078. genTrojanLink(address = '', remark = '', clientIndex = 0) {
  1079. let settings = this.settings;
  1080. const port = this.port;
  1081. const type = this.stream.network;
  1082. const params = new Map();
  1083. switch (type) {
  1084. case "tcp":
  1085. const tcp = this.stream.tcp;
  1086. if (tcp.type === 'http') {
  1087. const request = tcp.request;
  1088. params.set("path", request.path.join(','));
  1089. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1090. if (index >= 0) {
  1091. const host = request.headers[index].value;
  1092. params.set("host", host);
  1093. }
  1094. params.set("headerType", 'http');
  1095. }
  1096. break;
  1097. case "kcp":
  1098. const kcp = this.stream.kcp;
  1099. params.set("headerType", kcp.type);
  1100. params.set("seed", kcp.seed);
  1101. break;
  1102. case "ws":
  1103. const ws = this.stream.ws;
  1104. params.set("path", ws.path);
  1105. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1106. if (index >= 0) {
  1107. const host = ws.headers[index].value;
  1108. params.set("host", host);
  1109. }
  1110. break;
  1111. case "http":
  1112. const http = this.stream.http;
  1113. params.set("path", http.path);
  1114. params.set("host", http.host);
  1115. break;
  1116. case "quic":
  1117. const quic = this.stream.quic;
  1118. params.set("quicSecurity", quic.security);
  1119. params.set("key", quic.key);
  1120. params.set("headerType", quic.type);
  1121. break;
  1122. case "grpc":
  1123. const grpc = this.stream.grpc;
  1124. params.set("serviceName", grpc.serviceName);
  1125. break;
  1126. }
  1127. if (this.stream.security === 'tls') {
  1128. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1129. address = this.stream.tls.server;
  1130. params.set("fp" , this.stream.tls.settings[0]['fingerprint']);
  1131. params.set("alpn", this.stream.tls.alpn[0]);
  1132. if (this.stream.tls.settings[0]['serverName'] !== ''){
  1133. params.set("sni", this.stream.tls.settings[0]['serverName']);
  1134. }
  1135. else{
  1136. params.set("sni", address);
  1137. }
  1138. }
  1139. }
  1140. if (this.stream.security === 'xtls') {
  1141. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1142. address = this.stream.tls.server;
  1143. if (type === "tcp" && this.settings.trojans[clientIndex].flow.length > 0) {
  1144. params.set("flow", this.settings.trojans[clientIndex].flow);
  1145. }
  1146. }
  1147. }
  1148. const link = `trojan://${settings.trojans[clientIndex].password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1149. const url = new URL(link);
  1150. for (const [key, value] of params) {
  1151. url.searchParams.set(key, value)
  1152. }
  1153. url.hash = encodeURIComponent(remark);
  1154. return url.toString();
  1155. }
  1156. genLink(address='', remark='', clientIndex=0) {
  1157. switch (this.protocol) {
  1158. case Protocols.VMESS:
  1159. if (this.settings.vmesses[clientIndex].email != ""){
  1160. remark += '-' + this.settings.vmesses[clientIndex].email
  1161. }
  1162. return this.genVmessLink(address, remark, clientIndex);
  1163. case Protocols.VLESS:
  1164. if (this.settings.vlesses[clientIndex].email != ""){
  1165. remark += '-' + this.settings.vlesses[clientIndex].email
  1166. }
  1167. return this.genVLESSLink(address, remark, clientIndex);
  1168. case Protocols.SHADOWSOCKS: return this.genSSLink(address, remark);
  1169. case Protocols.TROJAN:
  1170. if (this.settings.trojans[clientIndex].email != ""){
  1171. remark += '-' + this.settings.trojans[clientIndex].email
  1172. }
  1173. return this.genTrojanLink(address, remark, clientIndex);
  1174. default: return '';
  1175. }
  1176. }
  1177. genInboundLinks(address = '', remark = '') {
  1178. let link = '';
  1179. switch (this.protocol) {
  1180. case Protocols.VMESS:
  1181. case Protocols.VLESS:
  1182. case Protocols.TROJAN:
  1183. JSON.parse(this.settings).clients.forEach((_,index) => {
  1184. link += this.genLink(address, remark, index) + '\r\n';
  1185. });
  1186. return link;
  1187. case Protocols.SHADOWSOCKS:
  1188. return (this.genSSLink(address, remark) + '\r\n');
  1189. default: return '';
  1190. }
  1191. }
  1192. static fromJson(json={}) {
  1193. return new Inbound(
  1194. json.port,
  1195. json.listen,
  1196. json.protocol,
  1197. Inbound.Settings.fromJson(json.protocol, json.settings),
  1198. StreamSettings.fromJson(json.streamSettings),
  1199. json.tag,
  1200. Sniffing.fromJson(json.sniffing),
  1201. json.clientStats
  1202. )
  1203. }
  1204. toJson() {
  1205. let streamSettings;
  1206. if (this.canEnableStream() || this.protocol === Protocols.TROJAN) {
  1207. streamSettings = this.stream.toJson();
  1208. }
  1209. return {
  1210. port: this.port,
  1211. listen: this.listen,
  1212. protocol: this.protocol,
  1213. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1214. streamSettings: streamSettings,
  1215. tag: this.tag,
  1216. sniffing: this.sniffing.toJson(),
  1217. clientStats: this.clientStats
  1218. };
  1219. }
  1220. }
  1221. Inbound.Settings = class extends XrayCommonClass {
  1222. constructor(protocol) {
  1223. super();
  1224. this.protocol = protocol;
  1225. }
  1226. static getSettings(protocol) {
  1227. switch (protocol) {
  1228. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1229. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1230. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1231. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1232. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1233. case Protocols.MTPROTO: return new Inbound.MtprotoSettings(protocol);
  1234. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1235. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1236. default: return null;
  1237. }
  1238. }
  1239. static fromJson(protocol, json) {
  1240. switch (protocol) {
  1241. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1242. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1243. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1244. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1245. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1246. case Protocols.MTPROTO: return Inbound.MtprotoSettings.fromJson(json);
  1247. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1248. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1249. default: return null;
  1250. }
  1251. }
  1252. toJson() {
  1253. return {};
  1254. }
  1255. };
  1256. Inbound.VmessSettings = class extends Inbound.Settings {
  1257. constructor(protocol,
  1258. vmesses=[new Inbound.VmessSettings.Vmess()],
  1259. disableInsecureEncryption=false) {
  1260. super(protocol);
  1261. this.vmesses = vmesses;
  1262. this.disableInsecure = disableInsecureEncryption;
  1263. }
  1264. indexOfVmessById(id) {
  1265. return this.vmesses.findIndex(vmess => vmess.id === id);
  1266. }
  1267. addVmess(vmess) {
  1268. if (this.indexOfVmessById(vmess.id) >= 0) {
  1269. return false;
  1270. }
  1271. this.vmesses.push(vmess);
  1272. }
  1273. delVmess(vmess) {
  1274. const i = this.indexOfVmessById(vmess.id);
  1275. if (i >= 0) {
  1276. this.vmesses.splice(i, 1);
  1277. }
  1278. }
  1279. static fromJson(json={}) {
  1280. return new Inbound.VmessSettings(
  1281. Protocols.VMESS,
  1282. json.clients.map(client => Inbound.VmessSettings.Vmess.fromJson(client)),
  1283. ObjectUtil.isEmpty(json.disableInsecureEncryption) ? false : json.disableInsecureEncryption,
  1284. );
  1285. }
  1286. toJson() {
  1287. return {
  1288. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1289. disableInsecureEncryption: this.disableInsecure,
  1290. };
  1291. }
  1292. };
  1293. Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
  1294. constructor(id=RandomUtil.randomUUID(), alterId=0, email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime='') {
  1295. super();
  1296. this.id = id;
  1297. this.alterId = alterId;
  1298. this.email = email;
  1299. this.limitIp = limitIp;
  1300. this.totalGB = totalGB;
  1301. this.expiryTime = expiryTime;
  1302. }
  1303. static fromJson(json={}) {
  1304. return new Inbound.VmessSettings.Vmess(
  1305. json.id,
  1306. json.alterId,
  1307. json.email,
  1308. json.limitIp,
  1309. json.totalGB,
  1310. json.expiryTime,
  1311. );
  1312. }
  1313. get _expiryTime() {
  1314. if (this.expiryTime === 0 || this.expiryTime === "") {
  1315. return null;
  1316. }
  1317. return moment(this.expiryTime);
  1318. }
  1319. set _expiryTime(t) {
  1320. if (t == null || t === "") {
  1321. this.expiryTime = 0;
  1322. } else {
  1323. this.expiryTime = t.valueOf();
  1324. }
  1325. }
  1326. get _totalGB() {
  1327. return toFixed(this.totalGB / ONE_GB, 2);
  1328. }
  1329. set _totalGB(gb) {
  1330. this.totalGB = toFixed(gb * ONE_GB, 0);
  1331. }
  1332. };
  1333. Inbound.VLESSSettings = class extends Inbound.Settings {
  1334. constructor(protocol,
  1335. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1336. decryption='none',
  1337. fallbacks=[],) {
  1338. super(protocol);
  1339. this.vlesses = vlesses;
  1340. this.decryption = 'none';
  1341. this.fallbacks = fallbacks;
  1342. }
  1343. addFallback() {
  1344. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1345. }
  1346. delFallback(index) {
  1347. this.fallbacks.splice(index, 1);
  1348. }
  1349. static fromJson(json={}) {
  1350. return new Inbound.VLESSSettings(
  1351. Protocols.VLESS,
  1352. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1353. 'none',
  1354. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1355. );
  1356. }
  1357. toJson() {
  1358. return {
  1359. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1360. decryption: this.decryption,
  1361. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1362. };
  1363. }
  1364. };
  1365. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1366. constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime='') {
  1367. super();
  1368. this.id = id;
  1369. this.flow = flow;
  1370. this.email = email;
  1371. this.limitIp = limitIp;
  1372. this.totalGB = totalGB;
  1373. this.expiryTime = expiryTime;
  1374. }
  1375. static fromJson(json={}) {
  1376. return new Inbound.VLESSSettings.VLESS(
  1377. json.id,
  1378. json.flow,
  1379. json.email,
  1380. json.limitIp,
  1381. json.totalGB,
  1382. json.expiryTime,
  1383. );
  1384. }
  1385. get _expiryTime() {
  1386. if (this.expiryTime === 0 || this.expiryTime === "") {
  1387. return null;
  1388. }
  1389. return moment(this.expiryTime);
  1390. }
  1391. set _expiryTime(t) {
  1392. if (t == null || t === "") {
  1393. this.expiryTime = 0;
  1394. } else {
  1395. this.expiryTime = t.valueOf();
  1396. }
  1397. }
  1398. get _totalGB() {
  1399. return toFixed(this.totalGB / ONE_GB, 2);
  1400. }
  1401. set _totalGB(gb) {
  1402. this.totalGB = toFixed(gb * ONE_GB, 0);
  1403. }
  1404. };
  1405. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1406. constructor(name="", alpn='', path='', dest='', xver=0) {
  1407. super();
  1408. this.name = name;
  1409. this.alpn = alpn;
  1410. this.path = path;
  1411. this.dest = dest;
  1412. this.xver = xver;
  1413. }
  1414. toJson() {
  1415. let xver = this.xver;
  1416. if (!Number.isInteger(xver)) {
  1417. xver = 0;
  1418. }
  1419. return {
  1420. name: this.name,
  1421. alpn: this.alpn,
  1422. path: this.path,
  1423. dest: this.dest,
  1424. xver: xver,
  1425. }
  1426. }
  1427. static fromJson(json=[]) {
  1428. const fallbacks = [];
  1429. for (let fallback of json) {
  1430. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1431. fallback.name,
  1432. fallback.alpn,
  1433. fallback.path,
  1434. fallback.dest,
  1435. fallback.xver,
  1436. ))
  1437. }
  1438. return fallbacks;
  1439. }
  1440. };
  1441. Inbound.TrojanSettings = class extends Inbound.Settings {
  1442. constructor(protocol,
  1443. trojans=[new Inbound.TrojanSettings.Trojan()],
  1444. fallbacks=[],) {
  1445. super(protocol);
  1446. this.trojans = trojans;
  1447. this.fallbacks = fallbacks;
  1448. }
  1449. addTrojanFallback() {
  1450. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1451. }
  1452. delTrojanFallback(index) {
  1453. this.fallbacks.splice(index, 1);
  1454. }
  1455. static fromJson(json={}) {
  1456. return new Inbound.TrojanSettings(
  1457. Protocols.TROJAN,
  1458. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1459. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1460. }
  1461. toJson() {
  1462. return {
  1463. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1464. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
  1465. };
  1466. }
  1467. };
  1468. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1469. constructor(password=RandomUtil.randomSeq(10), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime='') {
  1470. super();
  1471. this.password = password;
  1472. this.flow = flow;
  1473. this.email = email;
  1474. this.limitIp = limitIp;
  1475. this.totalGB = totalGB;
  1476. this.expiryTime = expiryTime;
  1477. }
  1478. toJson() {
  1479. return {
  1480. password: this.password,
  1481. flow: this.flow,
  1482. email: this.email,
  1483. limitIp: this.limitIp,
  1484. totalGB: this.totalGB,
  1485. expiryTime: this.expiryTime,
  1486. };
  1487. }
  1488. static fromJson(json={}) {
  1489. return new Inbound.TrojanSettings.Trojan(
  1490. json.password,
  1491. json.flow,
  1492. json.email,
  1493. json.limitIp,
  1494. json.totalGB,
  1495. json.expiryTime,
  1496. );
  1497. }
  1498. get _expiryTime() {
  1499. if (this.expiryTime === 0 || this.expiryTime === "") {
  1500. return null;
  1501. }
  1502. return moment(this.expiryTime);
  1503. }
  1504. set _expiryTime(t) {
  1505. if (t == null || t === "") {
  1506. this.expiryTime = 0;
  1507. } else {
  1508. this.expiryTime = t.valueOf();
  1509. }
  1510. }
  1511. get _totalGB() {
  1512. return toFixed(this.totalGB / ONE_GB, 2);
  1513. }
  1514. set _totalGB(gb) {
  1515. this.totalGB = toFixed(gb * ONE_GB, 0);
  1516. }
  1517. };
  1518. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1519. constructor(name="", alpn='', path='', dest='', xver=0) {
  1520. super();
  1521. this.name = name;
  1522. this.alpn = alpn;
  1523. this.path = path;
  1524. this.dest = dest;
  1525. this.xver = xver;
  1526. }
  1527. toJson() {
  1528. let xver = this.xver;
  1529. if (!Number.isInteger(xver)) {
  1530. xver = 0;
  1531. }
  1532. return {
  1533. name: this.name,
  1534. alpn: this.alpn,
  1535. path: this.path,
  1536. dest: this.dest,
  1537. xver: xver,
  1538. }
  1539. }
  1540. static fromJson(json=[]) {
  1541. const fallbacks = [];
  1542. for (let fallback of json) {
  1543. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1544. fallback.name,
  1545. fallback.alpn,
  1546. fallback.path,
  1547. fallback.dest,
  1548. fallback.xver,
  1549. ))
  1550. }
  1551. return fallbacks;
  1552. }
  1553. };
  1554. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1555. constructor(protocol,
  1556. method = SSMethods.BLAKE3_AES_256_GCM,
  1557. password = RandomUtil.randomSeq(44),
  1558. network = 'tcp,udp'
  1559. ) {
  1560. super(protocol);
  1561. this.method = method;
  1562. this.password = password;
  1563. this.network = network;
  1564. }
  1565. static fromJson(json = {}) {
  1566. return new Inbound.ShadowsocksSettings(
  1567. Protocols.SHADOWSOCKS,
  1568. json.method,
  1569. json.password,
  1570. json.network,
  1571. );
  1572. }
  1573. toJson() {
  1574. return {
  1575. method: this.method,
  1576. password: this.password,
  1577. network: this.network,
  1578. };
  1579. }
  1580. };
  1581. Inbound.DokodemoSettings = class extends Inbound.Settings {
  1582. constructor(protocol, address, port, network='tcp,udp', followRedirect=false) {
  1583. super(protocol);
  1584. this.address = address;
  1585. this.port = port;
  1586. this.network = network;
  1587. this.followRedirect = followRedirect;
  1588. }
  1589. static fromJson(json={}) {
  1590. return new Inbound.DokodemoSettings(
  1591. Protocols.DOKODEMO,
  1592. json.address,
  1593. json.port,
  1594. json.network,
  1595. json.followRedirect,
  1596. );
  1597. }
  1598. toJson() {
  1599. return {
  1600. address: this.address,
  1601. port: this.port,
  1602. network: this.network,
  1603. followRedirect: this.followRedirect,
  1604. };
  1605. }
  1606. };
  1607. Inbound.MtprotoSettings = class extends Inbound.Settings {
  1608. constructor(protocol, users=[new Inbound.MtprotoSettings.MtUser()]) {
  1609. super(protocol);
  1610. this.users = users;
  1611. }
  1612. static fromJson(json={}) {
  1613. return new Inbound.MtprotoSettings(
  1614. Protocols.MTPROTO,
  1615. json.users.map(user => Inbound.MtprotoSettings.MtUser.fromJson(user)),
  1616. );
  1617. }
  1618. toJson() {
  1619. return {
  1620. users: XrayCommonClass.toJsonArray(this.users),
  1621. };
  1622. }
  1623. };
  1624. Inbound.MtprotoSettings.MtUser = class extends XrayCommonClass {
  1625. constructor(secret=RandomUtil.randomMTSecret()) {
  1626. super();
  1627. this.secret = secret;
  1628. }
  1629. static fromJson(json={}) {
  1630. return new Inbound.MtprotoSettings.MtUser(json.secret);
  1631. }
  1632. };
  1633. Inbound.SocksSettings = class extends Inbound.Settings {
  1634. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  1635. super(protocol);
  1636. this.auth = auth;
  1637. this.accounts = accounts;
  1638. this.udp = udp;
  1639. this.ip = ip;
  1640. }
  1641. addAccount(account) {
  1642. this.accounts.push(account);
  1643. }
  1644. delAccount(index) {
  1645. this.accounts.splice(index, 1);
  1646. }
  1647. static fromJson(json={}) {
  1648. let accounts;
  1649. if (json.auth === 'password') {
  1650. accounts = json.accounts.map(
  1651. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  1652. )
  1653. }
  1654. return new Inbound.SocksSettings(
  1655. Protocols.SOCKS,
  1656. json.auth,
  1657. accounts,
  1658. json.udp,
  1659. json.ip,
  1660. );
  1661. }
  1662. toJson() {
  1663. return {
  1664. auth: this.auth,
  1665. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  1666. udp: this.udp,
  1667. ip: this.ip,
  1668. };
  1669. }
  1670. };
  1671. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  1672. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1673. super();
  1674. this.user = user;
  1675. this.pass = pass;
  1676. }
  1677. static fromJson(json={}) {
  1678. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  1679. }
  1680. };
  1681. Inbound.HttpSettings = class extends Inbound.Settings {
  1682. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  1683. super(protocol);
  1684. this.accounts = accounts;
  1685. }
  1686. addAccount(account) {
  1687. this.accounts.push(account);
  1688. }
  1689. delAccount(index) {
  1690. this.accounts.splice(index, 1);
  1691. }
  1692. static fromJson(json={}) {
  1693. return new Inbound.HttpSettings(
  1694. Protocols.HTTP,
  1695. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  1696. );
  1697. }
  1698. toJson() {
  1699. return {
  1700. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  1701. };
  1702. }
  1703. };
  1704. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  1705. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1706. super();
  1707. this.user = user;
  1708. this.pass = pass;
  1709. }
  1710. static fromJson(json={}) {
  1711. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  1712. }
  1713. };