Question:
Hi guys,
I tried to put all request from an application by the ssh tunnel via Chilkat library. Currently, the main problem is that custom URLProtocol can't use a local proxy for receiving data:
there some code snippets: ssh tunnel + local socks5 proxy (from example)
tunnel = CkoSshTunnel()
var sshHostname: String? = Server.current?.ipAddress
var sshPort: Int = 22
success = tunnel!.connect(sshHostname, port: sshPort as NSNumber)
if success != true {
print("\(tunnel?.lastErrorText! ?? "unknown error")")
return
}
success = (tunnel?.authenticatePw("some", password: "password"))!
if success != true {
print("\(String(describing: tunnel?.lastErrorText))")
return
}
tunnel?.dynamicPortForwarding = true
success = tunnel!.beginAccepting(1082)
if success != true {
print("\(String(describing: tunnel?.lastErrorText))")
return
}
self.httpProxyServer.socksHostname = "localhost"
httpProxyServer.verboseLogging = true
httpProxyServer.basicAuth = true
httpProxyServer.socksPort = 1082
httpProxyServer.socksVersion = 5
httpProxyServer.sendCookies = true
httpProxyServer.saveCookies = true
httpProxyServer.cookieDir = "memory"
httpProxyServer.socksVersion = 5
and custom URLProtocol start method:
override func startLoading() {
let userName = "test"
let userPassword = "test"
let userPasswordString = userName + ":" + userPassword
let userPasswordData = userPasswordString.data(using: String.Encoding.utf8)
let base64EncodedCredential:String = userPasswordString.base64Encoded()!
let authString = "Basic \(base64EncodedCredential)"
mutableRequest.addValue(authString, forHTTPHeaderField: "Proxy-Authorization")
let configuration = URLSessionConfiguration.default
var proxyConfiguration = [NSObject: AnyObject]()
proxyConfiguration[kCFNetworkProxiesHTTPProxy] = "localhost" as AnyObject
proxyConfiguration[kCFNetworkProxiesHTTPPort] = 1082 as AnyObject
proxyConfiguration[kCFNetworkProxiesHTTPEnable] = 1 as AnyObject
configuration.connectionProxyDictionary = proxyConfiguration
let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
dataTask = session.dataTask(with: mutableRequest)
dataTask?.resume()
}
maybe somebody did the same thing before and can help me with it.
P.S. Excuse me for my English.
I figured it out: the main problem was at the proxyConfiguration dictionary the correct version looks like:
var proxyConfiguration = [NSObject: AnyObject]()
proxyConfiguration[kCFNetworkProxiesHTTPEnable] = 1 as AnyObject
proxyConfiguration[kCFProxyTypeKey] = kCFProxyTypeSOCKS as AnyObject
proxyConfiguration[kCFStreamPropertySOCKSProxyHost] = "localhost" as AnyObject
proxyConfiguration[kCFStreamPropertySOCKSProxyPort] = 1082 as AnyObject