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"
|
2021-08-23 21:43:05 +02:00
|
|
|
"github.com/gorilla/mux"
|
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"
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2022-06-14 10:32:54 +02:00
|
|
|
"net/http"
|
2021-08-12 12:49:10 +02:00
|
|
|
)
|
|
|
|
|
2022-09-26 18:02:45 +02:00
|
|
|
// Peers is a handler that returns peers of the account
|
2021-08-12 12:49:10 +02:00
|
|
|
type Peers struct {
|
2022-02-22 11:28:19 +01:00
|
|
|
accountManager server.AccountManager
|
2022-01-24 11:21:30 +01:00
|
|
|
authAudience string
|
2022-02-23 20:02:02 +01:00
|
|
|
jwtExtractor jwtclaims.ClaimsExtractor
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 11:28:19 +01:00
|
|
|
func NewPeers(accountManager server.AccountManager, authAudience string) *Peers {
|
2021-08-12 12:49:10 +02:00
|
|
|
return &Peers{
|
|
|
|
accountManager: accountManager,
|
2022-01-24 11:21:30 +01:00
|
|
|
authAudience: authAudience,
|
2022-02-23 20:02:02 +01:00
|
|
|
jwtExtractor: *jwtclaims.NewClaimsExtractor(nil),
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
func (h *Peers) updatePeer(account *server.Account, peer *server.Peer, w http.ResponseWriter, r *http.Request) {
|
|
|
|
req := &api.PutApiPeersIdJSONBody{}
|
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
|
|
|
|
|
|
|
update := &server.Peer{Key: peer.Key, SSHEnabled: req.SshEnabled, Name: req.Name}
|
|
|
|
peer, err = h.accountManager.UpdatePeer(account.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()
|
|
|
|
util.WriteJSONObject(w, toPeerResponse(peer, account, dnsDomain))
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
2022-02-22 18:18:05 +01:00
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
func (h *Peers) deletePeer(accountID, userID string, peer *server.Peer, w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := h.accountManager.DeletePeer(accountID, peer.Key, 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
|
|
|
|
}
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, "")
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Peers) HandlePeer(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
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)
|
|
|
|
peerId := vars["id"] //effectively peer IP address
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-27 13:17:15 +01:00
|
|
|
peer, err := h.accountManager.GetPeerByIP(account.Id, peerId)
|
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()
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodDelete:
|
2023-01-02 15:11:32 +01:00
|
|
|
h.deletePeer(account.Id, user.Id, peer, w, r)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
case http.MethodPut:
|
2022-06-14 10:32:54 +02:00
|
|
|
h.updatePeer(account, peer, w, r)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
case http.MethodGet:
|
2022-11-21 11:14:42 +01:00
|
|
|
util.WriteJSONObject(w, toPeerResponse(peer, account, dnsDomain))
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
func (h *Peers) GetPeers(w http.ResponseWriter, r *http.Request) {
|
2021-08-12 12:49:10 +02:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
|
|
|
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()
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
respBody := []*api.Peer{}
|
2022-11-05 10:24:50 +01:00
|
|
|
for _, peer := range peers {
|
2022-11-21 11:14:42 +01:00
|
|
|
respBody = append(respBody, toPeerResponse(peer, account, dnsDomain))
|
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
|
|
|
|
2022-11-21 11:14:42 +01:00
|
|
|
func toPeerResponse(peer *server.Peer, account *server.Account, dnsDomain string) *api.Peer {
|
2022-06-14 10:32:54 +02:00
|
|
|
var groupsInfo []api.GroupMinimum
|
|
|
|
groupsChecked := make(map[string]struct{})
|
|
|
|
for _, group := range account.Groups {
|
|
|
|
_, ok := groupsChecked[group.ID]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groupsChecked[group.ID] = struct{}{}
|
|
|
|
for _, pk := range group.Peers {
|
|
|
|
if pk == peer.Key {
|
|
|
|
info := api.GroupMinimum{
|
|
|
|
Id: group.ID,
|
|
|
|
Name: group.Name,
|
|
|
|
PeersCount: len(group.Peers),
|
|
|
|
}
|
|
|
|
groupsInfo = append(groupsInfo, info)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-02 15:11:32 +01:00
|
|
|
|
|
|
|
fqdn := peer.FQDN(dnsDomain)
|
|
|
|
if fqdn == "" {
|
|
|
|
fqdn = peer.DNSLabel
|
2022-11-21 11:14:42 +01:00
|
|
|
}
|
2022-06-14 10:32:54 +02:00
|
|
|
return &api.Peer{
|
2022-06-23 17:04:53 +02:00
|
|
|
Id: peer.IP.String(),
|
|
|
|
Name: peer.Name,
|
|
|
|
Ip: peer.IP.String(),
|
|
|
|
Connected: peer.Status.Connected,
|
|
|
|
LastSeen: peer.Status.LastSeen,
|
|
|
|
Os: fmt.Sprintf("%s %s", peer.Meta.OS, peer.Meta.Core),
|
|
|
|
Version: peer.Meta.WtVersion,
|
|
|
|
Groups: groupsInfo,
|
|
|
|
SshEnabled: peer.SSHEnabled,
|
2022-09-26 18:02:45 +02:00
|
|
|
Hostname: peer.Meta.Hostname,
|
|
|
|
UserId: &peer.UserID,
|
|
|
|
UiVersion: &peer.Meta.UIVersion,
|
2022-11-21 11:14:42 +01:00
|
|
|
DnsLabel: fqdn,
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
}
|