fix: multi issues - TUN read loop, SDWAN routing for TenantID=0, WS keepalive 10s
This commit is contained in:
@@ -11,6 +11,10 @@ func (s *Server) GetSDWAN() protocol.SDWANConfig {
|
||||
return s.sdwan.get()
|
||||
}
|
||||
|
||||
func (s *Server) GetSDWANTenant(tenantID int64) protocol.SDWANConfig {
|
||||
return s.sdwan.getTenant(tenantID)
|
||||
}
|
||||
|
||||
func (s *Server) SetSDWAN(cfg protocol.SDWANConfig) error {
|
||||
if err := s.sdwan.save(cfg); err != nil {
|
||||
return err
|
||||
@@ -19,6 +23,14 @@ func (s *Server) SetSDWAN(cfg protocol.SDWANConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) SetSDWANTenant(tenantID int64, cfg protocol.SDWANConfig) error {
|
||||
if err := s.sdwan.saveTenant(tenantID, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
s.broadcastSDWANTenant(tenantID, s.sdwan.getTenant(tenantID))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) broadcastSDWAN(cfg protocol.SDWANConfig) {
|
||||
if !cfg.Enabled || cfg.GatewayCIDR == "" {
|
||||
return
|
||||
@@ -33,6 +45,20 @@ func (s *Server) broadcastSDWAN(cfg protocol.SDWANConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) broadcastSDWANTenant(tenantID int64, cfg protocol.SDWANConfig) {
|
||||
if !cfg.Enabled || cfg.GatewayCIDR == "" {
|
||||
return
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
for _, n := range s.nodes {
|
||||
if !n.IsOnline() || n.TenantID != tenantID {
|
||||
continue
|
||||
}
|
||||
_ = n.Conn.Write(protocol.MsgPush, protocol.SubPushSDWANConfig, cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) pushSDWANPeer(to *NodeInfo, peer protocol.SDWANPeer) {
|
||||
if to == nil || !to.IsOnline() {
|
||||
return
|
||||
@@ -48,7 +74,14 @@ func (s *Server) pushSDWANDel(to *NodeInfo, peer protocol.SDWANPeer) {
|
||||
}
|
||||
|
||||
func (s *Server) announceSDWANNodeOnline(nodeName string) {
|
||||
cfg := s.sdwan.get()
|
||||
// pick tenant config by node
|
||||
s.mu.RLock()
|
||||
newNode := s.nodes[nodeName]
|
||||
s.mu.RUnlock()
|
||||
if newNode == nil {
|
||||
return
|
||||
}
|
||||
cfg := s.sdwan.getTenant(newNode.TenantID)
|
||||
if cfg.GatewayCIDR == "" {
|
||||
return
|
||||
}
|
||||
@@ -64,7 +97,7 @@ func (s *Server) announceSDWANNodeOnline(nodeName string) {
|
||||
}
|
||||
|
||||
s.mu.RLock()
|
||||
newNode := s.nodes[nodeName]
|
||||
newNode = s.nodes[nodeName]
|
||||
if newNode == nil || !newNode.IsOnline() {
|
||||
s.mu.RUnlock()
|
||||
return
|
||||
@@ -74,7 +107,7 @@ func (s *Server) announceSDWANNodeOnline(nodeName string) {
|
||||
continue
|
||||
}
|
||||
other := s.nodes[n.Node]
|
||||
if other == nil || !other.IsOnline() {
|
||||
if other == nil || !other.IsOnline() || other.TenantID != newNode.TenantID {
|
||||
continue
|
||||
}
|
||||
// existing -> new
|
||||
@@ -86,7 +119,13 @@ func (s *Server) announceSDWANNodeOnline(nodeName string) {
|
||||
}
|
||||
|
||||
func (s *Server) announceSDWANNodeOffline(nodeName string) {
|
||||
cfg := s.sdwan.get()
|
||||
s.mu.RLock()
|
||||
old := s.nodes[nodeName]
|
||||
s.mu.RUnlock()
|
||||
if old == nil {
|
||||
return
|
||||
}
|
||||
cfg := s.sdwan.getTenant(old.TenantID)
|
||||
if cfg.GatewayCIDR == "" {
|
||||
return
|
||||
}
|
||||
@@ -100,7 +139,7 @@ func (s *Server) announceSDWANNodeOffline(nodeName string) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
for _, n := range s.nodes {
|
||||
if n.Name == nodeName || !n.IsOnline() {
|
||||
if n.Name == nodeName || !n.IsOnline() || n.TenantID != old.TenantID {
|
||||
continue
|
||||
}
|
||||
s.pushSDWANDel(n, protocol.SDWANPeer{Node: nodeName, IP: selfIP, Online: false})
|
||||
@@ -112,7 +151,13 @@ func (s *Server) RouteSDWANPacket(from *NodeInfo, pkt protocol.SDWANPacket) {
|
||||
if from == nil {
|
||||
return
|
||||
}
|
||||
cfg := s.sdwan.get()
|
||||
// Use global config for untrusted nodes (TenantID=0), otherwise use tenant config
|
||||
var cfg protocol.SDWANConfig
|
||||
if from.TenantID == 0 {
|
||||
cfg = s.sdwan.get()
|
||||
} else {
|
||||
cfg = s.sdwan.getTenant(from.TenantID)
|
||||
}
|
||||
if cfg.GatewayCIDR == "" || pkt.DstIP == "" || len(pkt.Payload) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -124,12 +169,18 @@ func (s *Server) RouteSDWANPacket(from *NodeInfo, pkt protocol.SDWANPacket) {
|
||||
toNode := ""
|
||||
for _, n := range cfg.Nodes {
|
||||
if n.IP == pkt.DstIP {
|
||||
toNode = n.Node
|
||||
break
|
||||
candidate := s.GetNodeForUser(n.Node, from.Token)
|
||||
if candidate != nil && candidate.TenantID == from.TenantID {
|
||||
toNode = n.Node
|
||||
break
|
||||
}
|
||||
}
|
||||
if p, err := netip.ParseAddr(n.IP); err == nil && p == dst {
|
||||
toNode = n.Node
|
||||
break
|
||||
candidate := s.GetNodeForUser(n.Node, from.Token)
|
||||
if candidate != nil && candidate.TenantID == from.TenantID {
|
||||
toNode = n.Node
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if toNode == "" || toNode == from.Name {
|
||||
@@ -138,6 +189,9 @@ func (s *Server) RouteSDWANPacket(from *NodeInfo, pkt protocol.SDWANPacket) {
|
||||
|
||||
s.mu.RLock()
|
||||
to := s.nodes[toNode]
|
||||
if to != nil && to.TenantID != from.TenantID {
|
||||
to = nil
|
||||
}
|
||||
s.mu.RUnlock()
|
||||
if to == nil || !to.IsOnline() {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user