Skip to content

Commit

Permalink
Move to separate extension
Browse files Browse the repository at this point in the history
  • Loading branch information
keeshux committed Feb 3, 2025
1 parent ba66133 commit 2193361
Showing 1 changed file with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,8 @@ public final class WireGuardConnection: Connection {
fatalError("No WireGuard configuration defined?")
}

// tweak AllowedIPs from included routes
var tweakedBuilder = configuration.builder()
let ipModules = parameters.controller.profile.activeModules
.compactMap {
$0 as? IPModule
}

ipModules.forEach { ipModule in
tweakedBuilder.peers = configuration.peers
.map { oldPeer in
var peer = oldPeer.builder()
ipModule.ipv4?.includedRoutes.forEach { route in
peer.allowedIPs.append(route.destination?.rawValue ?? "0.0.0.0/0")
}
ipModule.ipv6?.includedRoutes.forEach { route in
peer.allowedIPs.append(route.destination?.rawValue ?? "::/0")
}
return peer
}
}
let tweakedConfiguration = try tweakedBuilder.tryBuild()
let tweakedConfiguration = try configuration.withModules(from: parameters.controller.profile)
tunnelConfiguration = try tweakedConfiguration.toWireGuardConfiguration()
// tunnelConfiguration = try configuration.toWireGuardConfiguration()

let interval = TimeInterval(parameters.options.minDataCountInterval) / 1000.0
dataCountTimer = Timer.publish(every: interval, on: .main, in: .common)
Expand Down Expand Up @@ -279,6 +258,31 @@ private extension String {

// MARK: - Helpers

private extension WireGuard.Configuration {
func withModules(from profile: Profile) throws -> Self {
var newBuilder = builder()
let ipModules = profile.activeModules
.compactMap {
$0 as? IPModule
}

ipModules.forEach { ipModule in
newBuilder.peers = peers
.map { oldPeer in
var peer = oldPeer.builder()
ipModule.ipv4?.includedRoutes.forEach { route in
peer.allowedIPs.append(route.destination?.rawValue ?? "0.0.0.0/0")
}
ipModule.ipv6?.includedRoutes.forEach { route in
peer.allowedIPs.append(route.destination?.rawValue ?? "::/0")
}
return peer
}
}
return try newBuilder.tryBuild()
}
}

private extension WireGuardLogLevel {
var osLogLevel: OSLogType {
switch self {
Expand Down

0 comments on commit 2193361

Please sign in to comment.