From 35204217570546e5658be5cbcde5702df86ee06c Mon Sep 17 00:00:00 2001 From: CXM <16154023+littlecxm@users.noreply.github.com> Date: Sat, 28 Jun 2025 01:57:37 +0800 Subject: [PATCH] fix: determine bind network from bind address (#714) * fix: determine bind network from bind address * docs: update CHANGELOG Signed-off-by: Xe Iaso --------- Signed-off-by: Xe Iaso Co-authored-by: Xe Iaso --- cmd/anubis/main.go | 33 +++++++++++++++++++++++++++++++++ docs/docs/CHANGELOG.md | 1 + 2 files changed, 34 insertions(+) diff --git a/cmd/anubis/main.go b/cmd/anubis/main.go index 422b439..19f1ece 100644 --- a/cmd/anubis/main.go +++ b/cmd/anubis/main.go @@ -107,8 +107,41 @@ func doHealthCheck() error { return nil } +// parseBindNetFromAddr determine bind network and address based on the given network and address. +func parseBindNetFromAddr(address string) (string, string) { + defaultScheme := "http://" + if !strings.Contains(address, "://") { + if strings.HasPrefix(address, ":") { + address = defaultScheme + "localhost" + address + } else { + address = defaultScheme + address + } + } + + bindUri, err := url.Parse(address) + if err != nil { + log.Fatal(fmt.Errorf("failed to parse bind URL: %w", err)) + } + + switch bindUri.Scheme { + case "unix": + return "unix", bindUri.Path + case "tcp", "http", "https": + return "tcp", bindUri.Host + default: + log.Fatal(fmt.Errorf("unsupported network scheme %s in address %s", bindUri.Scheme, address)) + } + return "", address +} + func setupListener(network string, address string) (net.Listener, string) { formattedAddress := "" + + if network == "" { + // keep compatibility + network, address = parseBindNetFromAddr(address) + } + switch network { case "unix": formattedAddress = "unix:" + address diff --git a/docs/docs/CHANGELOG.md b/docs/docs/CHANGELOG.md index b008a5c..519d49d 100644 --- a/docs/docs/CHANGELOG.md +++ b/docs/docs/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Determine the `BIND_NETWORK`/`--bind-network` value from the bind address ([#677](https://github.com/TecharoHQ/anubis/issues/677)) - Implement localization system. Find locale files in lib/localization/locales/. ## v1.20.0: Thancred Waters