Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 197 additions & 38 deletions native/macos/Sources/LocalBolt/BoltBridge.swift

Large diffs are not rendered by default.

173 changes: 113 additions & 60 deletions native/macos/Sources/LocalBolt/LocalBoltApp.swift

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions native/macos/Sources/LocalBolt/LocalDeviceName.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Foundation
import SystemConfiguration

/// Computer name advertised to peers.
///
/// `Host.localizedName` is NSHost, which on some Macs returns the wildcard bind
/// address `0.0.0.0` instead of the name in System Settings. Peers then list the
/// machine as "0.0.0.0". Prefer the System Configuration computer name, and never
/// advertise a bind address.
enum LocalDeviceName {
static func current() -> String {
for candidate in candidates() {
let preferred = BoltBridge.preferDeviceName(current: "", candidate: candidate)
if !preferred.isEmpty && !BoltBridge.deviceNameIsUnusable(preferred) {
return preferred
}
}
return "Mac"
}

private static func candidates() -> [String] {
var names: [String] = []
if let computer = SCDynamicStoreCopyComputerName(nil, nil) as String? {
names.append(computer)
}
if let localized = Host.current().localizedName {
names.append(localized)
}
var host = ProcessInfo.processInfo.hostName
if host.hasSuffix(".local") {
host.removeLast(".local".count)
}
names.append(host)
return names
}
}
131 changes: 131 additions & 0 deletions native/macos/Sources/LocalBolt/TransportAdvertisement.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import CBoltBridge
import Foundation

struct AdvertisedEndpoint: Equatable {
let url: String
let certHash: String
}

/// Reads daemon `wt_info.json` / `quic_info.json`. Empty and missing files are nil
/// so they compare equal to a "no baseline" snapshot.
enum TransportMetadataFile {
static func read(dataDir: String, name: String) -> String? {
guard !dataDir.isEmpty else { return nil }
let url = URL(fileURLWithPath: dataDir).appendingPathComponent(name)
guard let data = try? Data(contentsOf: url),
let text = String(data: data, encoding: .utf8) else { return nil }
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}
}

extension BoltBridge {
/// Live WebTransport URL + cert hash, or nil when the on-disk record is stale.
static func selectWt(
currentJSON: String?,
baselineJSON: String?,
wsPort: UInt16,
host: String
) -> AdvertisedEndpoint? {
withOptionalCString(currentJSON) { current in
withOptionalCString(baselineJSON) { baseline in
host.withCString { hostPtr in
guard let ptr = bolt_select_wt_advertisement(current, baseline, wsPort, hostPtr) else {
return nil
}
defer { bolt_free_string(ptr) }
return parseAdvertisement(String(cString: ptr))
}
}
}
}

/// Live QUIC `host:port` + cert hash. Nil leaves the native path on WebSocket.
static func selectQuic(
currentJSON: String?,
baselineJSON: String?,
wsPort: UInt16,
host: String
) -> AdvertisedEndpoint? {
withOptionalCString(currentJSON) { current in
withOptionalCString(baselineJSON) { baseline in
host.withCString { hostPtr in
guard let ptr = bolt_select_quic_advertisement(current, baseline, wsPort, hostPtr) else {
return nil
}
defer { bolt_free_string(ptr) }
return parseAdvertisement(String(cString: ptr))
}
}
}
}

/// Name shown in the device list, ask sheet, and session header.
/// Socket labels and bind addresses become "Mac".
static func displayDeviceName(_ raw: String) -> String {
raw.withCString { ptr in
guard let out = bolt_device_display_name(ptr) else { return "Mac" }
defer { bolt_free_string(out) }
return String(cString: out)
}
}

/// Keep a real name. Replace a socket label or bind address when a real one exists.
static func preferDeviceName(current: String, candidate: String) -> String {
current.withCString { currentPtr in
candidate.withCString { candidatePtr in
guard let out = bolt_prefer_device_name(currentPtr, candidatePtr) else { return current }
defer { bolt_free_string(out) }
return String(cString: out)
}
}
}

static func deviceNameIsUnusable(_ name: String) -> Bool {
name.withCString { bolt_device_name_is_unusable($0) == 1 }
}

/// LAN addresses are usable URL hosts. `0.0.0.0` and `::` are not.
/// Distinct from `deviceNameIsUnusable`, which rejects every socket label.
static func hostIsUsable(_ host: String) -> Bool {
host.withCString { bolt_host_is_usable($0) == 1 }
}

static func deviceTypeIsPlaceholder(_ typeName: String) -> Bool {
typeName.withCString { bolt_device_type_is_placeholder($0) == 1 }
}

/// Keep a real type. Replace the daemon's `"unknown"` when signaling advertised one.
static func preferDeviceType(current: String, candidate: String) -> String {
current.withCString { currentPtr in
candidate.withCString { candidatePtr in
guard let out = bolt_prefer_device_type(currentPtr, candidatePtr) else { return current }
defer { bolt_free_string(out) }
return String(cString: out)
}
}
}

/// Ask-sheet type label. `"unknown"` becomes empty so the row can be omitted.
static func displayDeviceType(_ raw: String) -> String {
raw.withCString { ptr in
guard let out = bolt_device_display_type(ptr) else { return "" }
defer { bolt_free_string(out) }
return String(cString: out)
}
}

private static func parseAdvertisement(_ json: String) -> AdvertisedEndpoint? {
guard let data = json.data(using: .utf8),
let obj = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let url = obj["url"] as? String, !url.isEmpty,
let certHash = obj["certHash"] as? String, !certHash.isEmpty
else { return nil }
return AdvertisedEndpoint(url: url, certHash: certHash)
}
}

