xray.js 82 KB

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