This repository was archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
[WIP] Apple: Adopt FFI changes and update the API exposed to Apple clients #20
Open
roop
wants to merge
12
commits into
firezone:master
Choose a base branch
from
roop:connlib_apple_updates
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
339b822
apple: Adapter: start() can take in portalURL and token
roop dbe3f2c
apple: Remove previous Connlib.xcframework before creating the new one
roop 761ad6f
Apple: Improve how the the CallbackHandler is initialized and stored
roop ba7d00c
apple: CallbackHandler: Adapter need not be an Optional
roop 1f59153
apple: Add Resource
roop 2eb8ca1
apple: Add ability to parse ResourceList into [Resource]
roop ef9deb9
apple: Add InterfaceAddresses
roop 2d953f3
apple: Add ability to convert TunnelAddresses into InterfaceAddresses
roop 2937c3d
apple: Add IPAddressRange from WireGuardKit
roop 7c42864
apple: Use IPAddressRange in Resource
roop 1b558e3
apple: Simplify CallbackHandler
roop 751aa9d
apple: Adapter: Apply network settings based on ffi callbacks
roop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,12 @@ import NetworkExtension | |
import os.log | ||
|
||
public protocol CallbackHandlerDelegate: AnyObject { | ||
func didUpdateResources(_ resourceList: ResourceList) | ||
func didUpdateResources(_ resources: [Resource]) | ||
func didUpdateInterfaceAddresses(_ interfaceAddresses: InterfaceAddresses) | ||
} | ||
|
||
public class CallbackHandler { | ||
// TODO: Add a table view property here to update? | ||
var adapter: Adapter? | ||
var adapter: Adapter | ||
public weak var delegate: CallbackHandlerDelegate? | ||
|
||
init(adapter: Adapter) { | ||
|
@@ -23,71 +23,47 @@ public class CallbackHandler { | |
|
||
func onUpdateResources(resourceList: ResourceList) -> Bool { | ||
|
||
// If there's any entity that assigned itself as this callbackHandler's delegate, it will be called everytime this `onUpdateResources` method is, allowing that entity to react to resource updates and do whatever they want. | ||
|
||
delegate?.didUpdateResources(resourceList) | ||
|
||
let addresses4 = | ||
self.adapter?.lastNetworkSettings?.ipv4Settings?.addresses ?? ["100.100.111.2"] | ||
let addresses6 = | ||
self.adapter?.lastNetworkSettings?.ipv6Settings?.addresses ?? [ | ||
"fd00:0222:2021:1111::2" | ||
] | ||
let logger = Logger(subsystem: "dev.firezone.firezone", category: "packet-tunnel") | ||
|
||
// TODO: Use actual passed in resources to achieve split tunnel | ||
let ipv4Routes = [NEIPv4Route(destinationAddress: "100.64.0.0", subnetMask: "255.192.0.0")] | ||
let ipv6Routes = [ | ||
NEIPv6Route(destinationAddress: "fd00:0222:2021:1111::0", networkPrefixLength: 64) | ||
] | ||
do { | ||
let resources = try resourceList.toResources() | ||
logger.debug("Resources updated: \(resources)") | ||
delegate?.didUpdateResources(resources) | ||
} catch { | ||
logger.error("Error parsing resource list: \(String(describing: error)) (JSON: \(resourceList.resources.toString()))") | ||
return false | ||
} | ||
|
||
return setTunnelSettingsKeepingSomeExisting( | ||
addresses4: addresses4, addresses6: addresses6, ipv4Routes: ipv4Routes, ipv6Routes: ipv6Routes | ||
) | ||
return true | ||
} | ||
|
||
func onSetTunnelAddresses(tunnelAddresses: TunnelAddresses) -> Bool { | ||
let addresses4 = [tunnelAddresses.address4.toString()] | ||
let addresses6 = [tunnelAddresses.address6.toString()] | ||
let ipv4Routes = | ||
Adapter.currentAdapter?.lastNetworkSettings?.ipv4Settings?.includedRoutes ?? [] | ||
let ipv6Routes = | ||
Adapter.currentAdapter?.lastNetworkSettings?.ipv6Settings?.includedRoutes ?? [] | ||
|
||
return setTunnelSettingsKeepingSomeExisting( | ||
addresses4: addresses4, addresses6: addresses6, ipv4Routes: ipv4Routes, ipv6Routes: ipv6Routes | ||
) | ||
} | ||
|
||
private func setTunnelSettingsKeepingSomeExisting( | ||
addresses4: [String], addresses6: [String], ipv4Routes: [NEIPv4Route], ipv6Routes: [NEIPv6Route] | ||
) -> Bool { | ||
let logger = Logger(subsystem: "dev.firezone.firezone", category: "packet-tunnel") | ||
|
||
if self.adapter != nil { | ||
do { | ||
/* If the tunnel interface addresses are being updated, it's impossible for the tunnel to | ||
stay up due to the way WireGuard works. Still, we try not to change the tunnel's routes | ||
here Just In Case™. | ||
*/ | ||
Comment on lines
-68
to
-71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment still relevant? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe not. I think we can update the routes without bringing down the tunnel, but I'm not fully sure because I haven't tried it in practice. Documentation and forums suggest we are allowed to call setTunnelNetworkSettings() while the tunnel is up. Have not checked it in practice. |
||
try self.adapter!.setNetworkSettings( | ||
self.adapter!.generateNetworkSettings( | ||
addresses4: addresses4, | ||
addresses6: addresses6, | ||
ipv4Routes: ipv4Routes, | ||
ipv6Routes: ipv6Routes | ||
) | ||
) | ||
let interfaceAddresses = tunnelAddresses.toInterfaceAddresses() | ||
logger.debug("Interface addresses updated: (\(interfaceAddresses.ipv4), \(interfaceAddresses.ipv6))") | ||
delegate?.didUpdateInterfaceAddresses(interfaceAddresses) | ||
|
||
return true | ||
} catch let error { | ||
logger.log(level: .debug, "Error setting adapter settings: \(String(describing: error))") | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} else { | ||
logger.log(level: .debug, "Adapter not initialized!") | ||
extension ResourceList { | ||
enum ParseError: Error { | ||
case notUTF8 | ||
} | ||
|
||
return false | ||
func toResources() throws -> [Resource] { | ||
let jsonString = resources.toString() | ||
guard let jsonData = jsonString.data(using: .utf8) else { | ||
throw ParseError.notUTF8 | ||
} | ||
return try JSONDecoder().decode([Resource].self, from: jsonData) | ||
} | ||
} | ||
|
||
extension TunnelAddresses { | ||
func toInterfaceAddresses() -> InterfaceAddresses { | ||
InterfaceAddresses(ipv4: address4.toString(), ipv6: address6.toString()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the deleted comment beginning with "iOS requires a tunnel endpoint" still relevant/helpful here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's still relevant (comes from WireguardKit as well). I'll retain it.