1
0

xray.js 81 KB

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