我们正在尝试与 VPN 建立连接以监控网络活动。 我可以获取网址,但我的互联网无法正常工作。当我开始建立连接时,它显示连接处于连接模式。但一段时间后连接就会断开。下面给出的代码用于建立连接:
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)
}
连接过程中记录消息:
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
连接失败后记录消息:
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))
谁能帮助我。
当您在
proxySettings
上配置了 NEPacketTunnelNetworkSettings
时,系统将识别此代理配置,浏览器(例如)首先向您的代理发送 HTTP CONNECT 消息,代理必须回复 200 才能开始接收更多数据包。
我在你的日志中看到了一些 NIO 代码,这是 swift NIO 示例代码 显示代理应如何响应,第 201+ 行的函数可能很有用。