xray.js 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  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(path='/', host=['']) {
  339. super();
  340. this.path = path;
  341. this.host = host.length === 0 ? [''] : host;
  342. }
  343. addHost(host) {
  344. this.host.push(host);
  345. }
  346. removeHost(index) {
  347. this.host.splice(index, 1);
  348. }
  349. static fromJson(json={}) {
  350. return new HttpStreamSettings(json.path, json.host);
  351. }
  352. toJson() {
  353. let host = [];
  354. for (let i = 0; i < this.host.length; ++i) {
  355. if (!ObjectUtil.isEmpty(this.host[i])) {
  356. host.push(this.host[i]);
  357. }
  358. }
  359. return {
  360. path: this.path,
  361. host: host,
  362. }
  363. }
  364. }
  365. class QuicStreamSettings extends XrayCommonClass {
  366. constructor(security=VmessMethods.NONE,
  367. key='', type='none') {
  368. super();
  369. this.security = security;
  370. this.key = key;
  371. this.type = type;
  372. }
  373. static fromJson(json={}) {
  374. return new QuicStreamSettings(
  375. json.security,
  376. json.key,
  377. json.header ? json.header.type : 'none',
  378. );
  379. }
  380. toJson() {
  381. return {
  382. security: this.security,
  383. key: this.key,
  384. header: {
  385. type: this.type,
  386. }
  387. }
  388. }
  389. }
  390. class GrpcStreamSettings extends XrayCommonClass {
  391. constructor(
  392. serviceName="",
  393. multiMode=false
  394. ) {
  395. super();
  396. this.serviceName = serviceName;
  397. this.multiMode = multiMode;
  398. }
  399. static fromJson(json={}) {
  400. return new GrpcStreamSettings(
  401. json.serviceName,
  402. json.multiMode
  403. );
  404. }
  405. toJson() {
  406. return {
  407. serviceName: this.serviceName,
  408. multiMode: this.multiMode
  409. }
  410. }
  411. }
  412. class TlsStreamSettings extends XrayCommonClass {
  413. constructor(serverName='',
  414. minVersion = TLS_VERSION_OPTION.TLS12,
  415. maxVersion = TLS_VERSION_OPTION.TLS13,
  416. cipherSuites = '',
  417. rejectUnknownSni = false,
  418. certificates=[new TlsStreamSettings.Cert()],
  419. alpn=[ALPN_OPTION.H2,ALPN_OPTION.HTTP1],
  420. settings=new TlsStreamSettings.Settings()) {
  421. super();
  422. this.server = serverName;
  423. this.minVersion = minVersion;
  424. this.maxVersion = maxVersion;
  425. this.cipherSuites = cipherSuites;
  426. this.rejectUnknownSni = rejectUnknownSni;
  427. this.certs = certificates;
  428. this.alpn = alpn;
  429. this.settings = settings;
  430. }
  431. addCert() {
  432. this.certs.push(new TlsStreamSettings.Cert());
  433. }
  434. removeCert(index) {
  435. this.certs.splice(index, 1);
  436. }
  437. static fromJson(json={}) {
  438. let certs;
  439. let settings;
  440. if (!ObjectUtil.isEmpty(json.certificates)) {
  441. certs = json.certificates.map(cert => TlsStreamSettings.Cert.fromJson(cert));
  442. }
  443. if (!ObjectUtil.isEmpty(json.settings)) {
  444. settings = new TlsStreamSettings.Settings(json.settings.allowInsecure , json.settings.fingerprint, json.settings.serverName, json.settings.domains); }
  445. return new TlsStreamSettings(
  446. json.serverName,
  447. json.minVersion,
  448. json.maxVersion,
  449. json.cipherSuites,
  450. json.rejectUnknownSni,
  451. certs,
  452. json.alpn,
  453. settings,
  454. );
  455. }
  456. toJson() {
  457. return {
  458. serverName: this.server,
  459. minVersion: this.minVersion,
  460. maxVersion: this.maxVersion,
  461. cipherSuites: this.cipherSuites,
  462. rejectUnknownSni: this.rejectUnknownSni,
  463. certificates: TlsStreamSettings.toJsonArray(this.certs),
  464. alpn: this.alpn,
  465. settings: this.settings,
  466. };
  467. }
  468. }
  469. TlsStreamSettings.Cert = class extends XrayCommonClass {
  470. constructor(useFile=true, certificateFile='', keyFile='', certificate='', key='') {
  471. super();
  472. this.useFile = useFile;
  473. this.certFile = certificateFile;
  474. this.keyFile = keyFile;
  475. this.cert = certificate instanceof Array ? certificate.join('\n') : certificate;
  476. this.key = key instanceof Array ? key.join('\n') : key;
  477. }
  478. static fromJson(json={}) {
  479. if ('certificateFile' in json && 'keyFile' in json) {
  480. return new TlsStreamSettings.Cert(
  481. true,
  482. json.certificateFile,
  483. json.keyFile,
  484. );
  485. } else {
  486. return new TlsStreamSettings.Cert(
  487. false, '', '',
  488. json.certificate.join('\n'),
  489. json.key.join('\n'),
  490. );
  491. }
  492. }
  493. toJson() {
  494. if (this.useFile) {
  495. return {
  496. certificateFile: this.certFile,
  497. keyFile: this.keyFile,
  498. };
  499. } else {
  500. return {
  501. certificate: this.cert.split('\n'),
  502. key: this.key.split('\n'),
  503. };
  504. }
  505. }
  506. };
  507. TlsStreamSettings.Settings = class extends XrayCommonClass {
  508. constructor(allowInsecure = false, fingerprint = '', serverName = '', domains = []) {
  509. super();
  510. this.allowInsecure = allowInsecure;
  511. this.fingerprint = fingerprint;
  512. this.serverName = serverName;
  513. this.domains = domains;
  514. }
  515. static fromJson(json = {}) {
  516. return new TlsStreamSettings.Settings(
  517. json.allowInsecure,
  518. json.fingerprint,
  519. json.serverName,
  520. json.domains,
  521. );
  522. }
  523. toJson() {
  524. return {
  525. allowInsecure: this.allowInsecure,
  526. fingerprint: this.fingerprint,
  527. serverName: this.serverName,
  528. domains: this.domains,
  529. };
  530. }
  531. };
  532. class XtlsStreamSettings extends XrayCommonClass {
  533. constructor(serverName='',
  534. certificates=[new XtlsStreamSettings.Cert()],
  535. alpn=[ALPN_OPTION.H2,ALPN_OPTION.HTTP1],
  536. settings=new XtlsStreamSettings.Settings()) {
  537. super();
  538. this.server = serverName;
  539. this.certs = certificates;
  540. this.alpn = alpn;
  541. this.settings = settings;
  542. }
  543. addCert() {
  544. this.certs.push(new XtlsStreamSettings.Cert());
  545. }
  546. removeCert(index) {
  547. this.certs.splice(index, 1);
  548. }
  549. static fromJson(json={}) {
  550. let certs;
  551. let settings;
  552. if (!ObjectUtil.isEmpty(json.certificates)) {
  553. certs = json.certificates.map(cert => XtlsStreamSettings.Cert.fromJson(cert));
  554. }
  555. if (!ObjectUtil.isEmpty(json.settings)) {
  556. settings = new XtlsStreamSettings.Settings(json.settings.allowInsecure , json.settings.serverName);
  557. }
  558. return new XtlsStreamSettings(
  559. json.serverName,
  560. certs,
  561. json.alpn,
  562. settings,
  563. );
  564. }
  565. toJson() {
  566. return {
  567. serverName: this.server,
  568. certificates: XtlsStreamSettings.toJsonArray(this.certs),
  569. alpn: this.alpn,
  570. settings: this.settings,
  571. };
  572. }
  573. }
  574. XtlsStreamSettings.Cert = class extends XrayCommonClass {
  575. constructor(useFile=true, certificateFile='', keyFile='', certificate='', key='') {
  576. super();
  577. this.useFile = useFile;
  578. this.certFile = certificateFile;
  579. this.keyFile = keyFile;
  580. this.cert = certificate instanceof Array ? certificate.join('\n') : certificate;
  581. this.key = key instanceof Array ? key.join('\n') : key;
  582. }
  583. static fromJson(json={}) {
  584. if ('certificateFile' in json && 'keyFile' in json) {
  585. return new XtlsStreamSettings.Cert(
  586. true,
  587. json.certificateFile,
  588. json.keyFile,
  589. );
  590. } else {
  591. return new XtlsStreamSettings.Cert(
  592. false, '', '',
  593. json.certificate.join('\n'),
  594. json.key.join('\n'),
  595. );
  596. }
  597. }
  598. toJson() {
  599. if (this.useFile) {
  600. return {
  601. certificateFile: this.certFile,
  602. keyFile: this.keyFile,
  603. };
  604. } else {
  605. return {
  606. certificate: this.cert.split('\n'),
  607. key: this.key.split('\n'),
  608. };
  609. }
  610. }
  611. };
  612. XtlsStreamSettings.Settings = class extends XrayCommonClass {
  613. constructor(allowInsecure = false, serverName = '') {
  614. super();
  615. this.allowInsecure = allowInsecure;
  616. this.serverName = serverName;
  617. }
  618. static fromJson(json = {}) {
  619. return new XtlsStreamSettings.Settings(
  620. json.allowInsecure,
  621. json.servername,
  622. );
  623. }
  624. toJson() {
  625. return {
  626. allowInsecure: this.allowInsecure,
  627. serverName: this.serverName,
  628. };
  629. }
  630. };
  631. class RealityStreamSettings extends XrayCommonClass {
  632. constructor(
  633. show = false,xver = 0,
  634. dest = 'yahoo.com:443',
  635. serverNames = 'yahoo.com,www.yahoo.com',
  636. privateKey = '',
  637. minClient = '',
  638. maxClient = '',
  639. maxTimediff = 0,
  640. shortIds = RandomUtil.randomShortId(),
  641. settings= new RealityStreamSettings.Settings()
  642. ){
  643. super();
  644. this.show = show;
  645. this.xver = xver;
  646. this.dest = dest;
  647. this.serverNames = serverNames instanceof Array ? serverNames.join(",") : serverNames;
  648. this.privateKey = privateKey;
  649. this.minClient = minClient;
  650. this.maxClient = maxClient;
  651. this.maxTimediff = maxTimediff;
  652. this.shortIds = shortIds instanceof Array ? shortIds.join(",") : shortIds;
  653. this.settings = settings;
  654. }
  655. static fromJson(json = {}) {
  656. let settings;
  657. if (!ObjectUtil.isEmpty(json.settings)) {
  658. settings = new RealityStreamSettings.Settings(json.settings.publicKey , json.settings.fingerprint, json.settings.serverName, json.settings.spiderX);
  659. }
  660. return new RealityStreamSettings(
  661. json.show,
  662. json.xver,
  663. json.dest,
  664. json.serverNames,
  665. json.privateKey,
  666. json.minClient,
  667. json.maxClient,
  668. json.maxTimediff,
  669. json.shortIds,
  670. json.settings,
  671. );
  672. }
  673. toJson() {
  674. return {
  675. show: this.show,
  676. xver: this.xver,
  677. dest: this.dest,
  678. serverNames: this.serverNames.split(","),
  679. privateKey: this.privateKey,
  680. minClient: this.minClient,
  681. maxClient: this.maxClient,
  682. maxTimediff: this.maxTimediff,
  683. shortIds: this.shortIds.split(","),
  684. settings: this.settings,
  685. };
  686. }
  687. }
  688. RealityStreamSettings.Settings = class extends XrayCommonClass {
  689. constructor(publicKey = '', fingerprint = UTLS_FINGERPRINT.UTLS_FIREFOX, serverName = '', spiderX= '/') {
  690. super();
  691. this.publicKey = publicKey;
  692. this.fingerprint = fingerprint;
  693. this.serverName = serverName;
  694. this.spiderX = spiderX;
  695. }
  696. static fromJson(json = {}) {
  697. return new RealityStreamSettings.Settings(
  698. json.publicKey,
  699. json.fingerprint,
  700. json.serverName,
  701. json.spiderX,
  702. );
  703. }
  704. toJson() {
  705. return {
  706. publicKey: this.publicKey,
  707. fingerprint: this.fingerprint,
  708. serverName: this.serverName,
  709. spiderX: this.spiderX,
  710. };
  711. }
  712. };
  713. class StreamSettings extends XrayCommonClass {
  714. constructor(network='tcp',
  715. security='none',
  716. tlsSettings=new TlsStreamSettings(),
  717. xtlsSettings=new XtlsStreamSettings(),
  718. realitySettings = new RealityStreamSettings(),
  719. tcpSettings=new TcpStreamSettings(),
  720. kcpSettings=new KcpStreamSettings(),
  721. wsSettings=new WsStreamSettings(),
  722. httpSettings=new HttpStreamSettings(),
  723. quicSettings=new QuicStreamSettings(),
  724. grpcSettings=new GrpcStreamSettings(),
  725. ) {
  726. super();
  727. this.network = network;
  728. this.security = security;
  729. this.tls = tlsSettings;
  730. this.xtls = xtlsSettings;
  731. this.reality = realitySettings;
  732. this.tcp = tcpSettings;
  733. this.kcp = kcpSettings;
  734. this.ws = wsSettings;
  735. this.http = httpSettings;
  736. this.quic = quicSettings;
  737. this.grpc = grpcSettings;
  738. }
  739. get isTls() {
  740. return this.security === "tls";
  741. }
  742. set isTls(isTls) {
  743. if (isTls) {
  744. this.security = 'tls';
  745. } else {
  746. this.security = 'none';
  747. }
  748. }
  749. get isXtls() {
  750. return this.security === "xtls";
  751. }
  752. set isXtls(isXtls) {
  753. if (isXtls) {
  754. this.security = 'xtls';
  755. } else {
  756. this.security = 'none';
  757. }
  758. }
  759. //for Reality
  760. get isReality() {
  761. return this.security === "reality";
  762. }
  763. set isReality(isReality) {
  764. if (isReality) {
  765. this.security = 'reality';
  766. } else {
  767. this.security = 'none';
  768. }
  769. }
  770. static fromJson(json={}) {
  771. return new StreamSettings(
  772. json.network,
  773. json.security,
  774. TlsStreamSettings.fromJson(json.tlsSettings),
  775. XtlsStreamSettings.fromJson(json.xtlsSettings),
  776. RealityStreamSettings.fromJson(json.realitySettings),
  777. TcpStreamSettings.fromJson(json.tcpSettings),
  778. KcpStreamSettings.fromJson(json.kcpSettings),
  779. WsStreamSettings.fromJson(json.wsSettings),
  780. HttpStreamSettings.fromJson(json.httpSettings),
  781. QuicStreamSettings.fromJson(json.quicSettings),
  782. GrpcStreamSettings.fromJson(json.grpcSettings),
  783. );
  784. }
  785. toJson() {
  786. const network = this.network;
  787. return {
  788. network: network,
  789. security: this.security,
  790. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  791. xtlsSettings: this.isXtls ? this.xtls.toJson() : undefined,
  792. realitySettings: this.isReality ? this.reality.toJson() : undefined,
  793. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  794. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  795. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  796. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  797. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  798. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  799. };
  800. }
  801. }
  802. class Sniffing extends XrayCommonClass {
  803. constructor(enabled=true, destOverride=['http', 'tls', 'quic']) {
  804. super();
  805. this.enabled = enabled;
  806. this.destOverride = destOverride;
  807. }
  808. static fromJson(json={}) {
  809. let destOverride = ObjectUtil.clone(json.destOverride);
  810. if (!ObjectUtil.isEmpty(destOverride) && !ObjectUtil.isArrEmpty(destOverride)) {
  811. if (ObjectUtil.isEmpty(destOverride[0])) {
  812. destOverride = ['http', 'tls', 'quic'];
  813. }
  814. }
  815. return new Sniffing(
  816. !!json.enabled,
  817. destOverride,
  818. );
  819. }
  820. }
  821. class Inbound extends XrayCommonClass {
  822. constructor(port=RandomUtil.randomIntRange(10000, 60000),
  823. listen='',
  824. protocol=Protocols.VLESS,
  825. settings=null,
  826. streamSettings=new StreamSettings(),
  827. tag='',
  828. sniffing=new Sniffing(),
  829. clientStats='',
  830. ) {
  831. super();
  832. this.port = port;
  833. this.listen = listen;
  834. this._protocol = protocol;
  835. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  836. this.stream = streamSettings;
  837. this.tag = tag;
  838. this.sniffing = sniffing;
  839. this.clientStats = clientStats;
  840. }
  841. getClientStats() {
  842. return this.clientStats;
  843. }
  844. get protocol() {
  845. return this._protocol;
  846. }
  847. set protocol(protocol) {
  848. this._protocol = protocol;
  849. this.settings = Inbound.Settings.getSettings(protocol);
  850. if (protocol === Protocols.TROJAN) {
  851. this.tls = false;
  852. }
  853. }
  854. get tls() {
  855. return this.stream.security === 'tls';
  856. }
  857. set tls(isTls) {
  858. if (isTls) {
  859. this.stream.security = 'tls';
  860. } else {
  861. this.stream.security = 'none';
  862. }
  863. }
  864. get xtls() {
  865. return this.stream.security === 'xtls';
  866. }
  867. set xtls(isXtls) {
  868. if (isXtls) {
  869. this.stream.security = 'xtls';
  870. } else {
  871. this.stream.security = 'none';
  872. }
  873. }
  874. //for Reality
  875. get reality() {
  876. return this.stream.security === 'reality';
  877. }
  878. set reality(isReality) {
  879. if (isReality) {
  880. this.stream.security = 'reality';
  881. } else {
  882. this.stream.security = 'none';
  883. }
  884. }
  885. get network() {
  886. return this.stream.network;
  887. }
  888. set network(network) {
  889. this.stream.network = network;
  890. }
  891. get isTcp() {
  892. return this.network === "tcp";
  893. }
  894. get isWs() {
  895. return this.network === "ws";
  896. }
  897. get isKcp() {
  898. return this.network === "kcp";
  899. }
  900. get isQuic() {
  901. return this.network === "quic"
  902. }
  903. get isGrpc() {
  904. return this.network === "grpc";
  905. }
  906. get isH2() {
  907. return this.network === "http";
  908. }
  909. // Shadowsocks
  910. get method() {
  911. switch (this.protocol) {
  912. case Protocols.SHADOWSOCKS:
  913. return this.settings.method;
  914. default:
  915. return "";
  916. }
  917. }
  918. get serverName() {
  919. if (this.stream.isTls || this.stream.isXtls || this.stream.isReality) {
  920. return this.stream.tls.server;
  921. }
  922. return "";
  923. }
  924. get host() {
  925. if (this.isTcp) {
  926. return this.stream.tcp.request.getHeader("Host");
  927. } else if (this.isWs) {
  928. return this.stream.ws.getHeader("Host");
  929. } else if (this.isH2) {
  930. return this.stream.http.host[0];
  931. }
  932. return null;
  933. }
  934. get path() {
  935. if (this.isTcp) {
  936. return this.stream.tcp.request.path[0];
  937. } else if (this.isWs) {
  938. return this.stream.ws.path;
  939. } else if (this.isH2) {
  940. return this.stream.http.path[0];
  941. }
  942. return null;
  943. }
  944. get quicSecurity() {
  945. return this.stream.quic.security;
  946. }
  947. get quicKey() {
  948. return this.stream.quic.key;
  949. }
  950. get quicType() {
  951. return this.stream.quic.type;
  952. }
  953. get kcpType() {
  954. return this.stream.kcp.type;
  955. }
  956. get kcpSeed() {
  957. return this.stream.kcp.seed;
  958. }
  959. get serviceName() {
  960. return this.stream.grpc.serviceName;
  961. }
  962. isExpiry(index) {
  963. switch (this.protocol) {
  964. case Protocols.VMESS:
  965. if(this.settings.vmesses[index].expiryTime > 0)
  966. return this.settings.vmesses[index].expiryTime < new Date().getTime();
  967. return false
  968. case Protocols.VLESS:
  969. if(this.settings.vlesses[index].expiryTime > 0)
  970. return this.settings.vlesses[index].expiryTime < new Date().getTime();
  971. return false
  972. case Protocols.TROJAN:
  973. if(this.settings.trojans[index].expiryTime > 0)
  974. return this.settings.trojans[index].expiryTime < new Date().getTime();
  975. return false
  976. case Protocols.SHADOWSOCKS:
  977. if(this.settings.shadowsockses[index].expiryTime > 0)
  978. return this.settings.shadowsockses[index].expiryTime < new Date().getTime();
  979. return false
  980. default:
  981. return false;
  982. }
  983. }
  984. canEnableTls() {
  985. switch (this.protocol) {
  986. case Protocols.VMESS:
  987. case Protocols.VLESS:
  988. case Protocols.TROJAN:
  989. break;
  990. default:
  991. return false;
  992. }
  993. switch (this.network) {
  994. case "tcp":
  995. case "ws":
  996. case "http":
  997. case "quic":
  998. case "grpc":
  999. return true;
  1000. default:
  1001. return false;
  1002. }
  1003. }
  1004. canEnableReality() {
  1005. switch (this.protocol) {
  1006. case Protocols.VLESS:
  1007. case Protocols.TROJAN:
  1008. break;
  1009. default:
  1010. return false;
  1011. }
  1012. switch (this.network) {
  1013. case "tcp":
  1014. case "http":
  1015. case "grpc":
  1016. return true;
  1017. default:
  1018. return false;
  1019. }
  1020. }
  1021. //this is used for xtls-rprx-vision
  1022. canEnableTlsFlow() {
  1023. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1024. switch (this.protocol) {
  1025. case Protocols.VLESS:
  1026. return true;
  1027. default:
  1028. return false;
  1029. }
  1030. }
  1031. return false;
  1032. }
  1033. canSetTls() {
  1034. return this.canEnableTls();
  1035. }
  1036. canEnableXtls() {
  1037. switch (this.protocol) {
  1038. case Protocols.VLESS:
  1039. case Protocols.TROJAN:
  1040. break;
  1041. default:
  1042. return false;
  1043. }
  1044. return this.network === "tcp";
  1045. }
  1046. canEnableStream() {
  1047. switch (this.protocol) {
  1048. case Protocols.VMESS:
  1049. case Protocols.VLESS:
  1050. case Protocols.TROJAN:
  1051. return true;
  1052. default:
  1053. return false;
  1054. }
  1055. }
  1056. canSniffing() {
  1057. switch (this.protocol) {
  1058. case Protocols.VMESS:
  1059. case Protocols.VLESS:
  1060. case Protocols.TROJAN:
  1061. case Protocols.SHADOWSOCKS:
  1062. return true;
  1063. default:
  1064. return false;
  1065. }
  1066. }
  1067. reset() {
  1068. this.port = RandomUtil.randomIntRange(10000, 60000);
  1069. this.listen = '';
  1070. this.protocol = Protocols.VMESS;
  1071. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1072. this.stream = new StreamSettings();
  1073. this.tag = '';
  1074. this.sniffing = new Sniffing();
  1075. }
  1076. genVmessLink(address='', remark='', clientIndex=0) {
  1077. if (this.protocol !== Protocols.VMESS) {
  1078. return '';
  1079. }
  1080. let obj = {
  1081. v: '2',
  1082. ps: remark,
  1083. add: address,
  1084. port: this.port,
  1085. id: this.settings.vmesses[clientIndex].id,
  1086. aid: this.settings.vmesses[clientIndex].alterId,
  1087. net: this.stream.network,
  1088. type: 'none',
  1089. tls: this.stream.security,
  1090. };
  1091. let network = this.stream.network;
  1092. if (network === 'tcp') {
  1093. let tcp = this.stream.tcp;
  1094. obj.type = tcp.type;
  1095. if (tcp.type === 'http') {
  1096. let request = tcp.request;
  1097. obj.path = request.path.join(',');
  1098. let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1099. if (index >= 0) {
  1100. obj.host = request.headers[index].value;
  1101. }
  1102. }
  1103. } else if (network === 'kcp') {
  1104. let kcp = this.stream.kcp;
  1105. obj.type = kcp.type;
  1106. obj.path = kcp.seed;
  1107. } else if (network === 'ws') {
  1108. let ws = this.stream.ws;
  1109. obj.path = ws.path;
  1110. let index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1111. if (index >= 0) {
  1112. obj.host = ws.headers[index].value;
  1113. }
  1114. } else if (network === 'http') {
  1115. obj.net = 'h2';
  1116. obj.path = this.stream.http.path;
  1117. obj.host = this.stream.http.host.join(',');
  1118. } else if (network === 'quic') {
  1119. obj.type = this.stream.quic.type;
  1120. obj.host = this.stream.quic.security;
  1121. obj.path = this.stream.quic.key;
  1122. } else if (network === 'grpc') {
  1123. obj.path = this.stream.grpc.serviceName;
  1124. if (this.stream.grpc.multiMode){
  1125. obj.type = 'multi'
  1126. }
  1127. }
  1128. if (this.stream.security === 'tls') {
  1129. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1130. obj.add = this.stream.tls.server;
  1131. }
  1132. if (!ObjectUtil.isEmpty(this.stream.tls.settings.serverName)){
  1133. obj.sni = this.stream.tls.settings.serverName;
  1134. }
  1135. if (!ObjectUtil.isEmpty(this.stream.tls.settings.fingerprint)){
  1136. obj.fp = this.stream.tls.settings.fingerprint;
  1137. }
  1138. if (this.stream.tls.alpn.length>0){
  1139. obj.alpn = this.stream.tls.alpn.join(',');
  1140. }
  1141. if (this.stream.tls.settings.allowInsecure){
  1142. obj.allowInsecure = this.stream.tls.settings.allowInsecure;
  1143. }
  1144. }
  1145. return 'vmess://' + base64(JSON.stringify(obj, null, 2));
  1146. }
  1147. genVLESSLink(address = '', remark='', clientIndex=0) {
  1148. const settings = this.settings;
  1149. const uuid = settings.vlesses[clientIndex].id;
  1150. const port = this.port;
  1151. const type = this.stream.network;
  1152. const params = new Map();
  1153. params.set("type", this.stream.network);
  1154. switch (type) {
  1155. case "tcp":
  1156. const tcp = this.stream.tcp;
  1157. if (tcp.type === 'http') {
  1158. const request = tcp.request;
  1159. params.set("path", request.path.join(','));
  1160. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1161. if (index >= 0) {
  1162. const host = request.headers[index].value;
  1163. params.set("host", host);
  1164. }
  1165. params.set("headerType", 'http');
  1166. }
  1167. break;
  1168. case "kcp":
  1169. const kcp = this.stream.kcp;
  1170. params.set("headerType", kcp.type);
  1171. params.set("seed", kcp.seed);
  1172. break;
  1173. case "ws":
  1174. const ws = this.stream.ws;
  1175. params.set("path", ws.path);
  1176. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1177. if (index >= 0) {
  1178. const host = ws.headers[index].value;
  1179. params.set("host", host);
  1180. }
  1181. break;
  1182. case "http":
  1183. const http = this.stream.http;
  1184. params.set("path", http.path);
  1185. params.set("host", http.host);
  1186. break;
  1187. case "quic":
  1188. const quic = this.stream.quic;
  1189. params.set("quicSecurity", quic.security);
  1190. params.set("key", quic.key);
  1191. params.set("headerType", quic.type);
  1192. break;
  1193. case "grpc":
  1194. const grpc = this.stream.grpc;
  1195. params.set("serviceName", grpc.serviceName);
  1196. if(grpc.multiMode){
  1197. params.set("mode", "multi");
  1198. }
  1199. break;
  1200. }
  1201. if (this.tls) {
  1202. params.set("security", "tls");
  1203. params.set("fp" , this.stream.tls.settings.fingerprint);
  1204. params.set("alpn", this.stream.tls.alpn);
  1205. if(this.stream.tls.settings.allowInsecure){
  1206. params.set("allowInsecure", "1");
  1207. }
  1208. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1209. address = this.stream.tls.server;
  1210. }
  1211. if (this.stream.tls.settings.serverName !== ''){
  1212. params.set("sni", this.stream.tls.settings.serverName);
  1213. }
  1214. if (type === "tcp" && this.settings.vlesses[clientIndex].flow.length > 0) {
  1215. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1216. }
  1217. }
  1218. if (this.xtls) {
  1219. params.set("security", "xtls");
  1220. params.set("alpn", this.stream.xtls.alpn);
  1221. if(this.stream.xtls.settings.allowInsecure){
  1222. params.set("allowInsecure", "1");
  1223. }
  1224. if (!ObjectUtil.isEmpty(this.stream.xtls.server)) {
  1225. address = this.stream.xtls.server;
  1226. }
  1227. if (this.stream.xtls.settings.serverName !== ''){
  1228. params.set("sni", this.stream.xtls.settings.serverName);
  1229. }
  1230. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1231. }
  1232. if (this.reality) {
  1233. params.set("security", "reality");
  1234. params.set("fp", this.stream.reality.settings.fingerprint);
  1235. params.set("pbk", this.stream.reality.settings.publicKey);
  1236. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1237. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1238. }
  1239. if (this.stream.network === 'tcp' && !ObjectUtil.isEmpty(this.settings.vlesses[clientIndex].flow)) {
  1240. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1241. }
  1242. if (this.stream.reality.shortIds.length > 0) {
  1243. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1244. }
  1245. if (!ObjectUtil.isEmpty(this.stream.reality.settings.serverName)) {
  1246. address = this.stream.reality.settings.serverName;
  1247. }
  1248. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1249. params.set("spx", this.stream.reality.settings.spiderX);
  1250. }
  1251. }
  1252. const link = `vless://${uuid}@${address}:${port}`;
  1253. const url = new URL(link);
  1254. for (const [key, value] of params) {
  1255. url.searchParams.set(key, value)
  1256. }
  1257. url.hash = encodeURIComponent(remark);
  1258. return url.toString();
  1259. }
  1260. genSSLink(address='', remark='', clientIndex = 0) {
  1261. let settings = this.settings;
  1262. const port = this.port;
  1263. return 'ss://' + safeBase64(settings.method + ':' + settings.password + ':' +settings.shadowsockses[clientIndex].password) + '@' + address + ':' + this.port + '#' + encodeURIComponent(remark);
  1264. }
  1265. genTrojanLink(address = '', remark = '', clientIndex = 0) {
  1266. let settings = this.settings;
  1267. const port = this.port;
  1268. const type = this.stream.network;
  1269. const params = new Map();
  1270. params.set("type", this.stream.network);
  1271. switch (type) {
  1272. case "tcp":
  1273. const tcp = this.stream.tcp;
  1274. if (tcp.type === 'http') {
  1275. const request = tcp.request;
  1276. params.set("path", request.path.join(','));
  1277. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1278. if (index >= 0) {
  1279. const host = request.headers[index].value;
  1280. params.set("host", host);
  1281. }
  1282. params.set("headerType", 'http');
  1283. }
  1284. break;
  1285. case "kcp":
  1286. const kcp = this.stream.kcp;
  1287. params.set("headerType", kcp.type);
  1288. params.set("seed", kcp.seed);
  1289. break;
  1290. case "ws":
  1291. const ws = this.stream.ws;
  1292. params.set("path", ws.path);
  1293. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1294. if (index >= 0) {
  1295. const host = ws.headers[index].value;
  1296. params.set("host", host);
  1297. }
  1298. break;
  1299. case "http":
  1300. const http = this.stream.http;
  1301. params.set("path", http.path);
  1302. params.set("host", http.host);
  1303. break;
  1304. case "quic":
  1305. const quic = this.stream.quic;
  1306. params.set("quicSecurity", quic.security);
  1307. params.set("key", quic.key);
  1308. params.set("headerType", quic.type);
  1309. break;
  1310. case "grpc":
  1311. const grpc = this.stream.grpc;
  1312. params.set("serviceName", grpc.serviceName);
  1313. if(grpc.multiMode){
  1314. params.set("mode", "multi");
  1315. }
  1316. break;
  1317. }
  1318. if (this.tls) {
  1319. params.set("security", "tls");
  1320. params.set("fp" , this.stream.tls.settings.fingerprint);
  1321. params.set("alpn", this.stream.tls.alpn);
  1322. if(this.stream.tls.settings.allowInsecure){
  1323. params.set("allowInsecure", "1");
  1324. }
  1325. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1326. address = this.stream.tls.server;
  1327. }
  1328. if (this.stream.tls.settings.serverName !== ''){
  1329. params.set("sni", this.stream.tls.settings.serverName);
  1330. }
  1331. }
  1332. if (this.reality) {
  1333. params.set("security", "reality");
  1334. params.set("fp", this.stream.reality.settings.fingerprint);
  1335. params.set("pbk", this.stream.reality.settings.publicKey);
  1336. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1337. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1338. }
  1339. if (this.stream.reality.shortIds.length > 0) {
  1340. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1341. }
  1342. if (!ObjectUtil.isEmpty(this.stream.reality.settings.serverName)) {
  1343. address = this.stream.reality.settings.serverName;
  1344. }
  1345. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1346. params.set("spx", this.stream.reality.settings.spiderX);
  1347. }
  1348. }
  1349. if (this.xtls) {
  1350. params.set("security", "xtls");
  1351. params.set("alpn", this.stream.xtls.alpn);
  1352. if(this.stream.xtls.settings.allowInsecure){
  1353. params.set("allowInsecure", "1");
  1354. }
  1355. if (!ObjectUtil.isEmpty(this.stream.xtls.server)) {
  1356. address = this.stream.xtls.server;
  1357. }
  1358. if (this.stream.xtls.settings.serverName !== ''){
  1359. params.set("sni", this.stream.xtls.settings.serverName);
  1360. }
  1361. params.set("flow", this.settings.trojans[clientIndex].flow);
  1362. }
  1363. const link = `trojan://${settings.trojans[clientIndex].password}@${address}:${this.port}`;
  1364. const url = new URL(link);
  1365. for (const [key, value] of params) {
  1366. url.searchParams.set(key, value)
  1367. }
  1368. url.hash = encodeURIComponent(remark);
  1369. return url.toString();
  1370. }
  1371. genLink(address='', remark='', clientIndex=0) {
  1372. switch (this.protocol) {
  1373. case Protocols.VMESS:
  1374. return this.genVmessLink(address, remark, clientIndex);
  1375. case Protocols.VLESS:
  1376. return this.genVLESSLink(address, remark, clientIndex);
  1377. case Protocols.SHADOWSOCKS:
  1378. return this.genSSLink(address, remark, clientIndex);
  1379. case Protocols.TROJAN:
  1380. return this.genTrojanLink(address, remark, clientIndex);
  1381. default: return '';
  1382. }
  1383. }
  1384. genInboundLinks(address = '', remark = '') {
  1385. let link = '';
  1386. switch (this.protocol) {
  1387. case Protocols.VMESS:
  1388. case Protocols.VLESS:
  1389. case Protocols.TROJAN:
  1390. case Protocols.SHADOWSOCKS:
  1391. JSON.parse(this.settings).clients.forEach((client,index) => {
  1392. if(this.tls && !ObjectUtil.isArrEmpty(this.stream.tls.settings.domains)){
  1393. this.stream.tls.settings.domains.forEach((domain) => {
  1394. link += this.genLink(domain.domain, remark + '-' + client.email + '-' + domain.remark, index) + '\r\n';
  1395. });
  1396. } else {
  1397. link += this.genLink(address, remark + '-' + client.email, index) + '\r\n';
  1398. }
  1399. });
  1400. return link;
  1401. default: return '';
  1402. }
  1403. }
  1404. static fromJson(json={}) {
  1405. return new Inbound(
  1406. json.port,
  1407. json.listen,
  1408. json.protocol,
  1409. Inbound.Settings.fromJson(json.protocol, json.settings),
  1410. StreamSettings.fromJson(json.streamSettings),
  1411. json.tag,
  1412. Sniffing.fromJson(json.sniffing),
  1413. json.clientStats
  1414. )
  1415. }
  1416. toJson() {
  1417. let streamSettings;
  1418. if (this.canEnableStream() || this.protocol === Protocols.TROJAN) {
  1419. streamSettings = this.stream.toJson();
  1420. }
  1421. return {
  1422. port: this.port,
  1423. listen: this.listen,
  1424. protocol: this.protocol,
  1425. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1426. streamSettings: streamSettings,
  1427. tag: this.tag,
  1428. sniffing: this.sniffing.toJson(),
  1429. clientStats: this.clientStats
  1430. };
  1431. }
  1432. }
  1433. Inbound.Settings = class extends XrayCommonClass {
  1434. constructor(protocol) {
  1435. super();
  1436. this.protocol = protocol;
  1437. }
  1438. static getSettings(protocol) {
  1439. switch (protocol) {
  1440. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1441. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1442. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1443. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1444. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1445. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1446. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1447. default: return null;
  1448. }
  1449. }
  1450. static fromJson(protocol, json) {
  1451. switch (protocol) {
  1452. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1453. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1454. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1455. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1456. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1457. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1458. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1459. default: return null;
  1460. }
  1461. }
  1462. toJson() {
  1463. return {};
  1464. }
  1465. };
  1466. Inbound.VmessSettings = class extends Inbound.Settings {
  1467. constructor(protocol,
  1468. vmesses=[new Inbound.VmessSettings.Vmess()],
  1469. disableInsecureEncryption=false) {
  1470. super(protocol);
  1471. this.vmesses = vmesses;
  1472. this.disableInsecure = disableInsecureEncryption;
  1473. }
  1474. indexOfVmessById(id) {
  1475. return this.vmesses.findIndex(vmess => vmess.id === id);
  1476. }
  1477. addVmess(vmess) {
  1478. if (this.indexOfVmessById(vmess.id) >= 0) {
  1479. return false;
  1480. }
  1481. this.vmesses.push(vmess);
  1482. }
  1483. delVmess(vmess) {
  1484. const i = this.indexOfVmessById(vmess.id);
  1485. if (i >= 0) {
  1486. this.vmesses.splice(i, 1);
  1487. }
  1488. }
  1489. static fromJson(json={}) {
  1490. return new Inbound.VmessSettings(
  1491. Protocols.VMESS,
  1492. json.clients.map(client => Inbound.VmessSettings.Vmess.fromJson(client)),
  1493. ObjectUtil.isEmpty(json.disableInsecureEncryption) ? false : json.disableInsecureEncryption,
  1494. );
  1495. }
  1496. toJson() {
  1497. return {
  1498. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1499. disableInsecureEncryption: this.disableInsecure,
  1500. };
  1501. }
  1502. };
  1503. Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
  1504. constructor(id=RandomUtil.randomUUID(), alterId=0, email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1505. super();
  1506. this.id = id;
  1507. this.alterId = alterId;
  1508. this.email = email;
  1509. this.limitIp = limitIp;
  1510. this.totalGB = totalGB;
  1511. this.expiryTime = expiryTime;
  1512. this.enable = enable;
  1513. this.tgId = tgId;
  1514. this.subId = subId;
  1515. }
  1516. static fromJson(json={}) {
  1517. return new Inbound.VmessSettings.Vmess(
  1518. json.id,
  1519. json.alterId,
  1520. json.email,
  1521. json.limitIp,
  1522. json.totalGB,
  1523. json.expiryTime,
  1524. json.enable,
  1525. json.tgId,
  1526. json.subId,
  1527. );
  1528. }
  1529. get _expiryTime() {
  1530. if (this.expiryTime === 0 || this.expiryTime === "") {
  1531. return null;
  1532. }
  1533. if (this.expiryTime < 0){
  1534. return this.expiryTime / -86400000;
  1535. }
  1536. return moment(this.expiryTime);
  1537. }
  1538. set _expiryTime(t) {
  1539. if (t == null || t === "") {
  1540. this.expiryTime = 0;
  1541. } else {
  1542. this.expiryTime = t.valueOf();
  1543. }
  1544. }
  1545. get _totalGB() {
  1546. return toFixed(this.totalGB / ONE_GB, 2);
  1547. }
  1548. set _totalGB(gb) {
  1549. this.totalGB = toFixed(gb * ONE_GB, 0);
  1550. }
  1551. };
  1552. Inbound.VLESSSettings = class extends Inbound.Settings {
  1553. constructor(protocol,
  1554. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1555. decryption='none',
  1556. fallbacks=[],) {
  1557. super(protocol);
  1558. this.vlesses = vlesses;
  1559. this.decryption = 'none'; // Using decryption is not implemented here
  1560. this.fallbacks = fallbacks;
  1561. }
  1562. addFallback() {
  1563. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1564. }
  1565. delFallback(index) {
  1566. this.fallbacks.splice(index, 1);
  1567. }
  1568. // decryption should be set to static value
  1569. static fromJson(json={}) {
  1570. return new Inbound.VLESSSettings(
  1571. Protocols.VLESS,
  1572. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1573. 'none',
  1574. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1575. );
  1576. }
  1577. toJson() {
  1578. return {
  1579. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1580. decryption: 'none',
  1581. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1582. };
  1583. }
  1584. };
  1585. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1586. constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1587. super();
  1588. this.id = id;
  1589. this.flow = flow;
  1590. this.email = email;
  1591. this.limitIp = limitIp;
  1592. this.totalGB = totalGB;
  1593. this.expiryTime = expiryTime;
  1594. this.enable = enable;
  1595. this.tgId = tgId;
  1596. this.subId = subId;
  1597. }
  1598. static fromJson(json={}) {
  1599. return new Inbound.VLESSSettings.VLESS(
  1600. json.id,
  1601. json.flow,
  1602. json.email,
  1603. json.limitIp,
  1604. json.totalGB,
  1605. json.expiryTime,
  1606. json.enable,
  1607. json.tgId,
  1608. json.subId,
  1609. );
  1610. }
  1611. get _expiryTime() {
  1612. if (this.expiryTime === 0 || this.expiryTime === "") {
  1613. return null;
  1614. }
  1615. if (this.expiryTime < 0){
  1616. return this.expiryTime / -86400000;
  1617. }
  1618. return moment(this.expiryTime);
  1619. }
  1620. set _expiryTime(t) {
  1621. if (t == null || t === "") {
  1622. this.expiryTime = 0;
  1623. } else {
  1624. this.expiryTime = t.valueOf();
  1625. }
  1626. }
  1627. get _totalGB() {
  1628. return toFixed(this.totalGB / ONE_GB, 2);
  1629. }
  1630. set _totalGB(gb) {
  1631. this.totalGB = toFixed(gb * ONE_GB, 0);
  1632. }
  1633. };
  1634. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1635. constructor(name="", alpn='', path='', dest='', xver=0) {
  1636. super();
  1637. this.name = name;
  1638. this.alpn = alpn;
  1639. this.path = path;
  1640. this.dest = dest;
  1641. this.xver = xver;
  1642. }
  1643. toJson() {
  1644. let xver = this.xver;
  1645. if (!Number.isInteger(xver)) {
  1646. xver = 0;
  1647. }
  1648. return {
  1649. name: this.name,
  1650. alpn: this.alpn,
  1651. path: this.path,
  1652. dest: this.dest,
  1653. xver: xver,
  1654. }
  1655. }
  1656. static fromJson(json=[]) {
  1657. const fallbacks = [];
  1658. for (let fallback of json) {
  1659. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1660. fallback.name,
  1661. fallback.alpn,
  1662. fallback.path,
  1663. fallback.dest,
  1664. fallback.xver,
  1665. ))
  1666. }
  1667. return fallbacks;
  1668. }
  1669. };
  1670. Inbound.TrojanSettings = class extends Inbound.Settings {
  1671. constructor(protocol,
  1672. trojans=[new Inbound.TrojanSettings.Trojan()],
  1673. fallbacks=[],) {
  1674. super(protocol);
  1675. this.trojans = trojans;
  1676. this.fallbacks = fallbacks;
  1677. }
  1678. addTrojanFallback() {
  1679. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1680. }
  1681. delTrojanFallback(index) {
  1682. this.fallbacks.splice(index, 1);
  1683. }
  1684. static fromJson(json={}) {
  1685. return new Inbound.TrojanSettings(
  1686. Protocols.TROJAN,
  1687. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1688. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1689. }
  1690. toJson() {
  1691. return {
  1692. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1693. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
  1694. };
  1695. }
  1696. };
  1697. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1698. constructor(password=RandomUtil.randomSeq(10), flow='', email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1699. super();
  1700. this.password = password;
  1701. this.flow = flow;
  1702. this.email = email;
  1703. this.limitIp = limitIp;
  1704. this.totalGB = totalGB;
  1705. this.expiryTime = expiryTime;
  1706. this.enable = enable;
  1707. this.tgId = tgId;
  1708. this.subId = subId;
  1709. }
  1710. toJson() {
  1711. return {
  1712. password: this.password,
  1713. flow: this.flow,
  1714. email: this.email,
  1715. limitIp: this.limitIp,
  1716. totalGB: this.totalGB,
  1717. expiryTime: this.expiryTime,
  1718. enable: this.enable,
  1719. tgId: this.tgId,
  1720. subId: this.subId,
  1721. };
  1722. }
  1723. static fromJson(json = {}) {
  1724. return new Inbound.TrojanSettings.Trojan(
  1725. json.password,
  1726. json.flow,
  1727. json.email,
  1728. json.limitIp,
  1729. json.totalGB,
  1730. json.expiryTime,
  1731. json.enable,
  1732. json.tgId,
  1733. json.subId,
  1734. );
  1735. }
  1736. get _expiryTime() {
  1737. if (this.expiryTime === 0 || this.expiryTime === "") {
  1738. return null;
  1739. }
  1740. if (this.expiryTime < 0){
  1741. return this.expiryTime / -86400000;
  1742. }
  1743. return moment(this.expiryTime);
  1744. }
  1745. set _expiryTime(t) {
  1746. if (t == null || t === "") {
  1747. this.expiryTime = 0;
  1748. } else {
  1749. this.expiryTime = t.valueOf();
  1750. }
  1751. }
  1752. get _totalGB() {
  1753. return toFixed(this.totalGB / ONE_GB, 2);
  1754. }
  1755. set _totalGB(gb) {
  1756. this.totalGB = toFixed(gb * ONE_GB, 0);
  1757. }
  1758. };
  1759. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1760. constructor(name="", alpn='', path='', dest='', xver=0) {
  1761. super();
  1762. this.name = name;
  1763. this.alpn = alpn;
  1764. this.path = path;
  1765. this.dest = dest;
  1766. this.xver = xver;
  1767. }
  1768. toJson() {
  1769. let xver = this.xver;
  1770. if (!Number.isInteger(xver)) {
  1771. xver = 0;
  1772. }
  1773. return {
  1774. name: this.name,
  1775. alpn: this.alpn,
  1776. path: this.path,
  1777. dest: this.dest,
  1778. xver: xver,
  1779. }
  1780. }
  1781. static fromJson(json=[]) {
  1782. const fallbacks = [];
  1783. for (let fallback of json) {
  1784. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1785. fallback.name,
  1786. fallback.alpn,
  1787. fallback.path,
  1788. fallback.dest,
  1789. fallback.xver,
  1790. ))
  1791. }
  1792. return fallbacks;
  1793. }
  1794. };
  1795. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1796. constructor(protocol,
  1797. method=SSMethods.BLAKE3_AES_256_GCM,
  1798. password=RandomUtil.randomShadowsocksPassword(),
  1799. network='tcp,udp',
  1800. shadowsockses=[new Inbound.ShadowsocksSettings.Shadowsocks()]
  1801. ) {
  1802. super(protocol);
  1803. this.method = method;
  1804. this.password = password;
  1805. this.network = network;
  1806. this.shadowsockses = shadowsockses;
  1807. }
  1808. static fromJson(json={}) {
  1809. return new Inbound.ShadowsocksSettings(
  1810. Protocols.SHADOWSOCKS,
  1811. json.method,
  1812. json.password,
  1813. json.network,
  1814. json.clients.map(client => Inbound.ShadowsocksSettings.Shadowsocks.fromJson(client)),
  1815. );
  1816. }
  1817. toJson() {
  1818. return {
  1819. method: this.method,
  1820. password: this.password,
  1821. network: this.network,
  1822. clients: Inbound.ShadowsocksSettings.toJsonArray(this.shadowsockses)
  1823. };
  1824. }
  1825. };
  1826. Inbound.ShadowsocksSettings.Shadowsocks = class extends XrayCommonClass {
  1827. constructor(password=RandomUtil.randomShadowsocksPassword(), email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1828. super();
  1829. this.password = password;
  1830. this.email = email;
  1831. this.limitIp = limitIp;
  1832. this.totalGB = totalGB;
  1833. this.expiryTime = expiryTime;
  1834. this.enable = enable;
  1835. this.tgId = tgId;
  1836. this.subId = subId;
  1837. }
  1838. toJson() {
  1839. return {
  1840. password: this.password,
  1841. email: this.email,
  1842. limitIp: this.limitIp,
  1843. totalGB: this.totalGB,
  1844. expiryTime: this.expiryTime,
  1845. enable: this.enable,
  1846. tgId: this.tgId,
  1847. subId: this.subId,
  1848. };
  1849. }
  1850. static fromJson(json = {}) {
  1851. return new Inbound.ShadowsocksSettings.Shadowsocks(
  1852. json.password,
  1853. json.email,
  1854. json.limitIp,
  1855. json.totalGB,
  1856. json.expiryTime,
  1857. json.enable,
  1858. json.tgId,
  1859. json.subId,
  1860. );
  1861. }
  1862. get _expiryTime() {
  1863. if (this.expiryTime === 0 || this.expiryTime === "") {
  1864. return null;
  1865. }
  1866. if (this.expiryTime < 0){
  1867. return this.expiryTime / -86400000;
  1868. }
  1869. return moment(this.expiryTime);
  1870. }
  1871. set _expiryTime(t) {
  1872. if (t == null || t === "") {
  1873. this.expiryTime = 0;
  1874. } else {
  1875. this.expiryTime = t.valueOf();
  1876. }
  1877. }
  1878. get _totalGB() {
  1879. return toFixed(this.totalGB / ONE_GB, 2);
  1880. }
  1881. set _totalGB(gb) {
  1882. this.totalGB = toFixed(gb * ONE_GB, 0);
  1883. }
  1884. };
  1885. Inbound.DokodemoSettings = class extends Inbound.Settings {
  1886. constructor(protocol, address, port, network='tcp,udp', followRedirect=false) {
  1887. super(protocol);
  1888. this.address = address;
  1889. this.port = port;
  1890. this.network = network;
  1891. this.followRedirect = followRedirect;
  1892. }
  1893. static fromJson(json={}) {
  1894. return new Inbound.DokodemoSettings(
  1895. Protocols.DOKODEMO,
  1896. json.address,
  1897. json.port,
  1898. json.network,
  1899. json.followRedirect,
  1900. );
  1901. }
  1902. toJson() {
  1903. return {
  1904. address: this.address,
  1905. port: this.port,
  1906. network: this.network,
  1907. followRedirect: this.followRedirect,
  1908. };
  1909. }
  1910. };
  1911. Inbound.SocksSettings = class extends Inbound.Settings {
  1912. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  1913. super(protocol);
  1914. this.auth = auth;
  1915. this.accounts = accounts;
  1916. this.udp = udp;
  1917. this.ip = ip;
  1918. }
  1919. addAccount(account) {
  1920. this.accounts.push(account);
  1921. }
  1922. delAccount(index) {
  1923. this.accounts.splice(index, 1);
  1924. }
  1925. static fromJson(json={}) {
  1926. let accounts;
  1927. if (json.auth === 'password') {
  1928. accounts = json.accounts.map(
  1929. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  1930. )
  1931. }
  1932. return new Inbound.SocksSettings(
  1933. Protocols.SOCKS,
  1934. json.auth,
  1935. accounts,
  1936. json.udp,
  1937. json.ip,
  1938. );
  1939. }
  1940. toJson() {
  1941. return {
  1942. auth: this.auth,
  1943. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  1944. udp: this.udp,
  1945. ip: this.ip,
  1946. };
  1947. }
  1948. };
  1949. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  1950. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1951. super();
  1952. this.user = user;
  1953. this.pass = pass;
  1954. }
  1955. static fromJson(json={}) {
  1956. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  1957. }
  1958. };
  1959. Inbound.HttpSettings = class extends Inbound.Settings {
  1960. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  1961. super(protocol);
  1962. this.accounts = accounts;
  1963. }
  1964. addAccount(account) {
  1965. this.accounts.push(account);
  1966. }
  1967. delAccount(index) {
  1968. this.accounts.splice(index, 1);
  1969. }
  1970. static fromJson(json={}) {
  1971. return new Inbound.HttpSettings(
  1972. Protocols.HTTP,
  1973. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  1974. );
  1975. }
  1976. toJson() {
  1977. return {
  1978. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  1979. };
  1980. }
  1981. };
  1982. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  1983. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1984. super();
  1985. this.user = user;
  1986. this.pass = pass;
  1987. }
  1988. static fromJson(json={}) {
  1989. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  1990. }
  1991. };