diff --git a/controllers/network.go b/controllers/network.go index 2c90b785..0a78b64c 100644 --- a/controllers/network.go +++ b/controllers/network.go @@ -123,7 +123,7 @@ func updateNetwork(w http.ResponseWriter, r *http.Request) { newNetwork.DefaultPostUp = network.DefaultPostUp } - rangeupdate, localrangeupdate, err := logic.UpdateNetwork(&network, &newNetwork) + rangeupdate, localrangeupdate, holepunchupdate, err := logic.UpdateNetwork(&network, &newNetwork) if err != nil { returnErrorResponse(w, r, formatError(err, "badrequest")) return @@ -148,6 +148,24 @@ func updateNetwork(w http.ResponseWriter, r *http.Request) { return } } + if holepunchupdate { + err = logic.UpdateNetworkHolePunching(network.NetID, newNetwork.DefaultUDPHolePunch) + if err != nil { + returnErrorResponse(w, r, formatError(err, "internal")) + return + } + } + if rangeupdate || localrangeupdate || holepunchupdate { + nodes, err := logic.GetNetworkNodes(network.NetID) + if err != nil { + returnErrorResponse(w, r, formatError(err, "internal")) + return + } + for _, node := range nodes { + runUpdates(&node, true) + } + } + logger.Log(1, r.Header.Get("user"), "updated network", netname) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(newNetwork) diff --git a/logic/networks.go b/logic/networks.go index d548ac7d..2936299e 100644 --- a/logic/networks.go +++ b/logic/networks.go @@ -389,7 +389,7 @@ func UpdateNetworkLocalAddresses(networkName string) error { node.Address = ipaddr newNodeData, err := json.Marshal(&node) if err != nil { - fmt.Println("error in node address assignment!") + logger.Log(1, "error in node address assignment!") return err } database.Insert(node.ID, string(newNodeData), database.NODES_TABLE_NAME) @@ -399,6 +399,28 @@ func UpdateNetworkLocalAddresses(networkName string) error { return nil } +// UpdateNetworkLocalAddresses - updates network localaddresses +func UpdateNetworkHolePunching(networkName string, holepunch string) error { + + nodes, err := GetNetworkNodes(networkName) + if err != nil { + return err + } + + for _, node := range nodes { + if node.IsServer != "yes" { + node.UDPHolePunch = holepunch + newNodeData, err := json.Marshal(&node) + if err != nil { + logger.Log(1, "error in node hole punch assignment") + return err + } + database.Insert(node.ID, string(newNodeData), database.NODES_TABLE_NAME) + } + } + return nil +} + // RemoveNetworkNodeIPv6Addresses - removes network node IPv6 addresses func RemoveNetworkNodeIPv6Addresses(networkName string) error { @@ -509,23 +531,24 @@ func IsNetworkNameUnique(network *models.Network) (bool, error) { } // UpdateNetwork - updates a network with another network's fields -func UpdateNetwork(currentNetwork *models.Network, newNetwork *models.Network) (bool, bool, error) { +func UpdateNetwork(currentNetwork *models.Network, newNetwork *models.Network) (bool, bool, bool, error) { if err := ValidateNetwork(newNetwork, true); err != nil { - return false, false, err + return false, false, false, err } if newNetwork.NetID == currentNetwork.NetID { hasrangeupdate := newNetwork.AddressRange != currentNetwork.AddressRange localrangeupdate := newNetwork.LocalRange != currentNetwork.LocalRange + hasholepunchupdate := newNetwork.DefaultUDPHolePunch != currentNetwork.DefaultUDPHolePunch data, err := json.Marshal(newNetwork) if err != nil { - return false, false, err + return false, false, false, err } newNetwork.SetNetworkLastModified() err = database.Insert(newNetwork.NetID, string(data), database.NETWORKS_TABLE_NAME) - return hasrangeupdate, localrangeupdate, err + return hasrangeupdate, localrangeupdate, hasholepunchupdate, err } // copy values - return false, false, errors.New("failed to update network " + newNetwork.NetID + ", cannot change netid.") + return false, false, false, errors.New("failed to update network " + newNetwork.NetID + ", cannot change netid.") } // Inc - increments an IP