add unix domain socket support for api & metrics services

This commit is contained in:
ginuerzh
2024-11-22 22:51:16 +08:00
parent fdc88af4ac
commit c45f148a88
4 changed files with 35 additions and 8 deletions
+17 -2
View File
@@ -1,6 +1,8 @@
package main package main
import ( import (
"strings"
"github.com/go-gost/core/auth" "github.com/go-gost/core/auth"
"github.com/go-gost/core/logger" "github.com/go-gost/core/logger"
"github.com/go-gost/core/service" "github.com/go-gost/core/service"
@@ -166,8 +168,14 @@ func buildAPIService(cfg *config.APIConfig) (service.Service, error) {
auther = xauth.AuthenticatorGroup(authers...) auther = xauth.AuthenticatorGroup(authers...)
} }
network := "tcp"
addr := cfg.Addr
if strings.HasPrefix(addr, "unix://") {
network = "unix"
addr = strings.TrimPrefix(addr, "unix://")
}
return api.NewService( return api.NewService(
cfg.Addr, network, addr,
api.PathPrefixOption(cfg.PathPrefix), api.PathPrefixOption(cfg.PathPrefix),
api.AccessLogOption(cfg.AccessLog), api.AccessLogOption(cfg.AccessLog),
api.AutherOption(auther), api.AutherOption(auther),
@@ -179,8 +187,15 @@ func buildMetricsService(cfg *config.MetricsConfig) (service.Service, error) {
if cfg.Auther != "" { if cfg.Auther != "" {
auther = registry.AutherRegistry().Get(cfg.Auther) auther = registry.AutherRegistry().Get(cfg.Auther)
} }
network := "tcp"
addr := cfg.Addr
if strings.HasPrefix(addr, "unix://") {
network = "unix"
addr = strings.TrimPrefix(addr, "unix://")
}
return metrics.NewService( return metrics.NewService(
cfg.Addr, network, addr,
metrics.PathOption(cfg.Path), metrics.PathOption(cfg.Path),
metrics.AutherOption(auther), metrics.AutherOption(auther),
) )
+15 -3
View File
@@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/go-gost/core/logger" "github.com/go-gost/core/logger"
"github.com/go-gost/core/service"
"github.com/go-gost/x/config" "github.com/go-gost/x/config"
"github.com/go-gost/x/config/cmd" "github.com/go-gost/x/config/cmd"
"github.com/go-gost/x/config/parsing" "github.com/go-gost/x/config/parsing"
@@ -18,7 +19,10 @@ import (
"github.com/judwhite/go-svc" "github.com/judwhite/go-svc"
) )
type program struct{} type program struct {
apiSrv service.Service
metricSrv service.Service
}
func (p *program) Init(env svc.Environment) error { func (p *program) Init(env svc.Environment) error {
cfg := &config.Config{} cfg := &config.Config{}
@@ -154,10 +158,11 @@ func (p *program) Start() error {
if err != nil { if err != nil {
return err return err
} }
p.apiSrv = s
go func() { go func() {
defer s.Close() defer s.Close()
log.Info("api service on ", s.Addr()) log.Info("api service on ", s.Addr())
log.Fatal(s.Serve()) log.Error(s.Serve())
}() }()
} }
if cfg.Profiling != nil { if cfg.Profiling != nil {
@@ -178,10 +183,11 @@ func (p *program) Start() error {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
p.metricSrv = s
go func() { go func() {
defer s.Close() defer s.Close()
log.Info("metrics service on ", s.Addr()) log.Info("metrics service on ", s.Addr())
log.Fatal(s.Serve()) log.Error(s.Serve())
}() }()
} }
} }
@@ -201,6 +207,12 @@ func (p *program) Stop() error {
srv.Close() srv.Close()
logger.Default().Debugf("service %s shutdown", name) logger.Default().Debugf("service %s shutdown", name)
} }
if p.apiSrv != nil {
p.apiSrv.Close()
}
if p.metricSrv != nil {
p.metricSrv.Close()
}
return nil return nil
} }
+1 -1
View File
@@ -6,7 +6,7 @@ toolchain go1.22.2
require ( require (
github.com/go-gost/core v0.1.11 github.com/go-gost/core v0.1.11
github.com/go-gost/x v0.2.18 github.com/go-gost/x v0.2.19
github.com/judwhite/go-svc v1.2.1 github.com/judwhite/go-svc v1.2.1
) )
+2 -2
View File
@@ -63,8 +63,8 @@ github.com/go-gost/relay v0.5.0 h1:JG1tgy/KWiVXS0ukuVXvbM0kbYuJTWxYpJ5JwzsCf/c=
github.com/go-gost/relay v0.5.0/go.mod h1:lcX+23LCQ3khIeASBo+tJ/WbwXFO32/N5YN6ucuYTG8= github.com/go-gost/relay v0.5.0/go.mod h1:lcX+23LCQ3khIeASBo+tJ/WbwXFO32/N5YN6ucuYTG8=
github.com/go-gost/tls-dissector v0.1.1 h1:2zUOTPzCQAUQ54Rpy0UEi3JPMQSYsIFSeFeKrzmkCoU= github.com/go-gost/tls-dissector v0.1.1 h1:2zUOTPzCQAUQ54Rpy0UEi3JPMQSYsIFSeFeKrzmkCoU=
github.com/go-gost/tls-dissector v0.1.1/go.mod h1:/9QfdewqmHdaE362Hv5nDaSWLx3pCmtD870d6GaquXs= github.com/go-gost/tls-dissector v0.1.1/go.mod h1:/9QfdewqmHdaE362Hv5nDaSWLx3pCmtD870d6GaquXs=
github.com/go-gost/x v0.2.18 h1:qNUUHoQMw3PaSS9ATLamkpYFw3FHWFh1vdzlxu9U6Ag= github.com/go-gost/x v0.2.19 h1:5gw+11L6H707huiuzYKj9JpdPHFo9G7HoBXE3kP44Zg=
github.com/go-gost/x v0.2.18/go.mod h1:tRK1pmbgYAeAEKm0/1kByL/xhxPZrCVZr5rsufUwFd8= github.com/go-gost/x v0.2.19/go.mod h1:tRK1pmbgYAeAEKm0/1kByL/xhxPZrCVZr5rsufUwFd8=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=