1
0

xray.js 80 KB

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