diff --git a/main.go b/main.go index b35efc1..5e5620a 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,8 @@ var ( //go:embed build/sysroot/etc/casaos/message-bus.conf.sample _confSample string + + unixSocketPath = "/tmp/message-bus.sock" ) func main() { @@ -116,6 +118,14 @@ func main() { panic(err) } + // remove unix socket file. don't need check whether it exists or not + os.Remove(unixSocketPath) + // socket listener + socketListener, err := net.Listen("unix", unixSocketPath) + if err != nil { + panic(err) + } + // register at gateway u, err := url.Parse(swagger.Servers[0].URL) if err != nil { @@ -164,8 +174,36 @@ func main() { ReadHeaderTimeout: 5 * time.Second, } - err = server.Serve(listener) - logger.Info("MessageBus service is stopped", zap.Error(err)) + socketServer := &http.Server{ + Handler: mux, + ReadHeaderTimeout: 5 * time.Second, + } + + httpServerErrChan := make(chan error, 1) + socketServerErrChan := make(chan error, 1) + + go func() { + err := server.Serve(listener) + httpServerErrChan <- err + }() + + go func() { + err := socketServer.Serve(socketListener) + socketServerErrChan <- err + }() + + select { + case err := <-httpServerErrChan: + if err != nil { + logger.Info("MessageBus service is stopped", zap.Error(err)) + panic(err) + } + case err := <-socketServerErrChan: + if err != nil { + logger.Info("MessageBus socket service is stopped", zap.Error(err)) + panic(err) + } + } } func writeAddressFile(runtimePath string, filename string, address string) (string, error) { diff --git a/route/routers.go b/route/routers.go index fdae3cf..6765982 100644 --- a/route/routers.go +++ b/route/routers.go @@ -39,6 +39,11 @@ func NewAPIRouter(swagger *openapi3.T, services *service.Services) (http.Handler e.Use(echo_middleware.JWTWithConfig(echo_middleware.JWTConfig{ Skipper: func(c echo.Context) bool { + // skip when source is unix socket + if c.Request().Host == "unix" { + return true + } + if c.RealIP() == "::1" || c.RealIP() == "127.0.0.1" { return true }