xray.js 80 KB

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