xray.js 68 KB

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