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