iOS Cloak/OVPN SplitTunnel
This commit is contained in:
parent
22b14dff5f
commit
a386d39495
1 changed files with 132 additions and 102 deletions
|
|
@ -130,8 +130,8 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
switch self.protoType {
|
switch self.protoType {
|
||||||
case .wireguard:
|
case .wireguard:
|
||||||
self.startWireguard(activationAttemptId: activationAttemptId,
|
self.startWireguard(activationAttemptId: activationAttemptId,
|
||||||
errorNotifier: errorNotifier,
|
errorNotifier: errorNotifier,
|
||||||
completionHandler: completionHandler)
|
completionHandler: completionHandler)
|
||||||
case .openvpn:
|
case .openvpn:
|
||||||
self.startOpenVPN(completionHandler: completionHandler)
|
self.startOpenVPN(completionHandler: completionHandler)
|
||||||
case .shadowsocks:
|
case .shadowsocks:
|
||||||
|
|
@ -168,7 +168,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
handleOpenVPNStatusMessage(messageData, completionHandler: completionHandler)
|
handleOpenVPNStatusMessage(messageData, completionHandler: completionHandler)
|
||||||
case .shadowsocks:
|
case .shadowsocks:
|
||||||
break
|
break
|
||||||
// handleShadowSocksAppMessage(messageData, completionHandler: completionHandler)
|
// handleShadowSocksAppMessage(messageData, completionHandler: completionHandler)
|
||||||
case .none:
|
case .none:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
@ -180,12 +180,12 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
errorNotifier: ErrorNotifier,
|
errorNotifier: ErrorNotifier,
|
||||||
completionHandler: @escaping (Error?) -> Void) {
|
completionHandler: @escaping (Error?) -> Void) {
|
||||||
guard let protocolConfiguration = self.protocolConfiguration as? NETunnelProviderProtocol,
|
guard let protocolConfiguration = self.protocolConfiguration as? NETunnelProviderProtocol,
|
||||||
let providerConfiguration = protocolConfiguration.providerConfiguration,
|
let providerConfiguration = protocolConfiguration.providerConfiguration,
|
||||||
let wgConfig: Data = providerConfiguration[Constants.wireGuardConfigKey] as? Data else {
|
let wgConfig: Data = providerConfiguration[Constants.wireGuardConfigKey] as? Data else {
|
||||||
wg_log(.error, message: "Can't start WireGuard config missing")
|
wg_log(.error, message: "Can't start WireGuard config missing")
|
||||||
completionHandler(nil)
|
completionHandler(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let wgConfigStr = String(data: wgConfig, encoding: .utf8)!
|
let wgConfigStr = String(data: wgConfig, encoding: .utf8)!
|
||||||
|
|
@ -196,26 +196,20 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
wg_log(.error, message: tunnelConfiguration.peers.first!.allowedIPs.map { $0.stringRepresentation }.joined(separator: ", "))
|
|
||||||
|
|
||||||
if (tunnelConfiguration.peers.first!.allowedIPs.map { $0.stringRepresentation }.joined(separator: ", ") == "0.0.0.0/0, ::/0"){
|
if (tunnelConfiguration.peers.first!.allowedIPs.map { $0.stringRepresentation }.joined(separator: ", ") == "0.0.0.0/0, ::/0") {
|
||||||
if (SplitTunnelType == "1") {
|
if (SplitTunnelType == "1") {
|
||||||
wg_log(.error, message: SplitTunnelSites!)
|
|
||||||
for index in tunnelConfiguration.peers.indices {
|
for index in tunnelConfiguration.peers.indices {
|
||||||
tunnelConfiguration.peers[index].allowedIPs.removeAll()
|
tunnelConfiguration.peers[index].allowedIPs.removeAll()
|
||||||
var allowedIPs = [IPAddressRange]()
|
var allowedIPs = [IPAddressRange]()
|
||||||
|
let STSdata = Data(SplitTunnelSites!.utf8)
|
||||||
let data = Data(SplitTunnelSites!.utf8)
|
|
||||||
do {
|
do {
|
||||||
let array = try JSONSerialization.jsonObject(with: data) as! [String]
|
let STSArray = try JSONSerialization.jsonObject(with: STSdata) as! [String]
|
||||||
for allowedIPString in array {
|
for allowedIPString in STSArray {
|
||||||
wg_log(.error,message: allowedIPString)
|
if let allowedIP = IPAddressRange(from: allowedIPString) {
|
||||||
guard let allowedIP = IPAddressRange(from: allowedIPString) else {
|
allowedIPs.append(allowedIP)
|
||||||
wg_log(.error,message: "Parse SplitTunnelSites Error")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
allowedIPs.append(allowedIP)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} catch {
|
} catch {
|
||||||
wg_log(.error,message: "Parse JSONSerialization Error")
|
wg_log(.error,message: "Parse JSONSerialization Error")
|
||||||
|
|
@ -225,22 +219,16 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
} else {
|
} else {
|
||||||
if (SplitTunnelType == "2")
|
if (SplitTunnelType == "2")
|
||||||
{
|
{
|
||||||
wg_log(.error, message: SplitTunnelSites!)
|
|
||||||
for index in tunnelConfiguration.peers.indices {
|
for index in tunnelConfiguration.peers.indices {
|
||||||
var excludeIPs = [IPAddressRange]()
|
var excludeIPs = [IPAddressRange]()
|
||||||
|
let STSdata = Data(SplitTunnelSites!.utf8)
|
||||||
let data = Data(SplitTunnelSites!.utf8)
|
|
||||||
do {
|
do {
|
||||||
let array = try JSONSerialization.jsonObject(with: data) as! [String]
|
let STSarray = try JSONSerialization.jsonObject(with: STSdata) as! [String]
|
||||||
for excludeIPString in array {
|
for excludeIPString in STSarray {
|
||||||
wg_log(.error,message: excludeIPString)
|
if let excludeIP = IPAddressRange(from: excludeIPString) {
|
||||||
guard let excludeIP = IPAddressRange(from: excludeIPString) else {
|
excludeIPs.append(excludeIP)
|
||||||
wg_log(.error,message: "Parse SplitTunnelSites Error")
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
excludeIPs.append(excludeIP)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
} catch {
|
} catch {
|
||||||
wg_log(.error,message: "Parse JSONSerialization Error")
|
wg_log(.error,message: "Parse JSONSerialization Error")
|
||||||
}
|
}
|
||||||
|
|
@ -250,8 +238,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wg_log(.error, message: tunnelConfiguration.peers.first!.allowedIPs.map { $0.stringRepresentation }.joined(separator: ", "))
|
|
||||||
|
|
||||||
wg_log(.info, message: "Starting wireguard tunnel from the " + (activationAttemptId == nil ? "OS directly, rather than the app" : "app"))
|
wg_log(.info, message: "Starting wireguard tunnel from the " + (activationAttemptId == nil ? "OS directly, rather than the app" : "app"))
|
||||||
|
|
||||||
// Start the tunnel
|
// Start the tunnel
|
||||||
|
|
@ -295,10 +281,10 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
|
|
||||||
private func startOpenVPN(completionHandler: @escaping (Error?) -> Void) {
|
private func startOpenVPN(completionHandler: @escaping (Error?) -> Void) {
|
||||||
guard let protocolConfiguration = self.protocolConfiguration as? NETunnelProviderProtocol,
|
guard let protocolConfiguration = self.protocolConfiguration as? NETunnelProviderProtocol,
|
||||||
let providerConfiguration = protocolConfiguration.providerConfiguration,
|
let providerConfiguration = protocolConfiguration.providerConfiguration,
|
||||||
let ovpnConfiguration: Data = providerConfiguration[Constants.ovpnConfigKey] as? Data else {
|
let ovpnConfiguration: Data = providerConfiguration[Constants.ovpnConfigKey] as? Data else {
|
||||||
// TODO: handle errors properly
|
// TODO: handle errors properly
|
||||||
wg_log(.error, message: "Can't start startOpenVPN()")
|
wg_log(.error, message: "Can't start startOpenVPN()")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -347,8 +333,8 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
for component in components{
|
for component in components{
|
||||||
let pair = component.components(separatedBy: "=")
|
let pair = component.components(separatedBy: "=")
|
||||||
if pair.count == 2 {
|
if pair.count == 2 {
|
||||||
settingsDictionary[pair[0]] = pair[1]
|
settingsDictionary[pair[0]] = pair[1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let response: [String: Any] = [
|
let response: [String: Any] = [
|
||||||
|
|
@ -406,15 +392,15 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
|
|
||||||
private func handleOpenVPNStatusMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)? = nil) {
|
private func handleOpenVPNStatusMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)? = nil) {
|
||||||
guard let completionHandler = completionHandler else { return }
|
guard let completionHandler = completionHandler else { return }
|
||||||
let bytesin = ovpnAdapter.transportStatistics.bytesIn
|
let bytesin = ovpnAdapter.transportStatistics.bytesIn
|
||||||
let bytesout = ovpnAdapter.transportStatistics.bytesOut
|
let bytesout = ovpnAdapter.transportStatistics.bytesOut
|
||||||
|
|
||||||
let response: [String: Any] = [
|
let response: [String: Any] = [
|
||||||
"rx_bytes" : bytesin,
|
"rx_bytes" : bytesin,
|
||||||
"tx_bytes" : bytesout
|
"tx_bytes" : bytesout
|
||||||
]
|
]
|
||||||
|
|
||||||
completionHandler(try? JSONSerialization.data(withJSONObject: response, options: []))
|
completionHandler(try? JSONSerialization.data(withJSONObject: response, options: []))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -451,11 +437,11 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
startHandler = completionHandler
|
startHandler = completionHandler
|
||||||
ovpnAdapter.connect(using: packetFlow)
|
ovpnAdapter.connect(using: packetFlow)
|
||||||
|
|
||||||
// let ifaces = Interface.allInterfaces()
|
// let ifaces = Interface.allInterfaces()
|
||||||
// .filter { $0.family == .ipv4 }
|
// .filter { $0.family == .ipv4 }
|
||||||
// .map { iface in iface.name }
|
// .map { iface in iface.name }
|
||||||
|
|
||||||
// wg_log(.error, message: "Available TUN Interfaces: \(ifaces)")
|
// wg_log(.error, message: "Available TUN Interfaces: \(ifaces)")
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: -- Network observing methods
|
// MARK: -- Network observing methods
|
||||||
|
|
@ -470,9 +456,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
override func observeValue(forKeyPath keyPath: String?,
|
override func observeValue(forKeyPath keyPath: String?,
|
||||||
of object: Any?,
|
of object: Any?,
|
||||||
change: [NSKeyValueChangeKey : Any]?,
|
change: [NSKeyValueChangeKey : Any]?,
|
||||||
context: UnsafeMutableRawPointer?) {
|
context: UnsafeMutableRawPointer?) {
|
||||||
guard Constants.kDefaultPathKey != keyPath else { return }
|
guard Constants.kDefaultPathKey != keyPath else { return }
|
||||||
// Since iOS 11, we have observed that this KVO event fires repeatedly when connecting over Wifi,
|
// Since iOS 11, we have observed that this KVO event fires repeatedly when connecting over Wifi,
|
||||||
// even though the underlying network has not changed (i.e. `isEqualToPath` returns false),
|
// even though the underlying network has not changed (i.e. `isEqualToPath` returns false),
|
||||||
|
|
@ -481,8 +467,8 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
guard let lastPath: NWPath = change?[.oldKey] as? NWPath,
|
guard let lastPath: NWPath = change?[.oldKey] as? NWPath,
|
||||||
let defPath = defaultPath,
|
let defPath = defaultPath,
|
||||||
lastPath != defPath || lastPath.description != defPath.description else {
|
lastPath != defPath || lastPath.description != defPath.description else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
DispatchQueue.main.async { [weak self] in
|
DispatchQueue.main.async { [weak self] in
|
||||||
guard let `self` = self, self.defaultPath != nil else { return }
|
guard let `self` = self, self.defaultPath != nil else { return }
|
||||||
self.handle(networkChange: self.defaultPath!) { _ in }
|
self.handle(networkChange: self.defaultPath!) { _ in }
|
||||||
|
|
@ -547,6 +533,50 @@ extension PacketTunnelProvider: OpenVPNAdapterDelegate {
|
||||||
// send empty string to NEDNSSettings.matchDomains
|
// send empty string to NEDNSSettings.matchDomains
|
||||||
networkSettings?.dnsSettings?.matchDomains = [""]
|
networkSettings?.dnsSettings?.matchDomains = [""]
|
||||||
|
|
||||||
|
if (SplitTunnelType == "1") {
|
||||||
|
var ipv4IncludedRoutes = [NEIPv4Route]()
|
||||||
|
let STSdata = Data(SplitTunnelSites!.utf8)
|
||||||
|
do {
|
||||||
|
let STSarray = try JSONSerialization.jsonObject(with: STSdata) as! [String]
|
||||||
|
for allowedIPString in STSarray {
|
||||||
|
if let allowedIP = IPAddressRange(from: allowedIPString){
|
||||||
|
ipv4IncludedRoutes.append(NEIPv4Route(destinationAddress: "\(allowedIP.address)", subnetMask: "\(allowedIP.subnetMask())"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
wg_log(.error,message: "Parse JSONSerialization Error")
|
||||||
|
}
|
||||||
|
networkSettings?.ipv4Settings?.includedRoutes = ipv4IncludedRoutes
|
||||||
|
} else {
|
||||||
|
if (SplitTunnelType == "2")
|
||||||
|
{
|
||||||
|
var ipv4ExcludedRoutes = [NEIPv4Route]()
|
||||||
|
var ipv4IncludedRoutes = [NEIPv4Route]()
|
||||||
|
var ipv6IncludedRoutes = [NEIPv6Route]()
|
||||||
|
let STSdata = Data(SplitTunnelSites!.utf8)
|
||||||
|
do {
|
||||||
|
let STSarray = try JSONSerialization.jsonObject(with: STSdata) as! [String]
|
||||||
|
for excludeIPString in STSarray {
|
||||||
|
if let excludeIP = IPAddressRange(from: excludeIPString) {
|
||||||
|
ipv4ExcludedRoutes.append(NEIPv4Route(destinationAddress: "\(excludeIP.address)", subnetMask: "\(excludeIP.subnetMask())"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
wg_log(.error,message: "Parse JSONSerialization Error")
|
||||||
|
}
|
||||||
|
if let allIPv4 = IPAddressRange(from: "0.0.0.0/0"){
|
||||||
|
ipv4IncludedRoutes.append(NEIPv4Route(destinationAddress: "\(allIPv4.address)", subnetMask: "\(allIPv4.subnetMask())"))
|
||||||
|
}
|
||||||
|
if let allIPv6 = IPAddressRange(from: "::/0") {
|
||||||
|
ipv6IncludedRoutes.append(NEIPv6Route(destinationAddress: "\(allIPv6.address)", networkPrefixLength: NSNumber(value: allIPv6.networkPrefixLength)))
|
||||||
|
}
|
||||||
|
networkSettings?.ipv4Settings?.includedRoutes = ipv4IncludedRoutes
|
||||||
|
networkSettings?.ipv6Settings?.includedRoutes = ipv6IncludedRoutes
|
||||||
|
networkSettings?.ipv4Settings?.excludedRoutes = ipv4ExcludedRoutes
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Set the network settings for the current tunneling session.
|
// Set the network settings for the current tunneling session.
|
||||||
setTunnelNetworkSettings(networkSettings, completionHandler: completionHandler)
|
setTunnelNetworkSettings(networkSettings, completionHandler: completionHandler)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue