xray.js 73 KB

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