api: add node info

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2024-03-15 23:48:22 +03:00
parent b0e8f71f8d
commit c9b5e56f14
Signed by: Hesham
GPG Key ID: 74876157D199B09E
3 changed files with 28 additions and 0 deletions

View File

@ -53,6 +53,7 @@ func run(ctx *cli.Context) {
slog.Debug("Starting run()") slog.Debug("Starting run()")
apiMux := http.NewServeMux() apiMux := http.NewServeMux()
apiMux.HandleFunc("GET /node", hvpnnode3.HandleGetNodeInfo(wgLink))
apiMux.HandleFunc("GET /peer/{pubkey}", hvpnnode3.HandleGetPeer(wgLink)) apiMux.HandleFunc("GET /peer/{pubkey}", hvpnnode3.HandleGetPeer(wgLink))
apiMux.HandleFunc("POST /peer", hvpnnode3.HandlePostPeer(wgLink)) apiMux.HandleFunc("POST /peer", hvpnnode3.HandlePostPeer(wgLink))
apiMux.HandleFunc("DELETE /peer/{pubkey}", hvpnnode3.HandleDeletePeer(wgLink)) apiMux.HandleFunc("DELETE /peer/{pubkey}", hvpnnode3.HandleDeletePeer(wgLink))

View File

@ -17,6 +17,26 @@ import (
type CtxKey string type CtxKey string
const CtxReqID CtxKey = "request_id" 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 { func HandleGetPeer(wgLink *WGLink) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
reqID := r.Context().Value(CtxReqID).(uuid.UUID) reqID := r.Context().Value(CtxReqID).(uuid.UUID)

7
proto/node.go Normal file
View File

@ -0,0 +1,7 @@
package proto
type NodePublicInfo struct {
PublicKey string `json:"public_key"`
UDPPort int `json:"udp_port"`
Endpoint string `json:"endpoint"`
}