Skip to content

Commit cd1d1a8

Browse files
authored
Sync code with mobile client (#896)
1 parent 98f48b0 commit cd1d1a8

4 files changed

Lines changed: 89 additions & 4 deletions

File tree

src-tauri/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swift/extension/VPNExtension/FileLogger.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ final class Log {
2424
/// The category for this logger instance (usually class name), e.g. "PacketTunnelProvider"
2525
let category: String
2626
private let systemLogger: Logger
27+
#if os(macOS)
2728
private let fileLogger = FileLogger.shared
29+
#endif
2830

2931
init(category: String) {
3032
self.category = category
@@ -36,29 +38,40 @@ final class Log {
3638

3739
func debug(_ message: String) {
3840
systemLogger.debug("\(message, privacy: .public)")
41+
#if os(macOS)
3942
fileLogger.log(level: .debug, message: message, category: category)
43+
#endif
4044
}
4145

4246
func info(_ message: String) {
4347
systemLogger.info("\(message, privacy: .public)")
48+
#if os(macOS)
4449
fileLogger.log(level: .info, message: message, category: category)
50+
#endif
4551
}
4652

4753
func warning(_ message: String) {
4854
systemLogger.warning("\(message, privacy: .public)")
55+
#if os(macOS)
4956
fileLogger.log(level: .warning, message: message, category: category)
57+
#endif
5058
}
5159

5260
func error(_ message: String) {
5361
systemLogger.error("\(message, privacy: .public)")
62+
#if os(macOS)
5463
fileLogger.log(level: .error, message: message, category: category)
64+
#endif
5565
}
5666

5767
func flush() {
68+
#if os(macOS)
5869
fileLogger.flush()
70+
#endif
5971
}
6072
}
6173

74+
#if os(macOS)
6275
/// A file-based logger that writes to an App Group shared container.
6376
/// This allows the main rust app to read logs from the network extension.
6477
/// Use the `Log` class instead of this directly for unified logging.
@@ -235,3 +248,4 @@ final class FileLogger {
235248
return logFileURL?.path
236249
}
237250
}
251+
#endif

swift/extension/VPNExtension/PacketTunnelProvider.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,29 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
2020
}
2121

2222
guard let protocolConfig = self.protocolConfiguration as? NETunnelProviderProtocol,
23-
let providerConfig = protocolConfig.providerConfiguration,
24-
let tunnelConfig = try? TunnelConfiguration.from(dictionary: providerConfig)
23+
let providerConfig = protocolConfig.providerConfiguration
24+
else {
25+
log.error("Failed to parse provider configuration")
26+
completionHandler(WireGuardTunnelError.invalidTunnelConfiguration)
27+
return
28+
}
29+
30+
#if os(macOS)
31+
guard let tunnelConfig = try? TunnelConfiguration.from(dictionary: providerConfig)
32+
else {
33+
log.error("Failed to parse tunnel configuration")
34+
completionHandler(WireGuardTunnelError.invalidTunnelConfiguration)
35+
return
36+
}
37+
#else
38+
guard let startData = try? TunnelStartData.from(dictionary: providerConfig)
2539
else {
2640
log.error("Failed to parse tunnel configuration")
2741
completionHandler(WireGuardTunnelError.invalidTunnelConfiguration)
2842
return
2943
}
44+
let tunnelConfig = TunnelConfiguration(fromStartData: startData)
45+
#endif
3046

3147
let networkSettings = tunnelConfig.asNetworkSettings()
3248
self.setTunnelNetworkSettings(networkSettings) { error in

swift/plugin/Sources/Defguard/TunnelConfiguration.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,61 @@ final class TunnelConfiguration: Codable {
124124
return (ipv4IncludedRoutes, ipv6IncludedRoutes)
125125
}
126126

127+
#if os(iOS)
128+
/// Helper function allowing to parse comma-separated string of addresses.
129+
private func parseAddresses(fromString string: String) -> [IpAddrMask] {
130+
var addresses: [IpAddrMask] = []
131+
132+
for addr in string.split(separator: ",").map({
133+
String($0.trimmingCharacters(in: .whitespaces))
134+
}) {
135+
if let addr_mask = IpAddrMask(fromString: addr) {
136+
addresses.append(addr_mask)
137+
}
138+
}
139+
140+
return addresses
141+
}
142+
143+
init(fromStartData startData: TunnelStartData) {
144+
name = startData.locationName
145+
privateKey = startData.privateKey
146+
let peer = Peer(publicKey: startData.publicKey)
147+
peers = [peer]
148+
149+
addresses = self.parseAddresses(fromString: startData.address)
150+
151+
// DNS settings
152+
let dnsRecords = startData.dns?.split(separator: ",").map {
153+
$0.trimmingCharacters(in: .whitespaces)
154+
} ?? []
155+
if !dnsRecords.isEmpty {
156+
for record in dnsRecords {
157+
if IPv4Address(record) != nil || IPv6Address(record) != nil {
158+
dns.append(record)
159+
} else {
160+
dnsSearch.append(record)
161+
}
162+
}
163+
}
164+
165+
// Peer settings
166+
peer.preSharedKey = startData.presharedKey
167+
peer.endpoint = Endpoint(from: startData.endpoint)
168+
peer.persistentKeepAlive = UInt16(startData.keepalive)
169+
peer.allowedIPs =
170+
switch startData.traffic {
171+
case .All:
172+
[
173+
IpAddrMask(address: IPv4Address.any, cidr: 0),
174+
IpAddrMask(address: IPv6Address.any, cidr: 0),
175+
]
176+
case .Predefined:
177+
self.parseAddresses(fromString: startData.allowedIps)
178+
}
179+
}
180+
#endif
181+
127182
/// Client connection expects one peer, so check for that.
128183
func isValidForClientConnection() -> Bool {
129184
return peers.count == 1

0 commit comments

Comments
 (0)