Skip to content

Commit 68f5ed5

Browse files
Pablo LopezPablo Lopez
Pablo Lopez
authored and
Pablo Lopez
committed
Save log to file
1 parent c5f218a commit 68f5ed5

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

src/ios/PinnedHTTPS.m

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,38 @@ @interface CustomURLConnectionDelegate : NSObject<NSURLConnectionDelegate>
1515
@property (retain) NSString *_foundFingerprint;
1616
@property (retain) NSString *_fingerprintType;
1717
@property (retain) NSString *_allFingerprints;
18+
@property (retain) NSString *log;
1819

1920
- (id)initWithPlugin:(CDVPlugin*)plugin callbackId:(NSString*)callbackId fingerprints:(NSArray*)fingerprints fingerprintType:(NSString*)fingerprintType;
2021

2122
@end
2223

2324
@implementation CustomURLConnectionDelegate
2425

26+
-(void)WriteToStringFile:(NSString *)textToWrite{
27+
NSFileManager *fileMgr;
28+
NSString *homeDir;
29+
fileMgr = [NSFileManager defaultManager];
30+
homeDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
31+
32+
NSString *filepath;
33+
filepath = [[NSString alloc] init];
34+
NSError *err;
35+
36+
filepath = [homeDir stringByAppendingPathComponent:@"pinnedhttps.log"];
37+
38+
BOOL ok = [textToWrite writeToFile:filepath atomically:YES encoding:NSUnicodeStringEncoding error:&err];
39+
40+
if (ok) {
41+
NSLog(@"File writen %@\n%@",
42+
filepath, textToWrite);
43+
} else {
44+
NSLog(@"Error writing sfile at %@\n%@",
45+
filepath, [err localizedFailureReason]);
46+
}
47+
48+
}
49+
2550
- (id)initWithPlugin:(CDVPlugin*)plugin callbackId:(NSString*)callbackId fingerprints:(NSArray*)fingerprints fingerprintType:(NSString*)fingerprintType
2651
{
2752
self.validFingerprint = false;
@@ -41,27 +66,44 @@ - (void)connection: (NSURLConnection*)connection willSendRequestForAuthenticatio
4166
NSString* connFingerprint;
4267
bool isValid = false;
4368
int certCount = SecTrustGetCertificateCount(serverCert);
44-
printf("\ncertificados: %lu", (unsigned long)certCount);
69+
4570
self._allFingerprints = @"";
46-
for (int i = 0; i < certCount; i++){
71+
self.log = @"Start fingerprint validation...";
72+
73+
printf("\nFound fingerprint size: %lu", (unsigned long)certCount);
74+
self.log = [self.log stringByAppendingString:[NSString stringWithFormat:@"\nFound fingerprint size: %lu", (unsigned long)certCount]];
75+
76+
printf("\nAuthorized fingerprint size: %lu", (unsigned long)self._fingerprints.count);
77+
self.log = [self.log stringByAppendingString:[NSString stringWithFormat:@"\nAuthorized fingerprint size: %lu", (unsigned long)self._fingerprints.count]];
78+
79+
for (int i = 0; i < certCount; i++){
4780
if ([self._fingerprintType isEqual: @"SHA1"]) connFingerprint = [self getSHA1Fingerprint: SecTrustGetCertificateAtIndex(serverCert, i)];
4881
else connFingerprint = [self getSHA256Fingerprint: SecTrustGetCertificateAtIndex(serverCert, i)];
49-
printf("\n%s", [connFingerprint UTF8String]);
50-
printf("\nlista finger: %lu", (unsigned long)self._fingerprints.count);
82+
83+
printf("\n Found fingerprint: %s", [connFingerprint UTF8String]);
84+
self.log = [self.log stringByAppendingString:[NSString stringWithFormat:@"\n Found fingerprint: %s", [connFingerprint UTF8String]]];
85+
86+
printf("\n Compare with:");
87+
self.log = [self.log stringByAppendingString:[NSString stringWithFormat:@"\n Compare with:"]];
5188

5289
self._allFingerprints = [self._allFingerprints stringByAppendingString:connFingerprint];
5390
self._allFingerprints = [self._allFingerprints stringByAppendingString:@","];
5491

5592
for (int j = 0; j < self._fingerprints.count; j++){
56-
printf("\n%s", [[self._fingerprints objectAtIndex: j] UTF8String] );
57-
if ([connFingerprint caseInsensitiveCompare: [self._fingerprints objectAtIndex: j]] == NSOrderedSame){
93+
94+
printf("\n %s", [[self._fingerprints objectAtIndex: j] UTF8String] );
95+
self.log = [self.log stringByAppendingString:[NSString stringWithFormat:@"\n %s", [[self._fingerprints objectAtIndex: j] UTF8String]]];
96+
97+
98+
if ([connFingerprint caseInsensitiveCompare: [self._fingerprints objectAtIndex: j]] == NSOrderedSame){
5899
isValid = true;
59100
break;
60101
}
61102
}
62103
if (isValid) break;
63104
}
64105
printf("\n");
106+
65107
self._foundFingerprint = connFingerprint;
66108
//NSLog(@"Found fingerprint for %@ %@: %@", connection.originalRequest.HTTPMethod, connection.originalRequest.URL.host, connFingerprint);
67109

@@ -84,8 +126,11 @@ - (void)connection: (NSURLConnection*)connection willSendRequestForAuthenticatio
84126
resultWithStatus : CDVCommandStatus_ERROR
85127
messageAsDictionary : jsonObj
86128
];
87-
88129

130+
printf("self.log error");
131+
printf("\n%s", [self.log UTF8String]);
132+
[self WriteToStringFile : self.log];
133+
89134
// CDVPluginResult *rslt = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"INVALID_CERT"];
90135

91136
[self._plugin.commandDelegate sendPluginResult: rslt callbackId: self._callbackId];
@@ -185,6 +230,10 @@ - (void)connectionDidFinishLoading:(NSURLConnection*)connection {
185230
nil
186231
];
187232

233+
printf("self.log ok\n");
234+
printf("%s", [self.log UTF8String]);
235+
[self WriteToStringFile : self.log];
236+
188237
CDVPluginResult *pluginResult = [ CDVPluginResult
189238
resultWithStatus : CDVCommandStatus_OK
190239
messageAsDictionary : jsonObj
@@ -372,4 +421,5 @@ - (void)req:(CDVInvokedUrlCommand*)command {
372421
NSMutableString *resultStr =
373422
}*/
374423

424+
375425
@end

0 commit comments

Comments
 (0)