xray.js 62 KB

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