xray.js 55 KB

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