inbound.js 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239
  1. import dayjs from 'dayjs';
  2. import { ObjectUtil, RandomUtil, Base64, NumberFormatter, SizeFormatter, Wireguard } from '@/utils';
  3. export const Protocols = {
  4. VMESS: 'vmess',
  5. VLESS: 'vless',
  6. TROJAN: 'trojan',
  7. SHADOWSOCKS: 'shadowsocks',
  8. WIREGUARD: 'wireguard',
  9. HYSTERIA: 'hysteria',
  10. MIXED: 'mixed',
  11. HTTP: 'http',
  12. TUNNEL: 'tunnel',
  13. TUN: 'tun',
  14. };
  15. export const SSMethods = {
  16. CHACHA20_POLY1305: 'chacha20-poly1305',
  17. CHACHA20_IETF_POLY1305: 'chacha20-ietf-poly1305',
  18. XCHACHA20_IETF_POLY1305: 'xchacha20-ietf-poly1305',
  19. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  20. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  21. BLAKE3_CHACHA20_POLY1305: '2022-blake3-chacha20-poly1305',
  22. };
  23. export const TLS_FLOW_CONTROL = {
  24. VISION: "xtls-rprx-vision",
  25. VISION_UDP443: "xtls-rprx-vision-udp443",
  26. };
  27. export const TLS_VERSION_OPTION = {
  28. TLS10: "1.0",
  29. TLS11: "1.1",
  30. TLS12: "1.2",
  31. TLS13: "1.3",
  32. };
  33. export const TLS_CIPHER_OPTION = {
  34. AES_128_GCM: "TLS_AES_128_GCM_SHA256",
  35. AES_256_GCM: "TLS_AES_256_GCM_SHA384",
  36. CHACHA20_POLY1305: "TLS_CHACHA20_POLY1305_SHA256",
  37. ECDHE_ECDSA_AES_128_CBC: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
  38. ECDHE_ECDSA_AES_256_CBC: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
  39. ECDHE_RSA_AES_128_CBC: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
  40. ECDHE_RSA_AES_256_CBC: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
  41. ECDHE_ECDSA_AES_128_GCM: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
  42. ECDHE_ECDSA_AES_256_GCM: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  43. ECDHE_RSA_AES_128_GCM: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  44. ECDHE_RSA_AES_256_GCM: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
  45. ECDHE_ECDSA_CHACHA20_POLY1305: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
  46. ECDHE_RSA_CHACHA20_POLY1305: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
  47. };
  48. export const UTLS_FINGERPRINT = {
  49. UTLS_CHROME: "chrome",
  50. UTLS_FIREFOX: "firefox",
  51. UTLS_SAFARI: "safari",
  52. UTLS_IOS: "ios",
  53. UTLS_android: "android",
  54. UTLS_EDGE: "edge",
  55. UTLS_360: "360",
  56. UTLS_QQ: "qq",
  57. UTLS_RANDOM: "random",
  58. UTLS_RANDOMIZED: "randomized",
  59. UTLS_RONDOMIZEDNOALPN: "randomizednoalpn",
  60. UTLS_UNSAFE: "unsafe",
  61. };
  62. export const ALPN_OPTION = {
  63. H3: "h3",
  64. H2: "h2",
  65. HTTP1: "http/1.1",
  66. };
  67. export const SNIFFING_OPTION = {
  68. HTTP: "http",
  69. TLS: "tls",
  70. QUIC: "quic",
  71. FAKEDNS: "fakedns"
  72. };
  73. export const USAGE_OPTION = {
  74. ENCIPHERMENT: "encipherment",
  75. VERIFY: "verify",
  76. ISSUE: "issue",
  77. };
  78. export const DOMAIN_STRATEGY_OPTION = {
  79. AS_IS: "AsIs",
  80. USE_IP: "UseIP",
  81. USE_IPV6V4: "UseIPv6v4",
  82. USE_IPV6: "UseIPv6",
  83. USE_IPV4V6: "UseIPv4v6",
  84. USE_IPV4: "UseIPv4",
  85. FORCE_IP: "ForceIP",
  86. FORCE_IPV6V4: "ForceIPv6v4",
  87. FORCE_IPV6: "ForceIPv6",
  88. FORCE_IPV4V6: "ForceIPv4v6",
  89. FORCE_IPV4: "ForceIPv4",
  90. };
  91. export const TCP_CONGESTION_OPTION = {
  92. BBR: "bbr",
  93. CUBIC: "cubic",
  94. RENO: "reno",
  95. };
  96. export const USERS_SECURITY = {
  97. AES_128_GCM: "aes-128-gcm",
  98. CHACHA20_POLY1305: "chacha20-poly1305",
  99. AUTO: "auto",
  100. NONE: "none",
  101. ZERO: "zero",
  102. };
  103. export const MODE_OPTION = {
  104. AUTO: "auto",
  105. PACKET_UP: "packet-up",
  106. STREAM_UP: "stream-up",
  107. STREAM_ONE: "stream-one",
  108. };
  109. Object.freeze(Protocols);
  110. Object.freeze(SSMethods);
  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. Object.freeze(MODE_OPTION);
  122. export class XrayCommonClass {
  123. static toJsonArray(arr) {
  124. return arr.map(obj => obj.toJson());
  125. }
  126. static fromJson() {
  127. return new XrayCommonClass();
  128. }
  129. toJson() {
  130. return this;
  131. }
  132. // Build a clean Xray fallback entry. Per docs, name/alpn/path empty = "any",
  133. // and xver=0 means PROXY protocol off — omit them so the generated config
  134. // stays minimal and readable. dest is required and always emitted.
  135. static fallbackToJson(fb) {
  136. const out = { dest: fb.dest };
  137. if (fb.name) out.name = fb.name;
  138. if (fb.alpn) out.alpn = fb.alpn;
  139. if (fb.path) out.path = fb.path;
  140. const xver = Number(fb.xver);
  141. if (Number.isInteger(xver) && xver > 0) out.xver = xver;
  142. return out;
  143. }
  144. toString(format = true) {
  145. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  146. }
  147. static toHeaders(v2Headers) {
  148. let newHeaders = [];
  149. if (v2Headers) {
  150. Object.keys(v2Headers).forEach(key => {
  151. let values = v2Headers[key];
  152. if (typeof (values) === 'string') {
  153. newHeaders.push({ name: key, value: values });
  154. } else {
  155. for (let i = 0; i < values.length; ++i) {
  156. newHeaders.push({ name: key, value: values[i] });
  157. }
  158. }
  159. });
  160. }
  161. return newHeaders;
  162. }
  163. static toV2Headers(headers, arr = true) {
  164. let v2Headers = {};
  165. for (let i = 0; i < headers.length; ++i) {
  166. let name = headers[i].name;
  167. let value = headers[i].value;
  168. if (ObjectUtil.isEmpty(name) || ObjectUtil.isEmpty(value)) {
  169. continue;
  170. }
  171. if (!(name in v2Headers)) {
  172. v2Headers[name] = arr ? [value] : value;
  173. } else {
  174. if (arr) {
  175. v2Headers[name].push(value);
  176. } else {
  177. v2Headers[name] = value;
  178. }
  179. }
  180. }
  181. return v2Headers;
  182. }
  183. }
  184. export class TcpStreamSettings extends XrayCommonClass {
  185. constructor(
  186. acceptProxyProtocol = false,
  187. type = 'none',
  188. request = new TcpStreamSettings.TcpRequest(),
  189. response = new TcpStreamSettings.TcpResponse(),
  190. ) {
  191. super();
  192. this.acceptProxyProtocol = acceptProxyProtocol;
  193. this.type = type;
  194. this.request = request;
  195. this.response = response;
  196. }
  197. static fromJson(json = {}) {
  198. let header = json.header;
  199. if (!header) {
  200. header = {};
  201. }
  202. return new TcpStreamSettings(json.acceptProxyProtocol,
  203. header.type,
  204. TcpStreamSettings.TcpRequest.fromJson(header.request),
  205. TcpStreamSettings.TcpResponse.fromJson(header.response),
  206. );
  207. }
  208. toJson() {
  209. return {
  210. acceptProxyProtocol: this.acceptProxyProtocol,
  211. header: {
  212. type: this.type,
  213. request: this.type === 'http' ? this.request.toJson() : undefined,
  214. response: this.type === 'http' ? this.response.toJson() : undefined,
  215. },
  216. };
  217. }
  218. }
  219. TcpStreamSettings.TcpRequest = class extends XrayCommonClass {
  220. constructor(
  221. version = '1.1',
  222. method = 'GET',
  223. path = ['/'],
  224. headers = [],
  225. ) {
  226. super();
  227. this.version = version;
  228. this.method = method;
  229. this.path = path.length === 0 ? ['/'] : path;
  230. this.headers = headers;
  231. }
  232. addPath(path) {
  233. this.path.push(path);
  234. }
  235. removePath(index) {
  236. this.path.splice(index, 1);
  237. }
  238. addHeader(name, value) {
  239. this.headers.push({ name: name, value: value });
  240. }
  241. removeHeader(index) {
  242. this.headers.splice(index, 1);
  243. }
  244. static fromJson(json = {}) {
  245. return new TcpStreamSettings.TcpRequest(
  246. json.version,
  247. json.method,
  248. json.path,
  249. XrayCommonClass.toHeaders(json.headers),
  250. );
  251. }
  252. toJson() {
  253. return {
  254. version: this.version,
  255. method: this.method,
  256. path: ObjectUtil.clone(this.path),
  257. headers: XrayCommonClass.toV2Headers(this.headers),
  258. };
  259. }
  260. };
  261. TcpStreamSettings.TcpResponse = class extends XrayCommonClass {
  262. constructor(
  263. version = '1.1',
  264. status = '200',
  265. reason = 'OK',
  266. headers = [],
  267. ) {
  268. super();
  269. this.version = version;
  270. this.status = status;
  271. this.reason = reason;
  272. this.headers = headers;
  273. }
  274. addHeader(name, value) {
  275. this.headers.push({ name: name, value: value });
  276. }
  277. removeHeader(index) {
  278. this.headers.splice(index, 1);
  279. }
  280. static fromJson(json = {}) {
  281. return new TcpStreamSettings.TcpResponse(
  282. json.version,
  283. json.status,
  284. json.reason,
  285. XrayCommonClass.toHeaders(json.headers),
  286. );
  287. }
  288. toJson() {
  289. return {
  290. version: this.version,
  291. status: this.status,
  292. reason: this.reason,
  293. headers: XrayCommonClass.toV2Headers(this.headers),
  294. };
  295. }
  296. };
  297. export class KcpStreamSettings extends XrayCommonClass {
  298. constructor(
  299. mtu = 1350,
  300. tti = 20,
  301. uplinkCapacity = 5,
  302. downlinkCapacity = 20,
  303. cwndMultiplier = 1,
  304. maxSendingWindow = 2097152,
  305. ) {
  306. super();
  307. this.mtu = mtu;
  308. this.tti = tti;
  309. this.upCap = uplinkCapacity;
  310. this.downCap = downlinkCapacity;
  311. this.cwndMultiplier = cwndMultiplier;
  312. this.maxSendingWindow = maxSendingWindow;
  313. }
  314. static fromJson(json = {}) {
  315. return new KcpStreamSettings(
  316. json.mtu,
  317. json.tti,
  318. json.uplinkCapacity,
  319. json.downlinkCapacity,
  320. json.cwndMultiplier,
  321. json.maxSendingWindow,
  322. );
  323. }
  324. toJson() {
  325. return {
  326. mtu: this.mtu,
  327. tti: this.tti,
  328. uplinkCapacity: this.upCap,
  329. downlinkCapacity: this.downCap,
  330. cwndMultiplier: this.cwndMultiplier,
  331. maxSendingWindow: this.maxSendingWindow,
  332. };
  333. }
  334. }
  335. export class WsStreamSettings extends XrayCommonClass {
  336. constructor(
  337. acceptProxyProtocol = false,
  338. path = '/',
  339. host = '',
  340. headers = [],
  341. heartbeatPeriod = 0,
  342. ) {
  343. super();
  344. this.acceptProxyProtocol = acceptProxyProtocol;
  345. this.path = path;
  346. this.host = host;
  347. this.headers = headers;
  348. this.heartbeatPeriod = heartbeatPeriod;
  349. }
  350. addHeader(name, value) {
  351. this.headers.push({ name: name, value: value });
  352. }
  353. removeHeader(index) {
  354. this.headers.splice(index, 1);
  355. }
  356. static fromJson(json = {}) {
  357. return new WsStreamSettings(
  358. json.acceptProxyProtocol,
  359. json.path,
  360. json.host,
  361. XrayCommonClass.toHeaders(json.headers),
  362. json.heartbeatPeriod,
  363. );
  364. }
  365. toJson() {
  366. return {
  367. acceptProxyProtocol: this.acceptProxyProtocol,
  368. path: this.path,
  369. host: this.host,
  370. headers: XrayCommonClass.toV2Headers(this.headers, false),
  371. heartbeatPeriod: this.heartbeatPeriod,
  372. };
  373. }
  374. }
  375. export class GrpcStreamSettings extends XrayCommonClass {
  376. constructor(
  377. serviceName = "",
  378. authority = "",
  379. multiMode = false,
  380. ) {
  381. super();
  382. this.serviceName = serviceName;
  383. this.authority = authority;
  384. this.multiMode = multiMode;
  385. }
  386. static fromJson(json = {}) {
  387. return new GrpcStreamSettings(
  388. json.serviceName,
  389. json.authority,
  390. json.multiMode
  391. );
  392. }
  393. toJson() {
  394. return {
  395. serviceName: this.serviceName,
  396. authority: this.authority,
  397. multiMode: this.multiMode,
  398. }
  399. }
  400. }
  401. export class HTTPUpgradeStreamSettings extends XrayCommonClass {
  402. constructor(
  403. acceptProxyProtocol = false,
  404. path = '/',
  405. host = '',
  406. headers = []
  407. ) {
  408. super();
  409. this.acceptProxyProtocol = acceptProxyProtocol;
  410. this.path = path;
  411. this.host = host;
  412. this.headers = headers;
  413. }
  414. addHeader(name, value) {
  415. this.headers.push({ name: name, value: value });
  416. }
  417. removeHeader(index) {
  418. this.headers.splice(index, 1);
  419. }
  420. static fromJson(json = {}) {
  421. return new HTTPUpgradeStreamSettings(
  422. json.acceptProxyProtocol,
  423. json.path,
  424. json.host,
  425. XrayCommonClass.toHeaders(json.headers),
  426. );
  427. }
  428. toJson() {
  429. return {
  430. acceptProxyProtocol: this.acceptProxyProtocol,
  431. path: this.path,
  432. host: this.host,
  433. headers: XrayCommonClass.toV2Headers(this.headers, false),
  434. };
  435. }
  436. }
  437. // Mirrors the inbound (server-side) view of Xray-core's SplitHTTPConfig
  438. // (infra/conf/transport_internet.go). Only fields the server actually
  439. // reads at runtime, plus the bidirectional fields the server enforces,
  440. // live here. Client-only fields (uplinkHTTPMethod, uplinkChunkSize,
  441. // noGRPCHeader, scMinPostsIntervalMs, xmux, downloadSettings) belong on
  442. // the outbound class instead.
  443. //
  444. // `headers` is technically client-only at runtime (xray's listener
  445. // doesn't read it) but we keep it here so the admin can set request
  446. // headers that get embedded into the share link's `extra` blob — the
  447. // client picks them up from there.
  448. export class xHTTPStreamSettings extends XrayCommonClass {
  449. constructor(
  450. // Bidirectional — must match between client and server
  451. path = '/',
  452. host = '',
  453. mode = MODE_OPTION.AUTO,
  454. xPaddingBytes = "100-1000",
  455. xPaddingObfsMode = false,
  456. xPaddingKey = '',
  457. xPaddingHeader = '',
  458. xPaddingPlacement = '',
  459. xPaddingMethod = '',
  460. sessionPlacement = '',
  461. sessionKey = '',
  462. seqPlacement = '',
  463. seqKey = '',
  464. uplinkDataPlacement = '',
  465. uplinkDataKey = '',
  466. scMaxEachPostBytes = "1000000",
  467. // Server-side only
  468. noSSEHeader = false,
  469. scMaxBufferedPosts = 30,
  470. scStreamUpServerSecs = "20-80",
  471. serverMaxHeaderBytes = 0,
  472. // URL-share only — embedded in the link's `extra` blob so clients
  473. // pick them up; xray's listener ignores them at runtime.
  474. headers = [],
  475. ) {
  476. super();
  477. this.path = path;
  478. this.host = host;
  479. this.mode = mode;
  480. this.xPaddingBytes = xPaddingBytes;
  481. this.xPaddingObfsMode = xPaddingObfsMode;
  482. this.xPaddingKey = xPaddingKey;
  483. this.xPaddingHeader = xPaddingHeader;
  484. this.xPaddingPlacement = xPaddingPlacement;
  485. this.xPaddingMethod = xPaddingMethod;
  486. this.sessionPlacement = sessionPlacement;
  487. this.sessionKey = sessionKey;
  488. this.seqPlacement = seqPlacement;
  489. this.seqKey = seqKey;
  490. this.uplinkDataPlacement = uplinkDataPlacement;
  491. this.uplinkDataKey = uplinkDataKey;
  492. this.scMaxEachPostBytes = scMaxEachPostBytes;
  493. this.noSSEHeader = noSSEHeader;
  494. this.scMaxBufferedPosts = scMaxBufferedPosts;
  495. this.scStreamUpServerSecs = scStreamUpServerSecs;
  496. this.serverMaxHeaderBytes = serverMaxHeaderBytes;
  497. this.headers = headers;
  498. }
  499. addHeader(name, value) {
  500. this.headers.push({ name: name, value: value });
  501. }
  502. removeHeader(index) {
  503. this.headers.splice(index, 1);
  504. }
  505. static fromJson(json = {}) {
  506. return new xHTTPStreamSettings(
  507. json.path,
  508. json.host,
  509. json.mode,
  510. json.xPaddingBytes,
  511. json.xPaddingObfsMode,
  512. json.xPaddingKey,
  513. json.xPaddingHeader,
  514. json.xPaddingPlacement,
  515. json.xPaddingMethod,
  516. json.sessionPlacement,
  517. json.sessionKey,
  518. json.seqPlacement,
  519. json.seqKey,
  520. json.uplinkDataPlacement,
  521. json.uplinkDataKey,
  522. json.scMaxEachPostBytes,
  523. json.noSSEHeader,
  524. json.scMaxBufferedPosts,
  525. json.scStreamUpServerSecs,
  526. json.serverMaxHeaderBytes,
  527. XrayCommonClass.toHeaders(json.headers),
  528. );
  529. }
  530. toJson() {
  531. return {
  532. path: this.path,
  533. host: this.host,
  534. mode: this.mode,
  535. xPaddingBytes: this.xPaddingBytes,
  536. xPaddingObfsMode: this.xPaddingObfsMode,
  537. xPaddingKey: this.xPaddingKey,
  538. xPaddingHeader: this.xPaddingHeader,
  539. xPaddingPlacement: this.xPaddingPlacement,
  540. xPaddingMethod: this.xPaddingMethod,
  541. sessionPlacement: this.sessionPlacement,
  542. sessionKey: this.sessionKey,
  543. seqPlacement: this.seqPlacement,
  544. seqKey: this.seqKey,
  545. uplinkDataPlacement: this.uplinkDataPlacement,
  546. uplinkDataKey: this.uplinkDataKey,
  547. scMaxEachPostBytes: this.scMaxEachPostBytes,
  548. noSSEHeader: this.noSSEHeader,
  549. scMaxBufferedPosts: this.scMaxBufferedPosts,
  550. scStreamUpServerSecs: this.scStreamUpServerSecs,
  551. serverMaxHeaderBytes: this.serverMaxHeaderBytes,
  552. headers: XrayCommonClass.toV2Headers(this.headers, false),
  553. };
  554. }
  555. }
  556. export class HysteriaStreamSettings extends XrayCommonClass {
  557. constructor(
  558. protocol,
  559. version = 2,
  560. auth = '',
  561. udpIdleTimeout = 60,
  562. masquerade,
  563. ) {
  564. super(protocol);
  565. this.version = version;
  566. this.auth = auth;
  567. this.udpIdleTimeout = udpIdleTimeout;
  568. this.masquerade = masquerade;
  569. }
  570. static fromJson(json = {}) {
  571. return new HysteriaStreamSettings(
  572. json.protocol,
  573. json.version ?? 2,
  574. json.auth ?? '',
  575. json.udpIdleTimeout ?? 60,
  576. json.masquerade ? HysteriaMasquerade.fromJson(json.masquerade) : undefined,
  577. );
  578. }
  579. toJson() {
  580. return {
  581. protocol: this.protocol,
  582. version: this.version,
  583. auth: this.auth,
  584. udpIdleTimeout: this.udpIdleTimeout,
  585. masquerade: this.masqueradeSwitch ? this.masquerade.toJson() : undefined,
  586. };
  587. }
  588. get masqueradeSwitch() {
  589. return this.masquerade != undefined;
  590. }
  591. set masqueradeSwitch(value) {
  592. this.masquerade = value ? new HysteriaMasquerade() : undefined;
  593. }
  594. };
  595. export class HysteriaMasquerade extends XrayCommonClass {
  596. constructor(
  597. type = 'proxy',
  598. dir = '',
  599. url = '',
  600. rewriteHost = false,
  601. insecure = false,
  602. content = '',
  603. headers = [],
  604. statusCode = 0,
  605. ) {
  606. super();
  607. this.type = type;
  608. this.dir = dir;
  609. this.url = url;
  610. this.rewriteHost = rewriteHost;
  611. this.insecure = insecure;
  612. this.content = content;
  613. this.headers = headers;
  614. this.statusCode = statusCode;
  615. }
  616. addHeader(name, value) {
  617. this.headers.push({ name: name, value: value });
  618. }
  619. removeHeader(index) {
  620. this.headers.splice(index, 1);
  621. }
  622. static fromJson(json = {}) {
  623. const type = ['proxy', 'file', 'string'].includes(json.type) ? json.type : 'proxy';
  624. return new HysteriaMasquerade(
  625. type,
  626. json.dir,
  627. json.url,
  628. json.rewriteHost,
  629. json.insecure,
  630. json.content,
  631. XrayCommonClass.toHeaders(json.headers),
  632. json.statusCode,
  633. );
  634. }
  635. toJson() {
  636. return {
  637. type: this.type,
  638. dir: this.dir,
  639. url: this.url,
  640. rewriteHost: this.rewriteHost,
  641. insecure: this.insecure,
  642. content: this.content,
  643. headers: XrayCommonClass.toV2Headers(this.headers, false),
  644. statusCode: this.statusCode,
  645. };
  646. }
  647. };
  648. export class TlsStreamSettings extends XrayCommonClass {
  649. constructor(
  650. serverName = '',
  651. minVersion = TLS_VERSION_OPTION.TLS12,
  652. maxVersion = TLS_VERSION_OPTION.TLS13,
  653. cipherSuites = '',
  654. rejectUnknownSni = false,
  655. disableSystemRoot = false,
  656. enableSessionResumption = false,
  657. certificates = [new TlsStreamSettings.Cert()],
  658. alpn = [ALPN_OPTION.H2, ALPN_OPTION.HTTP1],
  659. echServerKeys = '',
  660. settings = new TlsStreamSettings.Settings()
  661. ) {
  662. super();
  663. this.sni = serverName;
  664. this.minVersion = minVersion;
  665. this.maxVersion = maxVersion;
  666. this.cipherSuites = cipherSuites;
  667. this.rejectUnknownSni = rejectUnknownSni;
  668. this.disableSystemRoot = disableSystemRoot;
  669. this.enableSessionResumption = enableSessionResumption;
  670. this.certs = certificates;
  671. this.alpn = alpn;
  672. this.echServerKeys = echServerKeys;
  673. this.settings = settings;
  674. }
  675. addCert() {
  676. this.certs.push(new TlsStreamSettings.Cert());
  677. }
  678. removeCert(index) {
  679. this.certs.splice(index, 1);
  680. }
  681. static fromJson(json = {}) {
  682. let certs;
  683. let settings;
  684. if (!ObjectUtil.isEmpty(json.certificates)) {
  685. certs = json.certificates.map(cert => TlsStreamSettings.Cert.fromJson(cert));
  686. }
  687. if (!ObjectUtil.isEmpty(json.settings)) {
  688. settings = new TlsStreamSettings.Settings(json.settings.fingerprint, json.settings.echConfigList);
  689. }
  690. return new TlsStreamSettings(
  691. json.serverName,
  692. json.minVersion,
  693. json.maxVersion,
  694. json.cipherSuites,
  695. json.rejectUnknownSni,
  696. json.disableSystemRoot,
  697. json.enableSessionResumption,
  698. certs,
  699. json.alpn,
  700. json.echServerKeys,
  701. settings,
  702. );
  703. }
  704. toJson() {
  705. return {
  706. serverName: this.sni,
  707. minVersion: this.minVersion,
  708. maxVersion: this.maxVersion,
  709. cipherSuites: this.cipherSuites,
  710. rejectUnknownSni: this.rejectUnknownSni,
  711. disableSystemRoot: this.disableSystemRoot,
  712. enableSessionResumption: this.enableSessionResumption,
  713. certificates: TlsStreamSettings.toJsonArray(this.certs),
  714. alpn: this.alpn,
  715. echServerKeys: this.echServerKeys,
  716. settings: this.settings,
  717. };
  718. }
  719. }
  720. TlsStreamSettings.Cert = class extends XrayCommonClass {
  721. constructor(
  722. useFile = true,
  723. certificateFile = '',
  724. keyFile = '',
  725. certificate = '',
  726. key = '',
  727. oneTimeLoading = false,
  728. usage = USAGE_OPTION.ENCIPHERMENT,
  729. buildChain = false,
  730. ) {
  731. super();
  732. this.useFile = useFile;
  733. this.certFile = certificateFile;
  734. this.keyFile = keyFile;
  735. this.cert = Array.isArray(certificate) ? certificate.join('\n') : certificate;
  736. this.key = Array.isArray(key) ? key.join('\n') : key;
  737. this.oneTimeLoading = oneTimeLoading;
  738. this.usage = usage;
  739. this.buildChain = buildChain
  740. }
  741. static fromJson(json = {}) {
  742. if ('certificateFile' in json && 'keyFile' in json) {
  743. return new TlsStreamSettings.Cert(
  744. true,
  745. json.certificateFile,
  746. json.keyFile, '', '',
  747. json.oneTimeLoading,
  748. json.usage,
  749. json.buildChain,
  750. );
  751. } else {
  752. return new TlsStreamSettings.Cert(
  753. false, '', '',
  754. json.certificate.join('\n'),
  755. json.key.join('\n'),
  756. json.oneTimeLoading,
  757. json.usage,
  758. json.buildChain,
  759. );
  760. }
  761. }
  762. toJson() {
  763. if (this.useFile) {
  764. return {
  765. certificateFile: this.certFile,
  766. keyFile: this.keyFile,
  767. oneTimeLoading: this.oneTimeLoading,
  768. usage: this.usage,
  769. buildChain: this.buildChain,
  770. };
  771. } else {
  772. return {
  773. certificate: this.cert.split('\n'),
  774. key: this.key.split('\n'),
  775. oneTimeLoading: this.oneTimeLoading,
  776. usage: this.usage,
  777. buildChain: this.buildChain,
  778. };
  779. }
  780. }
  781. };
  782. TlsStreamSettings.Settings = class extends XrayCommonClass {
  783. constructor(
  784. fingerprint = UTLS_FINGERPRINT.UTLS_CHROME,
  785. echConfigList = '',
  786. ) {
  787. super();
  788. this.fingerprint = fingerprint;
  789. this.echConfigList = echConfigList;
  790. }
  791. static fromJson(json = {}) {
  792. return new TlsStreamSettings.Settings(
  793. json.fingerprint,
  794. json.echConfigList,
  795. );
  796. }
  797. toJson() {
  798. return {
  799. fingerprint: this.fingerprint,
  800. echConfigList: this.echConfigList
  801. };
  802. }
  803. };
  804. export class RealityStreamSettings extends XrayCommonClass {
  805. constructor(
  806. show = false,
  807. xver = 0,
  808. target = '',
  809. serverNames = '',
  810. privateKey = '',
  811. minClientVer = '',
  812. maxClientVer = '',
  813. maxTimediff = 0,
  814. shortIds = RandomUtil.randomShortIds(),
  815. mldsa65Seed = '',
  816. settings = new RealityStreamSettings.Settings()
  817. ) {
  818. super();
  819. // If target/serverNames are not provided, use random values
  820. if (!target && !serverNames) {
  821. const randomTarget = typeof getRandomRealityTarget !== 'undefined'
  822. ? getRandomRealityTarget()
  823. : { target: 'www.amazon.com:443', sni: 'www.amazon.com,amazon.com' };
  824. target = randomTarget.target;
  825. serverNames = randomTarget.sni;
  826. }
  827. this.show = show;
  828. this.xver = xver;
  829. this.target = target;
  830. this.serverNames = Array.isArray(serverNames) ? serverNames.join(",") : serverNames;
  831. this.privateKey = privateKey;
  832. this.minClientVer = minClientVer;
  833. this.maxClientVer = maxClientVer;
  834. this.maxTimediff = maxTimediff;
  835. this.shortIds = Array.isArray(shortIds) ? shortIds.join(",") : shortIds;
  836. this.mldsa65Seed = mldsa65Seed;
  837. this.settings = settings;
  838. }
  839. static fromJson(json = {}) {
  840. let settings;
  841. if (!ObjectUtil.isEmpty(json.settings)) {
  842. settings = new RealityStreamSettings.Settings(
  843. json.settings.publicKey,
  844. json.settings.fingerprint,
  845. json.settings.serverName,
  846. json.settings.spiderX,
  847. json.settings.mldsa65Verify,
  848. );
  849. }
  850. return new RealityStreamSettings(
  851. json.show,
  852. json.xver,
  853. json.target,
  854. json.serverNames,
  855. json.privateKey,
  856. json.minClientVer,
  857. json.maxClientVer,
  858. json.maxTimediff,
  859. json.shortIds,
  860. json.mldsa65Seed,
  861. settings,
  862. );
  863. }
  864. toJson() {
  865. return {
  866. show: this.show,
  867. xver: this.xver,
  868. target: this.target,
  869. serverNames: this.serverNames.split(","),
  870. privateKey: this.privateKey,
  871. minClientVer: this.minClientVer,
  872. maxClientVer: this.maxClientVer,
  873. maxTimediff: this.maxTimediff,
  874. shortIds: this.shortIds.split(","),
  875. mldsa65Seed: this.mldsa65Seed,
  876. settings: this.settings,
  877. };
  878. }
  879. }
  880. RealityStreamSettings.Settings = class extends XrayCommonClass {
  881. constructor(
  882. publicKey = '',
  883. fingerprint = UTLS_FINGERPRINT.UTLS_CHROME,
  884. serverName = '',
  885. spiderX = '/',
  886. mldsa65Verify = ''
  887. ) {
  888. super();
  889. this.publicKey = publicKey;
  890. this.fingerprint = fingerprint;
  891. this.serverName = serverName;
  892. this.spiderX = spiderX;
  893. this.mldsa65Verify = mldsa65Verify;
  894. }
  895. static fromJson(json = {}) {
  896. return new RealityStreamSettings.Settings(
  897. json.publicKey,
  898. json.fingerprint,
  899. json.serverName,
  900. json.spiderX,
  901. json.mldsa65Verify
  902. );
  903. }
  904. toJson() {
  905. return {
  906. publicKey: this.publicKey,
  907. fingerprint: this.fingerprint,
  908. serverName: this.serverName,
  909. spiderX: this.spiderX,
  910. mldsa65Verify: this.mldsa65Verify
  911. };
  912. }
  913. };
  914. export class SockoptStreamSettings extends XrayCommonClass {
  915. constructor(
  916. acceptProxyProtocol = false,
  917. tcpFastOpen = false,
  918. mark = 0,
  919. tproxy = "off",
  920. tcpMptcp = false,
  921. penetrate = false,
  922. domainStrategy = DOMAIN_STRATEGY_OPTION.USE_IP,
  923. tcpMaxSeg = 1440,
  924. dialerProxy = "",
  925. tcpKeepAliveInterval = 0,
  926. tcpKeepAliveIdle = 300,
  927. tcpUserTimeout = 10000,
  928. tcpcongestion = TCP_CONGESTION_OPTION.BBR,
  929. V6Only = false,
  930. tcpWindowClamp = 600,
  931. interfaceName = "",
  932. trustedXForwardedFor = [],
  933. ) {
  934. super();
  935. this.acceptProxyProtocol = acceptProxyProtocol;
  936. this.tcpFastOpen = tcpFastOpen;
  937. this.mark = mark;
  938. this.tproxy = tproxy;
  939. this.tcpMptcp = tcpMptcp;
  940. this.penetrate = penetrate;
  941. this.domainStrategy = domainStrategy;
  942. this.tcpMaxSeg = tcpMaxSeg;
  943. this.dialerProxy = dialerProxy;
  944. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  945. this.tcpKeepAliveIdle = tcpKeepAliveIdle;
  946. this.tcpUserTimeout = tcpUserTimeout;
  947. this.tcpcongestion = tcpcongestion;
  948. this.V6Only = V6Only;
  949. this.tcpWindowClamp = tcpWindowClamp;
  950. this.interfaceName = interfaceName;
  951. this.trustedXForwardedFor = trustedXForwardedFor;
  952. }
  953. static fromJson(json = {}) {
  954. if (Object.keys(json).length === 0) return undefined;
  955. return new SockoptStreamSettings(
  956. json.acceptProxyProtocol,
  957. json.tcpFastOpen,
  958. json.mark,
  959. json.tproxy,
  960. json.tcpMptcp,
  961. json.penetrate,
  962. json.domainStrategy,
  963. json.tcpMaxSeg,
  964. json.dialerProxy,
  965. json.tcpKeepAliveInterval,
  966. json.tcpKeepAliveIdle,
  967. json.tcpUserTimeout,
  968. json.tcpcongestion,
  969. json.V6Only,
  970. json.tcpWindowClamp,
  971. json.interface,
  972. json.trustedXForwardedFor || [],
  973. );
  974. }
  975. toJson() {
  976. const result = {
  977. acceptProxyProtocol: this.acceptProxyProtocol,
  978. tcpFastOpen: this.tcpFastOpen,
  979. mark: this.mark,
  980. tproxy: this.tproxy,
  981. tcpMptcp: this.tcpMptcp,
  982. penetrate: this.penetrate,
  983. domainStrategy: this.domainStrategy,
  984. tcpMaxSeg: this.tcpMaxSeg,
  985. dialerProxy: this.dialerProxy,
  986. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  987. tcpKeepAliveIdle: this.tcpKeepAliveIdle,
  988. tcpUserTimeout: this.tcpUserTimeout,
  989. tcpcongestion: this.tcpcongestion,
  990. V6Only: this.V6Only,
  991. tcpWindowClamp: this.tcpWindowClamp,
  992. interface: this.interfaceName,
  993. };
  994. if (this.trustedXForwardedFor && this.trustedXForwardedFor.length > 0) {
  995. result.trustedXForwardedFor = this.trustedXForwardedFor;
  996. }
  997. return result;
  998. }
  999. }
  1000. export class UdpMask extends XrayCommonClass {
  1001. constructor(type = 'salamander', settings = {}) {
  1002. super();
  1003. this.type = type;
  1004. this.settings = this._getDefaultSettings(type, settings);
  1005. }
  1006. _getDefaultSettings(type, settings = {}) {
  1007. switch (type) {
  1008. case 'salamander':
  1009. case 'mkcp-aes128gcm':
  1010. return { password: settings.password || '' };
  1011. case 'header-dns':
  1012. return { domain: settings.domain || '' };
  1013. case 'xdns':
  1014. return { domains: Array.isArray(settings.domains) ? settings.domains : [] };
  1015. case 'xicmp':
  1016. return { ip: settings.ip || '', id: settings.id ?? 0 };
  1017. case 'mkcp-original':
  1018. case 'header-dtls':
  1019. case 'header-srtp':
  1020. case 'header-utp':
  1021. case 'header-wechat':
  1022. case 'header-wireguard':
  1023. return {};
  1024. case 'header-custom':
  1025. return {
  1026. client: Array.isArray(settings.client) ? settings.client : [],
  1027. server: Array.isArray(settings.server) ? settings.server : [],
  1028. };
  1029. case 'noise':
  1030. return {
  1031. reset: settings.reset ?? 0,
  1032. noise: Array.isArray(settings.noise) ? settings.noise : [],
  1033. };
  1034. default:
  1035. return settings;
  1036. }
  1037. }
  1038. static fromJson(json = {}) {
  1039. return new UdpMask(
  1040. json.type || 'salamander',
  1041. json.settings || {}
  1042. );
  1043. }
  1044. toJson() {
  1045. const cleanItem = item => {
  1046. const out = { ...item };
  1047. if (out.type === 'array') {
  1048. delete out.packet;
  1049. } else {
  1050. delete out.rand;
  1051. delete out.randRange;
  1052. }
  1053. return out;
  1054. };
  1055. let settings = this.settings;
  1056. if (this.type === 'noise' && settings && Array.isArray(settings.noise)) {
  1057. settings = { ...settings, noise: settings.noise.map(cleanItem) };
  1058. } else if (this.type === 'header-custom' && settings) {
  1059. settings = {
  1060. ...settings,
  1061. client: Array.isArray(settings.client) ? settings.client.map(cleanItem) : settings.client,
  1062. server: Array.isArray(settings.server) ? settings.server.map(cleanItem) : settings.server,
  1063. };
  1064. }
  1065. return {
  1066. type: this.type,
  1067. settings: (settings && Object.keys(settings).length > 0) ? settings : undefined
  1068. };
  1069. }
  1070. }
  1071. export class TcpMask extends XrayCommonClass {
  1072. constructor(type = 'fragment', settings = {}) {
  1073. super();
  1074. this.type = type;
  1075. this.settings = this._getDefaultSettings(type, settings);
  1076. }
  1077. _getDefaultSettings(type, settings = {}) {
  1078. switch (type) {
  1079. case 'fragment':
  1080. return {
  1081. packets: settings.packets ?? 'tlshello',
  1082. length: settings.length ?? '',
  1083. delay: settings.delay ?? '',
  1084. maxSplit: settings.maxSplit ?? '',
  1085. };
  1086. case 'sudoku':
  1087. return {
  1088. password: settings.password ?? '',
  1089. ascii: settings.ascii ?? '',
  1090. customTable: settings.customTable ?? '',
  1091. customTables: Array.isArray(settings.customTables) ? settings.customTables : [],
  1092. paddingMin: settings.paddingMin ?? 0,
  1093. paddingMax: settings.paddingMax ?? 0,
  1094. };
  1095. case 'header-custom':
  1096. return {
  1097. clients: Array.isArray(settings.clients) ? settings.clients : [],
  1098. servers: Array.isArray(settings.servers) ? settings.servers : [],
  1099. };
  1100. default:
  1101. return settings;
  1102. }
  1103. }
  1104. static fromJson(json = {}) {
  1105. return new TcpMask(
  1106. json.type || 'fragment',
  1107. json.settings || {}
  1108. );
  1109. }
  1110. toJson() {
  1111. const cleanItem = item => {
  1112. const out = { ...item };
  1113. if (out.type === 'array') {
  1114. delete out.packet;
  1115. } else {
  1116. delete out.rand;
  1117. delete out.randRange;
  1118. }
  1119. return out;
  1120. };
  1121. let settings = this.settings;
  1122. if (this.type === 'header-custom' && settings) {
  1123. const cleanGroup = group => Array.isArray(group) ? group.map(cleanItem) : group;
  1124. settings = {
  1125. ...settings,
  1126. clients: Array.isArray(settings.clients) ? settings.clients.map(cleanGroup) : settings.clients,
  1127. servers: Array.isArray(settings.servers) ? settings.servers.map(cleanGroup) : settings.servers,
  1128. };
  1129. }
  1130. return {
  1131. type: this.type,
  1132. settings: (settings && Object.keys(settings).length > 0) ? settings : undefined
  1133. };
  1134. }
  1135. }
  1136. export class QuicParams extends XrayCommonClass {
  1137. constructor(
  1138. congestion = 'bbr',
  1139. debug = false,
  1140. brutalUp = 65537,
  1141. brutalDown = 65537,
  1142. udpHop = undefined,
  1143. initStreamReceiveWindow = 8388608,
  1144. maxStreamReceiveWindow = 8388608,
  1145. initConnectionReceiveWindow = 20971520,
  1146. maxConnectionReceiveWindow = 20971520,
  1147. maxIdleTimeout = 30,
  1148. keepAlivePeriod = 5,
  1149. disablePathMTUDiscovery = false,
  1150. maxIncomingStreams = 1024,
  1151. ) {
  1152. super();
  1153. this.congestion = congestion;
  1154. this.debug = debug;
  1155. this.brutalUp = brutalUp;
  1156. this.brutalDown = brutalDown;
  1157. this.udpHop = udpHop;
  1158. this.initStreamReceiveWindow = initStreamReceiveWindow;
  1159. this.maxStreamReceiveWindow = maxStreamReceiveWindow;
  1160. this.initConnectionReceiveWindow = initConnectionReceiveWindow;
  1161. this.maxConnectionReceiveWindow = maxConnectionReceiveWindow;
  1162. this.maxIdleTimeout = maxIdleTimeout;
  1163. this.keepAlivePeriod = keepAlivePeriod;
  1164. this.disablePathMTUDiscovery = disablePathMTUDiscovery;
  1165. this.maxIncomingStreams = maxIncomingStreams;
  1166. }
  1167. get hasUdpHop() {
  1168. return this.udpHop != null;
  1169. }
  1170. set hasUdpHop(value) {
  1171. this.udpHop = value ? (this.udpHop || { ports: '20000-50000', interval: '5-10' }) : undefined;
  1172. }
  1173. static fromJson(json = {}) {
  1174. if (!json || Object.keys(json).length === 0) return undefined;
  1175. return new QuicParams(
  1176. json.congestion,
  1177. json.debug,
  1178. json.brutalUp,
  1179. json.brutalDown,
  1180. json.udpHop ? { ports: json.udpHop.ports, interval: json.udpHop.interval } : undefined,
  1181. json.initStreamReceiveWindow,
  1182. json.maxStreamReceiveWindow,
  1183. json.initConnectionReceiveWindow,
  1184. json.maxConnectionReceiveWindow,
  1185. json.maxIdleTimeout,
  1186. json.keepAlivePeriod,
  1187. json.disablePathMTUDiscovery,
  1188. json.maxIncomingStreams,
  1189. );
  1190. }
  1191. toJson() {
  1192. const result = { congestion: this.congestion };
  1193. if (this.debug) result.debug = this.debug;
  1194. if (['brutal', 'force-brutal'].includes(this.congestion)) {
  1195. if (this.brutalUp) result.brutalUp = this.brutalUp;
  1196. if (this.brutalDown) result.brutalDown = this.brutalDown;
  1197. }
  1198. if (this.udpHop) result.udpHop = { ports: this.udpHop.ports, interval: this.udpHop.interval };
  1199. if (this.initStreamReceiveWindow > 0) result.initStreamReceiveWindow = this.initStreamReceiveWindow;
  1200. if (this.maxStreamReceiveWindow > 0) result.maxStreamReceiveWindow = this.maxStreamReceiveWindow;
  1201. if (this.initConnectionReceiveWindow > 0) result.initConnectionReceiveWindow = this.initConnectionReceiveWindow;
  1202. if (this.maxConnectionReceiveWindow > 0) result.maxConnectionReceiveWindow = this.maxConnectionReceiveWindow;
  1203. if (this.maxIdleTimeout !== 30 && this.maxIdleTimeout > 0) result.maxIdleTimeout = this.maxIdleTimeout;
  1204. if (this.keepAlivePeriod > 0) result.keepAlivePeriod = this.keepAlivePeriod;
  1205. if (this.disablePathMTUDiscovery) result.disablePathMTUDiscovery = this.disablePathMTUDiscovery;
  1206. if (this.maxIncomingStreams > 0) result.maxIncomingStreams = this.maxIncomingStreams;
  1207. return result;
  1208. }
  1209. }
  1210. export class FinalMaskStreamSettings extends XrayCommonClass {
  1211. constructor(tcp = [], udp = [], quicParams = undefined) {
  1212. super();
  1213. this.tcp = Array.isArray(tcp) ? tcp.map(t => t instanceof TcpMask ? t : new TcpMask(t.type, t.settings)) : [];
  1214. this.udp = Array.isArray(udp) ? udp.map(u => new UdpMask(u.type, u.settings)) : [new UdpMask(udp.type, udp.settings)];
  1215. this.quicParams = quicParams instanceof QuicParams ? quicParams : (quicParams ? QuicParams.fromJson(quicParams) : undefined);
  1216. }
  1217. get enableQuicParams() {
  1218. return this.quicParams != null;
  1219. }
  1220. set enableQuicParams(value) {
  1221. this.quicParams = value ? (this.quicParams || new QuicParams()) : undefined;
  1222. }
  1223. static fromJson(json = {}) {
  1224. return new FinalMaskStreamSettings(
  1225. json.tcp || [],
  1226. json.udp || [],
  1227. json.quicParams ? QuicParams.fromJson(json.quicParams) : undefined,
  1228. );
  1229. }
  1230. toJson() {
  1231. const result = {};
  1232. if (this.tcp && this.tcp.length > 0) {
  1233. result.tcp = this.tcp.map(t => t.toJson());
  1234. }
  1235. if (this.udp && this.udp.length > 0) {
  1236. result.udp = this.udp.map(udp => udp.toJson());
  1237. }
  1238. if (this.quicParams) {
  1239. result.quicParams = this.quicParams.toJson();
  1240. }
  1241. return result;
  1242. }
  1243. }
  1244. export class StreamSettings extends XrayCommonClass {
  1245. constructor(network = 'tcp',
  1246. security = 'none',
  1247. externalProxy = [],
  1248. tlsSettings = new TlsStreamSettings(),
  1249. realitySettings = new RealityStreamSettings(),
  1250. tcpSettings = new TcpStreamSettings(),
  1251. kcpSettings = new KcpStreamSettings(),
  1252. wsSettings = new WsStreamSettings(),
  1253. grpcSettings = new GrpcStreamSettings(),
  1254. httpupgradeSettings = new HTTPUpgradeStreamSettings(),
  1255. xhttpSettings = new xHTTPStreamSettings(),
  1256. hysteriaSettings = new HysteriaStreamSettings(),
  1257. finalmask = new FinalMaskStreamSettings(),
  1258. sockopt = undefined,
  1259. ) {
  1260. super();
  1261. this.network = network;
  1262. this.security = security;
  1263. this.externalProxy = externalProxy;
  1264. this.tls = tlsSettings;
  1265. this.reality = realitySettings;
  1266. this.tcp = tcpSettings;
  1267. this.kcp = kcpSettings;
  1268. this.ws = wsSettings;
  1269. this.grpc = grpcSettings;
  1270. this.httpupgrade = httpupgradeSettings;
  1271. this.xhttp = xhttpSettings;
  1272. this.hysteria = hysteriaSettings;
  1273. this.finalmask = finalmask;
  1274. this.sockopt = sockopt;
  1275. }
  1276. addTcpMask(type = 'fragment') {
  1277. this.finalmask.tcp.push(new TcpMask(type));
  1278. }
  1279. delTcpMask(index) {
  1280. if (this.finalmask.tcp) {
  1281. this.finalmask.tcp.splice(index, 1);
  1282. }
  1283. }
  1284. addUdpMask(type = 'salamander') {
  1285. this.finalmask.udp.push(new UdpMask(type));
  1286. }
  1287. delUdpMask(index) {
  1288. if (this.finalmask.udp) {
  1289. this.finalmask.udp.splice(index, 1);
  1290. }
  1291. }
  1292. get hasFinalMask() {
  1293. const hasTcp = this.finalmask.tcp && this.finalmask.tcp.length > 0;
  1294. const hasUdp = this.finalmask.udp && this.finalmask.udp.length > 0;
  1295. const hasQuicParams = this.finalmask.quicParams != null;
  1296. return hasTcp || hasUdp || hasQuicParams;
  1297. }
  1298. get isTls() {
  1299. return this.security === "tls";
  1300. }
  1301. set isTls(isTls) {
  1302. if (isTls) {
  1303. this.security = 'tls';
  1304. } else {
  1305. this.security = 'none';
  1306. }
  1307. }
  1308. //for Reality
  1309. get isReality() {
  1310. return this.security === "reality";
  1311. }
  1312. set isReality(isReality) {
  1313. if (isReality) {
  1314. this.security = 'reality';
  1315. } else {
  1316. this.security = 'none';
  1317. }
  1318. }
  1319. get sockoptSwitch() {
  1320. return this.sockopt != undefined;
  1321. }
  1322. set sockoptSwitch(value) {
  1323. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  1324. }
  1325. static fromJson(json = {}) {
  1326. return new StreamSettings(
  1327. json.network,
  1328. json.security,
  1329. json.externalProxy,
  1330. TlsStreamSettings.fromJson(json.tlsSettings),
  1331. RealityStreamSettings.fromJson(json.realitySettings),
  1332. TcpStreamSettings.fromJson(json.tcpSettings),
  1333. KcpStreamSettings.fromJson(json.kcpSettings),
  1334. WsStreamSettings.fromJson(json.wsSettings),
  1335. GrpcStreamSettings.fromJson(json.grpcSettings),
  1336. HTTPUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  1337. xHTTPStreamSettings.fromJson(json.xhttpSettings),
  1338. HysteriaStreamSettings.fromJson(json.hysteriaSettings),
  1339. FinalMaskStreamSettings.fromJson(json.finalmask),
  1340. SockoptStreamSettings.fromJson(json.sockopt),
  1341. );
  1342. }
  1343. toJson() {
  1344. const network = this.network;
  1345. return {
  1346. network: network,
  1347. security: this.security,
  1348. externalProxy: this.externalProxy,
  1349. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  1350. realitySettings: this.isReality ? this.reality.toJson() : undefined,
  1351. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  1352. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  1353. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  1354. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  1355. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  1356. xhttpSettings: network === 'xhttp' ? this.xhttp.toJson() : undefined,
  1357. hysteriaSettings: network === 'hysteria' ? this.hysteria.toJson() : undefined,
  1358. finalmask: this.hasFinalMask ? this.finalmask.toJson() : undefined,
  1359. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  1360. };
  1361. }
  1362. }
  1363. export class Sniffing extends XrayCommonClass {
  1364. constructor(
  1365. enabled = false,
  1366. destOverride = ['http', 'tls', 'quic', 'fakedns'],
  1367. metadataOnly = false,
  1368. routeOnly = false,
  1369. ipsExcluded = [],
  1370. domainsExcluded = []) {
  1371. super();
  1372. this.enabled = enabled;
  1373. this.destOverride = Array.isArray(destOverride) && destOverride.length > 0 ? destOverride : ['http', 'tls', 'quic', 'fakedns'];
  1374. this.metadataOnly = metadataOnly;
  1375. this.routeOnly = routeOnly;
  1376. this.ipsExcluded = Array.isArray(ipsExcluded) ? ipsExcluded : [];
  1377. this.domainsExcluded = Array.isArray(domainsExcluded) ? domainsExcluded : [];
  1378. }
  1379. static fromJson(json = {}) {
  1380. let destOverride = ObjectUtil.clone(json.destOverride);
  1381. if (ObjectUtil.isEmpty(destOverride) || ObjectUtil.isArrEmpty(destOverride) || ObjectUtil.isEmpty(destOverride[0])) {
  1382. destOverride = ['http', 'tls', 'quic', 'fakedns'];
  1383. }
  1384. return new Sniffing(
  1385. !!json.enabled,
  1386. destOverride,
  1387. json.metadataOnly,
  1388. json.routeOnly,
  1389. json.ipsExcluded || [],
  1390. json.domainsExcluded || [],
  1391. );
  1392. }
  1393. toJson() {
  1394. return {
  1395. enabled: this.enabled,
  1396. destOverride: this.destOverride,
  1397. metadataOnly: this.metadataOnly,
  1398. routeOnly: this.routeOnly,
  1399. ipsExcluded: this.ipsExcluded.length > 0 ? this.ipsExcluded : undefined,
  1400. domainsExcluded: this.domainsExcluded.length > 0 ? this.domainsExcluded : undefined,
  1401. };
  1402. }
  1403. }
  1404. export class Inbound extends XrayCommonClass {
  1405. constructor(
  1406. port = RandomUtil.randomInteger(10000, 60000),
  1407. listen = '',
  1408. protocol = Protocols.VLESS,
  1409. settings = null,
  1410. streamSettings = new StreamSettings(),
  1411. tag = '',
  1412. sniffing = new Sniffing(),
  1413. clientStats = '',
  1414. ) {
  1415. super();
  1416. this.port = port;
  1417. this.listen = listen;
  1418. this._protocol = protocol;
  1419. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  1420. this.stream = streamSettings;
  1421. this.tag = tag;
  1422. this.sniffing = sniffing;
  1423. this.clientStats = clientStats;
  1424. }
  1425. getClientStats() {
  1426. return this.clientStats;
  1427. }
  1428. // Looks for a "host"-named entry in xhttp.headers and returns its value,
  1429. // or '' if not found. Used as a fallback when xhttp.host is empty so the
  1430. // share URL still carries a usable Host hint.
  1431. static xhttpHostFallback(xhttp) {
  1432. if (!xhttp || !Array.isArray(xhttp.headers)) return '';
  1433. for (const h of xhttp.headers) {
  1434. if (h && typeof h.name === 'string' && h.name.toLowerCase() === 'host') {
  1435. return h.value || '';
  1436. }
  1437. }
  1438. return '';
  1439. }
  1440. // Build the JSON blob that goes into the URL's `extra` param (or, for
  1441. // VMess, into the base64-encoded link object). Carries ONLY the
  1442. // bidirectional fields from xray-core's SplitHTTPConfig — i.e. the
  1443. // ones the server enforces and the client must match. Strictly
  1444. // one-sided fields are excluded:
  1445. //
  1446. // - server-only (noSSEHeader, scMaxBufferedPosts,
  1447. // scStreamUpServerSecs, serverMaxHeaderBytes) — client wouldn't
  1448. // read them, so emitting them just bloats the URL.
  1449. // - client-only (headers, uplinkHTTPMethod, uplinkChunkSize,
  1450. // noGRPCHeader, scMinPostsIntervalMs, xmux, downloadSettings) —
  1451. // not on the inbound class at all; the client configures them
  1452. // locally.
  1453. //
  1454. // Truthy-only guards keep default inbounds emitting the same compact
  1455. // URL they did before this helper grew.
  1456. static buildXhttpExtra(xhttp) {
  1457. if (!xhttp) return null;
  1458. const extra = {};
  1459. if (typeof xhttp.xPaddingBytes === 'string' && xhttp.xPaddingBytes.length > 0) {
  1460. extra.xPaddingBytes = xhttp.xPaddingBytes;
  1461. }
  1462. if (xhttp.xPaddingObfsMode === true) {
  1463. extra.xPaddingObfsMode = true;
  1464. ["xPaddingKey", "xPaddingHeader", "xPaddingPlacement", "xPaddingMethod"].forEach(k => {
  1465. if (typeof xhttp[k] === 'string' && xhttp[k].length > 0) {
  1466. extra[k] = xhttp[k];
  1467. }
  1468. });
  1469. }
  1470. const stringFields = [
  1471. "sessionPlacement", "sessionKey",
  1472. "seqPlacement", "seqKey",
  1473. "uplinkDataPlacement", "uplinkDataKey",
  1474. "scMaxEachPostBytes",
  1475. ];
  1476. for (const k of stringFields) {
  1477. const v = xhttp[k];
  1478. if (typeof v === 'string' && v.length > 0) extra[k] = v;
  1479. }
  1480. // Headers — emitted as the {name: value} map upstream's struct
  1481. // expects. The server runtime ignores this field, but the client
  1482. // (consuming the share link) honors it.
  1483. if (Array.isArray(xhttp.headers) && xhttp.headers.length > 0) {
  1484. const headersMap = {};
  1485. for (const h of xhttp.headers) {
  1486. if (h && h.name && h.name.toLowerCase() !== 'host') {
  1487. headersMap[h.name] = h.value || '';
  1488. }
  1489. }
  1490. if (Object.keys(headersMap).length > 0) extra.headers = headersMap;
  1491. }
  1492. return Object.keys(extra).length > 0 ? extra : null;
  1493. }
  1494. // Inject the inbound-side xhttp config into URL query params for
  1495. // vless/trojan/ss links. Sets path/host/mode at top level (xray's
  1496. // Build() always lets these win over `extra`) and packs the
  1497. // bidirectional fields into a JSON `extra` param. Also writes the
  1498. // flat `x_padding_bytes` param sing-box-family clients understand.
  1499. //
  1500. // Without this, the admin's custom xPaddingBytes / sessionKey / etc.
  1501. // never reach the client and handshakes are silently rejected with
  1502. // `invalid padding (...) length: 0`.
  1503. static applyXhttpExtraToParams(xhttp, params) {
  1504. if (!xhttp) return;
  1505. params.set("path", xhttp.path);
  1506. const host = xhttp.host?.length > 0 ? xhttp.host : Inbound.xhttpHostFallback(xhttp);
  1507. params.set("host", host);
  1508. params.set("mode", xhttp.mode);
  1509. // Flat fallback for sing-box-family clients that don't read `extra`.
  1510. if (typeof xhttp.xPaddingBytes === 'string' && xhttp.xPaddingBytes.length > 0) {
  1511. params.set("x_padding_bytes", xhttp.xPaddingBytes);
  1512. }
  1513. const extra = Inbound.buildXhttpExtra(xhttp);
  1514. if (extra) params.set("extra", JSON.stringify(extra));
  1515. }
  1516. // VMess variant: VMess links are a base64-encoded JSON object, so we
  1517. // copy the same bidirectional fields directly into the JSON instead
  1518. // of building a query string. (The base VMess link generator already
  1519. // sets net/type/path/host, so we only contribute the SplitHTTPConfig
  1520. // extra side here.)
  1521. static applyXhttpExtraToObj(xhttp, obj) {
  1522. if (!xhttp || !obj) return;
  1523. if (typeof xhttp.xPaddingBytes === 'string' && xhttp.xPaddingBytes.length > 0) {
  1524. obj.x_padding_bytes = xhttp.xPaddingBytes;
  1525. }
  1526. const extra = Inbound.buildXhttpExtra(xhttp);
  1527. if (!extra) return;
  1528. for (const [k, v] of Object.entries(extra)) {
  1529. obj[k] = v;
  1530. }
  1531. }
  1532. static hasShareableFinalMaskValue(value) {
  1533. if (value == null) {
  1534. return false;
  1535. }
  1536. if (Array.isArray(value)) {
  1537. return value.some(item => Inbound.hasShareableFinalMaskValue(item));
  1538. }
  1539. if (typeof value === 'object') {
  1540. return Object.values(value).some(item => Inbound.hasShareableFinalMaskValue(item));
  1541. }
  1542. if (typeof value === 'string') {
  1543. return value.length > 0;
  1544. }
  1545. return true;
  1546. }
  1547. static serializeFinalMask(finalmask) {
  1548. if (!finalmask) {
  1549. return '';
  1550. }
  1551. const value = typeof finalmask.toJson === 'function' ? finalmask.toJson() : finalmask;
  1552. return Inbound.hasShareableFinalMaskValue(value) ? JSON.stringify(value) : '';
  1553. }
  1554. // Export finalmask with the same compact JSON payload shape that
  1555. // v2rayN-compatible share links use: fm=<json>.
  1556. static applyFinalMaskToParams(finalmask, params) {
  1557. if (!params) return;
  1558. const payload = Inbound.serializeFinalMask(finalmask);
  1559. if (payload.length > 0) {
  1560. params.set("fm", payload);
  1561. }
  1562. }
  1563. // VMess links are a base64 JSON object, so keep the same fm payload
  1564. // under a flat property instead of a URL query string.
  1565. static applyFinalMaskToObj(finalmask, obj) {
  1566. if (!obj) return;
  1567. const payload = Inbound.serializeFinalMask(finalmask);
  1568. if (payload.length > 0) {
  1569. obj.fm = payload;
  1570. }
  1571. }
  1572. get clients() {
  1573. switch (this.protocol) {
  1574. case Protocols.VMESS: return this.settings.vmesses;
  1575. case Protocols.VLESS: return this.settings.vlesses;
  1576. case Protocols.TROJAN: return this.settings.trojans;
  1577. case Protocols.SHADOWSOCKS: return this.isSSMultiUser ? this.settings.shadowsockses : null;
  1578. case Protocols.HYSTERIA: return this.settings.hysterias;
  1579. default: return null;
  1580. }
  1581. }
  1582. get protocol() {
  1583. return this._protocol;
  1584. }
  1585. set protocol(protocol) {
  1586. this._protocol = protocol;
  1587. this.settings = Inbound.Settings.getSettings(protocol);
  1588. this.stream = new StreamSettings();
  1589. if (protocol === Protocols.TROJAN) {
  1590. this.tls = false;
  1591. }
  1592. if (protocol === Protocols.HYSTERIA) {
  1593. this.stream.network = 'hysteria';
  1594. this.stream.security = 'tls';
  1595. // Hysteria runs over QUIC and must not inherit TCP TLS ALPN defaults.
  1596. this.stream.tls.alpn = [ALPN_OPTION.H3];
  1597. }
  1598. }
  1599. get network() {
  1600. return this.stream.network;
  1601. }
  1602. set network(network) {
  1603. this.stream.network = network;
  1604. }
  1605. get isTcp() {
  1606. return this.network === "tcp";
  1607. }
  1608. get isWs() {
  1609. return this.network === "ws";
  1610. }
  1611. get isKcp() {
  1612. return this.network === "kcp";
  1613. }
  1614. get isGrpc() {
  1615. return this.network === "grpc";
  1616. }
  1617. get isHttpupgrade() {
  1618. return this.network === "httpupgrade";
  1619. }
  1620. get isXHTTP() {
  1621. return this.network === "xhttp";
  1622. }
  1623. // Shadowsocks
  1624. get method() {
  1625. switch (this.protocol) {
  1626. case Protocols.SHADOWSOCKS:
  1627. return this.settings.method;
  1628. default:
  1629. return "";
  1630. }
  1631. }
  1632. get isSSMultiUser() {
  1633. return this.method != SSMethods.BLAKE3_CHACHA20_POLY1305;
  1634. }
  1635. get isSS2022() {
  1636. return this.method.substring(0, 4) === "2022";
  1637. }
  1638. get serverName() {
  1639. if (this.stream.isTls) return this.stream.tls.sni;
  1640. if (this.stream.isReality) return this.stream.reality.serverNames;
  1641. return "";
  1642. }
  1643. getHeader(obj, name) {
  1644. for (const header of obj.headers) {
  1645. if (header.name.toLowerCase() === name.toLowerCase()) {
  1646. return header.value;
  1647. }
  1648. }
  1649. return "";
  1650. }
  1651. get host() {
  1652. if (this.isTcp) {
  1653. return this.getHeader(this.stream.tcp.request, 'host');
  1654. } else if (this.isWs) {
  1655. return this.stream.ws.host?.length > 0 ? this.stream.ws.host : this.getHeader(this.stream.ws, 'host');
  1656. } else if (this.isHttpupgrade) {
  1657. return this.stream.httpupgrade.host?.length > 0 ? this.stream.httpupgrade.host : this.getHeader(this.stream.httpupgrade, 'host');
  1658. } else if (this.isXHTTP) {
  1659. return this.stream.xhttp.host?.length > 0 ? this.stream.xhttp.host : this.getHeader(this.stream.xhttp, 'host');
  1660. }
  1661. return null;
  1662. }
  1663. get path() {
  1664. if (this.isTcp) {
  1665. return this.stream.tcp.request.path[0];
  1666. } else if (this.isWs) {
  1667. return this.stream.ws.path;
  1668. } else if (this.isHttpupgrade) {
  1669. return this.stream.httpupgrade.path;
  1670. } else if (this.isXHTTP) {
  1671. return this.stream.xhttp.path;
  1672. }
  1673. return null;
  1674. }
  1675. get serviceName() {
  1676. return this.stream.grpc.serviceName;
  1677. }
  1678. isExpiry(index) {
  1679. let exp = this.clients[index].expiryTime;
  1680. return exp > 0 ? exp < new Date().getTime() : false;
  1681. }
  1682. canEnableTls() {
  1683. if (this.protocol === Protocols.HYSTERIA) return true;
  1684. if (![Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol)) return false;
  1685. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.network);
  1686. }
  1687. //this is used for xtls-rprx-vision
  1688. canEnableTlsFlow() {
  1689. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1690. return this.protocol === Protocols.VLESS;
  1691. }
  1692. return false;
  1693. }
  1694. // Vision seed applies only when the XTLS Vision (TCP/TLS) flow is selected.
  1695. // Excludes the UDP variant per spec.
  1696. canEnableVisionSeed() {
  1697. if (!this.canEnableTlsFlow()) return false;
  1698. const clients = this.settings?.vlesses;
  1699. if (!Array.isArray(clients)) return false;
  1700. return clients.some(c => c?.flow === TLS_FLOW_CONTROL.VISION);
  1701. }
  1702. canEnableReality() {
  1703. if (![Protocols.VLESS, Protocols.TROJAN].includes(this.protocol)) return false;
  1704. return ["tcp", "http", "grpc", "xhttp"].includes(this.network);
  1705. }
  1706. canEnableStream() {
  1707. return [Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS, Protocols.HYSTERIA].includes(this.protocol);
  1708. }
  1709. reset() {
  1710. this.port = RandomUtil.randomInteger(10000, 60000);
  1711. this.listen = '';
  1712. this.protocol = Protocols.VMESS;
  1713. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1714. this.stream = new StreamSettings();
  1715. this.tag = '';
  1716. this.sniffing = new Sniffing();
  1717. }
  1718. genVmessLink(address = '', port = this.port, forceTls, remark = '', clientId, security) {
  1719. if (this.protocol !== Protocols.VMESS) {
  1720. return '';
  1721. }
  1722. const tls = forceTls == 'same' ? this.stream.security : forceTls;
  1723. let obj = {
  1724. v: '2',
  1725. ps: remark,
  1726. add: address,
  1727. port: port,
  1728. id: clientId,
  1729. scy: security,
  1730. net: this.stream.network,
  1731. tls: tls,
  1732. };
  1733. const network = this.stream.network;
  1734. if (network === 'tcp') {
  1735. const tcp = this.stream.tcp;
  1736. obj.type = tcp.type;
  1737. if (tcp.type === 'http') {
  1738. const request = tcp.request;
  1739. obj.path = request.path.join(',');
  1740. const host = this.getHeader(request, 'host');
  1741. if (host) obj.host = host;
  1742. }
  1743. } else if (network === 'kcp') {
  1744. const kcp = this.stream.kcp;
  1745. obj.mtu = kcp.mtu;
  1746. obj.tti = kcp.tti;
  1747. } else if (network === 'ws') {
  1748. const ws = this.stream.ws;
  1749. obj.path = ws.path;
  1750. obj.host = ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host');
  1751. } else if (network === 'grpc') {
  1752. obj.path = this.stream.grpc.serviceName;
  1753. obj.authority = this.stream.grpc.authority;
  1754. if (this.stream.grpc.multiMode) {
  1755. obj.type = 'multi'
  1756. }
  1757. } else if (network === 'httpupgrade') {
  1758. const httpupgrade = this.stream.httpupgrade;
  1759. obj.path = httpupgrade.path;
  1760. obj.host = httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host');
  1761. } else if (network === 'xhttp') {
  1762. const xhttp = this.stream.xhttp;
  1763. obj.path = xhttp.path;
  1764. obj.host = xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host');
  1765. obj.type = xhttp.mode;
  1766. Inbound.applyXhttpExtraToObj(xhttp, obj);
  1767. }
  1768. Inbound.applyFinalMaskToObj(this.stream.finalmask, obj);
  1769. if (tls === 'tls') {
  1770. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1771. obj.sni = this.stream.tls.sni;
  1772. }
  1773. if (!ObjectUtil.isEmpty(this.stream.tls.settings.fingerprint)) {
  1774. obj.fp = this.stream.tls.settings.fingerprint;
  1775. }
  1776. if (this.stream.tls.alpn.length > 0) {
  1777. obj.alpn = this.stream.tls.alpn.join(',');
  1778. }
  1779. }
  1780. return 'vmess://' + Base64.encode(JSON.stringify(obj, null, 2));
  1781. }
  1782. genVLESSLink(address = '', port = this.port, forceTls, remark = '', clientId, flow) {
  1783. const uuid = clientId;
  1784. const type = this.stream.network;
  1785. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1786. const params = new Map();
  1787. params.set("type", this.stream.network);
  1788. params.set("encryption", this.settings.encryption);
  1789. switch (type) {
  1790. case "tcp":
  1791. const tcp = this.stream.tcp;
  1792. if (tcp.type === 'http') {
  1793. const request = tcp.request;
  1794. params.set("path", request.path.join(','));
  1795. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1796. if (index >= 0) {
  1797. const host = request.headers[index].value;
  1798. params.set("host", host);
  1799. }
  1800. params.set("headerType", 'http');
  1801. }
  1802. break;
  1803. case "kcp":
  1804. const kcp = this.stream.kcp;
  1805. params.set("mtu", kcp.mtu);
  1806. params.set("tti", kcp.tti);
  1807. break;
  1808. case "ws":
  1809. const ws = this.stream.ws;
  1810. params.set("path", ws.path);
  1811. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1812. break;
  1813. case "grpc":
  1814. const grpc = this.stream.grpc;
  1815. params.set("serviceName", grpc.serviceName);
  1816. params.set("authority", grpc.authority);
  1817. if (grpc.multiMode) {
  1818. params.set("mode", "multi");
  1819. }
  1820. break;
  1821. case "httpupgrade":
  1822. const httpupgrade = this.stream.httpupgrade;
  1823. params.set("path", httpupgrade.path);
  1824. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1825. break;
  1826. case "xhttp":
  1827. Inbound.applyXhttpExtraToParams(this.stream.xhttp, params);
  1828. break;
  1829. }
  1830. Inbound.applyFinalMaskToParams(this.stream.finalmask, params);
  1831. if (security === 'tls') {
  1832. params.set("security", "tls");
  1833. if (this.stream.isTls) {
  1834. params.set("fp", this.stream.tls.settings.fingerprint);
  1835. params.set("alpn", this.stream.tls.alpn);
  1836. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1837. params.set("sni", this.stream.tls.sni);
  1838. }
  1839. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1840. params.set("ech", this.stream.tls.settings.echConfigList);
  1841. }
  1842. if (type == "tcp" && !ObjectUtil.isEmpty(flow)) {
  1843. params.set("flow", flow);
  1844. }
  1845. }
  1846. }
  1847. else if (security === 'reality') {
  1848. params.set("security", "reality");
  1849. params.set("pbk", this.stream.reality.settings.publicKey);
  1850. params.set("fp", this.stream.reality.settings.fingerprint);
  1851. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1852. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1853. }
  1854. if (this.stream.reality.shortIds.length > 0) {
  1855. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1856. }
  1857. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1858. params.set("spx", this.stream.reality.settings.spiderX);
  1859. }
  1860. if (!ObjectUtil.isEmpty(this.stream.reality.settings.mldsa65Verify)) {
  1861. params.set("pqv", this.stream.reality.settings.mldsa65Verify);
  1862. }
  1863. if (type == 'tcp' && !ObjectUtil.isEmpty(flow)) {
  1864. params.set("flow", flow);
  1865. }
  1866. }
  1867. else {
  1868. params.set("security", "none");
  1869. }
  1870. const link = `vless://${uuid}@${address}:${port}`;
  1871. const url = new URL(link);
  1872. for (const [key, value] of params) {
  1873. url.searchParams.set(key, value)
  1874. }
  1875. url.hash = encodeURIComponent(remark);
  1876. return url.toString();
  1877. }
  1878. genSSLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1879. let settings = this.settings;
  1880. const type = this.stream.network;
  1881. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1882. const params = new Map();
  1883. params.set("type", this.stream.network);
  1884. switch (type) {
  1885. case "tcp":
  1886. const tcp = this.stream.tcp;
  1887. if (tcp.type === 'http') {
  1888. const request = tcp.request;
  1889. params.set("path", request.path.join(','));
  1890. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1891. if (index >= 0) {
  1892. const host = request.headers[index].value;
  1893. params.set("host", host);
  1894. }
  1895. params.set("headerType", 'http');
  1896. }
  1897. break;
  1898. case "kcp":
  1899. const kcp = this.stream.kcp;
  1900. params.set("mtu", kcp.mtu);
  1901. params.set("tti", kcp.tti);
  1902. break;
  1903. case "ws":
  1904. const ws = this.stream.ws;
  1905. params.set("path", ws.path);
  1906. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1907. break;
  1908. case "grpc":
  1909. const grpc = this.stream.grpc;
  1910. params.set("serviceName", grpc.serviceName);
  1911. params.set("authority", grpc.authority);
  1912. if (grpc.multiMode) {
  1913. params.set("mode", "multi");
  1914. }
  1915. break;
  1916. case "httpupgrade":
  1917. const httpupgrade = this.stream.httpupgrade;
  1918. params.set("path", httpupgrade.path);
  1919. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1920. break;
  1921. case "xhttp":
  1922. Inbound.applyXhttpExtraToParams(this.stream.xhttp, params);
  1923. break;
  1924. }
  1925. Inbound.applyFinalMaskToParams(this.stream.finalmask, params);
  1926. if (security === 'tls') {
  1927. params.set("security", "tls");
  1928. if (this.stream.isTls) {
  1929. params.set("fp", this.stream.tls.settings.fingerprint);
  1930. params.set("alpn", this.stream.tls.alpn);
  1931. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1932. params.set("ech", this.stream.tls.settings.echConfigList);
  1933. }
  1934. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1935. params.set("sni", this.stream.tls.sni);
  1936. }
  1937. }
  1938. }
  1939. let password = new Array();
  1940. if (this.isSS2022) password.push(settings.password);
  1941. if (this.isSSMultiUser) password.push(clientPassword);
  1942. let link = `ss://${Base64.encode(`${settings.method}:${password.join(':')}`, true)}@${address}:${port}`;
  1943. const url = new URL(link);
  1944. for (const [key, value] of params) {
  1945. url.searchParams.set(key, value)
  1946. }
  1947. url.hash = encodeURIComponent(remark);
  1948. return url.toString();
  1949. }
  1950. genTrojanLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1951. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1952. const type = this.stream.network;
  1953. const params = new Map();
  1954. params.set("type", this.stream.network);
  1955. switch (type) {
  1956. case "tcp":
  1957. const tcp = this.stream.tcp;
  1958. if (tcp.type === 'http') {
  1959. const request = tcp.request;
  1960. params.set("path", request.path.join(','));
  1961. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1962. if (index >= 0) {
  1963. const host = request.headers[index].value;
  1964. params.set("host", host);
  1965. }
  1966. params.set("headerType", 'http');
  1967. }
  1968. break;
  1969. case "kcp":
  1970. const kcp = this.stream.kcp;
  1971. params.set("mtu", kcp.mtu);
  1972. params.set("tti", kcp.tti);
  1973. break;
  1974. case "ws":
  1975. const ws = this.stream.ws;
  1976. params.set("path", ws.path);
  1977. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1978. break;
  1979. case "grpc":
  1980. const grpc = this.stream.grpc;
  1981. params.set("serviceName", grpc.serviceName);
  1982. params.set("authority", grpc.authority);
  1983. if (grpc.multiMode) {
  1984. params.set("mode", "multi");
  1985. }
  1986. break;
  1987. case "httpupgrade":
  1988. const httpupgrade = this.stream.httpupgrade;
  1989. params.set("path", httpupgrade.path);
  1990. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1991. break;
  1992. case "xhttp":
  1993. Inbound.applyXhttpExtraToParams(this.stream.xhttp, params);
  1994. break;
  1995. }
  1996. Inbound.applyFinalMaskToParams(this.stream.finalmask, params);
  1997. if (security === 'tls') {
  1998. params.set("security", "tls");
  1999. if (this.stream.isTls) {
  2000. params.set("fp", this.stream.tls.settings.fingerprint);
  2001. params.set("alpn", this.stream.tls.alpn);
  2002. if (this.stream.tls.settings.echConfigList?.length > 0) {
  2003. params.set("ech", this.stream.tls.settings.echConfigList);
  2004. }
  2005. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  2006. params.set("sni", this.stream.tls.sni);
  2007. }
  2008. }
  2009. }
  2010. else if (security === 'reality') {
  2011. params.set("security", "reality");
  2012. params.set("pbk", this.stream.reality.settings.publicKey);
  2013. params.set("fp", this.stream.reality.settings.fingerprint);
  2014. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  2015. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  2016. }
  2017. if (this.stream.reality.shortIds.length > 0) {
  2018. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  2019. }
  2020. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  2021. params.set("spx", this.stream.reality.settings.spiderX);
  2022. }
  2023. if (!ObjectUtil.isEmpty(this.stream.reality.settings.mldsa65Verify)) {
  2024. params.set("pqv", this.stream.reality.settings.mldsa65Verify);
  2025. }
  2026. }
  2027. else {
  2028. params.set("security", "none");
  2029. }
  2030. const link = `trojan://${clientPassword}@${address}:${port}`;
  2031. const url = new URL(link);
  2032. for (const [key, value] of params) {
  2033. url.searchParams.set(key, value)
  2034. }
  2035. url.hash = encodeURIComponent(remark);
  2036. return url.toString();
  2037. }
  2038. genHysteriaLink(address = '', port = this.port, remark = '', clientAuth) {
  2039. const protocol = this.settings.version == 2 ? "hysteria2" : "hysteria";
  2040. const link = `${protocol}://${clientAuth}@${address}:${port}`;
  2041. const params = new Map();
  2042. params.set("security", "tls");
  2043. if (this.stream.tls.settings.fingerprint?.length > 0) params.set("fp", this.stream.tls.settings.fingerprint);
  2044. if (this.stream.tls.alpn?.length > 0) params.set("alpn", this.stream.tls.alpn);
  2045. if (this.stream.tls.settings.allowInsecure) params.set("insecure", "1");
  2046. if (this.stream.tls.settings.echConfigList?.length > 0) params.set("ech", this.stream.tls.settings.echConfigList);
  2047. if (this.stream.tls.sni?.length > 0) params.set("sni", this.stream.tls.sni);
  2048. const udpMasks = this.stream?.finalmask?.udp;
  2049. if (Array.isArray(udpMasks)) {
  2050. const salamanderMask = udpMasks.find(mask => mask?.type === 'salamander');
  2051. const obfsPassword = salamanderMask?.settings?.password;
  2052. if (typeof obfsPassword === 'string' && obfsPassword.length > 0) {
  2053. params.set("obfs", "salamander");
  2054. params.set("obfs-password", obfsPassword);
  2055. }
  2056. }
  2057. Inbound.applyFinalMaskToParams(this.stream.finalmask, params);
  2058. const url = new URL(link);
  2059. for (const [key, value] of params) {
  2060. url.searchParams.set(key, value);
  2061. }
  2062. url.hash = encodeURIComponent(remark);
  2063. return url.toString();
  2064. }
  2065. getWireguardTxt(address, port, remark, peerId) {
  2066. let txt = `[Interface]\n`
  2067. txt += `PrivateKey = ${this.settings.peers[peerId].privateKey}\n`
  2068. txt += `Address = ${this.settings.peers[peerId].allowedIPs[0]}\n`
  2069. txt += `DNS = 1.1.1.1, 1.0.0.1\n`
  2070. if (this.settings.mtu) {
  2071. txt += `MTU = ${this.settings.mtu}\n`
  2072. }
  2073. txt += `\n# ${remark}\n`
  2074. txt += `[Peer]\n`
  2075. txt += `PublicKey = ${this.settings.pubKey}\n`
  2076. txt += `AllowedIPs = 0.0.0.0/0, ::/0\n`
  2077. txt += `Endpoint = ${address}:${port}`
  2078. if (this.settings.peers[peerId].psk) {
  2079. txt += `\nPresharedKey = ${this.settings.peers[peerId].psk}`
  2080. }
  2081. if (this.settings.peers[peerId].keepAlive) {
  2082. txt += `\nPersistentKeepalive = ${this.settings.peers[peerId].keepAlive}\n`
  2083. }
  2084. return txt;
  2085. }
  2086. getWireguardLink(address, port, remark, peerId) {
  2087. const peer = this.settings?.peers?.[peerId];
  2088. if (!peer) return '';
  2089. const link = `wireguard://${address}:${port}`;
  2090. const url = new URL(link);
  2091. url.username = peer.privateKey || '';
  2092. if (this.settings?.pubKey) {
  2093. url.searchParams.set("publickey", this.settings.pubKey);
  2094. }
  2095. if (Array.isArray(peer.allowedIPs) && peer.allowedIPs.length > 0 && peer.allowedIPs[0]) {
  2096. url.searchParams.set("address", peer.allowedIPs[0]);
  2097. }
  2098. if (this.settings?.mtu) {
  2099. url.searchParams.set("mtu", this.settings.mtu);
  2100. }
  2101. url.hash = encodeURIComponent(remark);
  2102. return url.toString();
  2103. }
  2104. // resolveAddr picks the host that goes into share/sub links. Order:
  2105. // 1. hostOverride (caller supplies node address for node-managed inbounds)
  2106. // 2. inbound's bind listen (when explicit, not 0.0.0.0)
  2107. // 3. browser's location.hostname (single-panel default)
  2108. // Centralised so genAllLinks/genInboundLinks/genWireguard*
  2109. // all share the same chain — pre-Phase 3 we had four duplicated lines.
  2110. _resolveAddr(hostOverride = '') {
  2111. if (hostOverride) return hostOverride;
  2112. if (!ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0") return this.listen;
  2113. return location.hostname;
  2114. }
  2115. genWireguardLinks(remark = '', remarkModel = '-ieo', hostOverride = '') {
  2116. const addr = this._resolveAddr(hostOverride);
  2117. const separationChar = remarkModel.charAt(0);
  2118. let links = [];
  2119. this.settings.peers.forEach((p, index) => {
  2120. links.push(this.getWireguardLink(addr, this.port, remark + separationChar + (index + 1), index));
  2121. });
  2122. return links.join('\r\n');
  2123. }
  2124. genWireguardConfigs(remark = '', remarkModel = '-ieo', hostOverride = '') {
  2125. const addr = this._resolveAddr(hostOverride);
  2126. const separationChar = remarkModel.charAt(0);
  2127. let links = [];
  2128. this.settings.peers.forEach((p, index) => {
  2129. links.push(this.getWireguardTxt(addr, this.port, remark + separationChar + (index + 1), index));
  2130. });
  2131. return links.join('\r\n');
  2132. }
  2133. genLink(address = '', port = this.port, forceTls = 'same', remark = '', client) {
  2134. switch (this.protocol) {
  2135. case Protocols.VMESS:
  2136. return this.genVmessLink(address, port, forceTls, remark, client.id, client.security);
  2137. case Protocols.VLESS:
  2138. return this.genVLESSLink(address, port, forceTls, remark, client.id, client.flow);
  2139. case Protocols.SHADOWSOCKS:
  2140. return this.genSSLink(address, port, forceTls, remark, this.isSSMultiUser ? client.password : '');
  2141. case Protocols.TROJAN:
  2142. return this.genTrojanLink(address, port, forceTls, remark, client.password);
  2143. case Protocols.HYSTERIA:
  2144. return this.genHysteriaLink(address, port, remark, client.auth.length > 0 ? client.auth : this.stream.hysteria.auth);
  2145. default: return '';
  2146. }
  2147. }
  2148. genAllLinks(remark = '', remarkModel = '-ieo', client, hostOverride = '') {
  2149. let result = [];
  2150. let email = client ? client.email : '';
  2151. let addr = this._resolveAddr(hostOverride);
  2152. let port = this.port;
  2153. const separationChar = remarkModel.charAt(0);
  2154. const orderChars = remarkModel.slice(1);
  2155. let orders = {
  2156. 'i': remark,
  2157. 'e': email,
  2158. 'o': '',
  2159. };
  2160. if (ObjectUtil.isArrEmpty(this.stream.externalProxy)) {
  2161. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  2162. result.push({
  2163. remark: r,
  2164. link: this.genLink(addr, port, 'same', r, client)
  2165. });
  2166. } else {
  2167. this.stream.externalProxy.forEach((ep) => {
  2168. orders['o'] = ep.remark;
  2169. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  2170. result.push({
  2171. remark: r,
  2172. link: this.genLink(ep.dest, ep.port, ep.forceTls, r, client)
  2173. });
  2174. });
  2175. }
  2176. return result;
  2177. }
  2178. genInboundLinks(remark = '', remarkModel = '-ieo', hostOverride = '') {
  2179. let addr = this._resolveAddr(hostOverride);
  2180. if (this.clients) {
  2181. let links = [];
  2182. this.clients.forEach((client) => {
  2183. this.genAllLinks(remark, remarkModel, client, hostOverride).forEach(l => {
  2184. links.push(l.link);
  2185. })
  2186. });
  2187. return links.join('\r\n');
  2188. } else {
  2189. if (this.protocol == Protocols.SHADOWSOCKS && !this.isSSMultiUser) return this.genSSLink(addr, this.port, 'same', remark);
  2190. if (this.protocol == Protocols.WIREGUARD) {
  2191. return this.genWireguardConfigs(remark, remarkModel, hostOverride);
  2192. }
  2193. return '';
  2194. }
  2195. }
  2196. static fromJson(json = {}) {
  2197. return new Inbound(
  2198. json.port,
  2199. json.listen,
  2200. json.protocol,
  2201. Inbound.Settings.fromJson(json.protocol, json.settings),
  2202. StreamSettings.fromJson(json.streamSettings),
  2203. json.tag,
  2204. Sniffing.fromJson(json.sniffing),
  2205. json.clientStats
  2206. )
  2207. }
  2208. toJson() {
  2209. let streamSettings;
  2210. if (this.canEnableStream() || this.stream?.sockopt) {
  2211. streamSettings = this.stream.toJson();
  2212. }
  2213. return {
  2214. port: this.port,
  2215. listen: this.listen,
  2216. protocol: this.protocol,
  2217. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  2218. streamSettings: streamSettings,
  2219. tag: this.tag,
  2220. sniffing: this.sniffing.toJson(),
  2221. clientStats: this.clientStats
  2222. };
  2223. }
  2224. }
  2225. Inbound.Settings = class extends XrayCommonClass {
  2226. constructor(protocol) {
  2227. super();
  2228. this.protocol = protocol;
  2229. }
  2230. static getSettings(protocol) {
  2231. switch (protocol) {
  2232. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  2233. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  2234. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  2235. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  2236. case Protocols.TUNNEL: return new Inbound.TunnelSettings(protocol);
  2237. case Protocols.MIXED: return new Inbound.MixedSettings(protocol);
  2238. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  2239. case Protocols.WIREGUARD: return new Inbound.WireguardSettings(protocol);
  2240. case Protocols.TUN: return new Inbound.TunSettings(protocol);
  2241. case Protocols.HYSTERIA: return new Inbound.HysteriaSettings(protocol);
  2242. default: return null;
  2243. }
  2244. }
  2245. static fromJson(protocol, json) {
  2246. switch (protocol) {
  2247. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  2248. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  2249. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  2250. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  2251. case Protocols.TUNNEL: return Inbound.TunnelSettings.fromJson(json);
  2252. case Protocols.MIXED: return Inbound.MixedSettings.fromJson(json);
  2253. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  2254. case Protocols.WIREGUARD: return Inbound.WireguardSettings.fromJson(json);
  2255. case Protocols.TUN: return Inbound.TunSettings.fromJson(json);
  2256. case Protocols.HYSTERIA: return Inbound.HysteriaSettings.fromJson(json);
  2257. default: return null;
  2258. }
  2259. }
  2260. toJson() {
  2261. return {};
  2262. }
  2263. };
  2264. /** Shared user-quota fields and UI helpers for multi-user protocol clients. */
  2265. Inbound.ClientBase = class extends XrayCommonClass {
  2266. constructor(
  2267. email = RandomUtil.randomLowerAndNum(8),
  2268. limitIp = 0,
  2269. totalGB = 0,
  2270. expiryTime = 0,
  2271. enable = true,
  2272. tgId = '',
  2273. subId = RandomUtil.randomLowerAndNum(16),
  2274. comment = '',
  2275. reset = 0,
  2276. created_at = undefined,
  2277. updated_at = undefined,
  2278. ) {
  2279. super();
  2280. this.email = email;
  2281. this.limitIp = limitIp;
  2282. this.totalGB = totalGB;
  2283. this.expiryTime = expiryTime;
  2284. this.enable = enable;
  2285. this.tgId = tgId;
  2286. this.subId = subId;
  2287. this.comment = comment;
  2288. this.reset = reset;
  2289. this.created_at = created_at;
  2290. this.updated_at = updated_at;
  2291. }
  2292. static commonArgsFromJson(json = {}) {
  2293. return [
  2294. json.email,
  2295. json.limitIp,
  2296. json.totalGB,
  2297. json.expiryTime,
  2298. json.enable,
  2299. json.tgId,
  2300. json.subId,
  2301. json.comment,
  2302. json.reset,
  2303. json.created_at,
  2304. json.updated_at,
  2305. ];
  2306. }
  2307. _clientBaseToJson() {
  2308. return {
  2309. email: this.email,
  2310. limitIp: this.limitIp,
  2311. totalGB: this.totalGB,
  2312. expiryTime: this.expiryTime,
  2313. enable: this.enable,
  2314. tgId: this.tgId,
  2315. subId: this.subId,
  2316. comment: this.comment,
  2317. reset: this.reset,
  2318. created_at: this.created_at,
  2319. updated_at: this.updated_at,
  2320. };
  2321. }
  2322. get _expiryTime() {
  2323. if (this.expiryTime === 0 || this.expiryTime === '') {
  2324. return null;
  2325. }
  2326. if (this.expiryTime < 0) {
  2327. return this.expiryTime / -86400000;
  2328. }
  2329. return dayjs(this.expiryTime);
  2330. }
  2331. set _expiryTime(t) {
  2332. if (t == null || t === '') {
  2333. this.expiryTime = 0;
  2334. } else {
  2335. this.expiryTime = t.valueOf();
  2336. }
  2337. }
  2338. get _totalGB() {
  2339. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  2340. }
  2341. set _totalGB(gb) {
  2342. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  2343. }
  2344. };
  2345. Inbound.VmessSettings = class extends Inbound.Settings {
  2346. constructor(protocol,
  2347. vmesses = [new Inbound.VmessSettings.VMESS()]) {
  2348. super(protocol);
  2349. this.vmesses = vmesses;
  2350. }
  2351. indexOfVmessById(id) {
  2352. return this.vmesses.findIndex(VMESS => VMESS.id === id);
  2353. }
  2354. addVmess(VMESS) {
  2355. if (this.indexOfVmessById(VMESS.id) >= 0) {
  2356. return false;
  2357. }
  2358. this.vmesses.push(VMESS);
  2359. }
  2360. delVmess(VMESS) {
  2361. const i = this.indexOfVmessById(VMESS.id);
  2362. if (i >= 0) {
  2363. this.vmesses.splice(i, 1);
  2364. }
  2365. }
  2366. static fromJson(json = {}) {
  2367. return new Inbound.VmessSettings(
  2368. Protocols.VMESS,
  2369. (json.clients || []).map(client => Inbound.VmessSettings.VMESS.fromJson(client)),
  2370. );
  2371. }
  2372. toJson() {
  2373. return {
  2374. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  2375. };
  2376. }
  2377. };
  2378. Inbound.VmessSettings.VMESS = class extends Inbound.ClientBase {
  2379. constructor(
  2380. id = RandomUtil.randomUUID(),
  2381. security = USERS_SECURITY.AUTO,
  2382. email, limitIp, totalGB, expiryTime, enable, tgId, subId, comment, reset, created_at, updated_at,
  2383. ) {
  2384. super(email, limitIp, totalGB, expiryTime, enable, tgId, subId, comment, reset, created_at, updated_at);
  2385. this.id = id;
  2386. this.security = security;
  2387. }
  2388. static fromJson(json = {}) {
  2389. return new Inbound.VmessSettings.VMESS(
  2390. json.id,
  2391. json.security,
  2392. ...Inbound.ClientBase.commonArgsFromJson(json),
  2393. );
  2394. }
  2395. toJson() {
  2396. return {
  2397. id: this.id,
  2398. security: this.security,
  2399. ...this._clientBaseToJson(),
  2400. };
  2401. }
  2402. };
  2403. Inbound.VLESSSettings = class extends Inbound.Settings {
  2404. constructor(
  2405. protocol,
  2406. vlesses = [new Inbound.VLESSSettings.VLESS()],
  2407. decryption = "none",
  2408. encryption = "none",
  2409. fallbacks = [],
  2410. testseed = [],
  2411. ) {
  2412. super(protocol);
  2413. this.vlesses = vlesses;
  2414. this.decryption = decryption;
  2415. this.encryption = encryption;
  2416. this.fallbacks = fallbacks;
  2417. this.testseed = testseed;
  2418. }
  2419. addFallback() {
  2420. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  2421. }
  2422. delFallback(index) {
  2423. this.fallbacks.splice(index, 1);
  2424. }
  2425. // Empty array means "use server defaults" (won't be sent).
  2426. // Anything else must be exactly 4 positive integers.
  2427. static isValidTestseed(arr) {
  2428. if (!Array.isArray(arr) || arr.length === 0) return true;
  2429. if (arr.length !== 4) return false;
  2430. return arr.every(v => Number.isInteger(v) && v > 0);
  2431. }
  2432. static fromJson(json = {}) {
  2433. // Preserve a saved testseed only if it's a valid 4-positive-int array; otherwise leave empty
  2434. // so toJson omits it and the form falls back to placeholder defaults.
  2435. const saved = json.testseed;
  2436. const testseed = (Array.isArray(saved)
  2437. && saved.length === 4
  2438. && saved.every(v => Number.isInteger(v) && v > 0))
  2439. ? saved
  2440. : [];
  2441. const obj = new Inbound.VLESSSettings(
  2442. Protocols.VLESS,
  2443. (json.clients || []).map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  2444. json.decryption,
  2445. json.encryption,
  2446. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks || []),
  2447. testseed,
  2448. );
  2449. return obj;
  2450. }
  2451. toJson() {
  2452. const json = {
  2453. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  2454. };
  2455. if (this.decryption) {
  2456. json.decryption = this.decryption;
  2457. }
  2458. if (this.encryption) {
  2459. json.encryption = this.encryption;
  2460. }
  2461. if (this.fallbacks && this.fallbacks.length > 0) {
  2462. json.fallbacks = Inbound.VLESSSettings.toJsonArray(this.fallbacks);
  2463. }
  2464. // testseed is only meaningful for the exact xtls-rprx-vision flow, and only when
  2465. // the user supplied a complete 4-positive-int array. Otherwise omit and let the
  2466. // backend fall back to its safe defaults.
  2467. const hasVisionFlow = this.vlesses && this.vlesses.some(v => v.flow === TLS_FLOW_CONTROL.VISION);
  2468. if (hasVisionFlow
  2469. && Array.isArray(this.testseed)
  2470. && this.testseed.length === 4
  2471. && this.testseed.every(v => Number.isInteger(v) && v > 0)) {
  2472. json.testseed = this.testseed;
  2473. }
  2474. return json;
  2475. }
  2476. };
  2477. Inbound.VLESSSettings.VLESS = class extends Inbound.ClientBase {
  2478. constructor(
  2479. id = RandomUtil.randomUUID(),
  2480. flow = '',
  2481. reverseTag = '',
  2482. reverseSniffing = new Sniffing(),
  2483. email, limitIp, totalGB, expiryTime, enable, tgId, subId, comment, reset, created_at, updated_at,
  2484. ) {
  2485. super(email, limitIp, totalGB, expiryTime, enable, tgId, subId, comment, reset, created_at, updated_at);
  2486. this.id = id;
  2487. this.flow = flow;
  2488. this.reverseTag = reverseTag;
  2489. this.reverseSniffing = reverseSniffing;
  2490. }
  2491. static fromJson(json = {}) {
  2492. return new Inbound.VLESSSettings.VLESS(
  2493. json.id,
  2494. json.flow,
  2495. json.reverse?.tag ?? '',
  2496. Sniffing.fromJson(json.reverse?.sniffing || {}),
  2497. ...Inbound.ClientBase.commonArgsFromJson(json),
  2498. );
  2499. }
  2500. toJson() {
  2501. const json = {
  2502. id: this.id,
  2503. flow: this.flow,
  2504. ...this._clientBaseToJson(),
  2505. };
  2506. if (this.reverseTag) {
  2507. json.reverse = {
  2508. tag: this.reverseTag,
  2509. };
  2510. }
  2511. return json;
  2512. }
  2513. };
  2514. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  2515. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  2516. super();
  2517. this.name = name;
  2518. this.alpn = alpn;
  2519. this.path = path;
  2520. this.dest = dest;
  2521. this.xver = xver;
  2522. }
  2523. toJson() {
  2524. return XrayCommonClass.fallbackToJson(this);
  2525. }
  2526. static fromJson(json = []) {
  2527. return (json || []).map(f => new Inbound.VLESSSettings.Fallback(
  2528. f.name, f.alpn, f.path, f.dest, f.xver,
  2529. ));
  2530. }
  2531. };
  2532. Inbound.TrojanSettings = class extends Inbound.Settings {
  2533. constructor(protocol,
  2534. trojans = [new Inbound.TrojanSettings.Trojan()],
  2535. fallbacks = [],) {
  2536. super(protocol);
  2537. this.trojans = trojans;
  2538. this.fallbacks = fallbacks;
  2539. }
  2540. addFallback() {
  2541. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  2542. }
  2543. delFallback(index) {
  2544. this.fallbacks.splice(index, 1);
  2545. }
  2546. static fromJson(json = {}) {
  2547. return new Inbound.TrojanSettings(
  2548. Protocols.TROJAN,
  2549. (json.clients || []).map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  2550. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  2551. }
  2552. toJson() {
  2553. const json = {
  2554. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  2555. };
  2556. if (this.fallbacks && this.fallbacks.length > 0) {
  2557. json.fallbacks = Inbound.TrojanSettings.toJsonArray(this.fallbacks);
  2558. }
  2559. return json;
  2560. }
  2561. };
  2562. Inbound.TrojanSettings.Trojan = class extends Inbound.ClientBase {
  2563. constructor(
  2564. password = RandomUtil.randomSeq(10),
  2565. email, limitIp, totalGB, expiryTime, enable, tgId, subId, comment, reset, created_at, updated_at,
  2566. ) {
  2567. super(email, limitIp, totalGB, expiryTime, enable, tgId, subId, comment, reset, created_at, updated_at);
  2568. this.password = password;
  2569. }
  2570. toJson() {
  2571. return {
  2572. password: this.password,
  2573. ...this._clientBaseToJson(),
  2574. };
  2575. }
  2576. static fromJson(json = {}) {
  2577. return new Inbound.TrojanSettings.Trojan(
  2578. json.password,
  2579. ...Inbound.ClientBase.commonArgsFromJson(json),
  2580. );
  2581. }
  2582. };
  2583. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  2584. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  2585. super();
  2586. this.name = name;
  2587. this.alpn = alpn;
  2588. this.path = path;
  2589. this.dest = dest;
  2590. this.xver = xver;
  2591. }
  2592. toJson() {
  2593. return XrayCommonClass.fallbackToJson(this);
  2594. }
  2595. static fromJson(json = []) {
  2596. return (json || []).map(f => new Inbound.TrojanSettings.Fallback(
  2597. f.name, f.alpn, f.path, f.dest, f.xver,
  2598. ));
  2599. }
  2600. };
  2601. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  2602. constructor(protocol,
  2603. method = SSMethods.BLAKE3_AES_256_GCM,
  2604. password = RandomUtil.randomShadowsocksPassword(),
  2605. network = 'tcp,udp',
  2606. shadowsockses = [new Inbound.ShadowsocksSettings.Shadowsocks()],
  2607. ivCheck = false,
  2608. ) {
  2609. super(protocol);
  2610. this.method = method;
  2611. this.password = password;
  2612. this.network = network;
  2613. this.shadowsockses = shadowsockses;
  2614. this.ivCheck = ivCheck;
  2615. }
  2616. static fromJson(json = {}) {
  2617. return new Inbound.ShadowsocksSettings(
  2618. Protocols.SHADOWSOCKS,
  2619. json.method,
  2620. json.password,
  2621. json.network,
  2622. (json.clients || []).map(client => Inbound.ShadowsocksSettings.Shadowsocks.fromJson(client)),
  2623. json.ivCheck,
  2624. );
  2625. }
  2626. toJson() {
  2627. return {
  2628. method: this.method,
  2629. password: this.password,
  2630. network: this.network,
  2631. clients: Inbound.ShadowsocksSettings.toJsonArray(this.shadowsockses),
  2632. ivCheck: this.ivCheck,
  2633. };
  2634. }
  2635. };
  2636. Inbound.ShadowsocksSettings.Shadowsocks = class extends Inbound.ClientBase {
  2637. constructor(
  2638. method = '',
  2639. password = RandomUtil.randomShadowsocksPassword(),
  2640. email, limitIp, totalGB, expiryTime, enable, tgId, subId, comment, reset, created_at, updated_at,
  2641. ) {
  2642. super(email, limitIp, totalGB, expiryTime, enable, tgId, subId, comment, reset, created_at, updated_at);
  2643. this.method = method;
  2644. this.password = password;
  2645. }
  2646. toJson() {
  2647. return {
  2648. method: this.method,
  2649. password: this.password,
  2650. ...this._clientBaseToJson(),
  2651. };
  2652. }
  2653. static fromJson(json = {}) {
  2654. return new Inbound.ShadowsocksSettings.Shadowsocks(
  2655. json.method,
  2656. json.password,
  2657. ...Inbound.ClientBase.commonArgsFromJson(json),
  2658. );
  2659. }
  2660. };
  2661. Inbound.HysteriaSettings = class extends Inbound.Settings {
  2662. constructor(protocol, version = 2, hysterias = [new Inbound.HysteriaSettings.Hysteria()]) {
  2663. super(protocol);
  2664. this.version = version;
  2665. this.hysterias = hysterias;
  2666. }
  2667. static fromJson(json = {}) {
  2668. return new Inbound.HysteriaSettings(
  2669. Protocols.HYSTERIA,
  2670. json.version ?? 2,
  2671. (json.clients || []).map(client => Inbound.HysteriaSettings.Hysteria.fromJson(client)),
  2672. );
  2673. }
  2674. toJson() {
  2675. return {
  2676. version: this.version,
  2677. clients: Inbound.HysteriaSettings.toJsonArray(this.hysterias),
  2678. };
  2679. }
  2680. };
  2681. Inbound.HysteriaSettings.Hysteria = class extends Inbound.ClientBase {
  2682. constructor(
  2683. auth = RandomUtil.randomSeq(10),
  2684. email, limitIp, totalGB, expiryTime, enable, tgId, subId, comment, reset, created_at, updated_at,
  2685. ) {
  2686. super(email, limitIp, totalGB, expiryTime, enable, tgId, subId, comment, reset, created_at, updated_at);
  2687. this.auth = auth;
  2688. }
  2689. toJson() {
  2690. return {
  2691. auth: this.auth,
  2692. ...this._clientBaseToJson(),
  2693. };
  2694. }
  2695. static fromJson(json = {}) {
  2696. return new Inbound.HysteriaSettings.Hysteria(
  2697. json.auth,
  2698. ...Inbound.ClientBase.commonArgsFromJson(json),
  2699. );
  2700. }
  2701. };
  2702. Inbound.TunnelSettings = class extends Inbound.Settings {
  2703. constructor(
  2704. protocol,
  2705. address,
  2706. port,
  2707. portMap = [],
  2708. network = 'tcp,udp',
  2709. followRedirect = false
  2710. ) {
  2711. super(protocol);
  2712. this.address = address;
  2713. this.port = port;
  2714. this.portMap = portMap;
  2715. this.network = network;
  2716. this.followRedirect = followRedirect;
  2717. }
  2718. static fromJson(json = {}) {
  2719. return new Inbound.TunnelSettings(
  2720. Protocols.TUNNEL,
  2721. json.address,
  2722. json.port,
  2723. XrayCommonClass.toHeaders(json.portMap),
  2724. json.network,
  2725. json.followRedirect,
  2726. );
  2727. }
  2728. toJson() {
  2729. return {
  2730. address: this.address,
  2731. port: this.port,
  2732. portMap: XrayCommonClass.toV2Headers(this.portMap, false),
  2733. network: this.network,
  2734. followRedirect: this.followRedirect,
  2735. };
  2736. }
  2737. };
  2738. Inbound.MixedSettings = class extends Inbound.Settings {
  2739. constructor(protocol, auth = 'password', accounts = [new Inbound.MixedSettings.SocksAccount()], udp = false, ip = '127.0.0.1') {
  2740. super(protocol);
  2741. this.auth = auth;
  2742. this.accounts = accounts;
  2743. this.udp = udp;
  2744. this.ip = ip;
  2745. }
  2746. addAccount(account) {
  2747. this.accounts.push(account);
  2748. }
  2749. delAccount(index) {
  2750. this.accounts.splice(index, 1);
  2751. }
  2752. static fromJson(json = {}) {
  2753. let accounts;
  2754. if (json.auth === 'password') {
  2755. accounts = json.accounts.map(
  2756. account => Inbound.MixedSettings.SocksAccount.fromJson(account)
  2757. )
  2758. }
  2759. return new Inbound.MixedSettings(
  2760. Protocols.MIXED,
  2761. json.auth,
  2762. accounts,
  2763. json.udp,
  2764. json.ip,
  2765. );
  2766. }
  2767. toJson() {
  2768. return {
  2769. auth: this.auth,
  2770. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  2771. udp: this.udp,
  2772. ip: this.ip,
  2773. };
  2774. }
  2775. };
  2776. Inbound.MixedSettings.SocksAccount = class extends XrayCommonClass {
  2777. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2778. super();
  2779. this.user = user;
  2780. this.pass = pass;
  2781. }
  2782. static fromJson(json = {}) {
  2783. return new Inbound.MixedSettings.SocksAccount(json.user, json.pass);
  2784. }
  2785. };
  2786. Inbound.HttpSettings = class extends Inbound.Settings {
  2787. constructor(
  2788. protocol,
  2789. accounts = [new Inbound.HttpSettings.HttpAccount()],
  2790. allowTransparent = false,
  2791. ) {
  2792. super(protocol);
  2793. this.accounts = accounts;
  2794. this.allowTransparent = allowTransparent;
  2795. }
  2796. addAccount(account) {
  2797. this.accounts.push(account);
  2798. }
  2799. delAccount(index) {
  2800. this.accounts.splice(index, 1);
  2801. }
  2802. static fromJson(json = {}) {
  2803. return new Inbound.HttpSettings(
  2804. Protocols.HTTP,
  2805. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  2806. json.allowTransparent,
  2807. );
  2808. }
  2809. toJson() {
  2810. return {
  2811. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  2812. allowTransparent: this.allowTransparent,
  2813. };
  2814. }
  2815. };
  2816. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  2817. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2818. super();
  2819. this.user = user;
  2820. this.pass = pass;
  2821. }
  2822. static fromJson(json = {}) {
  2823. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  2824. }
  2825. };
  2826. Inbound.WireguardSettings = class extends XrayCommonClass {
  2827. constructor(
  2828. protocol,
  2829. mtu = 1420,
  2830. secretKey = Wireguard.generateKeypair().privateKey,
  2831. peers = [new Inbound.WireguardSettings.Peer()],
  2832. noKernelTun = false
  2833. ) {
  2834. super(protocol);
  2835. this.mtu = mtu;
  2836. this.secretKey = secretKey;
  2837. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  2838. this.peers = peers;
  2839. this.noKernelTun = noKernelTun;
  2840. }
  2841. addPeer() {
  2842. this.peers.push(new Inbound.WireguardSettings.Peer(null, null, '', ['10.0.0.' + (this.peers.length + 2)]));
  2843. }
  2844. delPeer(index) {
  2845. this.peers.splice(index, 1);
  2846. }
  2847. static fromJson(json = {}) {
  2848. return new Inbound.WireguardSettings(
  2849. Protocols.WIREGUARD,
  2850. json.mtu,
  2851. json.secretKey,
  2852. json.peers.map(peer => Inbound.WireguardSettings.Peer.fromJson(peer)),
  2853. json.noKernelTun,
  2854. );
  2855. }
  2856. toJson() {
  2857. return {
  2858. mtu: this.mtu ?? undefined,
  2859. secretKey: this.secretKey,
  2860. peers: Inbound.WireguardSettings.Peer.toJsonArray(this.peers),
  2861. noKernelTun: this.noKernelTun,
  2862. };
  2863. }
  2864. };
  2865. Inbound.WireguardSettings.Peer = class extends XrayCommonClass {
  2866. constructor(privateKey, publicKey, psk = '', allowedIPs = ['10.0.0.2/32'], keepAlive = 0) {
  2867. super();
  2868. this.privateKey = privateKey
  2869. this.publicKey = publicKey;
  2870. if (!this.publicKey) {
  2871. [this.publicKey, this.privateKey] = Object.values(Wireguard.generateKeypair())
  2872. }
  2873. this.psk = psk;
  2874. allowedIPs.forEach((a, index) => {
  2875. if (a.length > 0 && !a.includes('/')) allowedIPs[index] += '/32';
  2876. })
  2877. this.allowedIPs = allowedIPs;
  2878. this.keepAlive = keepAlive;
  2879. }
  2880. static fromJson(json = {}) {
  2881. return new Inbound.WireguardSettings.Peer(
  2882. json.privateKey,
  2883. json.publicKey,
  2884. json.preSharedKey,
  2885. json.allowedIPs,
  2886. json.keepAlive
  2887. );
  2888. }
  2889. toJson() {
  2890. this.allowedIPs.forEach((a, index) => {
  2891. if (a.length > 0 && !a.includes('/')) this.allowedIPs[index] += '/32';
  2892. });
  2893. return {
  2894. privateKey: this.privateKey,
  2895. publicKey: this.publicKey,
  2896. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  2897. allowedIPs: this.allowedIPs,
  2898. keepAlive: this.keepAlive ?? undefined,
  2899. };
  2900. }
  2901. };
  2902. Inbound.TunSettings = class extends Inbound.Settings {
  2903. constructor(
  2904. protocol,
  2905. name = 'xray0',
  2906. mtu = 1500,
  2907. gateway = [],
  2908. dns = [],
  2909. userLevel = 0,
  2910. autoSystemRoutingTable = [],
  2911. autoOutboundsInterface = 'auto'
  2912. ) {
  2913. super(protocol);
  2914. this.name = name;
  2915. this.mtu = Number(mtu) || 1500;
  2916. this.gateway = Array.isArray(gateway) ? gateway : [];
  2917. this.dns = Array.isArray(dns) ? dns : [];
  2918. this.userLevel = userLevel;
  2919. this.autoSystemRoutingTable = Array.isArray(autoSystemRoutingTable) ? autoSystemRoutingTable : [];
  2920. this.autoOutboundsInterface = autoOutboundsInterface;
  2921. }
  2922. static fromJson(json = {}) {
  2923. const rawMtu = json.mtu ?? json.MTU;
  2924. const mtu = Array.isArray(rawMtu) ? rawMtu[0] : rawMtu;
  2925. return new Inbound.TunSettings(
  2926. Protocols.TUN,
  2927. json.name ?? 'xray0',
  2928. mtu ?? 1500,
  2929. json.gateway ?? json.Gateway ?? [],
  2930. json.dns ?? json.DNS ?? [],
  2931. json.userLevel ?? 0,
  2932. json.autoSystemRoutingTable ?? [],
  2933. Object.prototype.hasOwnProperty.call(json, 'autoOutboundsInterface') ? json.autoOutboundsInterface : 'auto'
  2934. );
  2935. }
  2936. toJson() {
  2937. return {
  2938. name: this.name || 'xray0',
  2939. mtu: Number(this.mtu) || 1500,
  2940. gateway: this.gateway,
  2941. dns: this.dns,
  2942. userLevel: this.userLevel || 0,
  2943. autoSystemRoutingTable: this.autoSystemRoutingTable,
  2944. autoOutboundsInterface: this.autoOutboundsInterface,
  2945. };
  2946. }
  2947. };