2024-03-11 15:34:06 +01:00
|
|
|
package hvpnnode3
|
|
|
|
|
|
|
|
import (
|
2024-03-13 23:46:05 +01:00
|
|
|
"context"
|
2024-03-11 15:34:06 +01:00
|
|
|
"encoding/json"
|
2024-03-13 23:46:05 +01:00
|
|
|
"errors"
|
2024-03-11 15:34:06 +01:00
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
2024-03-19 00:56:52 +01:00
|
|
|
"net/url"
|
2024-03-24 16:13:26 +01:00
|
|
|
"time"
|
2024-03-11 15:34:06 +01:00
|
|
|
|
|
|
|
"github.com/felixge/httpsnoop"
|
2024-03-13 23:46:05 +01:00
|
|
|
"github.com/google/uuid"
|
2024-03-11 15:34:06 +01:00
|
|
|
|
|
|
|
"gitea.hbanafa.com/HeshamTB/hvpn-node3/proto"
|
|
|
|
)
|
|
|
|
|
2024-03-13 23:46:05 +01:00
|
|
|
type CtxKey string
|
|
|
|
const CtxReqID CtxKey = "request_id"
|
2024-03-11 15:34:06 +01:00
|
|
|
|
2024-03-15 21:48:22 +01:00
|
|
|
func HandleGetNodeInfo(wgLink *WGLink) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqID := r.Context().Value(CtxReqID).(uuid.UUID)
|
|
|
|
debug("Preparing node info for", reqID)
|
|
|
|
dev, err := wgLink.Device(wgLink.Name)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(
|
|
|
|
proto.NodePublicInfo{
|
|
|
|
PublicKey: dev.PublicKey.String(),
|
|
|
|
UDPPort: dev.ListenPort,
|
2024-03-17 22:58:17 +01:00
|
|
|
Endpoint: wgLink.Endpoint,
|
2024-03-18 00:06:33 +01:00
|
|
|
Type: dev.Type.String(),
|
2024-03-24 16:13:26 +01:00
|
|
|
StartedAt: wgLink.StartedAT,
|
|
|
|
Uptime: time.Since(wgLink.StartedAT),
|
2024-03-15 21:48:22 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 15:34:06 +01:00
|
|
|
func HandleGetPeer(wgLink *WGLink) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-13 23:46:05 +01:00
|
|
|
reqID := r.Context().Value(CtxReqID).(uuid.UUID)
|
|
|
|
pubkey := r.PathValue("pubkey")
|
|
|
|
debug(pubkey, reqID)
|
|
|
|
peer, err := wgLink.GetPeer(pubkey)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, proto.PeerDoesNotExist){
|
|
|
|
json.NewEncoder(w).Encode(
|
|
|
|
proto.ErrJSONResponse{Message: "Peer does not exist"},
|
|
|
|
)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.NewEncoder(w).Encode(proto.WgPeerToPeer(*peer))
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
}
|
2024-03-11 15:34:06 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleGetPeers(wgLink *WGLink) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-13 23:46:05 +01:00
|
|
|
reqID := r.Context().Value(CtxReqID).(uuid.UUID)
|
|
|
|
debug("GET Peers for", reqID)
|
2024-03-11 15:34:06 +01:00
|
|
|
dev, err := wgLink.Device(wgLink.Name)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
slog.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var peers []proto.Peer
|
|
|
|
for _, peer := range dev.Peers {
|
2024-03-13 23:46:05 +01:00
|
|
|
p := proto.WgPeerToPeer(peer)
|
2024-03-11 15:34:06 +01:00
|
|
|
peers = append(peers, p)
|
|
|
|
}
|
|
|
|
err = json.NewEncoder(w).Encode(peers)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-03-13 23:46:05 +01:00
|
|
|
|
|
|
|
func HandlePostPeer(wgLink *WGLink) http.HandlerFunc {
|
2024-03-11 15:34:06 +01:00
|
|
|
return func(w http.ResponseWriter, r* http.Request) {
|
2024-03-13 23:46:05 +01:00
|
|
|
reqID := r.Context().Value(CtxReqID).(uuid.UUID)
|
|
|
|
debug("POST Peer for", reqID)
|
2024-03-11 15:34:06 +01:00
|
|
|
peerRequest := proto.CreatePeerRequest{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&peerRequest)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
json.NewEncoder(w).Encode(
|
|
|
|
&proto.ErrJSONResponse{Message: "Invalid request JSON"},
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
2024-03-13 23:46:05 +01:00
|
|
|
|
|
|
|
if valid := ValidKey(peerRequest.PublicKey); !valid {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
json.NewEncoder(w).Encode(
|
|
|
|
proto.ErrJSONResponse{Message: "Invalid public key"},
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := wgLink.Exists(peerRequest.PublicKey)
|
2024-03-11 15:34:06 +01:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2024-03-13 23:46:05 +01:00
|
|
|
json.NewEncoder(w).Encode(
|
|
|
|
proto.ErrJSONResponse{Message: "Error while checking peer existance"},
|
|
|
|
)
|
2024-03-11 15:34:06 +01:00
|
|
|
return
|
|
|
|
}
|
2024-03-13 23:46:05 +01:00
|
|
|
if exists {
|
|
|
|
debugf("Peer %s already exists", reqID, peerRequest.PublicKey)
|
2024-03-19 00:56:52 +01:00
|
|
|
w.Header().Add("Location", fmt.Sprintf("/peer/%s", url.QueryEscape(peerRequest.PublicKey)))
|
|
|
|
w.WriteHeader(http.StatusConflict)
|
2024-03-13 23:46:05 +01:00
|
|
|
return
|
2024-03-11 15:34:06 +01:00
|
|
|
}
|
|
|
|
|
2024-03-13 23:46:05 +01:00
|
|
|
peer, err := wgLink.AddPeer(peerRequest.PublicKey)
|
2024-03-11 15:34:06 +01:00
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2024-03-13 23:46:05 +01:00
|
|
|
json.NewEncoder(w).Encode(
|
|
|
|
proto.ErrJSONResponse{Message: "Error while adding peer"},
|
|
|
|
)
|
2024-03-11 15:34:06 +01:00
|
|
|
return
|
|
|
|
}
|
2024-03-19 00:56:52 +01:00
|
|
|
infof("Allocated IP: %s", reqID, peer.AllowedIPs[0].IP.String())
|
|
|
|
infof("Peer %s added", reqID, peerRequest.PublicKey)
|
|
|
|
w.Header().Set("Content-Type", CONTENT_JSON)
|
2024-03-13 23:46:05 +01:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
json.NewEncoder(w).Encode(
|
|
|
|
proto.WgPeerToPeer(*peer),
|
2024-03-11 15:34:06 +01:00
|
|
|
)
|
2024-03-13 23:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleDeletePeer(wg *WGLink) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqId := r.Context().Value(CtxReqID).(uuid.UUID)
|
|
|
|
pubkey := r.PathValue("pubkey")
|
|
|
|
debugf("DELETE Peer %s", reqId, pubkey)
|
|
|
|
exists, err := wg.Exists(pubkey)
|
2024-03-11 15:34:06 +01:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2024-03-13 23:46:05 +01:00
|
|
|
slog.Error(err.Error())
|
2024-03-11 15:34:06 +01:00
|
|
|
return
|
|
|
|
}
|
2024-03-13 23:46:05 +01:00
|
|
|
if !exists {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
json.NewEncoder(w).Encode(
|
|
|
|
proto.ErrJSONResponse{Message: "Peer does not exist"},
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = wg.DeletePeer(pubkey)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
json.NewEncoder(w).Encode(
|
|
|
|
proto.ErrJSONResponse{Message: "Error while deleting peer"},
|
|
|
|
)
|
|
|
|
slog.Error(err.Error())
|
|
|
|
}
|
2024-03-19 00:56:52 +01:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2024-03-11 15:34:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HttpLogHandler2(h http.Handler) http.Handler {
|
|
|
|
// https://blog.kowalczyk.info/article/e00e89c3841e4f8c8c769a78b8a90b47/logging-http-requests-in-go.html
|
|
|
|
fn := func(w http.ResponseWriter, r* http.Request) {
|
2024-03-13 23:46:05 +01:00
|
|
|
reqID, _ := uuid.NewRandom()
|
|
|
|
rCtx := context.WithValue(r.Context(), CtxReqID, reqID)
|
|
|
|
r = r.WithContext(rCtx)
|
|
|
|
slog.Info(fmt.Sprintf("Starting request with ID: %s", reqID.String()))
|
2024-03-11 15:34:06 +01:00
|
|
|
m := httpsnoop.CaptureMetrics(h, w, r)
|
|
|
|
msg := fmt.Sprintf(
|
2024-03-13 23:46:05 +01:00
|
|
|
"[HTTP] %s %s %s %d %s %s", r.RemoteAddr, r.Method,
|
|
|
|
r.URL.String(),m.Code, m.Duration, reqID.String())
|
2024-03-11 15:34:06 +01:00
|
|
|
slog.Info(msg)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
2024-03-13 23:46:05 +01:00
|
|
|
|
2024-03-19 00:54:24 +01:00
|
|
|
func HttpAuthToken(h http.Handler, token string) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r* http.Request) {
|
|
|
|
if token != "" {
|
|
|
|
if r.Header.Get("Authorization") != token {
|
|
|
|
slog.Debug("Invalid api key")
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
|
2024-03-13 23:46:05 +01:00
|
|
|
func debugf(format string, reqID uuid.UUID, args ...any) {
|
|
|
|
format = format + " " + reqID.String()
|
|
|
|
slog.Debug(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func debug(msg string, reqID uuid.UUID) {
|
|
|
|
msg = msg + " " + reqID.String()
|
|
|
|
debugf("%s", reqID, msg)
|
|
|
|
}
|
2024-03-19 00:54:24 +01:00
|
|
|
|
|
|
|
func infof(format string, reqID uuid.UUID, args ...any) {
|
|
|
|
format = format + " " + reqID.String()
|
|
|
|
slog.Info(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func info(msg string, reqID uuid.UUID) {
|
|
|
|
msg = msg + " " + reqID.String()
|
|
|
|
infof("%s", reqID, msg)
|
|
|
|
}
|