http guest api

This commit is contained in:
KusakabeSi 2021-08-24 18:16:21 +00:00
parent c4183ca924
commit ad196d3f95
7 changed files with 100 additions and 20 deletions

View File

@ -8,8 +8,6 @@ all: generate-version-and-build
MAKEFLAGS += --no-print-directory
generate-version-and-build:
go mod download && \
go mod tidy && \
@export GIT_CEILING_DIRECTORIES="$(realpath $(CURDIR)/..)" && \
tag="$$(git describe --dirty 2>/dev/null)" && \
ver="$$(printf 'package main\n\nconst Version = "%s"\n' "$$tag")" && \
@ -19,6 +17,8 @@ generate-version-and-build:
@$(MAKE) etherguard-go
etherguard-go: $(wildcard *.go) $(wildcard */*.go)
go mod download && \
go mod tidy && \
go mod vendor && \
patch -p0 -i govpp_remove_crcstring_check.patch && \
go build -v -o "$@"

View File

@ -24,6 +24,7 @@ type SuperConfig struct {
ListenPort int
LogLevel LoggerInfo
RePushConfigInterval float64
statepasswordd string
GraphRecalculateSetting GraphRecalculateSetting
Peers []PeerInfo
}
@ -106,9 +107,7 @@ type HTTP_Peerinfo struct {
PSKey string
Connurl map[string]bool
}
type HTTP_Peers struct {
Peers map[string]HTTP_Peerinfo
}
type HTTP_Peers map[string]HTTP_Peerinfo
const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

View File

@ -262,20 +262,26 @@ func (device *Device) process_UpdatePeerMsg(content path.UpdatePeerMsg) error {
if device.LogControl {
fmt.Println("Download peerinfo from :" + downloadurl)
}
resp, err := http.Get(downloadurl)
client := http.Client{
Timeout: 30 * time.Second,
}
resp, err := client.Get(downloadurl)
if err != nil {
device.log.Errorf(err.Error())
return err
}
defer resp.Body.Close()
allbytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
device.log.Errorf(err.Error())
return err
}
if err := json.Unmarshal(allbytes, &peer_infos); err != nil {
device.log.Errorf(err.Error())
return err
}
for pubkey, peerinfo := range peer_infos.Peers {
for pubkey, peerinfo := range peer_infos {
if len(peerinfo.Connurl) == 0 {
return nil
}
@ -421,7 +427,9 @@ func (device *Device) RoutineRecalculateNhTable() {
return
}
for {
device.graph.RecalculateNhTable(false)
if time.Now().After(device.graph.NhTableExpire) {
device.graph.RecalculateNhTable(false)
}
time.Sleep(device.graph.NodeReportTimeout)
}
}
@ -497,16 +505,22 @@ func (device *Device) process_UpdateNhTableMsg(content path.UpdateNhTableMsg) er
if device.LogControl {
fmt.Println("Download NhTable from :" + downloadurl)
}
resp, err := http.Get(downloadurl)
client := http.Client{
Timeout: 30 * time.Second,
}
resp, err := client.Get(downloadurl)
if err != nil {
device.log.Errorf(err.Error())
return err
}
defer resp.Body.Close()
allbytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
device.log.Errorf(err.Error())
return err
}
if err := json.Unmarshal(allbytes, &NhTable); err != nil {
device.log.Errorf(err.Error())
return err
}
device.graph.SetNHTable(NhTable, content.State_hash)

View File

@ -2,8 +2,11 @@ package main
import (
"bytes"
"encoding/json"
"net"
"strconv"
"sync"
"time"
"net/http"
@ -23,9 +26,20 @@ var (
http_PeerInfoStr []byte
http_PeerState map[string]*PeerState
http_PeerID2Map map[config.Vertex]string
http_PeerInfos config.HTTP_Peers
http_PeerInfos config.HTTP_Peers // nodeID name pubkey, preshared key and more
http_peerinfos sync.Map // map[config.Vertex]string // nodeID and name, for guest visiting
http_StatePWD string
http_StateExpire time.Time
http_StateString []byte
)
type HttpState struct {
PeerInfo map[config.Vertex]string
Edges map[config.Vertex]map[config.Vertex]float64
NhTable config.NextHopTable
Dist config.DistTable
}
type PeerState struct {
NhTableState [32]byte
PeerInfoState [32]byte
@ -59,6 +73,7 @@ func get_peerinfo(w http.ResponseWriter, r *http.Request) {
if bytes.Equal(http_PeerInfo_hash[:], []byte(State)) {
if state := http_PeerState[PubKey]; state != nil {
copy(http_PeerState[PubKey].PeerInfoState[:], State)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(http_PeerInfoStr))
return
@ -87,6 +102,7 @@ func get_nhtable(w http.ResponseWriter, r *http.Request) {
if bytes.Equal(http_NhTable_Hash[:], []byte(State)) {
if state := http_PeerState[PubKey]; state != nil {
copy(http_PeerState[PubKey].NhTableState[:], State)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(http_NhTableStr))
return
@ -96,6 +112,39 @@ func get_nhtable(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Not found"))
}
func get_info(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
PwdA, has := params["Password"]
if !has {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Not found"))
return
}
password := PwdA[0]
if password != http_StatePWD {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Wrong password"))
return
}
if time.Now().After(http_StateExpire) {
hs := HttpState{
PeerInfo: make(map[config.Vertex]string),
NhTable: http_graph.GetNHTable(false),
Edges: http_graph.GetEdges(),
Dist: http_graph.GetDtst(),
}
http_peerinfos.Range(func(key interface{}, value interface{}) bool {
hs.PeerInfo[key.(config.Vertex)] = value.(string)
return true
})
http_StateExpire = time.Now().Add(5 * time.Second)
http_StateString, _ = json.Marshal(hs)
}
w.WriteHeader(http.StatusOK)
w.Write(http_StateString)
return
}
func HttpServer(http_port int, apiprefix string) {
mux := http.NewServeMux()
if apiprefix[0] != '/' {

View File

@ -97,7 +97,7 @@ func Super(configPath string, useUAPI bool, printExample bool) (err error) {
http_PeerState = make(map[string]*PeerState)
http_PeerID2Map = make(map[config.Vertex]string)
http_PeerInfos.Peers = make(map[string]config.HTTP_Peerinfo)
http_PeerInfos = make(map[string]config.HTTP_Peerinfo)
http_HashSalt = []byte(config.RandomStr(32, "Salt generate failed"))
super_chains := path.SUPER_Events{
@ -146,7 +146,7 @@ func Super(configPath string, useUAPI bool, printExample bool) (err error) {
return errors.New(fmt.Sprintf("Invalid Node_id at peer %s\n", peerconf.PubKey))
}
http_PeerID2Map[peerconf.NodeID] = peerconf.PubKey
http_PeerInfos.Peers[peerconf.PubKey] = config.HTTP_Peerinfo{
http_PeerInfos[peerconf.PubKey] = config.HTTP_Peerinfo{
NodeID: peerconf.NodeID,
PubKey: peerconf.PubKey,
PSKey: peerconf.PSKey,
@ -217,15 +217,16 @@ func Event_server_event_hendler(graph *path.IG, events path.SUPER_Events) {
case reg_msg := <-events.Event_server_register:
copy(http_PeerState[http_PeerID2Map[reg_msg.Node_id]].NhTableState[:], reg_msg.NhStateHash[:])
copy(http_PeerState[http_PeerID2Map[reg_msg.Node_id]].PeerInfoState[:], reg_msg.PeerStateHash[:])
http_peerinfos.Store(reg_msg.Node_id, reg_msg.Name)
PubKey := http_PeerID2Map[reg_msg.Node_id]
if peer := http_device4.LookupPeerByStr(PubKey); peer != nil {
if connstr := peer.GetEndpointDstStr(); connstr != "" {
http_PeerInfos.Peers[PubKey].Connurl[connstr] = true
http_PeerInfos[PubKey].Connurl[connstr] = true
}
}
if peer := http_device6.LookupPeerByStr(PubKey); peer != nil {
if connstr := peer.GetEndpointDstStr(); connstr != "" {
http_PeerInfos.Peers[PubKey].Connurl[connstr] = true
http_PeerInfos[PubKey].Connurl[connstr] = true
}
}
http_PeerInfoStr, _ = json.Marshal(&http_PeerInfos)

View File

@ -20,9 +20,10 @@ func GetByte(structIn interface{}) (bb []byte, err error) {
}
type RegisterMsg struct {
Node_id config.Vertex `struc:"uint32"`
Node_id config.Vertex `struc:"uint32"`
PeerStateHash [32]byte
NhStateHash [32]byte
NhStateHash [32]byte
Name string
}
func (c *RegisterMsg) ToString() string {

View File

@ -56,7 +56,7 @@ type IG struct {
dlTable config.DistTable
NhTable config.NextHopTable
NhTableHash [32]byte
nhTableExpire time.Time
NhTableExpire time.Time
IsSuperMode bool
}
@ -124,7 +124,7 @@ func (g *IG) RecalculateNhTable(checkchange bool) (changed bool) {
}
}
g.dlTable, g.NhTable = dist, next
g.nhTableExpire = time.Now().Add(g.NodeReportTimeout)
g.NhTableExpire = time.Now().Add(g.NodeReportTimeout)
g.RecalculateTime = time.Now()
}
return
@ -248,16 +248,32 @@ func Path(u, v config.Vertex, next config.NextHopTable) (path []config.Vertex) {
func (g *IG) SetNHTable(nh config.NextHopTable, table_hash [32]byte) { // set nhTable from supernode
g.NhTable = nh
g.NhTableHash = table_hash
g.nhTableExpire = time.Now().Add(g.SuperNodeInfoTimeout)
g.NhTableExpire = time.Now().Add(g.SuperNodeInfoTimeout)
}
func (g *IG) GetNHTable(checkChange bool) config.NextHopTable {
if time.Now().After(g.nhTableExpire) {
if time.Now().After(g.NhTableExpire) {
g.RecalculateNhTable(checkChange)
}
return g.NhTable
}
func (g *IG) GetDtst() config.DistTable {
return g.dlTable
}
func (g *IG) GetEdges() (edges map[config.Vertex]map[config.Vertex]float64) {
vert := g.Vertices()
edges = make(map[config.Vertex]map[config.Vertex]float64, len(vert))
for src, _ := range vert {
edges[src] = make(map[config.Vertex]float64, len(vert))
for dst, _ := range vert {
edges[src][dst] = g.Weight(src, dst)
}
}
return
}
func (g *IG) GetBoardcastList(id config.Vertex) (tosend map[config.Vertex]bool) {
tosend = make(map[config.Vertex]bool)
for _, element := range g.NhTable[id] {