2021-08-12 12:49:10 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-08-24 11:50:19 +02:00
|
|
|
"fmt"
|
2022-02-23 20:02:02 +01:00
|
|
|
"github.com/wiretrustee/wiretrustee/management/server/jwtclaims"
|
2022-02-22 11:28:19 +01:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
"github.com/gorilla/mux"
|
2021-08-12 12:49:10 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/wiretrustee/wiretrustee/management/server"
|
|
|
|
)
|
|
|
|
|
2021-08-23 21:43:05 +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
|
|
|
}
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
//PeerResponse is a response sent to the client
|
2021-08-12 12:49:10 +02:00
|
|
|
type PeerResponse struct {
|
|
|
|
Name string
|
|
|
|
IP string
|
|
|
|
Connected bool
|
|
|
|
LastSeen time.Time
|
2021-08-23 21:43:05 +02:00
|
|
|
OS string
|
2022-02-14 17:51:07 +01:00
|
|
|
Version string
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//PeerRequest is a request sent by the client
|
|
|
|
type PeerRequest struct {
|
|
|
|
Name string
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
func (h *Peers) updatePeer(accountId string, peer *server.Peer, w http.ResponseWriter, r *http.Request) {
|
|
|
|
req := &PeerRequest{}
|
|
|
|
peerIp := peer.IP
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
peer, err = h.accountManager.RenamePeer(accountId, peer.Key, req.Name)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed updating peer %s under account %s %v", peerIp, accountId, err)
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeJSONObject(w, toPeerResponse(peer))
|
|
|
|
}
|
2022-02-22 18:18:05 +01:00
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
func (h *Peers) deletePeer(accountId string, peer *server.Peer, w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := h.accountManager.DeletePeer(accountId, peer.Key)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed deleteing peer %s, %v", peer.IP, err)
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-08-25 14:16:17 +02:00
|
|
|
writeJSONObject(w, "")
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-02-11 17:18:18 +01:00
|
|
|
func (h *Peers) getPeerAccount(r *http.Request) (*server.Account, error) {
|
2022-02-23 20:02:02 +01:00
|
|
|
jwtClaims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
2022-02-11 17:18:18 +01:00
|
|
|
|
|
|
|
account, err := h.accountManager.GetAccountByUserOrAccountId(jwtClaims.UserId, jwtClaims.AccountId, jwtClaims.Domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed getting account of a user %s: %v", jwtClaims.UserId, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
func (h *Peers) HandlePeer(w http.ResponseWriter, r *http.Request) {
|
2022-02-11 17:18:18 +01:00
|
|
|
account, err := h.getPeerAccount(r)
|
2021-12-27 13:17:15 +01:00
|
|
|
if err != nil {
|
2022-02-11 17:18:18 +01:00
|
|
|
log.Error(err)
|
2021-12-27 13:17:15 +01:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-08-23 21:43:05 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
peerId := vars["id"] //effectively peer IP address
|
|
|
|
if len(peerId) == 0 {
|
|
|
|
http.Error(w, "invalid peer Id", http.StatusBadRequest)
|
|
|
|
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 {
|
|
|
|
http.Error(w, "peer not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodDelete:
|
2021-12-27 13:17:15 +01:00
|
|
|
h.deletePeer(account.Id, peer, w, r)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
case http.MethodPut:
|
2021-12-27 13:17:15 +01:00
|
|
|
h.updatePeer(account.Id, peer, w, r)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
|
|
|
case http.MethodGet:
|
|
|
|
writeJSONObject(w, toPeerResponse(peer))
|
|
|
|
return
|
|
|
|
|
|
|
|
default:
|
|
|
|
http.Error(w, "", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-02-11 17:18:18 +01:00
|
|
|
account, err := h.getPeerAccount(r)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
2022-02-11 17:18:18 +01:00
|
|
|
log.Error(err)
|
2021-08-12 12:49:10 +02:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respBody := []*PeerResponse{}
|
|
|
|
for _, peer := range account.Peers {
|
2021-08-23 21:43:05 +02:00
|
|
|
respBody = append(respBody, toPeerResponse(peer))
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
2021-08-23 21:43:05 +02:00
|
|
|
writeJSONObject(w, respBody)
|
|
|
|
return
|
2021-08-12 12:49:10 +02:00
|
|
|
default:
|
|
|
|
http.Error(w, "", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
2021-08-23 21:43:05 +02:00
|
|
|
|
|
|
|
func toPeerResponse(peer *server.Peer) *PeerResponse {
|
|
|
|
return &PeerResponse{
|
|
|
|
Name: peer.Name,
|
|
|
|
IP: peer.IP.String(),
|
2021-08-24 11:50:19 +02:00
|
|
|
Connected: peer.Status.Connected,
|
|
|
|
LastSeen: peer.Status.LastSeen,
|
2021-08-27 11:34:38 +02:00
|
|
|
OS: fmt.Sprintf("%s %s", peer.Meta.OS, peer.Meta.Core),
|
2022-02-14 17:51:07 +01:00
|
|
|
Version: peer.Meta.WtVersion,
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
}
|