xray.js 67 KB

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