浏览代码

Fix relay migration panic by covering every possible relay state (#1414)

brad-defined 3 周之前
父节点
当前提交
94142aded5
共有 1 个文件被更改,包括 21 次插入15 次删除
  1. 21 15
      connection_manager.go

+ 21 - 15
connection_manager.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"bytes"
 	"context"
 	"context"
 	"encoding/binary"
 	"encoding/binary"
+	"fmt"
 	"net/netip"
 	"net/netip"
 	"sync"
 	"sync"
 	"time"
 	"time"
@@ -227,21 +228,25 @@ func (n *connectionManager) migrateRelayUsed(oldhostinfo, newhostinfo *HostInfo)
 		var relayFrom netip.Addr
 		var relayFrom netip.Addr
 		var relayTo netip.Addr
 		var relayTo netip.Addr
 		switch {
 		switch {
-		case ok && existing.State == Established:
-			// This relay already exists in newhostinfo, then do nothing.
-			continue
-		case ok && existing.State == Requested:
-			// The relay exists in a Requested state; re-send the request
-			index = existing.LocalIndex
-			switch r.Type {
-			case TerminalType:
-				relayFrom = n.intf.myVpnAddrs[0]
-				relayTo = existing.PeerAddr
-			case ForwardingType:
-				relayFrom = existing.PeerAddr
-				relayTo = newhostinfo.vpnAddrs[0]
-			default:
-				// should never happen
+		case ok:
+			switch existing.State {
+			case Established, PeerRequested, Disestablished:
+				// This relay already exists in newhostinfo, then do nothing.
+				continue
+			case Requested:
+				// The relay exists in a Requested state; re-send the request
+				index = existing.LocalIndex
+				switch r.Type {
+				case TerminalType:
+					relayFrom = n.intf.myVpnAddrs[0]
+					relayTo = existing.PeerAddr
+				case ForwardingType:
+					relayFrom = existing.PeerAddr
+					relayTo = newhostinfo.vpnAddrs[0]
+				default:
+					// should never happen
+					panic(fmt.Sprintf("Migrating unknown relay type: %v", r.Type))
+				}
 			}
 			}
 		case !ok:
 		case !ok:
 			n.relayUsedLock.RLock()
 			n.relayUsedLock.RLock()
@@ -267,6 +272,7 @@ func (n *connectionManager) migrateRelayUsed(oldhostinfo, newhostinfo *HostInfo)
 				relayTo = newhostinfo.vpnAddrs[0]
 				relayTo = newhostinfo.vpnAddrs[0]
 			default:
 			default:
 				// should never happen
 				// should never happen
+				panic(fmt.Sprintf("Migrating unknown relay type: %v", r.Type))
 			}
 			}
 		}
 		}