代码之家  ›  专栏  ›  技术社区  ›  Vin

Swift:未调用didReceive质询函数时发生SSL错误

  •  0
  • Vin  · 技术社区  · 1 年前

    嗨,我正在进行修复SSL错误的测试项目。当我试图从以下来源获取数据时 https://1tamilmv.eu 我弄错了

    NSLocalizedDescription=发生SSL错误 无法连接到服务器。, NSErrorFailingURLKey=https://www.1tamilmv.eu/, NSUnderlyingError=0x600003f6c090{错误域=kCFErrorDomainCF网络 代码=-1200“(空)” 用户信息={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9816,_kCFStreamErrorDomainKey=3,_kCStreamErrorCodeKey=-9816;_NSURLErrorNWPathKey=满意(路径满意),可行,接口:en1,ipv4,ipv6,dns}},_kFStreamErrorCodeKey=-9816}

    我已经使用以下命令测试了cURL curl https://www.1tamilmv.eu 在终端。cURL也会出错

    curl:(35)LibreSSL SSL_connect:对等端重置连接 连接到 www.1tamilmv.eu:443

    但同样的URL在chrome、firefox网络浏览器中显示网站时没有任何问题。但我没有在safari浏览器中注意到 didReceive challenge 函数未被调用 几乎所有时候。但如果它被调用,那么网站加载成功。刚才装了一次。在网络抓取任务中,我在随机网站上也遇到了同样的错误。

    我也试过info.plist

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>1tamilmv.eu</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.0</string>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>
    

    代码:

    import Foundation
    
    class HTTP: NSObject, URLSessionDelegate, URLSessionTaskDelegate {
        
        private(set) var session: URLSession!
     
        override init() {
            
            let config = URLSessionConfiguration.default
            //config.tlsMaximumSupportedProtocolVersion = .TLSv13
            config.timeoutIntervalForRequest = 30
            config.timeoutIntervalForResource = 30
            config.httpAdditionalHeaders = [
                "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.2 Safari/605.1.15",
                "Accept-Encoding": "gzip, deflate, br",
                "Connection": "Keep-Alive"
            ]
         
            super.init()
           
            self.session = .init(configuration: config, delegate: self, delegateQueue: .main)
        }
        
        func load() {
            
            Task.detached(priority: .userInitiated) {
              
                var request = URLRequest(url: URL(string: "https://www.1tamilmv.eu/")!)
                request.httpMethod = "GET"
                
                do {
                    
                    let (data, response) = try await self.session.data(for: request)
                    
                    if let response = response as? HTTPURLResponse {
                        
                        print("-----------")
                        print("Status: \(response.statusCode)")
                        print("Data: \(data.count)")
                        print("-----------")
    
                    } else {
                        
                        print("Unable to load")
                    }
                    
                } catch {
                    
                    print("Error: \(error.localizedDescription)")
                } 
            }
           
        }
      
        func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge) async -> (URLSession.AuthChallengeDisposition, URLCredential?) {
    
            print("Challenging....")
            if challenge.protectionSpace.serverTrust == nil {
    
                return (.useCredential, nil)
            }
    
            let trust: SecTrust = challenge.protectionSpace.serverTrust!
            let credential = URLCredential(trust: trust)
            return (.useCredential, credential)
        }
    }
    

    我点击时没有下载数据 HTTP().load() 在大多数时候。还有其他解决方案可以解决这个问题吗?

    1 回复  |  直到 1 年前
        1
  •  0
  •   Pranay Vin    1 年前

    这是服务器端的问题。我通过像浏览器通常所做的那样发送额外的标头来解决同样的问题。

    "Accept-Language": "en-GB,en;q=0.9",
    "Origin": "https://www.1tamilmv.eu",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "cross-site"