@@ -21,13 +21,9 @@ public protocol EphemeralPeerExchangeActorProtocol {
21
21
public class EphemeralPeerExchangeActor : EphemeralPeerExchangeActorProtocol {
22
22
struct Negotiation {
23
23
var negotiator : EphemeralPeerNegotiating
24
- var inTunnelTCPConnection : NWTCPConnection
25
- var tcpConnectionObserver : NSKeyValueObservation
26
24
27
25
func cancel( ) {
28
26
negotiator. cancelKeyNegotiation ( )
29
- tcpConnectionObserver. invalidate ( )
30
- inTunnelTCPConnection. cancel ( )
31
27
}
32
28
}
33
29
@@ -54,15 +50,6 @@ public class EphemeralPeerExchangeActor: EphemeralPeerExchangeActorProtocol {
54
50
self . keyExchangeRetriesIterator = iteratorProvider ( )
55
51
}
56
52
57
- private func createTCPConnection( _ gatewayEndpoint: NWHostEndpoint ) -> NWTCPConnection {
58
- self . packetTunnel. createTCPConnectionThroughTunnel (
59
- to: gatewayEndpoint,
60
- enableTLS: false ,
61
- tlsParameters: nil ,
62
- delegate: nil
63
- )
64
- }
65
-
66
53
/// Starts a new key exchange.
67
54
///
68
55
/// Any ongoing key negotiation is stopped before starting a new one.
@@ -75,49 +62,46 @@ public class EphemeralPeerExchangeActor: EphemeralPeerExchangeActorProtocol {
75
62
endCurrentNegotiation ( )
76
63
let negotiator = negotiationProvider. init ( )
77
64
78
- let gatewayAddress = LocalNetworkIPs . gatewayAddress. rawValue
79
- let IPv4Gateway = IPv4Address ( gatewayAddress) !
80
- let endpoint = NWHostEndpoint ( hostname: gatewayAddress, port: " \( CONFIG_SERVICE_PORT) " )
81
- let inTunnelTCPConnection = createTCPConnection ( endpoint)
82
-
83
65
// This will become the new private key of the device
84
66
let ephemeralSharedKey = PrivateKey ( )
85
67
86
68
let tcpConnectionTimeout = keyExchangeRetriesIterator. next ( ) ?? . seconds( 10 )
87
69
// If the connection never becomes viable, force a reconnection after 10 seconds
88
- scheduleInTunnelConnectionTimeout ( startTime: . now( ) + tcpConnectionTimeout)
89
-
90
- let tcpConnectionObserver = inTunnelTCPConnection. observe ( \. isViable, options: [
91
- . initial,
92
- . new,
93
- ] ) { [ weak self] observedConnection, _ in
94
- guard let self, observedConnection. isViable else { return }
95
- self . negotiation? . tcpConnectionObserver. invalidate ( )
96
- self . timer? . cancel ( )
97
-
98
- if !negotiator. startNegotiation (
99
- gatewayIP: IPv4Gateway,
100
- devicePublicKey: privateKey. publicKey,
101
- presharedKey: ephemeralSharedKey,
102
- peerReceiver: packetTunnel,
103
- tcpConnection: inTunnelTCPConnection,
104
- peerExchangeTimeout: tcpConnectionTimeout,
105
- enablePostQuantum: enablePostQuantum,
106
- enableDaita: enableDaita
107
- ) {
108
- // Cancel the negotiation to shut down any remaining use of the TCP connection on the Rust side
109
- self . negotiation? . cancel ( )
110
- self . negotiation = nil
111
- self . onFailure ( )
112
- }
70
+ let peerParameters = EphemeralPeerParameters (
71
+ peer_exchange_timeout: UInt64 ( tcpConnectionTimeout. timeInterval) ,
72
+ enable_post_quantum: enablePostQuantum,
73
+ enable_daita: enableDaita,
74
+ funcs: mapWgFunctions ( functions: packetTunnel. wgFunctions ( ) )
75
+ )
76
+
77
+ if !negotiator. startNegotiation (
78
+ devicePublicKey: privateKey. publicKey,
79
+ presharedKey: ephemeralSharedKey,
80
+ peerReceiver: packetTunnel,
81
+ ephemeralPeerParams: peerParameters
82
+ ) {
83
+ // Cancel the negotiation to shut down any remaining use of the TCP connection on the Rust side
84
+ self . negotiation? . cancel ( )
85
+ self . negotiation = nil
86
+ self . onFailure ( )
113
87
}
88
+
114
89
negotiation = Negotiation (
115
- negotiator: negotiator,
116
- inTunnelTCPConnection: inTunnelTCPConnection,
117
- tcpConnectionObserver: tcpConnectionObserver
90
+ negotiator: negotiator
118
91
)
119
92
}
120
93
94
+ private func mapWgFunctions( functions: WgFunctionPointers ) -> WgTcpConnectionFunctions {
95
+ var mappedFunctions = WgTcpConnectionFunctions ( )
96
+
97
+ mappedFunctions. close_fn = functions. close
98
+ mappedFunctions. open_fn = functions. open
99
+ mappedFunctions. send_fn = functions. send
100
+ mappedFunctions. recv_fn = functions. receive
101
+
102
+ return mappedFunctions
103
+ }
104
+
121
105
/// Cancels the ongoing key exchange.
122
106
public func endCurrentNegotiation( ) {
123
107
negotiation? . cancel ( )
@@ -129,19 +113,4 @@ public class EphemeralPeerExchangeActor: EphemeralPeerExchangeActorProtocol {
129
113
keyExchangeRetriesIterator = iteratorProvider ( )
130
114
endCurrentNegotiation ( )
131
115
}
132
-
133
- private func scheduleInTunnelConnectionTimeout( startTime: DispatchWallTime ) {
134
- let newTimer = DispatchSource . makeTimerSource ( )
135
-
136
- newTimer. setEventHandler { [ weak self] in
137
- self ? . onFailure ( )
138
- self ? . timer? . cancel ( )
139
- }
140
-
141
- newTimer. schedule ( wallDeadline: startTime)
142
- newTimer. activate ( )
143
-
144
- timer? . cancel ( )
145
- timer = newTimer
146
- }
147
116
}
0 commit comments