go-http-peering/server/handler.go

239 lines
6.0 KiB
Go
Raw Permalink Normal View History

2019-02-03 20:56:58 +01:00
package server
import (
2019-02-22 17:35:49 +01:00
"crypto/rsa"
2019-02-03 20:56:58 +01:00
"encoding/json"
"errors"
"net/http"
"time"
peering "forge.cadoles.com/Cadoles/go-http-peering"
peeringCrypto "forge.cadoles.com/Cadoles/go-http-peering/crypto"
2019-02-03 20:56:58 +01:00
)
var (
ErrInvalidAdvertisingRequest = errors.New("invalid advertising request")
ErrInvalidUpdateRequest = errors.New("invalid update request")
ErrPeerRejected = errors.New("peer rejected")
ErrPeerIDAlreadyInUse = errors.New("peer id already in use")
ErrUnauthorized = errors.New("unauthorized")
)
2019-02-22 17:35:49 +01:00
func AdvertiseHandler(store peering.Store, key *rsa.PublicKey, funcs ...OptionFunc) http.HandlerFunc {
2019-02-03 20:56:58 +01:00
options := createOptions(funcs...)
logger := options.Logger
handler := func(w http.ResponseWriter, r *http.Request) {
2019-02-22 17:35:49 +01:00
serverToken := r.Header.Get(ServerTokenHeader)
if serverToken == "" {
2019-02-03 20:56:58 +01:00
options.ErrorHandler(w, r, ErrInvalidAdvertisingRequest)
return
}
2019-02-22 17:35:49 +01:00
serverClaims, err := assertServerToken(key, serverToken)
if err != nil {
logger.Printf("[ERROR] %s", err)
sendError(w, http.StatusUnauthorized)
return
}
advertising := &peering.AdvertisingRequest{}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(advertising); err != nil {
logger.Printf("[ERROR] %s", err)
2019-02-03 20:56:58 +01:00
options.ErrorHandler(w, r, ErrInvalidAdvertisingRequest)
return
}
2019-02-22 17:35:49 +01:00
if _, err := peeringCrypto.DecodePEMToPublicKey(advertising.PublicKey); err != nil {
2019-02-03 20:56:58 +01:00
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, ErrInvalidAdvertisingRequest)
return
}
2019-02-22 17:35:49 +01:00
peer, err := store.Get(serverClaims.PeerID)
2019-02-03 20:56:58 +01:00
if err == nil {
logger.Printf("[ERROR] %s", ErrPeerIDAlreadyInUse)
options.ErrorHandler(w, r, ErrPeerIDAlreadyInUse)
return
}
if err != peering.ErrPeerNotFound {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
attrs := filterAttributes(options.PeerAttributes, advertising.Attributes)
2019-02-22 17:35:49 +01:00
peer, err = store.Create(serverClaims.PeerID, attrs)
2019-02-03 20:56:58 +01:00
if err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
if err := store.UpdateLastContact(peer.ID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
2019-02-22 17:35:49 +01:00
if err := store.UpdateLastContact(peer.ID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
2019-02-03 20:56:58 +01:00
if err := store.UpdatePublicKey(peer.ID, advertising.PublicKey); err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
w.WriteHeader(http.StatusCreated)
}
return handler
}
func UpdateHandler(store peering.Store, funcs ...OptionFunc) http.HandlerFunc {
options := createOptions(funcs...)
logger := options.Logger
handler := func(w http.ResponseWriter, r *http.Request) {
update := &peering.UpdateRequest{}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(update); err != nil {
options.ErrorHandler(w, r, ErrInvalidUpdateRequest)
return
}
peerID, err := GetPeerID(r)
if err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
peer, err := store.Get(peerID)
if err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
if peer == nil {
logger.Printf("[ERROR] %s", ErrUnauthorized)
options.ErrorHandler(w, r, ErrUnauthorized)
return
}
if peer.Status == peering.StatusRejected {
logger.Printf("[ERROR] %s", ErrPeerRejected)
options.ErrorHandler(w, r, ErrPeerRejected)
return
}
if err := store.UpdateLastContact(peer.ID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
attrs := filterAttributes(options.PeerAttributes, update.Attributes)
if err := store.UpdateAttributes(peer.ID, attrs); err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
return handler
}
func PingHandler(store peering.Store, funcs ...OptionFunc) http.HandlerFunc {
options := createOptions(funcs...)
logger := options.Logger
handler := func(w http.ResponseWriter, r *http.Request) {
update := &peering.UpdateRequest{}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(update); err != nil {
options.ErrorHandler(w, r, ErrInvalidUpdateRequest)
return
}
peerID, err := GetPeerID(r)
if err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
peer, err := store.Get(peerID)
if err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
if peer == nil {
logger.Printf("[ERROR] %s", ErrUnauthorized)
options.ErrorHandler(w, r, ErrUnauthorized)
return
}
if peer.Status == peering.StatusRejected {
logger.Printf("[ERROR] %s", ErrPeerRejected)
options.ErrorHandler(w, r, ErrPeerRejected)
return
}
if err := store.UpdateLastContact(peer.ID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err)
options.ErrorHandler(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
return handler
}
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
switch err {
case ErrInvalidAdvertisingRequest:
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
case ErrPeerIDAlreadyInUse:
http.Error(w, http.StatusText(http.StatusConflict), http.StatusConflict)
case ErrUnauthorized:
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
case ErrPeerRejected:
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
default:
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}
func filterAttributes(filters []string, attrs peering.PeerAttributes) peering.PeerAttributes {
filtered := peering.PeerAttributes{}
for _, key := range filters {
if _, exists := attrs[key]; exists {
filtered[key] = attrs[key]
}
}
return filtered
}