1
0

xray.js 81 KB

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