private func withOptionalCString<T>(_ value: String?, _ body: (UnsafePointer<CChar>?) -> T) -> T {
guard let value else { return body(nil) }
return value.withCString { body($0) }
}
49 changes: 49 additions & 0 deletions native/shared/include/bolt_native_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,55 @@ int bolt_probe_signal_health(void);
/// Free a string returned by any bolt_ function.
void bolt_free_string(char* ptr);

/// Live WebTransport endpoint from wt_info.json, or null when the file is missing,
/// unchanged from the pre-spawn baseline, or not this daemon's WS+1 port.
/// Returned JSON is `{"url":"https://host:port","certHash":"...","port":N}`.
/// `baseline_json` may be null when the file did not exist before spawn.
/// Caller frees a non-null return with bolt_free_string.
char* bolt_select_wt_advertisement(
const char* current_json,
const char* baseline_json,
uint16_t ws_port,
const char* host
);

/// Live QUIC endpoint from quic_info.json, or null for the same reasons as
/// bolt_select_wt_advertisement. `url` is `host:port` (WS+2, no scheme).
/// Caller frees a non-null return with bolt_free_string.
char* bolt_select_quic_advertisement(
const char* current_json,
const char* baseline_json,
uint16_t ws_port,
const char* host
);

/// 1 when `name` is blank, a bind address, or a socket label
/// (`0.0.0.0`, `::`, `0.0.0.0:0`, `192.168.4.25:55170`). Not for URL hosts.
int bolt_device_name_is_unusable(const char* name);

/// Name to show in the UI. Socket labels, bind addresses, and blanks become "Mac".
/// Caller frees with bolt_free_string.
char* bolt_device_display_name(const char* raw);

/// Keep a usable `current`. Replace an unusable `current` with a usable `candidate`.
/// Caller frees with bolt_free_string.
char* bolt_prefer_device_name(const char* current, const char* candidate);

/// 1 when `host` may appear in a wtUrl or quicAddr. LAN addresses are usable.
/// `0.0.0.0` and `::` are not. Distinct from bolt_device_name_is_unusable.
int bolt_host_is_usable(const char* host);

/// 1 when `type_name` is blank or the daemon placeholder "unknown".
int bolt_device_type_is_placeholder(const char* type_name);

/// Keep a real `current` type. Replace "unknown" with an advertised `candidate`.
/// Caller frees with bolt_free_string.
char* bolt_prefer_device_type(const char* current, const char* candidate);

/// Type to show. "unknown" and blanks become an empty string.
/// Caller frees with bolt_free_string.
char* bolt_device_display_type(const char* raw);

// ── Daemon Lifecycle ────────────────────────────────────────

/// Opaque daemon handle.
Expand Down
Loading
Loading