app: fix desktop app stuck loading when OLLAMA_HOST is an unspecified bind address (#14885)

This commit is contained in:
Eva H
2026-03-19 12:57:57 -04:00
committed by GitHub
parent 96e36c0d90
commit 3f3a24b418
3 changed files with 55 additions and 1 deletions

View File

@@ -155,7 +155,7 @@ func (s *Server) ollamaProxy() http.Handler {
return
}
target := envconfig.Host()
target := envconfig.ConnectableHost()
s.log().Info("configuring ollama proxy", "target", target.String())
newProxy := httputil.NewSingleHostReverseProxy(target)

View File

@@ -59,6 +59,29 @@ func Host() *url.URL {
}
}
// ConnectableHost returns Host() with unspecified bind addresses (0.0.0.0, ::)
// replaced by the corresponding loopback address (127.0.0.1, ::1).
// Unspecified addresses are valid for binding a server socket but not for
// connecting as a client, which fails on Windows.
func ConnectableHost() *url.URL {
u := Host()
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
return u
}
if ip := net.ParseIP(host); ip != nil && ip.IsUnspecified() {
if ip.To4() != nil {
host = "127.0.0.1"
} else {
host = "::1"
}
u.Host = net.JoinHostPort(host, port)
}
return u
}
// AllowedOrigins returns a list of allowed origins. AllowedOrigins can be configured via the OLLAMA_ORIGINS environment variable.
func AllowedOrigins() (origins []string) {
if s := Var("OLLAMA_ORIGINS"); s != "" {

View File

@@ -52,6 +52,37 @@ func TestHost(t *testing.T) {
}
}
func TestConnectableHost(t *testing.T) {
cases := map[string]struct {
value string
expect string
}{
"empty": {"", "http://127.0.0.1:11434"},
"localhost": {"127.0.0.1", "http://127.0.0.1:11434"},
"localhost and port": {"127.0.0.1:1234", "http://127.0.0.1:1234"},
"ipv4 unspecified": {"0.0.0.0", "http://127.0.0.1:11434"},
"ipv4 unspecified + port": {"0.0.0.0:1234", "http://127.0.0.1:1234"},
"ipv6 unspecified": {"[::]", "http://[::1]:11434"},
"ipv6 unspecified + port": {"[::]:1234", "http://[::1]:1234"},
"ipv6 localhost": {"[::1]", "http://[::1]:11434"},
"ipv6 localhost + port": {"[::1]:1234", "http://[::1]:1234"},
"specific address": {"192.168.1.5", "http://192.168.1.5:11434"},
"specific address + port": {"192.168.1.5:8080", "http://192.168.1.5:8080"},
"hostname": {"example.com", "http://example.com:11434"},
"hostname and port": {"example.com:1234", "http://example.com:1234"},
"https unspecified + port": {"https://0.0.0.0:4321", "https://127.0.0.1:4321"},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
t.Setenv("OLLAMA_HOST", tt.value)
if host := ConnectableHost(); host.String() != tt.expect {
t.Errorf("%s: expected %s, got %s", name, tt.expect, host.String())
}
})
}
}
func TestOrigins(t *testing.T) {
cases := []struct {
value string