@@ -4,12 +4,15 @@ internal class ReadlineObfuscationNeedsFactory
44{
55 private readonly string [ ] _args ;
66 private readonly ObfuscationSettings _obfuscationSettings ;
7+ private readonly List < ProtectionSetting > _protectionSettings ;
78 private readonly ILogger _logger ;
89
9- public ReadlineObfuscationNeedsFactory ( string [ ] args , ObfuscationSettings obfuscationSettings , ILogger logger )
10+ public ReadlineObfuscationNeedsFactory ( string [ ] args , ObfuscationSettings obfuscationSettings ,
11+ List < ProtectionSetting > protectionSettings , ILogger logger )
1012 {
1113 _args = args ;
1214 _obfuscationSettings = obfuscationSettings ;
15+ _protectionSettings = protectionSettings ;
1316 _logger = logger . ForContext < ReadlineObfuscationNeedsFactory > ( ) ;
1417 }
1518
@@ -52,6 +55,96 @@ public ObfuscationNeeds Create(CancellationToken cancellationToken)
5255 }
5356 }
5457
58+ List < string > protections = [ ] ;
59+
60+ bool hasEnabledProtections = _protectionSettings != null && _protectionSettings . Any ( x => x . Enabled ) ;
61+ bool hasAnyProtectionSettings = _protectionSettings != null && _protectionSettings . Any ( ) ;
62+
63+ if ( ! hasEnabledProtections )
64+ {
65+ if ( ! hasAnyProtectionSettings )
66+ {
67+ _logger . Warning ( "No protection settings found (protections.json may be missing or empty)" ) ;
68+ _logger . Information ( "Please input the preferred protections with ',' delimiter, example: StringsEncryption,AntiDe4dot,ControlFlow" ) ;
69+ }
70+ else
71+ {
72+ _logger . Warning ( "No protection is enabled in protections.json file, please either enable any protection first or input the preferred with ',' delimiter, example: StringsEncryption,AntiDe4dot" ) ;
73+ }
74+
75+ while ( true )
76+ {
77+ try
78+ {
79+ cancellationToken . ThrowIfCancellationRequested ( ) ;
80+
81+ var protectionInput = Console . ReadLine ( ) ;
82+ cancellationToken . ThrowIfCancellationRequested ( ) ;
83+
84+ if ( ! string . IsNullOrWhiteSpace ( protectionInput ) )
85+ {
86+ var inputProtections = protectionInput . Split ( [ ',' ] , StringSplitOptions . RemoveEmptyEntries )
87+ . Select ( p => p . Trim ( ) )
88+ . Where ( p => ! string . IsNullOrWhiteSpace ( p ) )
89+ . ToList ( ) ;
90+
91+ if ( inputProtections . Any ( ) )
92+ {
93+ // If we have protection settings, validate against them but allow unknown protections
94+ if ( hasAnyProtectionSettings )
95+ {
96+ var availableProtections = _protectionSettings . Select ( p => p . Name ) . ToList ( ) ;
97+ var unknownProtections = inputProtections . Where ( p => ! availableProtections . Contains ( p , StringComparer . OrdinalIgnoreCase ) ) . ToList ( ) ;
98+
99+ if ( unknownProtections . Any ( ) )
100+ {
101+ _logger . Warning ( "The following protection(s) are not found in current protections.json but will be used anyway: {0}" ,
102+ string . Join ( ", " , unknownProtections ) ) ;
103+ _logger . Information ( "Available protections in config: {0}" ,
104+ availableProtections . Any ( ) ? string . Join ( ", " , availableProtections ) : "none" ) ;
105+ }
106+
107+ var knownProtections = inputProtections . Where ( p => availableProtections . Contains ( p , StringComparer . OrdinalIgnoreCase ) ) . ToList ( ) ;
108+ if ( knownProtections . Any ( ) )
109+ {
110+ _logger . Information ( "Recognized protection(s) from config: {0}" , string . Join ( ", " , knownProtections ) ) ;
111+ }
112+ }
113+ else
114+ {
115+ _logger . Warning ( "Cannot validate protection names as protections.json is missing/empty. Using specified protections as-is." ) ;
116+ }
117+
118+ protections = inputProtections ;
119+ _logger . Information ( "Protections successfully specified: {0}" , string . Join ( ", " , protections ) ) ;
120+ break ;
121+ }
122+
123+ _logger . Warning ( "No valid protections found in input, please try again!" ) ;
124+ }
125+ else
126+ {
127+ _logger . Information ( "No protections specified, continuing without additional protections..." ) ;
128+ break ;
129+ }
130+ }
131+ catch ( OperationCanceledException )
132+ {
133+ throw ;
134+ }
135+ catch ( Exception ex )
136+ {
137+ _logger . Error ( ex , "Something went wrong while specifying protections" ) ;
138+ }
139+ }
140+ }
141+ else
142+ {
143+ // Use enabled protections from settings
144+ protections = _protectionSettings . Where ( x => x . Enabled ) . Select ( x => x . Name ) . ToList ( ) ;
145+ _logger . Information ( "Using enabled protections from settings: {0}" , string . Join ( ", " , protections ) ) ;
146+ }
147+
55148 string dependenciesDirectoryName ;
56149 string outputDirectoryName ;
57150 var fileBaseDirectory = Path . GetDirectoryName ( fileName ) ;
@@ -112,7 +205,7 @@ public ObfuscationNeeds Create(CancellationToken cancellationToken)
112205 }
113206 else
114207 {
115- _logger . Information ( "Dependencies (libs) directory was automatically found in: {0}! " ,
208+ _logger . Information ( "Dependencies (libs) directory was automatically found in: {0}" ,
116209 dependenciesDirectoryName ) ;
117210 }
118211 }
@@ -125,7 +218,8 @@ public ObfuscationNeeds Create(CancellationToken cancellationToken)
125218 FileBaseDirectory = fileBaseDirectory ,
126219 ReferencesDirectoryName = dependenciesDirectoryName ,
127220 OutputPath = outputDirectoryName ,
128- Way = ObfuscationNeedsWay . Readline
221+ Way = ObfuscationNeedsWay . Readline ,
222+ Protections = protections ,
129223 } ;
130224 }
131225
0 commit comments