xray.js 72 KB

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