We're trying to establish the connection with VPN for monitoring the network activities. I'm able to get the url but my internet is not working. When I start establish the connection it shows that connection is in connecting mode. But after sometime connection would be disconnected. The given below code for establish the connection:
private func connect(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) {
let settings: NEPacketTunnelNetworkSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: proxyServerAddress)
/* proxy settings */
let proxySettings: NEProxySettings = NEProxySettings()
proxySettings.httpServer = NEProxyServer(
address: proxyServerAddress,
port: Int(proxyServerPort)
)
proxySettings.httpsServer = NEProxyServer(
address: proxyServerAddress,
port: Int(proxyServerPort)
)
proxySettings.autoProxyConfigurationEnabled = false
proxySettings.httpEnabled = true
proxySettings.httpsEnabled = true
proxySettings.excludeSimpleHostnames = true
proxySettings.exceptionList = [
"192.168.0.0/16",
"10.0.0.0/8",
"172.16.0.0/12",
"127.0.0.1",
"localhost",
"*.local"
]
settings.proxySettings = proxySettings
/* ipv4 settings */
let ipv4Settings: NEIPv4Settings = NEIPv4Settings(
addresses: [settings.tunnelRemoteAddress],
subnetMasks: ["255.255.255.255"]
)
ipv4Settings.includedRoutes = [NEIPv4Route.default()]
ipv4Settings.excludedRoutes = [
NEIPv4Route(destinationAddress: "192.168.0.0", subnetMask: "255.255.0.0"),
NEIPv4Route(destinationAddress: "10.0.0.0", subnetMask: "255.0.0.0"),
NEIPv4Route(destinationAddress: "172.16.0.0", subnetMask: "255.240.0.0")
]
settings.ipv4Settings = ipv4Settings
let dnsSettings = NEDNSSettings(servers: ["8.8.8.8", "1.1.1.1"])
settings.dnsSettings = dnsSettings
/* MTU */
settings.mtu = 1500
RawSocketFactory.TunnelProvider = self
self.setTunnelNetworkSettings(settings, completionHandler: { error in
guard error == nil else {
completionHandler(error)
return
}
let newProxyServer = GCDHTTPProxyServer(address: IPAddress(fromString: self.proxyServerAddress),
port: Port(port: self.proxyServerPort))
self.proxyServer = newProxyServer
do {
completionHandler(nil)
} catch let proxyError {
completionHandler(proxyError)
}
})
completionHandler(nil)
}
Log message when connection is being in process:
2023-10-16T16:00:28+0530 info com.apple.nio-connect-proxy.ConnectHandler : channel=ObjectIdentifier(0x0000000103e0c570) localAddress=Optional([IPv4]127.0.0.1/127.0.0.1:8080) remoteAddress=Optional([IPv4]127.0.0.1/127.0.0.1:52889) [VPN] CONNECT gateway.icloud.com:443 HTTP/1.1
2023-10-16T16:00:28+0530 info com.apple.nio-connect-proxy.ConnectHandler : channel=ObjectIdentifier(0x0000000103e0c570) localAddress=Optional([IPv4]127.0.0.1/127.0.0.1:8080) remoteAddress=Optional([IPv4]127.0.0.1/127.0.0.1:52889) [VPN] Connecting to gateway.icloud.com:443
2023-10-16T16:00:30+0530 info com.apple.nio-connect-proxy.ConnectHandler : channel=ObjectIdentifier(0x0000000101a0bea0) localAddress=Optional([IPv4]127.0.0.1/127.0.0.1:8080) remoteAddress=Optional([IPv4]127.0.0.1/127.0.0.1:52890) [VPN] CONNECT cl3.apple.com:443 HTTP/1.1
2023-10-16T16:00:30+0530 info com.apple.nio-connect-proxy.ConnectHandler : channel=ObjectIdentifier(0x0000000101a0bea0) localAddress=Optional([IPv4]127.0.0.1/127.0.0.1:8080) remoteAddress=Optional([IPv4]127.0.0.1/127.0.0.1:52890) [VPN] Connecting to cl3.apple.com:443
Log message after the connection failed:
2023-10-16T16:02:21+0530 error com.apple.nio-connect-proxy.ConnectHandler : channel=ObjectIdentifier(0x000000010610d1e0) localAddress=Optional([IPv4]127.0.0.1/127.0.0.1:8080) remoteAddress=Optional([IPv4]127.0.0.1/127.0.0.1:52898) [VPN] Connect failed: connectTimeout(NIOCore.TimeAmount(nanoseconds: 10000000000))
2023-10-16T16:02:28+0530 error com.apple.nio-connect-proxy.ConnectHandler : channel=ObjectIdentifier(0x000000010610a6f0) localAddress=Optional([IPv4]127.0.0.1/127.0.0.1:8080) remoteAddress=Optional([IPv4]127.0.0.1/127.0.0.1:52899) [VPN] Connect failed: connectTimeout(NIOCore.TimeAmount(nanoseconds: 10000000000))
Can anyone help me.
When you have
proxySettingsconfigured on yourNEPacketTunnelNetworkSettingssystem will recognise this a proxy configuration and browser (for example) first sends HTTP CONNECT message to your proxy and proxy has to reply 200 back to start receiving more packets.I saw some NIO code in your logs and here's swift NIO example code that shows how proxy should respond, function at line 201+ could be useful.