From 7f3d29eba221e66906547340e35994cf4d41c101 Mon Sep 17 00:00:00 2001 From: Kirill Nikiforov Date: Thu, 16 Apr 2026 00:51:02 +0400 Subject: [PATCH] fix ipv6 --- server/README.md | 2 +- server/README_JA.md | 2 +- server/README_ZH.md | 2 +- server/config/default.go | 2 +- server/main.go | 16 ++++++++++++---- server/middleware/loopback_http.go | 6 +++++- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/server/README.md b/server/README.md index f6524ec..5435b9c 100644 --- a/server/README.md +++ b/server/README.md @@ -28,7 +28,7 @@ The configuration file path is `/etc/kvm/server.yaml`. ```yaml # Network Settings proto: http # Access protocol. Can be changed to `https` only when certificates are configured. Default is `http` -host: 0.0.0.0 # The listening address for the HTTP/HTTPS service. +host: "" # The listening address for the HTTP/HTTPS service. If left empty, all network interfaces will be bound port: http: 80 # The listening port for the HTTP service. Default is `80` https: 443 # The listening port for the HTTPS service (effective when HTTPS is enabled). Default is `443` diff --git a/server/README_JA.md b/server/README_JA.md index aa9d194..92ad8c2 100644 --- a/server/README_JA.md +++ b/server/README_JA.md @@ -27,7 +27,7 @@ server ```yaml proto: http -host: 0.0.0.0 +host: "" port: http: 80 https: 443 diff --git a/server/README_ZH.md b/server/README_ZH.md index 893474b..94b82c3 100644 --- a/server/README_ZH.md +++ b/server/README_ZH.md @@ -26,7 +26,7 @@ server ```yaml # 网络设置 proto: http # 访问协议,默认为 `http`,仅当配置了证书时支持改为 `https` -host: 0.0.0.0 +host: "" port: http: 80 # HTTP 服务的监听端口,默认为 `80` https: 443 # HTTPS 服务的监听端口(启用 https 协议时生效),默认为 `443` diff --git a/server/config/default.go b/server/config/default.go index 7d74eeb..2208e9c 100644 --- a/server/config/default.go +++ b/server/config/default.go @@ -2,7 +2,7 @@ package config var defaultConfig = &Config{ Proto: "http", - Host: "0.0.0.0", + Host: "", Port: Port{ Http: 80, Https: 443, diff --git a/server/main.go b/server/main.go index e2beb69..e23154c 100644 --- a/server/main.go +++ b/server/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "net" "os" "os/signal" "strconv" @@ -68,13 +69,13 @@ func run() { router.Init(r) - httpAddr := fmt.Sprintf("%s:%d", conf.Host, conf.Port.Http) + httpAddr := listenAddr(conf.Host, strconv.Itoa(conf.Port.Http)) if conf.Proto == "https" { - httpsAddr := fmt.Sprintf("%s:%d", conf.Host, conf.Port.Https) + httpsPortStr := strconv.Itoa(conf.Port.Https) go func() { - err := r.RunTLS(httpsAddr, conf.Cert.Crt, conf.Cert.Key) + err := r.RunTLS(listenAddr(conf.Host, httpsPortStr), conf.Cert.Crt, conf.Cert.Key) if err != nil { panic("start https server failed") } @@ -82,7 +83,7 @@ func run() { if err := middleware.ListenAndServeLoopbackHTTPRedirect( httpAddr, - strconv.Itoa(conf.Port.Https), + httpsPortStr, r, router.PicoclawLoopbackHTTPAllowedPaths()..., ); err != nil { @@ -98,3 +99,10 @@ func run() { func dispose() { common.GetKvmVision().Close() } + +func listenAddr(host string, port string) string { + if host == "" { + return fmt.Sprintf(":%s", port) + } + return net.JoinHostPort(host, port) +} diff --git a/server/middleware/loopback_http.go b/server/middleware/loopback_http.go index fd47688..48dc55d 100644 --- a/server/middleware/loopback_http.go +++ b/server/middleware/loopback_http.go @@ -38,8 +38,12 @@ func ListenAndServeLoopbackHTTPRedirect( if h, _, err := net.SplitHostPort(host); err == nil { host = h } + + if strings.Contains(host, ":") { + host = "[" + host + "]" + } if httpsPort != "443" { - host = net.JoinHostPort(host, httpsPort) + host += ":" + httpsPort } http.Redirect(w, req, "https://"+host+req.URL.RequestURI(), http.StatusTemporaryRedirect)