浏览代码

fix(config): skip GUI port probing for UNIX sockets (fixes #9855) (#9858)

When creating an initial default config, we usually probe for a free
TCP port.  But when a UNIX socket is specified via the `STGUIADDRESS=`
override or the `--gui-address=unix:///...` command line syntax, parsing
that option will fail during port probing.

The solution is to just skip the port probing when the address is
determined to specify something other than a TCP socket.

### Testing

Start with a fresh home directory each time.
1. Specify a UNIX socket for the GUI (works with this PR):

TMPHOME=$(mktemp -d); ./syncthing --home=$TMPHOME
--gui-address=unix://$TMPHOME/socket

2. Specify no GUI address (probes for a free port if default is taken,
   as before):

       TMPHOME=$(mktemp -d); ./syncthing --home=$TMPHOME

3. Specify a TCP GUI address (probes whether the given port is taken,
   as before):

TMPHOME=$(mktemp -d); ./syncthing --home=$TMPHOME
--gui-address=127.0.0.1:8385
André Colomb 10 月之前
父节点
当前提交
b9c6d3ae09
共有 1 个文件被更改,包括 15 次插入13 次删除
  1. 15 13
      lib/config/config.go

+ 15 - 13
lib/config/config.go

@@ -139,21 +139,23 @@ func New(myID protocol.DeviceID) Configuration {
 }
 }
 
 
 func (cfg *Configuration) ProbeFreePorts() error {
 func (cfg *Configuration) ProbeFreePorts() error {
-	guiHost, guiPort, err := net.SplitHostPort(cfg.GUI.Address())
-	if err != nil {
-		return fmt.Errorf("get default port (GUI): %w", err)
-	}
-	port, err := strconv.Atoi(guiPort)
-	if err != nil {
-		return fmt.Errorf("convert default port (GUI): %w", err)
-	}
-	port, err = getFreePort(guiHost, port)
-	if err != nil {
-		return fmt.Errorf("get free port (GUI): %w", err)
+	if cfg.GUI.Network() == "tcp" {
+		guiHost, guiPort, err := net.SplitHostPort(cfg.GUI.Address())
+		if err != nil {
+			return fmt.Errorf("get default port (GUI): %w", err)
+		}
+		port, err := strconv.Atoi(guiPort)
+		if err != nil {
+			return fmt.Errorf("convert default port (GUI): %w", err)
+		}
+		port, err = getFreePort(guiHost, port)
+		if err != nil {
+			return fmt.Errorf("get free port (GUI): %w", err)
+		}
+		cfg.GUI.RawAddress = net.JoinHostPort(guiHost, strconv.Itoa(port))
 	}
 	}
-	cfg.GUI.RawAddress = net.JoinHostPort(guiHost, strconv.Itoa(port))
 
 
-	port, err = getFreePort("0.0.0.0", DefaultTCPPort)
+	port, err := getFreePort("0.0.0.0", DefaultTCPPort)
 	if err != nil {
 	if err != nil {
 		return fmt.Errorf("get free port (BEP): %w", err)
 		return fmt.Errorf("get free port (BEP): %w", err)
 	}
 	}