Skip to content

Commit

Permalink
Add MACVendors lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
halo committed Feb 13, 2024
1 parent e31173c commit e753872
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 75 deletions.
20 changes: 20 additions & 0 deletions LinkLiar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
C26699452AE55EFD00592008 /* LinkLiarUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C26699442AE55EFD00592008 /* LinkLiarUITests.swift */; };
C26699472AE55EFD00592008 /* LinkLiarUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C26699462AE55EFD00592008 /* LinkLiarUITestsLaunchTests.swift */; };
C266996B2AE57A3100592008 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C266996A2AE57A3100592008 /* main.swift */; };
C278B0B92B7BF750001DFA58 /* MACVendorsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C278B0B82B7BF750001DFA58 /* MACVendorsTest.swift */; };
C280C03E2B51225000C653F0 /* Airport.Scanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = C280C03B2B51225000C653F0 /* Airport.Scanner.swift */; };
C280C03F2B51225000C653F0 /* Airport.Scanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = C280C03B2B51225000C653F0 /* Airport.Scanner.swift */; };
C280C0442B5836E700C653F0 /* Command.swift in Sources */ = {isa = PBXBuildFile; fileRef = C280C0432B5836E700C653F0 /* Command.swift */; };
Expand Down Expand Up @@ -302,6 +303,7 @@
C26699682AE57A3100592008 /* io.github.halo.LinkLiar.linkdaemon */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = io.github.halo.LinkLiar.linkdaemon; sourceTree = BUILT_PRODUCTS_DIR; };
C266996A2AE57A3100592008 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
C266996F2AE57A3D00592008 /* linkdaemon.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = linkdaemon.entitlements; sourceTree = "<group>"; };
C278B0B82B7BF750001DFA58 /* MACVendorsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MACVendorsTest.swift; sourceTree = "<group>"; };
C280C03B2B51225000C653F0 /* Airport.Scanner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Airport.Scanner.swift; path = Classes/Backends/Airport.Scanner.swift; sourceTree = "<group>"; };
C280C0432B5836E700C653F0 /* Command.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Command.swift; sourceTree = "<group>"; };
C280C0462B584D6900C653F0 /* AirportConnectionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AirportConnectionTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -582,6 +584,7 @@
C26699392AE55EFD00592008 /* LinkLiarTests */ = {
isa = PBXGroup;
children = (
C278B0B62B7BF734001DFA58 /* LinkLiar */,
C23F0DC42B66929100EE94F5 /* LinkTools */,
C2AA40B12B5FC4B1007CD6CB /* linkdaemon */,
C266993A2AE55EFD00592008 /* LinkLiarTests.swift */,
Expand Down Expand Up @@ -613,6 +616,22 @@
path = linkdaemon;
sourceTree = "<group>";
};
C278B0B62B7BF734001DFA58 /* LinkLiar */ = {
isa = PBXGroup;
children = (
C278B0B72B7BF739001DFA58 /* Backends */,
);
name = LinkLiar;
sourceTree = "<group>";
};
C278B0B72B7BF739001DFA58 /* Backends */ = {
isa = PBXGroup;
children = (
C278B0B82B7BF750001DFA58 /* MACVendorsTest.swift */,
);
name = Backends;
sourceTree = "<group>";
};
C280C0482B58511800C653F0 /* Extensions */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1005,6 +1024,7 @@
C280C03F2B51225000C653F0 /* Airport.Scanner.swift in Sources */,
C2B45B832B49E87D004C1BB9 /* Advisor.swift in Sources */,
C20B96BD2B767D0000F1E2E8 /* CGKeyCode+optionKeyPressed.swift in Sources */,
C278B0B92B7BF750001DFA58 /* MACVendorsTest.swift in Sources */,
C24570342B486A0D007D7AEA /* JSONWriter.swift in Sources */,
C2F7017F2B74FC98001390E1 /* PopularOUIs.swift in Sources */,
C2F701732B7405CA001390E1 /* PopularVendorsDatabase.swift in Sources */,
Expand Down
48 changes: 20 additions & 28 deletions LinkLiar/Classes/Backends/MACVendors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,28 @@
import Foundation

struct MACVendors {

private static var dictionary: [String: String] = [:]

// static func load() {
// Log.debug("Loading MAC vendors asynchronously...")
// DispatchQueue.global(qos: .background).async(execute: { () -> Void in
// if dictionary = JSONReader(filePath: path).dictionary as [String: String] else {
// Log.debug("Could not parse MAC vendors.")
// return
// }
// Log.debug("MAC vendors loading completed. I got \(dictionary.count) prefixes.")
// })
// }
//
// static func name(address: MACAddress) -> String {
// Log.debug("Looking up vendor of MAC \(address.formatted)")
// guard let name = dictionary[address.prefix] else {
// return "No Vendor"
// }
// return name
// }
//
// private static var path = Bundle.main.url(forResource: "oui", withExtension: "json")!.path

/*
let bundle = Bundle(for: type(of: self))
let filename = name.components(separatedBy: ".")
static func load(_ callback: @escaping ([String: String]) -> Void) {
Log.debug("Loading MAC vendors asynchronously...")
DispatchQueue.global(qos: .background).async(execute: { () -> Void in
guard let parsed = JSONReader(path).dictionary as? [String: String] else {
Log.debug("Could not parse MAC vendors.")
return
}
self.dictionary = parsed
Log.debug("MAC vendors loading completed. I got \(parsed.count) prefixes.")
callback(parsed)
})
}

return bundle .url(forResource: filename.first, withExtension: filename.last)!.path
}()
*/
static func name(_ oui: OUI) -> String {
Log.debug("Looking up vendor of MAC \(oui.address)")
guard let name = dictionary[oui.address] else {
return "No Vendor"
}
return name
}

private static var path = Bundle.main.url(forResource: "oui", withExtension: "json")!.path
}
6 changes: 6 additions & 0 deletions LinkLiar/Views/Menu/InterfaceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ struct InterfaceView: View {
}).buttonStyle(.plain)
}

