2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2021-08-12 12:49:10 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-08-24 11:50:19 +02:00
|
|
|
"fmt"
|
2024-02-20 09:59:56 +01:00
|
|
|
"net"
|
2023-02-03 21:47:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *PeersHandler) getPeer(account *server.Account, peerID, userID string, w http.ResponseWriter) {
|
2023-02-07 20:11:08 +01:00
|
|
|
peer, err := h.accountManager.GetPeer(account.Id, peerID, userID)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-11 18:11:45 +02:00
|
|
|
peerToReturn, err := h.checkPeerStatus(peer)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
|
|
|
return
|
|
|
|
}
|
2023-11-07 14:38:36 +01:00
|
|
|
dnsDomain := h.accountManager.GetDNSDomain()
|
|
|
|
|
|
|
|
groupsInfo := toGroupsInfo(account.Groups, peer.ID)
|
|
|
|
|
|
|
|
netMap := account.GetPeerNetworkMap(peerID, h.accountManager.GetDNSDomain())
|
|
|
|
accessiblePeers := toAccessiblePeers(netMap, dnsDomain)
|
2023-10-11 18:11:45 +02:00
|
|
|
|
2023-11-07 14:38:36 +01:00
|
|
|
util.WriteJSONObject(w, toSinglePeerResponse(peerToReturn, groupsInfo, dnsDomain, accessiblePeers))
|
2023-02-07 20:11:08 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *PeersHandler) updatePeer(account *server.Account, user *server.User, 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
|
|
|
|
2023-11-28 15:15:51 +01:00
|
|
|
update := &nbpeer.Peer{ID: peerID, SSHEnabled: req.SshEnabled, Name: req.Name,
|
2023-02-14 10:14:00 +01:00
|
|
|
LoginExpirationEnabled: req.LoginExpirationEnabled}
|
2023-11-28 11:44:08 +01:00
|
|
|
|
|
|
|
if req.ApprovalRequired != nil {
|
2023-11-28 15:15:51 +01:00
|
|
|
update.Status = &nbpeer.PeerStatus{RequiresApproval: *req.ApprovalRequired}
|
2023-11-28 11:44:08 +01:00
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
peer, err := h.accountManager.UpdatePeer(account.Id, user.Id, update)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(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)
|
|
|
|
|
|
|
|
netMap := account.GetPeerNetworkMap(peerID, h.accountManager.GetDNSDomain())
|
|
|
|
accessiblePeers := toAccessiblePeers(netMap, dnsDomain)
|
|
|
|
|
|
|
|
util.WriteJSONObject(w, toSinglePeerResponse(peer, groupMinimumInfo, dnsDomain, accessiblePeers))
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
2022-02-22 18:18:05 +01:00
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
func (h *PeersHandler) deletePeer(accountID, userID string, peerID string, w http.ResponseWriter) {
|
2023-10-01 19:51:39 +02:00
|
|
|
err := h.accountManager.DeletePeer(accountID, peerID, userID)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
}
|
2023-03-29 18:46:40 +02:00
|
|
|
util.WriteJSONObject(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)
|
2023-01-02 15:11:32 +01:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2021-12-27 13:17:15 +01:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(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 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid peer ID"), w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodDelete:
|
2023-02-03 10:33:28 +01:00
|
|
|
h.deletePeer(account.Id, user.Id, peerID, w)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
case http.MethodPut:
|
2023-02-03 10:33:28 +01:00
|
|
|
h.updatePeer(account, user, peerID, w, r)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
2023-02-07 20:11:08 +01:00
|
|
|
case http.MethodGet:
|
|
|
|
h.getPeer(account, peerID, user.Id, w)
|
|
|
|
return
|
2021-08-23 21:43:05 +02:00
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(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) {
|
2021-08-12 12:49:10 +02:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2022-11-11 20:36:45 +01:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2021-08-12 12:49:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
peers, err := h.accountManager.GetPeers(account.Id, user.Id)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-11-05 10:24:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:14:42 +01:00
|
|
|
dnsDomain := h.accountManager.GetDNSDomain()
|
|
|
|
|
2023-11-07 14:38:36 +01:00
|
|
|
respBody := make([]*api.PeerBatch, 0, len(peers))
|
2022-11-05 10:24:50 +01:00
|
|
|
for _, peer := range peers {
|
2023-10-11 18:11:45 +02:00
|
|
|
peerToReturn, err := h.checkPeerStatus(peer)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
|
|
|
return
|
|
|
|
}
|
2023-11-07 14:38:36 +01:00
|
|
|
groupMinimumInfo := toGroupsInfo(account.Groups, peer.ID)
|
|
|
|
|
|
|
|
accessiblePeerNumbers := h.accessiblePeersNumber(account, peer.ID)
|
|
|
|
|
|
|
|
respBody = append(respBody, toPeerListItemResponse(peerToReturn, groupMinimumInfo, dnsDomain, accessiblePeerNumbers))
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, respBody)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
2021-08-12 12:49:10 +02:00
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.NotFound, "unknown METHOD"), w)
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2023-11-07 14:38:36 +01:00
|
|
|
func (h *PeersHandler) accessiblePeersNumber(account *server.Account, peerID string) int {
|
|
|
|
netMap := account.GetPeerNetworkMap(peerID, h.accountManager.GetDNSDomain())
|
|
|
|
return len(netMap.Peers) + len(netMap.OfflinePeers)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
ap := api.AccessiblePeer{
|
|
|
|
Id: p.ID,
|
|
|
|
Name: p.Name,
|
|
|
|
Ip: p.IP.String(),
|
|
|
|
DnsLabel: fqdn(p, dnsDomain),
|
|
|
|
UserId: p.UserID,
|
|
|
|
}
|
|
|
|
accessiblePeers = append(accessiblePeers, ap)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range netMap.OfflinePeers {
|
|
|
|
ap := api.AccessiblePeer{
|
|
|
|
Id: p.ID,
|
|
|
|
Name: p.Name,
|
|
|
|
Ip: p.IP.String(),
|
|
|
|
DnsLabel: fqdn(p, dnsDomain),
|
|
|
|
UserId: p.UserID,
|
|
|
|
}
|
|
|
|
accessiblePeers = append(accessiblePeers, ap)
|
|
|
|
}
|
|
|
|
return accessiblePeers
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGroupsInfo(groups map[string]*server.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-02-20 09:59:56 +01:00
|
|
|
func connectionIPoString(ip net.IP) *string {
|
|
|
|
publicIP := ""
|
|
|
|
if ip != nil {
|
|
|
|
publicIP = ip.String()
|
|
|
|
}
|
|
|
|
return &publicIP
|
|
|
|
}
|
|
|
|
|
2023-11-28 15:15:51 +01:00
|
|
|
func toSinglePeerResponse(peer *nbpeer.Peer, groupsInfo []api.GroupMinimum, dnsDomain string, accessiblePeer []api.AccessiblePeer) *api.Peer {
|
2024-02-20 09:59:56 +01:00
|
|
|
osVersion := peer.Meta.OSVersion
|
|
|
|
if osVersion == "" {
|
|
|
|
osVersion = peer.Meta.Core
|
|
|
|
}
|
|
|
|
geonameID := int(peer.Location.GeoNameID)
|
2023-11-07 14:38:36 +01:00
|
|
|
return &api.Peer{
|
|
|
|
Id: peer.ID,
|
|
|
|
Name: peer.Name,
|
|
|
|
Ip: peer.IP.String(),
|
2024-02-20 09:59:56 +01:00
|
|
|
ConnectionIp: connectionIPoString(peer.Location.ConnectionIP),
|
2023-11-07 14:38:36 +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),
|
|
|
|
KernelVersion: &peer.Meta.KernelVersion,
|
|
|
|
GeonameId: &geonameID,
|
2023-11-07 14:38:36 +01:00
|
|
|
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,
|
|
|
|
AccessiblePeers: accessiblePeer,
|
2023-11-28 15:15:51 +01:00
|
|
|
ApprovalRequired: &peer.Status.RequiresApproval,
|
2024-02-20 09:59:56 +01:00
|
|
|
CountryCode: &peer.Location.CountryCode,
|
|
|
|
CityName: &peer.Location.CityName,
|
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
|
|
|
|
}
|
|
|
|
geonameID := int(peer.Location.GeoNameID)
|
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-20 09:59:56 +01:00
|
|
|
ConnectionIp: connectionIPoString(peer.Location.ConnectionIP),
|
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),
|
|
|
|
KernelVersion: &peer.Meta.KernelVersion,
|
|
|
|
GeonameId: &geonameID,
|
2023-02-14 10:14:00 +01:00
|
|
|
Version: peer.Meta.WtVersion,
|
|
|
|
Groups: groupsInfo,
|
|
|
|
SshEnabled: peer.SSHEnabled,
|
|
|
|
Hostname: peer.Meta.Hostname,
|
|
|
|
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,
|
2023-11-28 15:15:51 +01:00
|
|
|
ApprovalRequired: &peer.Status.RequiresApproval,
|
2024-02-20 09:59:56 +01:00
|
|
|
CountryCode: &peer.Location.CountryCode,
|
|
|
|
CityName: &peer.Location.CityName,
|
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
|
|
|
}
|
|
|
|
}
|