Skip to content

Commit

Permalink
Implement new interface api
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 27, 2024
1 parent fa107e3 commit 9737c77
Showing 1 changed file with 73 additions and 9 deletions.
82 changes: 73 additions & 9 deletions Library/Network/ExtensionPlatformInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
}

public func usePlatformAutoDetectControl() -> Bool {
true
false
}

public func autoDetectControl(_: Int32) throws {}
Expand Down Expand Up @@ -223,20 +223,84 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
tunnel.writeMessage(message)
}

public func usePlatformDefaultInterfaceMonitor() -> Bool {
false
}
private var nwMonitor: NWPathMonitor? = nil

public func startDefaultInterfaceMonitor(_: LibboxInterfaceUpdateListenerProtocol?) throws {}
public func startDefaultInterfaceMonitor(_ listener: LibboxInterfaceUpdateListenerProtocol?) throws {
guard let listener else {
return
}
let monitor = NWPathMonitor()
nwMonitor = monitor
let semaphore = DispatchSemaphore(value: 0)
monitor.pathUpdateHandler = { path in
self.onUpdateDefaultInterface(listener, path)
semaphore.signal()
monitor.pathUpdateHandler = { path in
self.onUpdateDefaultInterface(listener, path)
}
}
monitor.start(queue: DispatchQueue.global())
semaphore.wait()
}

public func closeDefaultInterfaceMonitor(_: LibboxInterfaceUpdateListenerProtocol?) throws {}
private func onUpdateDefaultInterface(_ listener: LibboxInterfaceUpdateListenerProtocol, _ path: Network.NWPath) {
if path.status == .unsatisfied {
listener.updateDefaultInterface("", interfaceIndex: -1, isExpensive: false, isConstrained: false)
} else {
let defaultInterface = path.availableInterfaces.first!
listener.updateDefaultInterface(defaultInterface.name, interfaceIndex: Int32(defaultInterface.index), isExpensive: path.isExpensive, isConstrained: path.isConstrained)
}
}

public func useGetter() -> Bool {
false
public func closeDefaultInterfaceMonitor(_: LibboxInterfaceUpdateListenerProtocol?) throws {
nwMonitor?.cancel()
nwMonitor = nil
}

public func getInterfaces() throws -> LibboxNetworkInterfaceIteratorProtocol {
throw NSError(domain: "not implemented", code: 0)
guard let nwMonitor else {
throw NSError(domain: "NWMonitor not started", code: 0)
}
let path = nwMonitor.currentPath
if path.status == .unsatisfied {
return networkInterfaceArray([])
}
var interfaces: [LibboxNetworkInterface] = []
for it in path.availableInterfaces {
let interface = LibboxNetworkInterface()
interface.name = it.name
interface.index = Int32(it.index)
switch it.type {
case .wifi:
interface.type = LibboxInterfaceTypeWIFI
case .cellular:
interface.type = LibboxInterfaceTypeCellular
case .wiredEthernet:
interface.type = LibboxInterfaceTypeEthernet
default:
interface.type = LibboxInterfaceTypeOther
}
interfaces.append(interface)
}
return networkInterfaceArray(interfaces)
}

class networkInterfaceArray: NSObject, LibboxNetworkInterfaceIteratorProtocol {
private var iterator: IndexingIterator<[LibboxNetworkInterface]>
init(_ array: [LibboxNetworkInterface]) {
iterator = array.makeIterator()
}

private var nextValue: LibboxNetworkInterface? = nil

func hasNext() -> Bool {
nextValue = iterator.next()
return nextValue != nil
}

func next() -> LibboxNetworkInterface? {
nextValue
}
}

public func underNetworkExtension() -> Bool {
Expand Down

0 comments on commit 9737c77

Please sign in to comment.