inbound.js 73 KB

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