1+ const Blocklist = require ( '../models/Blocklist' )
2+ const { Op } = require ( 'sequelize' ) ;
3+
4+ async function sortBlocklistDuplicates ( newBlocklist , pa_id ) {
5+ const finalBlocklist = [ ] ;
6+ const finaleThrottlelist = [ ] ;
7+ const allSources = Object . fromEntries ( [ 'SourceIp' , 'Session' , 'UserAgent' ]
8+ . map ( key => [ key , [ ...newBlocklist . blocklist , ...newBlocklist . throttle ] . find ( item => item [ key ] ) ?. [ key ] ] )
9+ . filter ( ( [ , value ] ) => value !== undefined ) ) ;
10+ const existingBlocklist = await Blocklist . findAll ( { where : {
11+ pa_id,
12+ type : 'blocklist' ,
13+ [ Op . or ] : Object . entries ( allSources ) . map ( ( [ key , value ] ) => ( {
14+ [ `content.${ key } ` ] : value
15+ } ) ) }
16+ } ) ;
17+ const existingThrottlelist = await Blocklist . findAll ( { where : {
18+ pa_id,
19+ type : 'throttle' ,
20+ [ Op . or ] : Object . entries ( allSources ) . map ( ( [ key , value ] ) => ( {
21+ [ `content.${ key } ` ] : value
22+ } ) ) }
23+ } ) ;
24+ for ( const newItem of newBlocklist . blocklist ) {
25+ const sourceKeys = [ 'SourceIp' , 'Session' , 'UserAgent' ] . filter ( key => newItem [ key ] !== undefined ) ;
26+ const match = existingBlocklist . find ( existing =>
27+ sourceKeys . length > 0 &&
28+ sourceKeys . every ( key => existing . content [ key ] === newItem [ key ] ) &&
29+ sourceKeys . every ( key => existing . content [ key ] !== undefined ) &&
30+ sourceKeys . length === Object . keys ( existing . content ) . filter ( key => sourceKeys . includes ( key ) && existing . content [ key ] !== undefined ) . length ) ;
31+ if ( ! match ) {
32+ finalBlocklist . push ( newItem ) ;
33+ continue ;
34+ }
35+ if ( isNewBlocklistBehaviorPriority ( match , newItem ) ) {
36+ Blocklist . destroy ( { where : { id : match . id } } )
37+ finalBlocklist . push ( newItem ) ;
38+ }
39+ }
40+ for ( const newItem of newBlocklist . throttle ) {
41+ const sourceKeys = [ 'SourceIp' , 'Session' , 'UserAgent' ] . filter ( key => newItem [ key ] !== undefined ) ;
42+ const match = existingThrottlelist . find ( existing =>
43+ sourceKeys . length > 0 &&
44+ sourceKeys . every ( key => existing . content [ key ] === newItem [ key ] ) &&
45+ sourceKeys . every ( key => existing . content [ key ] !== undefined ) &&
46+ sourceKeys . length === Object . keys ( existing . content ) . filter ( key => sourceKeys . includes ( key ) && existing . content [ key ] !== undefined ) . length ) ;
47+ if ( match && isNewBlocklistBehaviorPriority ( match , newItem ) ) {
48+ Blocklist . destroy ( { where : { id : match . id } } )
49+ finaleThrottlelist . push ( newItem ) ;
50+ }
51+ }
52+ return { blocklist : finalBlocklist , throttle : finaleThrottlelist } ;
53+ }
54+
55+ function isNewBlocklistBehaviorPriority ( existingBlocklist , newBlocklist ) {
56+ const priorities = { clone : 1 , exhaust : 2 , drop : 3 , error : 4 } ;
57+ return priorities [ newBlocklist . Behavior ] < priorities [ existingBlocklist . content . Behavior ]
58+ }
59+
60+ module . exports = {
61+ sortBlocklistDuplicates
62+ }
0 commit comments