51 lines
1.7 KiB
Swift
51 lines
1.7 KiB
Swift
import NetworkExtension
|
|
import ProxyCore
|
|
|
|
class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
|
|
private var proxyServer: ProxyServer?
|
|
|
|
override func startTunnel(options: [String: NSObject]? = nil) async throws {
|
|
// Start the local proxy server
|
|
let server = ProxyServer()
|
|
try await server.start()
|
|
proxyServer = server
|
|
|
|
// Configure tunnel to redirect HTTP/HTTPS to our local proxy
|
|
let settings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "127.0.0.1")
|
|
|
|
let proxySettings = NEProxySettings()
|
|
proxySettings.httpServer = NEProxyServer(
|
|
address: ProxyConstants.proxyHost,
|
|
port: ProxyConstants.proxyPort
|
|
)
|
|
proxySettings.httpsServer = NEProxyServer(
|
|
address: ProxyConstants.proxyHost,
|
|
port: ProxyConstants.proxyPort
|
|
)
|
|
proxySettings.httpEnabled = true
|
|
proxySettings.httpsEnabled = true
|
|
proxySettings.matchDomains = [""] // Match all domains
|
|
settings.proxySettings = proxySettings
|
|
|
|
// DNS settings to ensure proper resolution
|
|
let dnsSettings = NEDNSSettings(servers: ["8.8.8.8", "8.8.4.4"])
|
|
dnsSettings.matchDomains = [""] // Match all
|
|
settings.dnsSettings = dnsSettings
|
|
|
|
try await setTunnelNetworkSettings(settings)
|
|
|
|
IPCManager.shared.post(.extensionStarted)
|
|
}
|
|
|
|
override func stopTunnel(with reason: NEProviderStopReason) async {
|
|
await proxyServer?.stop()
|
|
proxyServer = nil
|
|
IPCManager.shared.post(.extensionStopped)
|
|
}
|
|
|
|
override func handleAppMessage(_ messageData: Data) async -> Data? {
|
|
// Handle IPC messages from the main app
|
|
return nil
|
|
}
|
|
}
|