xray.js 76 KB

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