@@ -20,86 +20,84 @@ public class TyposquattingService : ITyposquattingService
2020 new ThresholdInfo ( lowerBound : 50 , upperBound : 129 , threshold : 2 )
2121 } ;
2222
23- private static int TyposquattingCheckListLength ;
24-
2523 private readonly IContentObjectService _contentObjectService ;
2624 private readonly IPackageService _packageService ;
2725 private readonly IReservedNamespaceService _reservedNamespaceService ;
2826 private readonly ITelemetryService _telemetryService ;
27+ private readonly ITyposquattingCheckListCacheService _typosquattingCheckListCacheService ;
2928
30- public TyposquattingService ( IContentObjectService contentObjectService , IPackageService packageService , IReservedNamespaceService reservedNamespaceService , ITelemetryService telemetryService )
29+ public TyposquattingService ( IContentObjectService contentObjectService ,
30+ IPackageService packageService ,
31+ IReservedNamespaceService reservedNamespaceService ,
32+ ITelemetryService telemetryService ,
33+ ITyposquattingCheckListCacheService typosquattingCheckListCacheService )
3134 {
3235 _contentObjectService = contentObjectService ?? throw new ArgumentNullException ( nameof ( contentObjectService ) ) ;
3336 _packageService = packageService ?? throw new ArgumentNullException ( nameof ( packageService ) ) ;
3437 _reservedNamespaceService = reservedNamespaceService ?? throw new ArgumentNullException ( nameof ( reservedNamespaceService ) ) ;
3538 _telemetryService = telemetryService ?? throw new ArgumentNullException ( nameof ( telemetryService ) ) ;
36-
37- TyposquattingCheckListLength = _contentObjectService . TyposquattingConfiguration . PackageIdChecklistLength ;
39+ _typosquattingCheckListCacheService = typosquattingCheckListCacheService ?? throw new ArgumentNullException ( nameof ( typosquattingCheckListCacheService ) ) ;
3840 }
3941
4042 public bool IsUploadedPackageIdTyposquatting ( string uploadedPackageId , User uploadedPackageOwner , out List < string > typosquattingCheckCollisionIds )
4143 {
44+ var checkListConfiguredLength = _contentObjectService . TyposquattingConfiguration . PackageIdChecklistLength ;
45+ var checkListExpireTimeInHours = TimeSpan . FromHours ( _contentObjectService . TyposquattingConfiguration . PackageIdChecklistCacheExpireTimeInHours ) ;
4246 typosquattingCheckCollisionIds = new List < string > ( ) ;
4347 var wasUploadBlocked = false ;
48+
4449 if ( ! _contentObjectService . TyposquattingConfiguration . IsCheckEnabled || _reservedNamespaceService . GetReservedNamespacesForId ( uploadedPackageId ) . Any ( ) )
4550 {
4651 return wasUploadBlocked ;
4752 }
48-
4953 if ( uploadedPackageId == null )
5054 {
5155 throw new ArgumentNullException ( nameof ( uploadedPackageId ) ) ;
5256 }
53-
5457 if ( uploadedPackageOwner == null )
5558 {
5659 throw new ArgumentNullException ( nameof ( uploadedPackageOwner ) ) ;
5760 }
5861
62+ var totalTimeStopwatch = Stopwatch . StartNew ( ) ;
5963 var checklistRetrievalStopwatch = Stopwatch . StartNew ( ) ;
60- var packageRegistrations = _packageService . GetAllPackageRegistrations ( ) ;
61- var packagesCheckList = packageRegistrations
62- . OrderByDescending ( pr => pr . IsVerified )
63- . ThenByDescending ( pr => pr . DownloadCount )
64- . Select ( pr => pr . Id )
65- . Take ( TyposquattingCheckListLength )
66- . ToList ( ) ;
64+ var packageIdsCheckList = _typosquattingCheckListCacheService . GetTyposquattingCheckList ( checkListConfiguredLength , checkListExpireTimeInHours , _packageService ) ;
6765 checklistRetrievalStopwatch . Stop ( ) ;
6866
67+ _telemetryService . TrackMetricForTyposquattingChecklistRetrievalTime ( uploadedPackageId , checklistRetrievalStopwatch . Elapsed ) ;
68+
6969 var algorithmProcessingStopwatch = Stopwatch . StartNew ( ) ;
7070 var threshold = GetThreshold ( uploadedPackageId ) ;
7171 var normalizedUploadedPackageId = TyposquattingStringNormalization . NormalizeString ( uploadedPackageId ) ;
72-
7372 var collisionIds = new ConcurrentBag < string > ( ) ;
74- Parallel . ForEach ( packagesCheckList , ( packageId , loopState ) =>
73+ Parallel . ForEach ( packageIdsCheckList , ( packageId , loopState ) =>
7574 {
7675 string normalizedPackageId = TyposquattingStringNormalization . NormalizeString ( packageId ) ;
7776 if ( TyposquattingDistanceCalculation . IsDistanceLessThanThreshold ( normalizedUploadedPackageId , normalizedPackageId , threshold ) )
7877 {
7978 collisionIds . Add ( packageId ) ;
8079 }
8180 } ) ;
82-
8381 algorithmProcessingStopwatch . Stop ( ) ;
8482
85- var totalTime = checklistRetrievalStopwatch . Elapsed . Add ( algorithmProcessingStopwatch . Elapsed ) ;
86- _telemetryService . TrackMetricForTyposquattingChecklistRetrievalTime ( uploadedPackageId , checklistRetrievalStopwatch . Elapsed ) ;
8783 _telemetryService . TrackMetricForTyposquattingAlgorithmProcessingTime ( uploadedPackageId , algorithmProcessingStopwatch . Elapsed ) ;
8884
8985 if ( collisionIds . Count == 0 )
9086 {
87+ totalTimeStopwatch . Stop ( ) ;
9188 _telemetryService . TrackMetricForTyposquattingCheckResultAndTotalTime (
9289 uploadedPackageId ,
93- totalTime ,
90+ totalTimeStopwatch . Elapsed ,
9491 wasUploadBlocked ,
9592 typosquattingCheckCollisionIds ,
96- TyposquattingCheckListLength ) ;
93+ packageIdsCheckList . Count ,
94+ checkListExpireTimeInHours ) ;
9795
9896 return false ;
9997 }
10098
10199 var ownersCheckStopwatch = Stopwatch . StartNew ( ) ;
102- var collisionPackagesIdAndOwners = packageRegistrations
100+ var collisionPackagesIdAndOwners = _packageService . GetAllPackageRegistrations ( )
103101 . Where ( pr => collisionIds . Contains ( pr . Id ) )
104102 . Select ( pr => new { Id = pr . Id , Owners = pr . Owners . Select ( x => x . Key ) . ToList ( ) } )
105103 . ToList ( ) ;
@@ -122,21 +120,21 @@ public bool IsUploadedPackageIdTyposquatting(string uploadedPackageId, User uplo
122120 . Any ( pio => pio . Owners . Any ( k => k == uploadedPackageOwner . Key ) ) ;
123121
124122 wasUploadBlocked = _contentObjectService . TyposquattingConfiguration . IsBlockUsersEnabled && ! isUserAllowedTyposquatting ;
125-
126123 ownersCheckStopwatch . Stop ( ) ;
127124
128- totalTime = totalTime . Add ( ownersCheckStopwatch . Elapsed ) ;
129125 _telemetryService . TrackMetricForTyposquattingOwnersCheckTime ( uploadedPackageId , ownersCheckStopwatch . Elapsed ) ;
126+
127+ totalTimeStopwatch . Stop ( ) ;
130128 _telemetryService . TrackMetricForTyposquattingCheckResultAndTotalTime (
131129 uploadedPackageId ,
132- totalTime ,
130+ totalTimeStopwatch . Elapsed ,
133131 wasUploadBlocked ,
134132 typosquattingCheckCollisionIds ,
135- TyposquattingCheckListLength ) ;
133+ packageIdsCheckList . Count ,
134+ checkListExpireTimeInHours ) ;
136135
137136 return wasUploadBlocked ;
138137 }
139-
140138 private static int GetThreshold ( string packageId )
141139 {
142140 foreach ( var thresholdInfo in ThresholdsList )
@@ -150,7 +148,6 @@ private static int GetThreshold(string packageId)
150148 throw new ArgumentException ( String . Format ( "There is no predefined typo-squatting threshold for this package Id: {0}" , packageId ) ) ;
151149 }
152150 }
153-
154151 public class ThresholdInfo
155152 {
156153 public int LowerBound { get ; }
0 commit comments