111 lines
3.1 KiB
Objective-C
111 lines
3.1 KiB
Objective-C
//
|
||
// YUMIUrl.m
|
||
// YUMI
|
||
//
|
||
// Created by YUMI on 2022/8/14.
|
||
//
|
||
|
||
#import "YUMIUrl.h"
|
||
#import <objc/message.h>
|
||
|
||
@interface NSString(YUMIBack)
|
||
|
||
- (NSString *)base64String;
|
||
|
||
@end
|
||
|
||
@implementation NSString(YUMIBack)
|
||
|
||
- (NSString *)base64String {
|
||
NSData *data= [self dataUsingEncoding:NSUTF8StringEncoding];
|
||
NSString *n = [data base64EncodedDataWithOptions:0];
|
||
return n;
|
||
|
||
}
|
||
|
||
@end
|
||
|
||
@interface YUMIBackUrlManager : NSObject
|
||
|
||
@property (nonatomic, strong) NSDictionary * recordMap;
|
||
|
||
@end
|
||
|
||
|
||
@implementation YUMIBackUrlManager
|
||
|
||
- (instancetype)init {
|
||
if (self = [super init]) {
|
||
self.recordMap = @{
|
||
@"YXBpLmN6cXoxMTEuY2(原域名加密)":@"新域名",
|
||
};
|
||
}
|
||
return self;
|
||
}
|
||
|
||
+ (instancetype)shared {
|
||
static dispatch_once_t onceToken;
|
||
static YUMIBackUrlManager* sharedInstance = nil;
|
||
dispatch_once(&onceToken, ^{
|
||
sharedInstance = [[YUMIBackUrlManager alloc] init];
|
||
});
|
||
return sharedInstance;
|
||
}
|
||
|
||
- (NSString *)newHostFromOld:(NSString *)oldHost {
|
||
NSString *codecString = [oldHost base64String];
|
||
return self.recordMap[codecString];
|
||
}
|
||
|
||
@end
|
||
|
||
|
||
@implementation NSURL(YUMIBack)
|
||
|
||
+(void)load {
|
||
static dispatch_once_t onceToken;
|
||
dispatch_once(&onceToken, ^{
|
||
Method fromMethod = class_getClassMethod(self, @selector(URLWithString:));
|
||
Method toMethod = class_getClassMethod(self, @selector(DuDuURLWithString:));
|
||
method_exchangeImplementations(toMethod, fromMethod);
|
||
|
||
fromMethod = class_getInstanceMethod(self, @selector(initWithString:));
|
||
toMethod = class_getInstanceMethod(self, @selector(DuDuURLWithString:));
|
||
method_exchangeImplementations(toMethod, fromMethod);
|
||
});
|
||
}
|
||
|
||
- (instancetype)DuDuURLWithString:(NSString *)URLString {
|
||
if (URLString.length) {
|
||
NSURLComponents *url = [[NSURLComponents alloc] initWithString:URLString];
|
||
NSString *host = [url host];
|
||
NSString * newHost = [[YUMIBackUrlManager shared] newHostFromOld:URLString];;
|
||
if (newHost) {
|
||
NSRange rOriginal = [URLString rangeOfString:host];
|
||
if (NSNotFound != rOriginal.location) { //替换第一个出现的地方
|
||
NSString *newUrl = [URLString stringByReplacingCharactersInRange:rOriginal withString:newHost];
|
||
return [self DuDuURLWithString:newUrl];
|
||
}
|
||
}
|
||
}
|
||
return [self DuDuURLWithString:URLString];
|
||
}
|
||
|
||
+ (instancetype)DuDuURLWithString:(NSString *)URLString {
|
||
if (URLString.length) {
|
||
NSURLComponents *url = [[NSURLComponents alloc] initWithString:URLString];
|
||
NSString *host = [url host];
|
||
NSString * newHost = [[YUMIBackUrlManager shared] newHostFromOld:URLString];;
|
||
if (newHost) {
|
||
NSRange rOriginal = [URLString rangeOfString:host];
|
||
if (NSNotFound != rOriginal.location) { //替换第一个出现的地方
|
||
NSString *newUrl = [URLString stringByReplacingCharactersInRange:rOriginal withString:newHost];
|
||
return [self DuDuURLWithString:newUrl];
|
||
}
|
||
}
|
||
}
|
||
return [self DuDuURLWithString:URLString];
|
||
}
|
||
|
||
@end
|