Skip to content

Commit 3edc3fc

Browse files
committed
Implement new interface api
1 parent a5d56af commit 3edc3fc

File tree

1 file changed

+73
-9
lines changed

1 file changed

+73
-9
lines changed

Library/Network/ExtensionPlatformInterface.swift

+73-9
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
191191
}
192192

193193
public func usePlatformAutoDetectControl() -> Bool {
194-
true
194+
false
195195
}
196196

197197
public func autoDetectControl(_: Int32) throws {}
@@ -219,20 +219,84 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
219219
tunnel.writeMessage(message)
220220
}
221221

222-
public func usePlatformDefaultInterfaceMonitor() -> Bool {
223-
false
224-
}
222+
private var nwMonitor: NWPathMonitor? = nil
225223

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

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

230-
public func useGetter() -> Bool {
231-
false
251+
public func closeDefaultInterfaceMonitor(_: LibboxInterfaceUpdateListenerProtocol?) throws {
252+
nwMonitor?.cancel()
253+
nwMonitor = nil
232254
}
233255

234256
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+
}
236300
}
237301

238302
public func underNetworkExtension() -> Bool {

0 commit comments

Comments
 (0)