xray.js 77 KB

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