xray.js 67 KB

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