Skip to content

Commit 715fb45

Browse files
committed
Allow binding to a specific IP address via exposeToNetwork setting
1 parent f978837 commit 715fb45

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

LlamaBarn/Menu/HeaderView.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,17 @@ final class HeaderView: ItemView {
7878
// Connect to server info
7979
appNameLabel.stringValue = "LlamaBarn"
8080

81-
let host =
82-
UserSettings.exposeToNetwork ? (LlamaServer.getLocalIpAddress() ?? "0.0.0.0") : "localhost"
81+
// Determine host: use bind address if set, otherwise localhost
82+
// For 0.0.0.0, show the actual local IP for user convenience
83+
let host: String
84+
if let bindAddress = UserSettings.networkBindAddress {
85+
host =
86+
bindAddress == "0.0.0.0"
87+
? (LlamaServer.getLocalIpAddress() ?? "0.0.0.0")
88+
: bindAddress
89+
} else {
90+
host = "localhost"
91+
}
8392
let linkText = "\(host):\(LlamaServer.defaultPort)"
8493
let apiUrlString = "http://\(linkText)/v1"
8594
let webUiUrlString = "http://\(linkText)/"

LlamaBarn/Menu/MenuController.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,16 @@ final class MenuController: NSObject, NSMenuDelegate {
245245
guard !models.isEmpty else { return }
246246

247247
// Build the /models endpoint URL
248-
let host =
249-
UserSettings.exposeToNetwork ? (LlamaServer.getLocalIpAddress() ?? "0.0.0.0") : "localhost"
248+
// For 0.0.0.0, show the actual local IP for user convenience
249+
let host: String
250+
if let bindAddress = UserSettings.networkBindAddress {
251+
host =
252+
bindAddress == "0.0.0.0"
253+
? (LlamaServer.getLocalIpAddress() ?? "0.0.0.0")
254+
: bindAddress
255+
} else {
256+
host = "localhost"
257+
}
250258
let modelsUrl = URL(string: "http://\(host):\(LlamaServer.defaultPort)/models")
251259

252260
// Create family item (not collapsible) with link to /models endpoint

LlamaBarn/Settings/UserSettings.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,24 @@ enum UserSettings {
5353
}
5454
}
5555

56-
/// Whether to expose llama-server to the network (bind to 0.0.0.0).
57-
/// Defaults to `false` (localhost only). When `true`, allows connections from other devices
58-
/// on the same network.
59-
static var exposeToNetwork: Bool {
60-
get {
61-
defaults.bool(forKey: Keys.exposeToNetwork)
56+
/// The network bind address for llama-server, or `nil` for localhost only.
57+
/// Accepts either a bool (`true` binds to `0.0.0.0`) or a specific IP address string.
58+
/// Examples:
59+
/// `defaults write app.llamabarn.LlamaBarn exposeToNetwork -bool true` → binds to 0.0.0.0
60+
/// `defaults write app.llamabarn.LlamaBarn exposeToNetwork -string "192.168.1.100"` → binds to that IP
61+
/// `defaults delete app.llamabarn.LlamaBarn exposeToNetwork` → localhost only
62+
static var networkBindAddress: String? {
63+
let obj = defaults.object(forKey: Keys.exposeToNetwork)
64+
// If it's a string, use it directly as the bind address
65+
if let str = obj as? String {
66+
return str
6267
}
63-
set {
64-
guard defaults.bool(forKey: Keys.exposeToNetwork) != newValue else { return }
65-
defaults.set(newValue, forKey: Keys.exposeToNetwork)
66-
NotificationCenter.default.post(name: .LBUserSettingsDidChange, object: nil)
68+
// If it's a bool and true, bind to all interfaces
69+
if let bool = obj as? Bool, bool {
70+
return "0.0.0.0"
6771
}
72+
// Not set or false → localhost only
73+
return nil
6874
}
6975

7076
/// The default context length in thousands of tokens.

LlamaBarn/System/LlamaServer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ class LlamaServer {
151151
"--fit-target", String(Int(CatalogEntry.memOverheadMb)),
152152
]
153153

154-
// Bind to 0.0.0.0 if exposeToNetwork is enabled
155-
if UserSettings.exposeToNetwork {
156-
arguments.append(contentsOf: ["--host", "0.0.0.0"])
154+
// Bind to custom address if network exposure is enabled
155+
if let bindAddress = UserSettings.networkBindAddress {
156+
arguments.append(contentsOf: ["--host", bindAddress])
157157
}
158158

159159
// Unload model from memory when idle

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ See complete API reference in `llama-server` [docs](https://github.com/ggml-org/
6060
**Expose to network** — By default, the server is only accessible from your Mac (`localhost`). This option allows connections from other devices on your local network. Only enable this if you understand the security risks.
6161

6262
```sh
63+
# bind to all interfaces (0.0.0.0)
6364
defaults write app.llamabarn.LlamaBarn exposeToNetwork -bool YES
65+
66+
# or bind to a specific IP (e.g., for Tailscale)
67+
defaults write app.llamabarn.LlamaBarn exposeToNetwork -string "100.x.x.x"
68+
69+
# disable (default)
70+
defaults delete app.llamabarn.LlamaBarn exposeToNetwork
6471
```
6572

6673
## Roadmap

0 commit comments

Comments
 (0)