The correct way to create tcp connection to localhost

Hi there,

I am using NetworkExtension and would like to create tcp connection to localhost. With below code, faced some strange problems: appproxy restart itself periodically with all the traffic during the test.

Code Block
import NetworkExtension
let ep = NWHostEndpoint(hostname: "127.0.0.1", port: String(8888))
let connection = createTCPConnection(to: ep, enableTLS: false, tlsParameters: nil, delegate: nil)


Wonder what is the correct way to achieving that?

Thanks in advance for any suggestion.


appproxy restart itself periodically with all the traffic during the test.

This usually indicates a crash on the Network Extension and indicates an issue in your code. Try adding the following to know for sure:

Code Block swift
final class TransparentProxyProvider: NEAppProxyProvider {
static let log = OSLog(subsystem: "com.example.yourbundle_id.TransparentProxy", category: "provider")
override init() {
self.log = Self.log
os_log(.debug, log: self.log, "init")
super.init()
}
private let log: OSLog
/* ... */
}


If you see your constructor being hit when your proxy restarts, then this indicates a crash.

It looks like you are using an in-provider networking class for NWTCPConnection, which is fine in most cases, but it also looks like you are using an NEAppProxyProvider if I understood you correctly, so you do have the flexibility of NWConnection as well. Can you tell me more about why you are using NWTCPConnection?


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
The correct way to create tcp connection to localhost
 
 
Q