xray.js 67 KB

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