From 527a12d24e2a3f490457ec5dfe970a561e0f76e1 Mon Sep 17 00:00:00 2001 From: Allo Date: Mon, 25 Dec 2023 13:27:38 +0800 Subject: [PATCH] fix(conf): add Windows support for Unix Domain Socket --- infra/conf/v4/trojan.go | 29 ++++++++++++++--------------- infra/conf/v4/v2ray.go | 3 ++- infra/conf/v4/vless.go | 29 ++++++++++++++--------------- infra/conf/v5cfg/inbound.go | 3 ++- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/infra/conf/v4/trojan.go b/infra/conf/v4/trojan.go index b54cace47..f7e385cac 100644 --- a/infra/conf/v4/trojan.go +++ b/infra/conf/v4/trojan.go @@ -2,8 +2,10 @@ package v4 import ( "encoding/json" + "path/filepath" "runtime" "strconv" + "strings" "syscall" "github.com/golang/protobuf/proto" @@ -141,22 +143,19 @@ func (c *TrojanServerConfig) Build() (proto.Message, error) { if fb.Type == "" && fb.Dest != "" { if fb.Dest == "serve-ws-none" { fb.Type = "serve" + } else if filepath.IsAbs(fb.Dest) || fb.Dest[0] == '@' { + fb.Type = "unix" + if strings.HasPrefix(fb.Dest, "@@") && (runtime.GOOS == "linux" || runtime.GOOS == "android") { + fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy + copy(fullAddr, fb.Dest[1:]) + fb.Dest = string(fullAddr) + } } else { - switch fb.Dest[0] { - case '@', '/': - fb.Type = "unix" - if fb.Dest[0] == '@' && len(fb.Dest) > 1 && fb.Dest[1] == '@' && (runtime.GOOS == "linux" || runtime.GOOS == "android") { - fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy - copy(fullAddr, fb.Dest[1:]) - fb.Dest = string(fullAddr) - } - default: - if _, err := strconv.Atoi(fb.Dest); err == nil { - fb.Dest = "127.0.0.1:" + fb.Dest - } - if _, _, err := net.SplitHostPort(fb.Dest); err == nil { - fb.Type = "tcp" - } + if _, err := strconv.Atoi(fb.Dest); err == nil { + fb.Dest = "127.0.0.1:" + fb.Dest + } + if _, _, err := net.SplitHostPort(fb.Dest); err == nil { + fb.Type = "tcp" } } } diff --git a/infra/conf/v4/v2ray.go b/infra/conf/v4/v2ray.go index d68fc6cc4..399fdc22a 100644 --- a/infra/conf/v4/v2ray.go +++ b/infra/conf/v4/v2ray.go @@ -2,6 +2,7 @@ package v4 import ( "encoding/json" + "path/filepath" "strings" "google.golang.org/protobuf/types/known/anypb" @@ -121,7 +122,7 @@ func (c *InboundDetourConfig) Build() (*core.InboundHandlerConfig, error) { } else { // Listen on specific IP or Unix Domain Socket receiverSettings.Listen = c.ListenOn.Build() - listenDS := c.ListenOn.Family().IsDomain() && (c.ListenOn.Domain()[0] == '/' || c.ListenOn.Domain()[0] == '@') + listenDS := c.ListenOn.Family().IsDomain() && (filepath.IsAbs(c.ListenOn.Domain()) || c.ListenOn.Domain()[0] == '@') listenIP := c.ListenOn.Family().IsIP() || (c.ListenOn.Family().IsDomain() && c.ListenOn.Domain() == "localhost") switch { case listenIP: diff --git a/infra/conf/v4/vless.go b/infra/conf/v4/vless.go index bb2fcb853..0c76c43d0 100644 --- a/infra/conf/v4/vless.go +++ b/infra/conf/v4/vless.go @@ -2,8 +2,10 @@ package v4 import ( "encoding/json" + "path/filepath" "runtime" "strconv" + "strings" "syscall" "github.com/golang/protobuf/proto" @@ -90,22 +92,19 @@ func (c *VLessInboundConfig) Build() (proto.Message, error) { if fb.Type == "" && fb.Dest != "" { if fb.Dest == "serve-ws-none" { fb.Type = "serve" + } else if filepath.IsAbs(fb.Dest) || fb.Dest[0] == '@' { + fb.Type = "unix" + if strings.HasPrefix(fb.Dest, "@@") && (runtime.GOOS == "linux" || runtime.GOOS == "android") { + fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy + copy(fullAddr, fb.Dest[1:]) + fb.Dest = string(fullAddr) + } } else { - switch fb.Dest[0] { - case '@', '/': - fb.Type = "unix" - if fb.Dest[0] == '@' && len(fb.Dest) > 1 && fb.Dest[1] == '@' && (runtime.GOOS == "linux" || runtime.GOOS == "android") { - fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy - copy(fullAddr, fb.Dest[1:]) - fb.Dest = string(fullAddr) - } - default: - if _, err := strconv.Atoi(fb.Dest); err == nil { - fb.Dest = "127.0.0.1:" + fb.Dest - } - if _, _, err := net.SplitHostPort(fb.Dest); err == nil { - fb.Type = "tcp" - } + if _, err := strconv.Atoi(fb.Dest); err == nil { + fb.Dest = "127.0.0.1:" + fb.Dest + } + if _, _, err := net.SplitHostPort(fb.Dest); err == nil { + fb.Type = "tcp" } } } diff --git a/infra/conf/v5cfg/inbound.go b/infra/conf/v5cfg/inbound.go index 3825e6dea..0b8631128 100644 --- a/infra/conf/v5cfg/inbound.go +++ b/infra/conf/v5cfg/inbound.go @@ -2,6 +2,7 @@ package v5cfg import ( "context" + "path/filepath" "github.com/golang/protobuf/proto" @@ -24,7 +25,7 @@ func (c InboundConfig) BuildV5(ctx context.Context) (proto.Message, error) { } else { // Listen on specific IP or Unix Domain Socket receiverSettings.Listen = c.ListenOn.Build() - listenDS := c.ListenOn.Family().IsDomain() && (c.ListenOn.Domain()[0] == '/' || c.ListenOn.Domain()[0] == '@') + listenDS := c.ListenOn.Family().IsDomain() && (filepath.IsAbs(c.ListenOn.Domain()) || c.ListenOn.Domain()[0] == '@') listenIP := c.ListenOn.Family().IsIP() || (c.ListenOn.Family().IsDomain() && c.ListenOn.Domain() == "localhost") switch { case listenIP: