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"` +}