xray.js 68 KB

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