Files
waf-platform/HttpDNSSDK/sdk/ios/NewHttpDNS/Scheduler/HttpdnsScheduleExecutor.m
2026-03-05 02:44:43 +08:00

132 lines
5.0 KiB
Objective-C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// HttpdnsScheduleExecutor.m
// NewHttpDNS
//
// Created by ElonChan(地风) on 2017/4/11.
// Copyright © 2017�trustapp.com. All rights reserved.
//
#import "HttpdnsScheduleExecutor.h"
#import "HttpdnsLog_Internal.h"
#import "HttpdnsService_Internal.h"
#import "HttpdnsInternalConstant.h"
#import "HttpdnsUtil.h"
#import "HttpdnsScheduleCenter.h"
#import "HttpdnsHostObject.h"
#import "HttpdnsReachability.h"
#import "HttpdnsPublicConstant.h"
#import "HttpdnsNWHTTPClient.h"
@interface HttpdnsScheduleExecutor ()
@property (nonatomic, strong) HttpdnsNWHTTPClient *httpClient;
@end
@implementation HttpdnsScheduleExecutor {
NSInteger _accountId;
NSTimeInterval _timeoutInterval;
}
- (instancetype)init {
if (!(self = [super init])) {
return nil;
}
// 兼容旧路径:使用全局å<E282AC>•ä¾è¯»å<C2BB>,但多账å<C2A6>·åœºæ™¯ä¸å»ºè®®ä½¿ç”¨æ°init接å<C2A5>£
_accountId = [HttpDnsService sharedInstance].accountID;
_timeoutInterval = [HttpDnsService sharedInstance].timeoutInterval;
_httpClient = [HttpdnsNWHTTPClient sharedInstance];
return self;
}
- (instancetype)initWithAccountId:(NSInteger)accountId timeout:(NSTimeInterval)timeoutInterval {
if (!(self = [self init])) {
return nil;
}
_accountId = accountId;
_timeoutInterval = timeoutInterval;
return self;
}
/**
* 拼接 URL
* 2024.6.12今天起,调度æœ<EFBFBD>务由å<EFBFBD>Žç«¯å°±è¿è°ƒåº¦ï¼Œä¸<EFBFBD>å†<EFBFBD>需è¦<EFBFBD>ä¼ å…¥regionå<EFBFBD>数,但为了兼容ä¸<EFBFBD>ä¼ region默认就是å½å†…region的逻è¾ï¼Œé»˜è®¤éƒ½ä¼ å…¥region=global
* https://203.107.1.1/100000/ss?region=global&platform=ios&sdk_version=3.1.7&sid=LpmJIA2CUoi4&net=wifi
*/
- (NSString *)constructRequestURLWithUpdateHost:(NSString *)updateHost {
NSString *urlPath = [NSString stringWithFormat:@"%ld/ss?region=global&platform=ios&sdk_version=%@", (long)_accountId, HTTPDNS_IOS_SDK_VERSION];
urlPath = [self urlFormatSidNetBssid:urlPath];
urlPath = [urlPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
return [NSString stringWithFormat:@"https://%@/%@", updateHost, urlPath];
}
// url 添加 sid net
- (NSString *)urlFormatSidNetBssid:(NSString *)url {
NSString *sessionId = [HttpdnsUtil generateSessionID];
if ([HttpdnsUtil isNotEmptyString:sessionId]) {
url = [NSString stringWithFormat:@"%@&sid=%@", url, sessionId];
}
NSString *netType = [[HttpdnsReachability sharedInstance] currentReachabilityString];
if ([HttpdnsUtil isNotEmptyString:netType]) {
url = [NSString stringWithFormat:@"%@&net=%@", url, netType];
}
return url;
}
- (NSDictionary *)fetchRegionConfigFromServer:(NSString *)updateHost error:(NSError **)pError {
NSString *fullUrlStr = [self constructRequestURLWithUpdateHost:updateHost];
HttpdnsLogDebug("ScRequest URL: %@", fullUrlStr);
NSTimeInterval timeout = _timeoutInterval > 0 ? _timeoutInterval : HTTPDNS_DEFAULT_REQUEST_TIMEOUT_INTERVAL;
NSString *userAgent = [HttpdnsUtil generateUserAgent];
NSError *requestError = nil;
HttpdnsNWHTTPClientResponse *response = [self.httpClient performRequestWithURLString:fullUrlStr
userAgent:userAgent
timeout:timeout
error:&requestError];
if (!response) {
if (pError) {
*pError = requestError;
HttpdnsLogDebug("ScRequest failed with url: %@, error: %@", fullUrlStr, requestError);
}
return nil;
}
if (response.statusCode != 200) {
NSDictionary *dict = @{@"ResponseCode": [NSString stringWithFormat:@"%ld", (long)response.statusCode]};
if (pError) {
*pError = [NSError errorWithDomain:NEW_HTTPDNS_ERROR_DOMAIN
code:NEW_HTTPDNS_HTTPS_NO_DATA_ERROR_CODE
userInfo:dict];
}
return nil;
}
NSError *jsonError = nil;
id jsonValue = [NSJSONSerialization JSONObjectWithData:response.body options:kNilOptions error:&jsonError];
if (jsonError) {
if (pError) {
*pError = jsonError;
HttpdnsLogDebug("ScRequest JSON parse error, url: %@, error: %@", fullUrlStr, jsonError);
}
return nil;
}
NSDictionary *result = [HttpdnsUtil getValidDictionaryFromJson:jsonValue];
if (result) {
HttpdnsLogDebug("ScRequest get response: %@", result);
return result;
}
if (pError) {
*pError = [NSError errorWithDomain:NEW_HTTPDNS_ERROR_DOMAIN
code:NEW_HTTP_PARSE_JSON_FAILED
userInfo:@{NSLocalizedDescriptionKey: @"Failed to parse JSON response"}];
}
if (pError != NULL) {
HttpdnsLogDebug("ScRequest failed with url: %@, response body invalid", fullUrlStr);
}
return nil;
}
@end