|
8 | 8 |
|
9 | 9 | #import "QNNExternalIp.h" |
10 | 10 |
|
| 11 | + |
11 | 12 | @implementation QNNExternalIp |
12 | 13 |
|
| 14 | ++(NSString*) externalIp{ |
| 15 | + NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://whatismyip.akamai.com"]]; |
| 16 | + [urlRequest setHTTPMethod:@"GET"]; |
| 17 | + |
| 18 | + NSHTTPURLResponse *response = nil; |
| 19 | + NSError *httpError = nil; |
| 20 | + NSData *d = [NSURLConnection sendSynchronousRequest:urlRequest |
| 21 | + returningResponse:&response |
| 22 | + error:&httpError]; |
| 23 | + if (httpError != nil || d == nil) { |
| 24 | + return @""; |
| 25 | + } |
| 26 | + NSString* s = [[NSString alloc]initWithData:d encoding:NSUTF8StringEncoding]; |
| 27 | + if (s == nil) { |
| 28 | + return @""; |
| 29 | + } |
| 30 | + return s; |
| 31 | +} |
| 32 | + |
| 33 | ++(NSString*) externalDNS{ |
| 34 | + return @""; |
| 35 | +} |
| 36 | + |
| 37 | ++(NSString*)getDiagUrl{ |
| 38 | + NSString* fetchurl = @"http://ns.pbt.cachecn.net/fast_tools/fetch_ldns_diag_client.php"; |
| 39 | + |
| 40 | + NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:fetchurl]]; |
| 41 | + [urlRequest setHTTPMethod:@"GET"]; |
| 42 | + |
| 43 | + NSHTTPURLResponse *response = nil; |
| 44 | + NSError *httpError = nil; |
| 45 | + NSData *d = [NSURLConnection sendSynchronousRequest:urlRequest |
| 46 | + returningResponse:&response |
| 47 | + error:&httpError]; |
| 48 | + if (httpError != nil || d == nil) { |
| 49 | + NSLog(@"fetch http error %@", httpError); |
| 50 | + return nil; |
| 51 | + } |
| 52 | + NSLog(@"fetch http code %d", response.statusCode); |
| 53 | + NSString* s = [[NSString alloc]initWithData:d encoding:NSUTF8StringEncoding]; |
| 54 | + if (s == nil || [s isEqualToString:@""]) { |
| 55 | + NSLog(@"fetch http code %d", response.statusCode); |
| 56 | + return nil; |
| 57 | + } |
| 58 | + NSRange range = [s rangeOfString:@"<iframe src=\""]; |
| 59 | + if (range.location > 4000) { |
| 60 | + return nil; |
| 61 | + } |
| 62 | + s = [s substringFromIndex:range.location + range.length]; |
| 63 | + range = [s rangeOfString:@".php\""]; |
| 64 | + if (range.location>4000) { |
| 65 | + return nil; |
| 66 | + } |
| 67 | + s = [s substringToIndex:range.location+4]; |
| 68 | + return s; |
| 69 | +} |
| 70 | + |
| 71 | +// this service provided by fastweb |
| 72 | +//<pre> |
| 73 | +//<table border="0" cellpadding="0" cellspacing="0" class="dns"> |
| 74 | +//<tr> |
| 75 | +//<th>您的IP:</th> |
| 76 | +//<td width="128" >60.168.142.119</td> |
| 77 | +//<td>电信_安徽省_合肥市</td> |
| 78 | +//</tr> |
| 79 | +//<tr> |
| 80 | +//<th>您的DNS:</th> |
| 81 | +//<td width="128" >61.132.161.9</td> |
| 82 | +//<td>电信_安徽省_阜阳市</td> |
| 83 | +//</tr> |
| 84 | +//</table> |
| 85 | +//</pre> |
| 86 | +//<p class="result">您的DNS配置正确! </p> |
| 87 | + |
| 88 | ++(NSString*)checkExternal{ |
| 89 | + NSString* url = [QNNExternalIp getDiagUrl]; |
| 90 | + if (url == nil) { |
| 91 | + return @"get fetch url failed"; |
| 92 | + } |
| 93 | + |
| 94 | + NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; |
| 95 | + [urlRequest setHTTPMethod:@"GET"]; |
| 96 | + |
| 97 | + NSHTTPURLResponse *response = nil; |
| 98 | + NSError *httpError = nil; |
| 99 | + NSData *d = [NSURLConnection sendSynchronousRequest:urlRequest |
| 100 | + returningResponse:&response |
| 101 | + error:&httpError]; |
| 102 | + if (httpError != nil || d == nil) { |
| 103 | + return @"check server error"; |
| 104 | + } |
| 105 | + NSLog(@"http code %d", response.statusCode); |
| 106 | + NSString* s = [[NSString alloc]initWithData:d encoding:NSUTF8StringEncoding]; |
| 107 | + if (s == nil || [s isEqualToString:@""]) { |
| 108 | + return @"invalid encoding"; |
| 109 | + } |
| 110 | + |
| 111 | + NSRange range = [s rangeOfString:@"<tr>"]; |
| 112 | + s = [s substringFromIndex:range.location + range.length]; |
| 113 | + range = [s rangeOfString:@"</table>"]; |
| 114 | + s = [s substringToIndex:range.location]; |
| 115 | + |
| 116 | + s = [s stringByReplacingOccurrencesOfString:@"</" withString:@"<"]; |
| 117 | + |
| 118 | + s = [s stringByReplacingOccurrencesOfString:@"<tr>" withString:@""]; |
| 119 | + s = [s stringByReplacingOccurrencesOfString:@"<th>" withString:@""]; |
| 120 | + s = [s stringByReplacingOccurrencesOfString:@"<td>" withString:@""]; |
| 121 | + s = [s stringByReplacingOccurrencesOfString:@"<td>" withString:@""]; |
| 122 | + s = [s stringByReplacingOccurrencesOfString:@"<td width=\"128\" >" withString:@""]; |
| 123 | + |
| 124 | + s = [s stringByReplacingOccurrencesOfString:@"<table>" withString:@""]; |
| 125 | + s = [s stringByReplacingOccurrencesOfString:@"<p class=\"result\">" withString:@""]; |
| 126 | + s = [s stringByReplacingOccurrencesOfString:@"<pre>" withString:@""]; |
| 127 | + s = [s stringByReplacingOccurrencesOfString:@"<p>" withString:@""]; |
| 128 | + |
| 129 | + s = [s stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"]; |
| 130 | + s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; |
| 131 | + return s; |
| 132 | +} |
| 133 | + |
13 | 134 | @end |
0 commit comments