1
0

inbound.js 83 KB

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