mirror of
https://github.com/gravitl/netmaker.git
synced 2024-11-11 01:54:34 +08:00
Merge pull request #815 from gravitl/feature_v0.11.0_hublogic
fixed hub logic
This commit is contained in:
commit
c01b950ca9
6 changed files with 15 additions and 12 deletions
|
@ -96,7 +96,7 @@ func GetPeersList(refnode *models.Node) ([]models.Node, error) {
|
||||||
network, err := GetNetwork(networkName)
|
network, err := GetNetwork(networkName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return peers, err
|
return peers, err
|
||||||
} else if network.IsHubAndSpoke == "yes" && refnode.IsHub != "yes" {
|
} else if network.IsPointToSite == "yes" && refnode.IsHub != "yes" {
|
||||||
return GetHubPeer(networkName), nil
|
return GetHubPeer(networkName), nil
|
||||||
}
|
}
|
||||||
if relayedNodeAddr == "" {
|
if relayedNodeAddr == "" {
|
||||||
|
|
|
@ -41,7 +41,7 @@ func ServerJoin(networkSettings *models.Network) (models.Node, error) {
|
||||||
}
|
}
|
||||||
var ishub = "no"
|
var ishub = "no"
|
||||||
|
|
||||||
if networkSettings.IsHubAndSpoke == "yes" || networkSettings.IsComms == "yes" {
|
if networkSettings.IsPointToSite == "yes" || networkSettings.IsComms == "yes" {
|
||||||
nodes, err := GetNetworkNodes(networkSettings.NetID)
|
nodes, err := GetNetworkNodes(networkSettings.NetID)
|
||||||
if err != nil || nodes == nil {
|
if err != nil || nodes == nil {
|
||||||
ishub = "yes"
|
ishub = "yes"
|
||||||
|
|
|
@ -24,7 +24,7 @@ type Network struct {
|
||||||
IsDualStack string `json:"isdualstack" bson:"isdualstack" validate:"checkyesorno"`
|
IsDualStack string `json:"isdualstack" bson:"isdualstack" validate:"checkyesorno"`
|
||||||
IsIPv4 string `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
|
IsIPv4 string `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
|
||||||
IsIPv6 string `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
|
IsIPv6 string `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
|
||||||
IsHubAndSpoke string `json:"ishubandspoke" bson:"ishubandspoke" validate:"checkyesorno"`
|
IsPointToSite string `json:"ispointtosite" bson:"ispointtosite" validate:"checkyesorno"`
|
||||||
IsComms string `json:"iscomms" bson:"iscomms" validate:"checkyesorno"`
|
IsComms string `json:"iscomms" bson:"iscomms" validate:"checkyesorno"`
|
||||||
LocalRange string `json:"localrange" bson:"localrange" validate:"omitempty,cidr"`
|
LocalRange string `json:"localrange" bson:"localrange" validate:"omitempty,cidr"`
|
||||||
DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
|
DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
|
||||||
|
@ -57,8 +57,8 @@ func (network *Network) SetDefaults() {
|
||||||
if network.IsLocal == "" {
|
if network.IsLocal == "" {
|
||||||
network.IsLocal = "no"
|
network.IsLocal = "no"
|
||||||
}
|
}
|
||||||
if network.IsHubAndSpoke == "" {
|
if network.IsPointToSite == "" {
|
||||||
network.IsHubAndSpoke = "no"
|
network.IsPointToSite = "no"
|
||||||
}
|
}
|
||||||
if network.IsComms == "" {
|
if network.IsComms == "" {
|
||||||
network.IsComms = "no"
|
network.IsComms = "no"
|
||||||
|
|
|
@ -184,7 +184,7 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = wireguard.SetPeers(iface, cfg.Node.Address, cfg.Node.PersistentKeepalive, peerUpdate.Peers)
|
err = wireguard.SetPeers(iface, &cfg.Node, peerUpdate.Peers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ncutils.Log("error syncing wg after peer update: " + err.Error())
|
ncutils.Log("error syncing wg after peer update: " + err.Error())
|
||||||
return
|
return
|
||||||
|
|
|
@ -25,8 +25,10 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetPeers - sets peers on a given WireGuard interface
|
// SetPeers - sets peers on a given WireGuard interface
|
||||||
func SetPeers(iface, currentNodeAddr string, keepalive int32, peers []wgtypes.PeerConfig) error {
|
func SetPeers(iface string, node *models.Node, peers []wgtypes.PeerConfig) error {
|
||||||
var devicePeers []wgtypes.Peer
|
var devicePeers []wgtypes.Peer
|
||||||
|
var currentNodeAddr = node.Address
|
||||||
|
var keepalive = node.PersistentKeepalive
|
||||||
var oldPeerAllowedIps = make(map[string][]net.IPNet, len(peers))
|
var oldPeerAllowedIps = make(map[string][]net.IPNet, len(peers))
|
||||||
var err error
|
var err error
|
||||||
if ncutils.IsFreeBSD() {
|
if ncutils.IsFreeBSD() {
|
||||||
|
@ -73,13 +75,14 @@ func SetPeers(iface, currentNodeAddr string, keepalive int32, peers []wgtypes.Pe
|
||||||
if keepAliveString == "0" {
|
if keepAliveString == "0" {
|
||||||
keepAliveString = "15"
|
keepAliveString = "15"
|
||||||
}
|
}
|
||||||
if peer.Endpoint != nil {
|
if node.IsHub == "yes" || peer.Endpoint == nil {
|
||||||
_, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
|
_, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
|
||||||
" endpoint "+udpendpoint+
|
|
||||||
" persistent-keepalive "+keepAliveString+
|
" persistent-keepalive "+keepAliveString+
|
||||||
" allowed-ips "+allowedips, true)
|
" allowed-ips "+allowedips, true)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
_, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
|
_, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
|
||||||
|
" endpoint "+udpendpoint+
|
||||||
" persistent-keepalive "+keepAliveString+
|
" persistent-keepalive "+keepAliveString+
|
||||||
" allowed-ips "+allowedips, true)
|
" allowed-ips "+allowedips, true)
|
||||||
}
|
}
|
||||||
|
@ -207,7 +210,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||||
err = SyncWGQuickConf(ifacename, confPath)
|
err = SyncWGQuickConf(ifacename, confPath)
|
||||||
}
|
}
|
||||||
if !ncutils.HasWgQuick() && ncutils.IsLinux() {
|
if !ncutils.HasWgQuick() && ncutils.IsLinux() {
|
||||||
err = SetPeers(ifacename, node.Address, node.PersistentKeepalive, peers)
|
err = SetPeers(ifacename, node, peers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ncutils.PrintLog("error setting peers: "+err.Error(), 1)
|
ncutils.PrintLog("error setting peers: "+err.Error(), 1)
|
||||||
}
|
}
|
||||||
|
@ -251,7 +254,7 @@ func SetWGConfig(network string, peerupdate bool) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = SetPeers(iface, nodecfg.Address, nodecfg.PersistentKeepalive, peers)
|
err = SetPeers(iface, &nodecfg, peers)
|
||||||
} else if peerupdate {
|
} else if peerupdate {
|
||||||
err = InitWireguard(&nodecfg, privkey, peers, hasGateway, gateways, true)
|
err = InitWireguard(&nodecfg, privkey, peers, hasGateway, gateways, true)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -33,7 +33,7 @@ func InitializeCommsNetwork() error {
|
||||||
var network models.Network
|
var network models.Network
|
||||||
network.NetID = COMMS_NETID
|
network.NetID = COMMS_NETID
|
||||||
network.AddressRange = servercfg.GetCommsCIDR()
|
network.AddressRange = servercfg.GetCommsCIDR()
|
||||||
network.IsHubAndSpoke = "yes"
|
network.IsPointToSite = "yes"
|
||||||
network.DefaultUDPHolePunch = "yes"
|
network.DefaultUDPHolePunch = "yes"
|
||||||
network.IsComms = "yes"
|
network.IsComms = "yes"
|
||||||
return logic.CreateNetwork(network)
|
return logic.CreateNetwork(network)
|
||||||
|
|
Loading…
Reference in a new issue