xray.js 73 KB

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