xray.js 69 KB

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