xray.js 75 KB

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