mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
webapi: New function /status/peers to return a list of all peers, including GeoIP info
This commit is contained in:
@@ -70,6 +70,7 @@ func Start(Backend *core.Backend, ListenAddresses []string, UseSSL bool, Certifi
|
||||
|
||||
api.Router.HandleFunc("/test", apiTest).Methods("GET")
|
||||
api.Router.HandleFunc("/status", api.apiStatus).Methods("GET")
|
||||
api.Router.HandleFunc("/status/peers", api.apiStatusPeers).Methods("GET")
|
||||
api.Router.HandleFunc("/account/info", api.apiAccountInfo).Methods("GET")
|
||||
api.Router.HandleFunc("/account/delete", api.apiAccountDelete).Methods("GET")
|
||||
api.Router.HandleFunc("/blockchain/header", api.apiBlockchainHeaderFunc).Methods("GET")
|
||||
|
||||
@@ -8,6 +8,7 @@ package webapi
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
@@ -67,7 +68,8 @@ func (api *WebapiInstance) apiAccountInfo(w http.ResponseWriter, r *http.Request
|
||||
apiAccountDelete deletes the current account. The confirm parameter must include the user's choice.
|
||||
Request: GET /account/delete?confirm=[0 or 1]
|
||||
Result: 204 if the user choses not to delete the account
|
||||
200 if successfully deleted
|
||||
|
||||
200 if successfully deleted
|
||||
*/
|
||||
func (api *WebapiInstance) apiAccountDelete(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
@@ -80,3 +82,31 @@ func (api *WebapiInstance) apiAccountDelete(w http.ResponseWriter, r *http.Reque
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
/*
|
||||
apiStatusPeers returns the information about peers currently connected
|
||||
Request: GET /status/peers
|
||||
Result: 200 with JSON array apiResponsePeerInfo
|
||||
*/
|
||||
func (api *WebapiInstance) apiStatusPeers(w http.ResponseWriter, r *http.Request) {
|
||||
var peers []apiResponsePeerInfo
|
||||
|
||||
// query all nodes
|
||||
for _, peer := range api.backend.PeerlistGet() {
|
||||
peerInfo := apiResponsePeerInfo{PeerID: hex.EncodeToString(peer.PublicKey.SerializeCompressed()), NodeID: hex.EncodeToString(peer.NodeID)}
|
||||
|
||||
if latitude, longitude, valid := api.Peer2GeoIP(peer); valid {
|
||||
peerInfo.GeoIP = fmt.Sprintf("%.4f", latitude) + "," + fmt.Sprintf("%.4f", longitude)
|
||||
}
|
||||
|
||||
peers = append(peers, peerInfo)
|
||||
}
|
||||
|
||||
EncodeJSON(api.backend, w, r, peers)
|
||||
}
|
||||
|
||||
type apiResponsePeerInfo struct {
|
||||
PeerID string `json:"peerid"` // Peer ID. This is derived from the public in compressed form.
|
||||
NodeID string `json:"nodeid"` // Node ID. This is the blake3 hash of the peer ID and used in the DHT.
|
||||
GeoIP string `json:"geopi"` // GeoIP location as "Latitude,Longitude" CSV format. Empty if location not available.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user