Make server unix socket file permission configurable via flag UnixSocketPerm (#2763)
* feat(any): Add flag unixsocketperm with default 0017 - #2625 Signed-off-by: johannesengl <hello@johannesengl.com> * feat(server): Update unix socket file perm based on config - #2625 Signed-off-by: johannesengl <hello@johannesengl.com> * Fix default value of socket. * Refactor unix socket file creation. * Remove misplaced comment --------- Signed-off-by: johannesengl <hello@johannesengl.com> Co-authored-by: Caio Cotts <caio@cotts.com.br> Co-authored-by: Deluan <deluan@navidrome.org>
This commit is contained in:
+25
-6
@@ -9,6 +9,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -71,13 +72,9 @@ func (s *Server) Run(ctx context.Context, addr string, port int, tlsCert string,
|
||||
var err error
|
||||
if strings.HasPrefix(addr, "unix:") {
|
||||
socketPath := strings.TrimPrefix(addr, "unix:")
|
||||
// Remove the socket file if it already exists
|
||||
if err := os.Remove(socketPath); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("error removing previous unix socket file: %w", err)
|
||||
}
|
||||
listener, err = net.Listen("unix", socketPath)
|
||||
listener, err = createUnixSocketFile(socketPath, conf.Server.UnixSocketPerm)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating unix socket listener: %w", err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
addr = fmt.Sprintf("%s:%d", addr, port)
|
||||
@@ -136,6 +133,28 @@ func (s *Server) Run(ctx context.Context, addr string, port int, tlsCert string,
|
||||
return nil
|
||||
}
|
||||
|
||||
func createUnixSocketFile(socketPath string, socketPerm string) (net.Listener, error) {
|
||||
// Remove the socket file if it already exists
|
||||
if err := os.Remove(socketPath); err != nil && !os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("error removing previous unix socket file: %w", err)
|
||||
}
|
||||
// Create listener
|
||||
listener, err := net.Listen("unix", socketPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating unix socket listener: %w", err)
|
||||
}
|
||||
// Converts the socketPerm to uint and updates the permission of the unix socket file
|
||||
perm, err := strconv.ParseUint(socketPerm, 8, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing unix socket file permissions: %w", err)
|
||||
}
|
||||
err = os.Chmod(socketPath, os.FileMode(perm))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error updating permission of unix socket file: %w", err)
|
||||
}
|
||||
return listener, nil
|
||||
}
|
||||
|
||||
func (s *Server) initRoutes() {
|
||||
s.appRoot = path.Join(conf.Server.BasePath, consts.URLPathUI)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user