@@ -191,7 +191,7 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
191
191
}
192
192
193
193
public func usePlatformAutoDetectControl( ) -> Bool {
194
- true
194
+ false
195
195
}
196
196
197
197
public func autoDetectControl( _: Int32 ) throws { }
@@ -219,20 +219,84 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
219
219
tunnel. writeMessage ( message)
220
220
}
221
221
222
- public func usePlatformDefaultInterfaceMonitor( ) -> Bool {
223
- false
224
- }
222
+ private var nwMonitor : NWPathMonitor ? = nil
225
223
226
- public func startDefaultInterfaceMonitor( _: LibboxInterfaceUpdateListenerProtocol ? ) throws { }
224
+ public func startDefaultInterfaceMonitor( _ listener: LibboxInterfaceUpdateListenerProtocol ? ) throws {
225
+ guard let listener else {
226
+ return
227
+ }
228
+ let monitor = NWPathMonitor ( )
229
+ nwMonitor = monitor
230
+ let semaphore = DispatchSemaphore ( value: 0 )
231
+ monitor. pathUpdateHandler = { path in
232
+ self . onUpdateDefaultInterface ( listener, path)
233
+ semaphore. signal ( )
234
+ monitor. pathUpdateHandler = { path in
235
+ self . onUpdateDefaultInterface ( listener, path)
236
+ }
237
+ }
238
+ monitor. start ( queue: DispatchQueue . global ( ) )
239
+ semaphore. wait ( )
240
+ }
227
241
228
- public func closeDefaultInterfaceMonitor( _: LibboxInterfaceUpdateListenerProtocol ? ) throws { }
242
+ private func onUpdateDefaultInterface( _ listener: LibboxInterfaceUpdateListenerProtocol , _ path: Network . NWPath ) {
243
+ if path. status == . unsatisfied {
244
+ listener. updateDefaultInterface ( " " , interfaceIndex: - 1 , isExpensive: false , isConstrained: false )
245
+ } else {
246
+ let defaultInterface = path. availableInterfaces. first!
247
+ listener. updateDefaultInterface ( defaultInterface. name, interfaceIndex: Int32 ( defaultInterface. index) , isExpensive: path. isExpensive, isConstrained: path. isConstrained)
248
+ }
249
+ }
229
250
230
- public func useGetter( ) -> Bool {
231
- false
251
+ public func closeDefaultInterfaceMonitor( _: LibboxInterfaceUpdateListenerProtocol ? ) throws {
252
+ nwMonitor? . cancel ( )
253
+ nwMonitor = nil
232
254
}
233
255
234
256
public func getInterfaces( ) throws -> LibboxNetworkInterfaceIteratorProtocol {
235
- throw NSError ( domain: " not implemented " , code: 0 )
257
+ guard let nwMonitor else {
258
+ throw NSError ( domain: " NWMonitor not started " , code: 0 )
259
+ }
260
+ let path = nwMonitor. currentPath
261
+ if path. status == . unsatisfied {
262
+ return networkInterfaceArray ( [ ] )
263
+ }
264
+ var interfaces : [ LibboxNetworkInterface ] = [ ]
265
+ for it in path. availableInterfaces {
266
+ let interface = LibboxNetworkInterface ( )
267
+ interface. name = it. name
268
+ interface. index = Int32 ( it. index)
269
+ switch it. type {
270
+ case . wifi:
271
+ interface. type = LibboxInterfaceTypeWIFI
272
+ case . cellular:
273
+ interface. type = LibboxInterfaceTypeCellular
274
+ case . wiredEthernet:
275
+ interface. type = LibboxInterfaceTypeEthernet
276
+ default :
277
+ interface. type = LibboxInterfaceTypeOther
278
+ }
279
+ interfaces. append ( interface)
280
+ }
281
+ return networkInterfaceArray ( interfaces)
282
+ }
283
+
284
+ class networkInterfaceArray : NSObject , LibboxNetworkInterfaceIteratorProtocol {
285
+ private var iterator : IndexingIterator < [ LibboxNetworkInterface ] >
286
+ init ( _ array: [ LibboxNetworkInterface ] ) {
287
+ iterator = array. makeIterator ( )
288
+ }
289
+
290
+ private var nextValue : LibboxNetworkInterface ? = nil
291
+
292
+ func hasNext( ) -> Bool {
293
+ nextValue = iterator. next ( )
294
+ return nextValue != nil
295
+ }
296
+
297
+ func next( ) -> LibboxNetworkInterface ? {
298
+ nextValue
299
+ }
236
300
}
237
301
238
302
public func underNetworkExtension( ) -> Bool {
0 commit comments