xray.js 81 KB

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