xray.js 64 KB

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