xray.js 68 KB

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