mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-17 14:24:30 +08:00
enable proxy when host is relay and on relayed hosts, add relay related feilds to api host model
This commit is contained in:
@@ -18,12 +18,12 @@ type hostNetworksUpdatePayload struct {
|
||||
}
|
||||
|
||||
func hostHandlers(r *mux.Router) {
|
||||
r.HandleFunc("/api/hosts", logic.SecurityCheck(true, http.HandlerFunc(getHosts))).Methods("GET")
|
||||
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(updateHost))).Methods("PUT")
|
||||
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(deleteHost))).Methods("DELETE")
|
||||
r.HandleFunc("/api/hosts/{hostid}/networks", logic.SecurityCheck(true, http.HandlerFunc(updateHostNetworks))).Methods("PUT")
|
||||
r.HandleFunc("/api/hosts/{hostid}/createrelay", authorize(false, true, "user", http.HandlerFunc(createHostRelay))).Methods(http.MethodPost)
|
||||
r.HandleFunc("/api/hosts/{hostid}/deleterelay", authorize(false, true, "user", http.HandlerFunc(deleteHostRelay))).Methods(http.MethodDelete)
|
||||
r.HandleFunc("/api/hosts", logic.SecurityCheck(true, http.HandlerFunc(getHosts))).Methods(http.MethodGet)
|
||||
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(updateHost))).Methods(http.MethodPut)
|
||||
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(deleteHost))).Methods(http.MethodDelete)
|
||||
r.HandleFunc("/api/hosts/{hostid}/networks", logic.SecurityCheck(true, http.HandlerFunc(updateHostNetworks))).Methods(http.MethodPut)
|
||||
r.HandleFunc("/api/hosts/{hostid}/relay", logic.SecurityCheck(false, http.HandlerFunc(createHostRelay))).Methods(http.MethodPost)
|
||||
r.HandleFunc("/api/hosts/{hostid}/relay", logic.SecurityCheck(false, http.HandlerFunc(deleteHostRelay))).Methods(http.MethodDelete)
|
||||
}
|
||||
|
||||
// swagger:route GET /api/hosts hosts getHosts
|
||||
@@ -192,7 +192,7 @@ func updateHostNetworks(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(payload)
|
||||
}
|
||||
|
||||
// swagger:route POST /api/hosts/{hostid}/createrelay hosts createHostRelay
|
||||
// swagger:route POST /api/hosts/{hostid}/relay hosts createHostRelay
|
||||
//
|
||||
// Create a relay.
|
||||
//
|
||||
@@ -235,12 +235,12 @@ func createHostRelay(w http.ResponseWriter, r *http.Request) {
|
||||
if err != nil {
|
||||
logger.Log(1, "error sending update to relay host ", relayHost.ID.String(), ": ", err.Error())
|
||||
}
|
||||
|
||||
apiHostData := relayHost.ConvertNMHostToAPI()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(relayHost)
|
||||
json.NewEncoder(w).Encode(apiHostData)
|
||||
}
|
||||
|
||||
// swagger:route DELETE /api/hosts/{hostid}/createrelay hosts deleteHostRelay
|
||||
// swagger:route DELETE /api/hosts/{hostid}/relay hosts deleteHostRelay
|
||||
//
|
||||
// Remove a relay.
|
||||
//
|
||||
@@ -272,7 +272,7 @@ func deleteHostRelay(w http.ResponseWriter, r *http.Request) {
|
||||
if err != nil {
|
||||
logger.Log(1, "error sending update to relayed host ", relayHost.ID.String(), ": ", err.Error())
|
||||
}
|
||||
|
||||
apiHostData := relayHost.ConvertNMHostToAPI()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(relayHost)
|
||||
json.NewEncoder(w).Encode(apiHostData)
|
||||
}
|
||||
|
@@ -25,7 +25,7 @@ func nodeHandlers(r *mux.Router) {
|
||||
r.HandleFunc("/api/nodes/{network}", authorize(false, true, "network", http.HandlerFunc(getNetworkNodes))).Methods(http.MethodGet)
|
||||
r.HandleFunc("/api/nodes/{network}/{nodeid}", authorize(true, true, "node", http.HandlerFunc(getNode))).Methods(http.MethodGet)
|
||||
r.HandleFunc("/api/nodes/{network}/{nodeid}", authorize(false, true, "node", http.HandlerFunc(updateNode))).Methods(http.MethodPut)
|
||||
r.HandleFunc("/api/nodes/{network}/{nodeid}/migrate", authorize(true, true, "node", http.HandlerFunc(nodeNodeUpdate))).Methods("PUT")
|
||||
r.HandleFunc("/api/nodes/{network}/{nodeid}/migrate", authorize(true, true, "node", http.HandlerFunc(nodeNodeUpdate))).Methods(http.MethodPut)
|
||||
r.HandleFunc("/api/nodes/{network}/{nodeid}", authorize(true, true, "node", http.HandlerFunc(deleteNode))).Methods(http.MethodDelete)
|
||||
r.HandleFunc("/api/nodes/{network}/{nodeid}/createrelay", authorize(false, true, "user", http.HandlerFunc(createRelay))).Methods(http.MethodPost)
|
||||
r.HandleFunc("/api/nodes/{network}/{nodeid}/deleterelay", authorize(false, true, "user", http.HandlerFunc(deleteRelay))).Methods(http.MethodDelete)
|
||||
|
@@ -309,7 +309,11 @@ func GetProxyUpdateForHost(host *models.Host) (proxy_models.ProxyManagerPayload,
|
||||
|
||||
// GetPeerUpdateForHost - gets the consolidated peer update for the host from all networks
|
||||
func GetPeerUpdateForHost(host *models.Host) (models.HostPeerUpdate, error) {
|
||||
if host == nil {
|
||||
return models.HostPeerUpdate{}, errors.New("host is nil")
|
||||
}
|
||||
hostPeerUpdate := models.HostPeerUpdate{
|
||||
Host: *host,
|
||||
Network: make(map[string]models.NetworkInfo),
|
||||
PeerIDs: make(models.HostPeerMap),
|
||||
ServerVersion: servercfg.GetVersion(),
|
||||
|
@@ -59,6 +59,7 @@ func CreateHostRelay(relay models.HostRelayRequest) (relayHost *models.Host, rel
|
||||
return
|
||||
}
|
||||
relayHost.IsRelay = true
|
||||
relayHost.ProxyEnabled = true
|
||||
relayHost.RelayedHosts = relay.RelayedHosts
|
||||
err = UpsertHost(relayHost)
|
||||
if err != nil {
|
||||
@@ -76,6 +77,7 @@ func SetRelayedHosts(setRelayed bool, relayHostID string, relayedHostIDs []strin
|
||||
if setRelayed {
|
||||
host.IsRelayed = true
|
||||
host.RelayedBy = relayHostID
|
||||
host.ProxyEnabled = true
|
||||
} else {
|
||||
host.IsRelayed = false
|
||||
host.RelayedBy = ""
|
||||
@@ -154,8 +156,10 @@ func ValidateRelay(relay models.RelayRequest) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO
|
||||
func ValidateHostRelay(relay models.HostRelayRequest) error {
|
||||
if len(relay.RelayedHosts) == 0 {
|
||||
return errors.New("relayed hosts are empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -25,6 +25,10 @@ type ApiHost struct {
|
||||
Nodes []string `json:"nodes"`
|
||||
ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
|
||||
IsDefault bool `json:"isdefault" yaml:"isdefault"`
|
||||
IsRelayed bool `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
|
||||
RelayedBy string `json:"relayed_by" bson:"relayed_by" yaml:"relayed_by"`
|
||||
IsRelay bool `json:"isrelay" bson:"isrelay" yaml:"isrelay"`
|
||||
RelayedHosts []string `json:"relay_hosts" bson:"relay_hosts" yaml:"relay_hosts"`
|
||||
}
|
||||
|
||||
// Host.ConvertNMHostToAPI - converts a Netmaker host to an API editable host
|
||||
@@ -57,7 +61,10 @@ func (h *Host) ConvertNMHostToAPI() *ApiHost {
|
||||
a.Verbosity = h.Verbosity
|
||||
a.Version = h.Version
|
||||
a.IsDefault = h.IsDefault
|
||||
|
||||
a.IsRelay = h.IsRelay
|
||||
a.RelayedHosts = h.RelayedHosts
|
||||
a.IsRelayed = h.IsRelayed
|
||||
a.RelayedBy = h.RelayedBy
|
||||
return &a
|
||||
}
|
||||
|
||||
|
@@ -18,6 +18,7 @@ type PeerUpdate struct {
|
||||
|
||||
// HostPeerUpdate - struct for host peer updates
|
||||
type HostPeerUpdate struct {
|
||||
Host Host `json:"host" bson:"host" yaml:"host"`
|
||||
ServerVersion string `json:"serverversion" bson:"serverversion" yaml:"serverversion"`
|
||||
ServerAddrs []ServerAddr `json:"serveraddrs" bson:"serveraddrs" yaml:"serveraddrs"`
|
||||
Network map[string]NetworkInfo `json:"network" bson:"network" yaml:"network"`
|
||||
|
Reference in New Issue
Block a user