xray.js 67 KB

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