login about faq

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.

asked Jun 14 at 08:58

kirill's gravatar image

kirill
262

edited Jun 14 at 10:00


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
link

answered Jun 14 at 11:27

kirill's gravatar image

kirill
262

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×98
×25
×3
×3
×2

Asked: Jun 14 at 08:58

Seen: 266 times

Last updated: Jun 14 at 11:27

powered by OSQA