From c9b5e56f145b4be34997d7150d060267c056eac7 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Fri, 15 Mar 2024 23:48:22 +0300 Subject: [PATCH] api: add node info Signed-off-by: HeshamTB --- cmd/hvpn-node/hvpn-node.go | 1 + handlers.go | 20 ++++++++++++++++++++ proto/node.go | 7 +++++++ 3 files changed, 28 insertions(+) create mode 100644 proto/node.go diff --git a/cmd/hvpn-node/hvpn-node.go b/cmd/hvpn-node/hvpn-node.go index 815bee6..c1f6772 100644 --- a/cmd/hvpn-node/hvpn-node.go +++ b/cmd/hvpn-node/hvpn-node.go @@ -53,6 +53,7 @@ func run(ctx *cli.Context) { slog.Debug("Starting run()") apiMux := http.NewServeMux() + apiMux.HandleFunc("GET /node", hvpnnode3.HandleGetNodeInfo(wgLink)) apiMux.HandleFunc("GET /peer/{pubkey}", hvpnnode3.HandleGetPeer(wgLink)) apiMux.HandleFunc("POST /peer", hvpnnode3.HandlePostPeer(wgLink)) apiMux.HandleFunc("DELETE /peer/{pubkey}", hvpnnode3.HandleDeletePeer(wgLink)) diff --git a/handlers.go b/handlers.go index 5c26223..368447d 100644 --- a/handlers.go +++ b/handlers.go @@ -17,6 +17,26 @@ import ( type CtxKey string const CtxReqID CtxKey = "request_id" +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, + // TODO: Send endpoint for clients to connect + }, + ) + } +} + func HandleGetPeer(wgLink *WGLink) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { reqID := r.Context().Value(CtxReqID).(uuid.UUID) diff --git a/proto/node.go b/proto/node.go new file mode 100644 index 0000000..a6bed7d --- /dev/null +++ b/proto/node.go @@ -0,0 +1,7 @@ +package proto + +type NodePublicInfo struct { + PublicKey string `json:"public_key"` + UDPPort int `json:"udp_port"` + Endpoint string `json:"endpoint"` +}