peermap: add api to query stun servers

This commit is contained in:
rkonfj
2025-03-24 21:40:43 +08:00
parent 17329cb92b
commit 2fcb02c6ba
3 changed files with 22 additions and 8 deletions

View File

@@ -8,6 +8,7 @@ import (
"log/slog"
"net/http"
"runtime/debug"
"strings"
"sync"
"time"
@@ -37,9 +38,10 @@ type ApiV1 struct {
func (a *ApiV1) init() {
a.initOnce.Do(func() {
a.mux.HandleFunc("GET /pg/api/v1/peers", a.handleQueryPeers)
a.mux.HandleFunc("GET /pg/api/v1/psns.json", a.handleDownloadSecret)
a.mux.HandleFunc("GET /pg/api/v1/server_info", a.handleQueryServerInfo)
a.mux.HandleFunc("GET /api/v1/r8/stuns", a.handleQuerySTUNs)
a.mux.HandleFunc("GET /api/v1/r5/peers", a.handleQueryPeers)
a.mux.HandleFunc("GET /api/v1/r5/psns.json", a.handleDownloadSecret)
a.mux.HandleFunc("GET /api/v1/r5/server_info", a.handleQueryServerInfo)
})
}
@@ -51,7 +53,7 @@ func (a *ApiV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
langs.Err(err).MarshalTo(w)
return
}
if !secret.Admin {
if strings.HasPrefix(r.URL.Path, "/api/v1/r5/") && !secret.Admin {
ErrForbidden.MarshalTo(w)
return
}
@@ -65,6 +67,18 @@ func (a *ApiV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.mux.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), contextKey("secret"), secret)))
}
func (a *ApiV1) handleQuerySTUNs(w http.ResponseWriter, r *http.Request) {
var ret []string
for _, stun := range a.Config.STUNs {
if strings.Contains(stun, "://") {
ret = append(ret, stun)
} else {
ret = append(ret, fmt.Sprintf("udp://%s", stun))
}
}
langs.Data[[]string]{Data: ret}.MarshalTo(w)
}
func (a *ApiV1) handleQueryPeers(w http.ResponseWriter, r *http.Request) {
peers, err := a.PeerStore.Peers(r.Context().Value(contextKey("secret")).(auth.JSONSecret).Network)
if err != nil {