mirror of
https://github.com/ollama/ollama.git
synced 2026-03-27 02:58:43 +07:00
app: fix desktop app stuck loading when OLLAMA_HOST is an unspecified bind address (#14885)
This commit is contained in:
@@ -155,7 +155,7 @@ func (s *Server) ollamaProxy() http.Handler {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
target := envconfig.Host()
|
target := envconfig.ConnectableHost()
|
||||||
s.log().Info("configuring ollama proxy", "target", target.String())
|
s.log().Info("configuring ollama proxy", "target", target.String())
|
||||||
|
|
||||||
newProxy := httputil.NewSingleHostReverseProxy(target)
|
newProxy := httputil.NewSingleHostReverseProxy(target)
|
||||||
|
|||||||
@@ -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.
|
// AllowedOrigins returns a list of allowed origins. AllowedOrigins can be configured via the OLLAMA_ORIGINS environment variable.
|
||||||
func AllowedOrigins() (origins []string) {
|
func AllowedOrigins() (origins []string) {
|
||||||
if s := Var("OLLAMA_ORIGINS"); s != "" {
|
if s := Var("OLLAMA_ORIGINS"); s != "" {
|
||||||
|
|||||||
@@ -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) {
|
func TestOrigins(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
value string
|
value string
|
||||||
|
|||||||
Reference in New Issue
Block a user