@@ -15,14 +15,32 @@ import { getServerCerts } from './generate-mtls-certs';
1515import { getXtlsApiPort } from './get-initial-ports' ;
1616import { IPolicyConfig } from './interfaces' ;
1717
18- export const generateApiConfig = (
19- config : Record < string , unknown > ,
20- isTorrentBlockerEnabled : boolean ,
18+ interface IRoutingXrayConfig {
19+ rules : {
20+ ruleTag ?: string ;
21+ webhook ?: {
22+ url : string ;
23+ deduplication : number ;
24+ } ;
25+ [ key : string ] : unknown ;
26+ } [ ] ;
27+ }
28+
29+ interface IGenerateApiConfigParams {
30+ config : Record < string , unknown > ;
31+ torrentBlockerState : {
32+ enabled : boolean ;
33+ includeRuleTags : Set < string > ;
34+ } ;
2135 internal : {
2236 socketPath : string ;
2337 token : string ;
24- } ,
25- ) : Record < string , unknown > => {
38+ } ;
39+ }
40+
41+ export const generateApiConfig = ( args : IGenerateApiConfigParams ) : Record < string , unknown > => {
42+ const { config, torrentBlockerState, internal } = args ;
43+
2644 const policyConfig = config . policy as undefined | IPolicyConfig ;
2745 const serverCerts = getServerCerts ( ) ;
2846 const hasCapNetAdminResult = hasCapNetAdmin ( ) ;
@@ -39,7 +57,7 @@ export const generateApiConfig = (
3957 system : XRAY_DEFAULT_POLICY_MODEL . policy . system ,
4058 } ;
4159
42- return {
60+ const result = {
4361 ...config ,
4462 ...XRAY_DEFAULT_STATS_MODEL ,
4563 ...XRAY_DEFAULT_API_MODEL ,
@@ -52,24 +70,44 @@ export const generateApiConfig = (
5270 } ) ,
5371 ...( Array . isArray ( config . inbounds ) ? config . inbounds : [ ] ) ,
5472 ] ,
55- outbounds : [
56- ...( Array . isArray ( config . outbounds ) ? config . outbounds : [ ] ) ,
57- ...( isTorrentBlockerEnabled ? [ XRAY_TORRENT_BLOCKER_OUTBOUND_MODEL ] : [ ] ) ,
58- ] ,
73+ outbounds : [ ...( Array . isArray ( config . outbounds ) ? config . outbounds : [ ] ) ] ,
5974 policy : builtPolicy ,
6075 routing : {
6176 ...( config . routing || { } ) ,
6277 rules : [
6378 XRAY_ROUTING_RULES_MODEL ,
64- ...( isTorrentBlockerEnabled
65- ? [
66- XRAY_TORRENT_BLOCKER_ROUTING_RULES_MODEL ( {
67- webhookUrl : `/${ internal . socketPath } :${ XRAY_INTERNAL_FULL_WEBHOOK_PATH } ?token=${ internal . token } ` ,
68- } ) ,
69- ]
70- : [ ] ) ,
7179 ...( ( config . routing as { rules ?: unknown [ ] } ) ?. rules || [ ] ) ,
7280 ] ,
7381 } ,
7482 } ;
83+
84+ if ( torrentBlockerState . enabled ) {
85+ const webhookUrl = buildWebhookUrl ( internal ) ;
86+ const routing = result . routing as IRoutingXrayConfig ;
87+
88+ result . outbounds . push ( XRAY_TORRENT_BLOCKER_OUTBOUND_MODEL ) ;
89+
90+ routing . rules . splice ( 1 , 0 , XRAY_TORRENT_BLOCKER_ROUTING_RULES_MODEL ( { webhookUrl } ) ) ;
91+
92+ if ( torrentBlockerState . includeRuleTags . size > 0 ) {
93+ for ( const rule of routing . rules ) {
94+ if (
95+ rule . ruleTag &&
96+ typeof rule . ruleTag === 'string' &&
97+ torrentBlockerState . includeRuleTags . has ( rule . ruleTag )
98+ ) {
99+ rule . webhook = {
100+ url : webhookUrl ,
101+ deduplication : 30 ,
102+ } ;
103+ }
104+ }
105+ }
106+ }
107+
108+ return result ;
109+ } ;
110+
111+ const buildWebhookUrl = ( internal : { socketPath : string ; token : string } ) : string => {
112+ return `/${ internal . socketPath } :${ XRAY_INTERNAL_FULL_WEBHOOK_PATH } ?token=${ internal . token } ` ;
75113} ;
0 commit comments