mirror of
https://github.com/sigcn/pg.git
synced 2025-11-03 09:10:48 +08:00
peermap: add api to query stun servers
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -788,7 +788,7 @@ func New(cfg config.Config) (*PeerMap, error) {
|
||||
|
||||
mux := http.NewServeMux()
|
||||
pm.httpServer = &http.Server{Handler: mux, Addr: cfg.Listen}
|
||||
mux.Handle("/pg/api/v1/", &api.ApiV1{Config: cfg, Auth: pm.authenticator, Grant: pm.Grant, PeerStore: &pm})
|
||||
mux.Handle("/api/v1/", &api.ApiV1{Config: cfg, Auth: pm.authenticator, Grant: pm.Grant, PeerStore: &pm})
|
||||
mux.HandleFunc("/", ui.HandleStaticFiles)
|
||||
mux.HandleFunc("GET /pg", pm.HandlePeerPacketConnect)
|
||||
mux.HandleFunc("GET /pg/networks", pm.HandleQueryNetworks)
|
||||
|
||||
@@ -7,7 +7,7 @@ const peers = ref([])
|
||||
const serverInfo = ref()
|
||||
|
||||
const loadPeers = async () => {
|
||||
let r = await http.get('/pg/api/v1/peers', { session: session.value })
|
||||
let r = await http.get('/api/v1/r5/peers', { session: session.value })
|
||||
if (r.code != 0) {
|
||||
alert(r.msg)
|
||||
return
|
||||
@@ -21,7 +21,7 @@ const loadPeers = async () => {
|
||||
}
|
||||
|
||||
const loadServerInfo = async () => {
|
||||
let r = await http.get('/pg/api/v1/server_info', { session: session.value })
|
||||
let r = await http.get('/api/v1/r5/server_info', { session: session.value })
|
||||
if (r.code != 0) {
|
||||
alert(r.msg)
|
||||
return
|
||||
@@ -36,7 +36,7 @@ const signout = () => {
|
||||
}
|
||||
|
||||
const downloadSecret = async () => {
|
||||
let r = await http.download('/pg/api/v1/psns.json', { session: session.value })
|
||||
let r = await http.download('/api/v1/r5/psns.json', { session: session.value })
|
||||
if (r.code != 0) {
|
||||
alert(r.msg)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user