Skip to content

Commit 9737c77

Browse files
committed
Implement new interface api
1 parent fa107e3 commit 9737c77

File tree

1 file changed

+73
-9
lines changed

1 file changed

+73
-9
lines changed

Library/Network/ExtensionPlatformInterface.swift

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
195195
}
196196

197197
public func usePlatformAutoDetectControl() -> Bool {
198-
true
198+
false
199199
}
200200

201201
public func autoDetectControl(_: Int32) throws {}
@@ -223,20 +223,84 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
223223
tunnel.writeMessage(message)
224224
}
225225

226-
public func usePlatformDefaultInterfaceMonitor() -> Bool {
227-
false
228-
}
226+
private var nwMonitor: NWPathMonitor? = nil
229227

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+
}
231245

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+
}
233254

234-
public func useGetter() -> Bool {
235-
false
255+
public func closeDefaultInterfaceMonitor(_: LibboxInterfaceUpdateListenerProtocol?) throws {
256+
nwMonitor?.cancel()
257+
nwMonitor = nil
236258
}
237259

238260
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+
}
240304
}
241305

242306
public func underNetworkExtension() -> Bool {

0 commit comments

Comments
 (0)