// Text(MACVendors.name(interface.softOUI))
.font(.system(.footnote, design: .monospaced))
.opacity(0.5)


if !interface.hasOriginalMAC {
HStack(spacing: 0) {
Text("Originally ")
Expand All @@ -49,6 +54,7 @@ struct InterfaceView: View {
}
}
}

// Padding parity on the right side (invisible).
Image("MenuIconLeaking").opacity(0)

Expand Down
2 changes: 1 addition & 1 deletion LinkLiar/oui.json

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions LinkLiarTests/MACVendorsTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) halo https://github.com/halo/LinkLiar
// SPDX-License-Identifier: MIT

import XCTest
@testable import LinkLiar

class MACVendorsTest: XCTestCase {
func testLoad() {
let expectation = XCTestExpectation(description: "asd")

MACVendors.load({ dictionary in
let name = MACVendors.name(OUI("b8:c2:53")!)
XCTAssertEqual("Juniper Networks", name)
expectation.fulfill()
})

let result = XCTWaiter.wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(result, .completed)
}
}
88 changes: 44 additions & 44 deletions LinkTools/Backends/PopularVendorsDatabase.swift

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion LinkTools/Models/OUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Foundation
/// Organizationally unique identifier.
/// Also known as MAC prefix.
///
struct OUI: Equatable {
struct OUI: Equatable, Hashable {
// MARK: Class Methods

init?(_ address: String) {
Expand Down
4 changes: 3 additions & 1 deletion lib/macs/vendors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def self.as_json
result = {}

rows do |row|
result[row.first] = row.last
oui = row.first.downcase # aabbcc
[2, 5].each { oui.insert _1, ':' } # aa:bb:cc
result[oui] = row.last
end

result
Expand Down

0 comments on commit e753872

Please sign in to comment.