Setting up a proxy with an Alamofire session

I am trying to bind a proxy with a specific HTTP request. So I created a proxy class as shown below that grabs the proxy and binds it using kCF properties.

class Proxy {

     func configureProxy(proxy: String) -> NSURLSessionConfiguration {
        let proxyArr = proxy.componentsSeparatedByString(":")

        let host = proxyArr[0]
        let port = proxyArr[1].toInt()

        if let p  = port{
            return newSession(host, port: p)
        }

        return NSURLSessionConfiguration.defaultSessionConfiguration()
    }

     func newSession(host: String, port: Int) -> NSURLSessionConfiguration  {


        let proxyInfo = [
            kCFStreamPropertyHTTPProxyHost : host as NSString,
            kCFStreamPropertyHTTPProxyPort : port,
            kCFNetworkProxiesHTTPEnable as NSString : true
        ]

        let config = NSURLSessionConfiguration.defaultSessionConfiguration()
        config.connectionProxyDictionary = proxyInfo


        return config
    }
} 

      

This is my service class that receives a request from any service class that extends it. EG (HttpBin: Service). After all the settings are done, including the correct router (which is only mutableUrlRequest), the service class is responsible for sending the HTTP request. For testing purposes, the configuration is done during the initialization of the Service class.

class Service {

    var res      = ResponseHandler()
    var alamofireManager : Alamofire.Manager?


    init(){
        let ipconfig = Proxy()
        let config = ipconfig.configureProxy(VALIDPROXY)
        alamofireManager = Alamofire.Manager(configuration: config)
    }

    func createRequest(Router : routes, type : String, completion:(response: ResponseHandler) -> Void){

        if let manager = alamofireManager {
            println(manager.session.configuration.connectionProxyDictionary)

        switch(type){
            case "download":
                manager.request(Router).responseImage() {
                    (_, _, image, error) in

                    self.res.image   = image
                    self.res.success = true
                    completion(response: self.res)
                }
                break;
            case "upload":
                manager.upload(Router, data: uploadData)
                    .responseJSON { (request,response, JSON, error) in
                        self.res = self.getResponse(JSON, error : error)
                    }.responseString{ (_,_,string,error) in
                        if (error != nil){
                            println(error)
                        }
                        self.res.responseString = string
                        completion(response: self.res)
            }
            default:
                manager.request(Router)
                    .responseJSON { (request,response, JSON, error) in
                        self.res = self.getResponse(JSON, error : error)
                    }.responseString{ (_,_,string,error) in
                        self.res.responseString = string
                        println(error)
                        println(string)
                        completion(response: self.res)
            }
        }
        }

    }


    func getResponse(serverData : AnyObject?, error: NSError?)-> ResponseHandler{

        if let data: AnyObject = serverData {

            self.res.response = SwiftyJSON.JSON(data.0)
            if(error == nil){
                self.res.success = true
            }
            else{
                self.res.error   = error
                println(error)
            }
        }
        return self.res
    }
}

      

When hitting my api test, the client ip from the request looks like my ip, not the one generated from the proxyInfo dictionary. manager.session.configuration.connectionProxyDictionary prints the values ​​set in the dictionary. Any ideas if something in the Alamofire framework is preventing this, or if this is my implementation, is it wrong?

EDIT:

I am trying to use some things by implementing NSURLProtocol and using CFReadStreamSetProperty functions to intercept requests before they are sent. No luck yet.

Edit1: :(

+3
proxy swift alamofire macos


source to share


No one has answered this question yet

Check out similar questions:

1710
What's the difference between a proxy and a reverse proxy?
880
Getting git to work with a proxy
827
Setting environment variables on OS X
458
How can I checkout from a Git repository via an HTTP proxy?
338
How can I connect to Tor hidden service using cURL in PHP?
2
Proxy connection error in Wordpress plugin via fsockopen
2
TableView does not display text with JSON data from API call
1
Alamofire is the result of an NSMutableDictionary for an array of string: Anyobject
0
POST Json to API with Alamofire?
0
How do I create a block response like Alamofire responses?



All Articles
Loading...
X
Show
Funny
Dev
Pics