mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-11 02:51:11 +00:00
37 lines
932 B
Swift
37 lines
932 B
Swift
//
|
|
// ActiveNetworkInterface.swift
|
|
// QuickDrop
|
|
//
|
|
// Created by Leon Böttger on 16.05.25.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
/// Returns the name of the currently active network interface (e.g. "en0", "en1")
|
|
func getActiveNetworkInterface() -> String? {
|
|
let process = Process()
|
|
let pipe = Pipe()
|
|
|
|
process.standardOutput = pipe
|
|
process.standardError = Pipe() // Ignore errors as in `2>/dev/null`
|
|
process.launchPath = "/bin/bash"
|
|
process.arguments = ["-c", "route get default 2>/dev/null | awk '/interface: / {print $2}'"]
|
|
|
|
do {
|
|
try process.run()
|
|
} catch {
|
|
log("Failed to run process: \(error)")
|
|
return nil
|
|
}
|
|
|
|
process.waitUntilExit()
|
|
|
|
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
|
if let output = String(data: data, encoding: .utf8)?
|
|
.trimmingCharacters(in: .whitespacesAndNewlines), !output.isEmpty {
|
|
return output
|
|
}
|
|
|
|
return nil
|
|
}
|