How to set socket proxy server?

I know developers can set http proxy server like the following codes: when send a http request, the local server(127.0.0.1:6000) will receive the http request.

Now I want to set socket proxy server: when the socket client send a socket message to a remote server, the local server(127.0.0.1:6000) will receive socket message.


+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
    return request;
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b {
    return [super requestIsCacheEquivalent:a toRequest:b];
}

+ (void)setLocalPort:(NSInteger)localPort {
    ssLocalPort = localPort;
}

- (void)startLoading
{
    if (!session) {
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        configuration.connectionProxyDictionary =
        @{(NSString *)kCFStreamPropertySOCKSProxyHost: @"127.0.0.1",
          (NSString *)kCFStreamPropertySOCKSProxyPort: @(6000)};
        session = [NSURLSession sessionWithConfiguration:configuration];
    }
    
    // ...
}

Replies

Now I want to set socket proxy server: when the socket client send a socket message

I need to clarify some terms here:

  • I suspect that “socket proxy” refers to the SOCKS proxy protocol. Is that right?

  • With regards “socket client”, my best guess is that you’re talking about a program using the BSD Sockets API, but I find that hard to reconcile with the previous point because BSD Sockets does not, in and of itself, support SOCKS.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

It does refer to SOCKS5.

I want to write a iOS app SDK , which include the following function:

  1. Start a local server as a proxy server, which intercepts all connect and send actions from the socket client(using BSD Sockets API) in app. The local proxy server is transparent to the socket client.
  2. Transfer these connect and send actions to remote server that supports SOCKS proxy protocol.
  3. Transfer messages from remote server to original socket client.


I do not know how to intercept all connect and send actions. Is there a way to do this?

Thanks for clarifying your requirements. I still have one question. You wrote:

Start a local server as a proxy server, which intercepts all connect and send actions from the socket client (using BSD Sockets API) in app.

BSD Sockets does not support SOCKS out of the box. Is your client using some library on top of BSD Sockets that implements SOCKS support? Or do you plan to modify the BSD Sockets code to add SOCKS support? Or are you trying to do this without changing the BSD Sockets code?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Actually, I haven't thought about it.

The first thing I think about is how intercepts all connect and send actions from the socket client.

I thought all Sockets support SOCKS.

I thought all Sockets support SOCKS.

Nope. BSD Sockets is a remarkably primitive API, and one of its many limitations is that it has no support for SOCKS. To get SOCKS support you have to add a layer on top of BSD Sockets.

On Apple platforms the preferred way to do this is via the Network framework, although older APIs also support this.

Do you have existing networking code? If so, what API is it using?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I don't have existing networking code.

I plan to put down this problem for the moment.

Thank your answer.

I plan to put down this problem for the moment.

Fair enough.

For the sake of other folks reading this thread, I want to clarify one potential point of confusion. Earlier I wrote:

To get SOCKS support you have to add a layer on top of BSD Sockets.

On Apple platforms the preferred way to do this is via the Network framework

which could be construed as implying that Network framework lets you customise the proxy configuration for each

NWConnection
. That’s not currently true.
NWConnection
will use the system-configured proxies by default, but you can’t tell a connection to use a specific proxy.

Curiously, you can do this with older APIs, like

NSStream
.

If you’d like to see this feature added to

NWConnection
, file an enhancement request describing your requirements and explaining your rationale. And if you do that, please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"