2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2021-08-12 12:49:10 +02:00
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2021-08-12 12:49:10 +02:00
|
|
|
"encoding/json"
|
2021-08-24 11:50:19 +02:00
|
|
|
"fmt"
|
2023-02-03 21:47:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
"github.com/gorilla/mux"
|
2024-10-13 14:52:43 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
2024-03-27 18:48:48 +01:00
|
|
|
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
2022-06-14 10:32:54 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/util"
|
2022-06-14 10:32:54 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
2023-11-28 15:15:51 +01:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2021-08-12 12:49:10 +02:00
|
|
|
)
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// PeersHandler is a handler that returns peers of the account
|
|
|
|
type PeersHandler struct {
|
2023-02-03 21:47:20 +01:00
|
|
|
accountManager server.AccountManager
|
|
|
|
claimsExtractor *jwtclaims.ClaimsExtractor
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:46:08 +01:00
|
|
|
// NewPeersHandler creates a new PeersHandler HTTP handler
|
2023-02-28 15:01:24 +01:00
|
|
|
func NewPeersHandler(accountManager server.AccountManager, authCfg AuthCfg) *PeersHandler {
|
|
|
|
return &PeersHandler{
|
2021-08-12 12:49:10 +02:00
|
|
|
accountManager: accountManager,
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
),
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 15:15:51 +01:00
|
|
|
func (h *PeersHandler) checkPeerStatus(peer *nbpeer.Peer) (*nbpeer.Peer, error) {
|
2023-10-11 18:11:45 +02:00
|
|
|
peerToReturn := peer.Copy()
|
|
|
|
if peer.Status.Connected {
|
|
|
|
// Although we have online status in store we do not yet have an updated channel so have to show it as disconnected
|
|
|
|
// This may happen after server restart when not all peers are yet connected
|
2023-12-05 14:17:56 +01:00
|
|
|
if !h.accountManager.HasConnectedChannel(peer.ID) {
|
2023-10-11 18:11:45 +02:00
|
|
|
peerToReturn.Status.Connected = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return peerToReturn, nil
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (h *PeersHandler) getPeer(ctx context.Context, account *server.Account, peerID, userID string, w http.ResponseWriter) {
|
|
|
|
peer, err := h.accountManager.GetPeer(ctx, account.Id, peerID, userID)
|
2023-02-07 20:11:08 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(ctx, err, w)
|
2023-02-07 20:11:08 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-11 18:11:45 +02:00
|
|
|
peerToReturn, err := h.checkPeerStatus(peer)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(ctx, err, w)
|
2023-10-11 18:11:45 +02:00
|
|
|
return
|
|
|
|
}
|
2023-11-07 14:38:36 +01:00
|
|
|
dnsDomain := h.accountManager.GetDNSDomain()
|
|
|
|
|
|
|
|
groupsInfo := toGroupsInfo(account.Groups, peer.ID)
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
validPeers, err := h.accountManager.GetValidatedPeers(account)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("failed to list appreoved peers: %v", err)
|
|
|
|
util.WriteError(ctx, fmt.Errorf("internal error"), w)
|
2024-03-27 18:48:48 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, valid := validPeers[peer.ID]
|
2024-09-12 15:19:27 +02:00
|
|
|
util.WriteJSONObject(ctx, w, toSinglePeerResponse(peerToReturn, groupsInfo, dnsDomain, valid))
|
2023-02-07 20:11:08 +01:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
func (h *PeersHandler) updatePeer(ctx context.Context, account *server.Account, userID, peerID string, w http.ResponseWriter, r *http.Request) {
|
2023-05-26 15:32:52 +02:00
|
|
|
req := &api.PeerRequest{}
|
2021-08-23 21:43:05 +02:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
}
|
2022-06-23 17:04:53 +02:00
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
update := &nbpeer.Peer{
|
|
|
|
ID: peerID,
|
|
|
|
SSHEnabled: req.SshEnabled,
|
|
|
|
Name: req.Name,
|
|
|
|
LoginExpirationEnabled: req.LoginExpirationEnabled,
|
2024-10-13 14:52:43 +02:00
|
|
|
|
|
|
|
InactivityExpirationEnabled: req.InactivityExpirationEnabled,
|
2024-03-27 18:48:48 +01:00
|
|
|
}
|
2023-11-28 11:44:08 +01:00
|
|
|
|
|
|
|
if req.ApprovalRequired != nil {
|
2024-03-27 18:48:48 +01:00
|
|
|
// todo: looks like that we reset all status property, is it right?
|
|
|
|
update.Status = &nbpeer.PeerStatus{
|
|
|
|
RequiresApproval: *req.ApprovalRequired,
|
|
|
|
}
|
2023-11-28 11:44:08 +01:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
peer, err := h.accountManager.UpdatePeer(ctx, account.Id, userID, update)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(ctx, err, w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
}
|
2022-11-21 11:14:42 +01:00
|
|
|
dnsDomain := h.accountManager.GetDNSDomain()
|
2023-11-07 14:38:36 +01:00
|
|
|
|
|
|
|
groupMinimumInfo := toGroupsInfo(account.Groups, peer.ID)
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
validPeers, err := h.accountManager.GetValidatedPeers(account)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("failed to list appreoved peers: %v", err)
|
|
|
|
util.WriteError(ctx, fmt.Errorf("internal error"), w)
|
2024-03-27 18:48:48 +01:00
|
|
|
return
|
|
|
|
}
|
2024-08-07 10:52:31 +02:00
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
_, valid := validPeers[peer.ID]
|
|
|
|
|
2024-09-12 15:19:27 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, toSinglePeerResponse(peer, groupMinimumInfo, dnsDomain, valid))
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
2022-02-22 18:18:05 +01:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (h *PeersHandler) deletePeer(ctx context.Context, accountID, userID string, peerID string, w http.ResponseWriter) {
|
|
|
|
err := h.accountManager.DeletePeer(ctx, accountID, peerID, userID)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("failed to delete peer: %v", err)
|
|
|
|
util.WriteError(ctx, err, w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(ctx, w, emptyObject{})
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 16:51:30 +01:00
|
|
|
// HandlePeer handles all peer requests for GET, PUT and DELETE operations
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *PeersHandler) HandlePeer(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-27 16:10:50 +02:00
|
|
|
accountID, userID, err := h.accountManager.GetAccountIDFromToken(r.Context(), claims)
|
2021-12-27 13:17:15 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2021-12-27 13:17:15 +01:00
|
|
|
return
|
|
|
|
}
|
2021-08-23 21:43:05 +02:00
|
|
|
vars := mux.Vars(r)
|
2023-05-03 00:15:25 +02:00
|
|
|
peerID := vars["peerId"]
|
2023-02-03 10:33:28 +01:00
|
|
|
if len(peerID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid peer ID"), w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodDelete:
|
2024-09-27 16:10:50 +02:00
|
|
|
h.deletePeer(r.Context(), accountID, userID, peerID, w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
2024-09-27 16:10:50 +02:00
|
|
|
case http.MethodGet, http.MethodPut:
|
|
|
|
account, err := h.accountManager.GetAccountByID(r.Context(), accountID, userID)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(r.Context(), err, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
h.getPeer(r.Context(), account, peerID, userID, w)
|
|
|
|
} else {
|
|
|
|
h.updatePeer(r.Context(), account, userID, peerID, w, r)
|
|
|
|
}
|
2023-02-07 20:11:08 +01:00
|
|
|
return
|
2021-08-23 21:43:05 +02:00
|
|
|
default:
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.NotFound, "unknown METHOD"), w)
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 16:51:30 +01:00
|
|
|
// GetAllPeers returns a list of all peers associated with a provided account
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *PeersHandler) GetAllPeers(w http.ResponseWriter, r *http.Request) {
|
2024-03-27 18:48:48 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-27 16:10:50 +02:00
|
|
|
accountID, userID, err := h.accountManager.GetAccountIDFromToken(r.Context(), claims)
|
2024-03-27 18:48:48 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-03-27 18:48:48 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
account, err := h.accountManager.GetAccountByID(r.Context(), accountID, userID)
|
2024-03-27 18:48:48 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-03-27 18:48:48 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsDomain := h.accountManager.GetDNSDomain()
|
2021-08-12 12:49:10 +02:00
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
respBody := make([]*api.PeerBatch, 0, len(account.Peers))
|
|
|
|
for _, peer := range account.Peers {
|
2024-03-27 18:48:48 +01:00
|
|
|
peerToReturn, err := h.checkPeerStatus(peer)
|
2022-11-05 10:24:50 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2022-11-05 10:24:50 +01:00
|
|
|
return
|
|
|
|
}
|
2024-03-27 18:48:48 +01:00
|
|
|
groupMinimumInfo := toGroupsInfo(account.Groups, peer.ID)
|
2022-11-05 10:24:50 +01:00
|
|
|
|
2024-08-07 10:52:31 +02:00
|
|
|
respBody = append(respBody, toPeerListItemResponse(peerToReturn, groupMinimumInfo, dnsDomain, 0))
|
2024-03-27 18:48:48 +01:00
|
|
|
}
|
2023-11-07 14:38:36 +01:00
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
validPeersMap, err := h.accountManager.GetValidatedPeers(account)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(r.Context()).Errorf("failed to list appreoved peers: %v", err)
|
|
|
|
util.WriteError(r.Context(), fmt.Errorf("internal error"), w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
2024-03-27 18:48:48 +01:00
|
|
|
h.setApprovalRequiredFlag(respBody, validPeersMap)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, respBody)
|
2024-03-27 18:48:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *PeersHandler) setApprovalRequiredFlag(respBody []*api.PeerBatch, approvedPeersMap map[string]struct{}) {
|
|
|
|
for _, peer := range respBody {
|
|
|
|
_, ok := approvedPeersMap[peer.Id]
|
|
|
|
if !ok {
|
|
|
|
peer.ApprovalRequired = true
|
|
|
|
}
|
|
|
|
}
|
2023-11-07 14:38:36 +01:00
|
|
|
}
|
|
|
|
|
2024-09-12 15:19:27 +02:00
|
|
|
// GetAccessiblePeers returns a list of all peers that the specified peer can connect to within the network.
|
|
|
|
func (h *PeersHandler) GetAccessiblePeers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-27 16:10:50 +02:00
|
|
|
accountID, userID, err := h.accountManager.GetAccountIDFromToken(r.Context(), claims)
|
2024-09-12 15:19:27 +02:00
|
|
|
if err != nil {
|
|
|
|
util.WriteError(r.Context(), err, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
peerID := vars["peerId"]
|
|
|
|
if len(peerID) == 0 {
|
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid peer ID"), w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
account, err := h.accountManager.GetAccountByID(r.Context(), accountID, userID)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(r.Context(), err, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := account.FindUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(r.Context(), err, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-20 11:36:58 +02:00
|
|
|
// If the user is regular user and does not own the peer
|
|
|
|
// with the given peerID return an empty list
|
|
|
|
if !user.HasAdminPower() && !user.IsServiceUser {
|
|
|
|
peer, ok := account.Peers[peerID]
|
|
|
|
if !ok {
|
|
|
|
util.WriteError(r.Context(), status.Errorf(status.NotFound, "peer not found"), w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.UserID != user.Id {
|
|
|
|
util.WriteJSONObject(r.Context(), w, []api.AccessiblePeer{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 15:19:27 +02:00
|
|
|
dnsDomain := h.accountManager.GetDNSDomain()
|
|
|
|
|
|
|
|
validPeers, err := h.accountManager.GetValidatedPeers(account)
|
|
|
|
if err != nil {
|
|
|
|
log.WithContext(r.Context()).Errorf("failed to list approved peers: %v", err)
|
|
|
|
util.WriteError(r.Context(), fmt.Errorf("internal error"), w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
customZone := account.GetPeersCustomZone(r.Context(), h.accountManager.GetDNSDomain())
|
|
|
|
netMap := account.GetPeerNetworkMap(r.Context(), peerID, customZone, validPeers, nil)
|
|
|
|
|
|
|
|
util.WriteJSONObject(r.Context(), w, toAccessiblePeers(netMap, dnsDomain))
|
|
|
|
}
|
|
|
|
|
2023-11-07 14:38:36 +01:00
|
|
|
func toAccessiblePeers(netMap *server.NetworkMap, dnsDomain string) []api.AccessiblePeer {
|
|
|
|
accessiblePeers := make([]api.AccessiblePeer, 0, len(netMap.Peers)+len(netMap.OfflinePeers))
|
|
|
|
for _, p := range netMap.Peers {
|
2024-09-12 15:19:27 +02:00
|
|
|
accessiblePeers = append(accessiblePeers, peerToAccessiblePeer(p, dnsDomain))
|
2023-11-07 14:38:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range netMap.OfflinePeers {
|
2024-09-12 15:19:27 +02:00
|
|
|
accessiblePeers = append(accessiblePeers, peerToAccessiblePeer(p, dnsDomain))
|
2023-11-07 14:38:36 +01:00
|
|
|
}
|
2024-09-12 15:19:27 +02:00
|
|
|
|
2023-11-07 14:38:36 +01:00
|
|
|
return accessiblePeers
|
|
|
|
}
|
|
|
|
|
2024-09-12 15:19:27 +02:00
|
|
|
func peerToAccessiblePeer(peer *nbpeer.Peer, dnsDomain string) api.AccessiblePeer {
|
|
|
|
return api.AccessiblePeer{
|
|
|
|
CityName: peer.Location.CityName,
|
|
|
|
Connected: peer.Status.Connected,
|
|
|
|
CountryCode: peer.Location.CountryCode,
|
|
|
|
DnsLabel: fqdn(peer, dnsDomain),
|
|
|
|
GeonameId: int(peer.Location.GeoNameID),
|
|
|
|
Id: peer.ID,
|
|
|
|
Ip: peer.IP.String(),
|
|
|
|
LastSeen: peer.Status.LastSeen,
|
|
|
|
Name: peer.Name,
|
|
|
|
Os: peer.Meta.OS,
|
|
|
|
UserId: peer.UserID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
func toGroupsInfo(groups map[string]*nbgroup.Group, peerID string) []api.GroupMinimum {
|
2022-06-14 10:32:54 +02:00
|
|
|
var groupsInfo []api.GroupMinimum
|
|
|
|
groupsChecked := make(map[string]struct{})
|
2023-11-07 14:38:36 +01:00
|
|
|
for _, group := range groups {
|
2022-06-14 10:32:54 +02:00
|
|
|
_, ok := groupsChecked[group.ID]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groupsChecked[group.ID] = struct{}{}
|
|
|
|
for _, pk := range group.Peers {
|
2023-11-29 09:33:05 +01:00
|
|
|
if pk == peerID {
|
2022-06-14 10:32:54 +02:00
|
|
|
info := api.GroupMinimum{
|
|
|
|
Id: group.ID,
|
|
|
|
Name: group.Name,
|
|
|
|
PeersCount: len(group.Peers),
|
|
|
|
}
|
|
|
|
groupsInfo = append(groupsInfo, info)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-07 14:38:36 +01:00
|
|
|
return groupsInfo
|
|
|
|
}
|
2023-01-02 15:11:32 +01:00
|
|
|
|
2024-09-12 15:19:27 +02:00
|
|
|
func toSinglePeerResponse(peer *nbpeer.Peer, groupsInfo []api.GroupMinimum, dnsDomain string, approved bool) *api.Peer {
|
2024-02-20 09:59:56 +01:00
|
|
|
osVersion := peer.Meta.OSVersion
|
|
|
|
if osVersion == "" {
|
|
|
|
osVersion = peer.Meta.Core
|
|
|
|
}
|
2024-02-22 17:22:43 +01:00
|
|
|
|
2023-11-07 14:38:36 +01:00
|
|
|
return &api.Peer{
|
2024-10-13 14:52:43 +02:00
|
|
|
Id: peer.ID,
|
|
|
|
Name: peer.Name,
|
|
|
|
Ip: peer.IP.String(),
|
|
|
|
ConnectionIp: peer.Location.ConnectionIP.String(),
|
|
|
|
Connected: peer.Status.Connected,
|
|
|
|
LastSeen: peer.Status.LastSeen,
|
|
|
|
Os: fmt.Sprintf("%s %s", peer.Meta.OS, osVersion),
|
|
|
|
KernelVersion: peer.Meta.KernelVersion,
|
|
|
|
GeonameId: int(peer.Location.GeoNameID),
|
|
|
|
Version: peer.Meta.WtVersion,
|
|
|
|
Groups: groupsInfo,
|
|
|
|
SshEnabled: peer.SSHEnabled,
|
|
|
|
Hostname: peer.Meta.Hostname,
|
|
|
|
UserId: peer.UserID,
|
|
|
|
UiVersion: peer.Meta.UIVersion,
|
|
|
|
DnsLabel: fqdn(peer, dnsDomain),
|
|
|
|
LoginExpirationEnabled: peer.LoginExpirationEnabled,
|
|
|
|
LastLogin: peer.LastLogin,
|
|
|
|
LoginExpired: peer.Status.LoginExpired,
|
|
|
|
ApprovalRequired: !approved,
|
|
|
|
CountryCode: peer.Location.CountryCode,
|
|
|
|
CityName: peer.Location.CityName,
|
|
|
|
SerialNumber: peer.Meta.SystemSerialNumber,
|
|
|
|
InactivityExpirationEnabled: peer.InactivityExpirationEnabled,
|
2022-11-21 11:14:42 +01:00
|
|
|
}
|
2023-11-07 14:38:36 +01:00
|
|
|
}
|
2023-02-14 10:14:00 +01:00
|
|
|
|
2023-11-28 15:15:51 +01:00
|
|
|
func toPeerListItemResponse(peer *nbpeer.Peer, groupsInfo []api.GroupMinimum, dnsDomain string, accessiblePeersCount int) *api.PeerBatch {
|
2024-02-20 09:59:56 +01:00
|
|
|
osVersion := peer.Meta.OSVersion
|
|
|
|
if osVersion == "" {
|
|
|
|
osVersion = peer.Meta.Core
|
|
|
|
}
|
2024-02-22 17:22:43 +01:00
|
|
|
|
2023-11-07 14:38:36 +01:00
|
|
|
return &api.PeerBatch{
|
2023-02-14 10:14:00 +01:00
|
|
|
Id: peer.ID,
|
|
|
|
Name: peer.Name,
|
|
|
|
Ip: peer.IP.String(),
|
2024-02-22 17:22:43 +01:00
|
|
|
ConnectionIp: peer.Location.ConnectionIP.String(),
|
2023-02-14 10:14:00 +01:00
|
|
|
Connected: peer.Status.Connected,
|
|
|
|
LastSeen: peer.Status.LastSeen,
|
2024-02-20 09:59:56 +01:00
|
|
|
Os: fmt.Sprintf("%s %s", peer.Meta.OS, osVersion),
|
2024-02-22 17:22:43 +01:00
|
|
|
KernelVersion: peer.Meta.KernelVersion,
|
|
|
|
GeonameId: int(peer.Location.GeoNameID),
|
2023-02-14 10:14:00 +01:00
|
|
|
Version: peer.Meta.WtVersion,
|
|
|
|
Groups: groupsInfo,
|
|
|
|
SshEnabled: peer.SSHEnabled,
|
|
|
|
Hostname: peer.Meta.Hostname,
|
2024-02-22 17:22:43 +01:00
|
|
|
UserId: peer.UserID,
|
|
|
|
UiVersion: peer.Meta.UIVersion,
|
2023-11-07 14:38:36 +01:00
|
|
|
DnsLabel: fqdn(peer, dnsDomain),
|
2023-02-14 10:14:00 +01:00
|
|
|
LoginExpirationEnabled: peer.LoginExpirationEnabled,
|
|
|
|
LastLogin: peer.LastLogin,
|
2023-02-15 11:27:22 +01:00
|
|
|
LoginExpired: peer.Status.LoginExpired,
|
2023-11-07 14:38:36 +01:00
|
|
|
AccessiblePeersCount: accessiblePeersCount,
|
2024-02-22 17:22:43 +01:00
|
|
|
CountryCode: peer.Location.CountryCode,
|
|
|
|
CityName: peer.Location.CityName,
|
2024-05-06 14:49:03 +02:00
|
|
|
SerialNumber: peer.Meta.SystemSerialNumber,
|
2024-10-13 14:52:43 +02:00
|
|
|
|
|
|
|
InactivityExpirationEnabled: peer.InactivityExpirationEnabled,
|
2023-11-07 14:38:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 15:15:51 +01:00
|
|
|
func fqdn(peer *nbpeer.Peer, dnsDomain string) string {
|
2023-11-07 14:38:36 +01:00
|
|
|
fqdn := peer.FQDN(dnsDomain)
|
|
|
|
if fqdn == "" {
|
|
|
|
return peer.DNSLabel
|
|
|
|
} else {
|
|
|
|
return fqdn
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
}
|