Prevent negtive AddCost, fix addpeer api bug

This commit is contained in:
KusakabeSi 2021-11-06 09:14:13 +00:00
parent 538ce57038
commit a8d5a2a602
6 changed files with 41 additions and 22 deletions

View File

@ -245,11 +245,11 @@ func (device *Device) NewPeer(pk NoisePublicKey, id config.Vertex, isSuper bool)
// map public key
_, ok := device.peers.keyMap[pk]
if ok {
return nil, errors.New("adding existing peer pubkey:" + fmt.Sprint(pk))
return nil, fmt.Errorf("adding existing peer pubkey: %v", pk.ToString())
}
_, ok = device.peers.IDMap[id]
if ok {
return nil, errors.New("adding existing peer id:" + fmt.Sprint(id))
return nil, fmt.Errorf("adding existing peer id: %v", id)
}
peer.ID = id

View File

@ -290,6 +290,7 @@ Parameter:
1. nodeid: Node ID
1. pubkey: Public Key
1. pskey: Pre shared Key
1. additionalcost: Additional cost for packet transfer. Unit: ms
1. nexthoptable: If the `graphrecalculatesetting` of your super node is in static mode, you need to provide a new `NextHopTable` in json format in this parameter.
Return value:
@ -437,6 +438,7 @@ Return value:
1. name: Peer name (displayed on the front end)
1. pubkey: peer public key
1. pskey: preshared key The PSK that this peer connects to this Supernode
1. additionalcost: Additional cost for packet transfer. Unit: ms
## V4 V6 Two Keys
Why we split IPv4 and IPv6 into two session?

View File

@ -296,6 +296,7 @@ curl -X POST "http://127.0.0.1:3000/api/peer/add?Password=passwd_addpeer" \
1. nodeid: Node ID
1. pubkey: Public Key
1. pskey: Preshared Key
1. additionalcost: 此節點進行封包轉發的額外成本。單位: 毫秒
1. nexthoptable: 如果你的super node的`graphrecalculatesetting`是static mode那麼你需要在這提供一張新的`NextHopTable`json格式
返回值:
@ -440,6 +441,7 @@ curl "http://127.0.0.1:3000/api/peer/del?privkey=IJtpnkm9ytbuCukx4VBMENJKuLngo9K
1. name: Peer名稱(顯示在前端)
1. pubkey: peer 公鑰
1. pskey: preshared key 該peer和本Supernode連線的PSK
1. additionalcost: 此節點進行封包轉發的額外成本。單位: 毫秒
## V4 V6 兩個公鑰

View File

@ -389,13 +389,18 @@ func peeradd(w http.ResponseWriter, r *http.Request) { //Waiting for test
}
http_graph.SetNHTable(NewNhTable, [32]byte{})
}
super_peeradd(config.SuperPeerInfo{
err = super_peeradd(config.SuperPeerInfo{
NodeID: NodeID,
Name: Name,
PubKey: PubKey,
PSKey: PSKey,
AdditionalCost: AdditionalCost,
})
if err != nil {
w.WriteHeader(http.StatusExpectationFailed)
w.Write([]byte(fmt.Sprintf("Error creating peer: %v", err)))
return
}
http_sconfig.Peers = append(http_sconfig.Peers, config.SuperPeerInfo{
NodeID: NodeID,
Name: Name,
@ -460,7 +465,7 @@ func peerdel(w http.ResponseWriter, r *http.Request) { //Waiting for test
}
if toDelete == config.Broadcast {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Wrong password"))
w.Write([]byte("Wrong password or private key."))
return
}
@ -477,7 +482,7 @@ func peerdel(w http.ResponseWriter, r *http.Request) { //Waiting for test
configbytes, _ := yaml.Marshal(http_sconfig)
ioutil.WriteFile(http_sconfig_path, configbytes, 0644)
w.WriteHeader(http.StatusOK)
w.Write([]byte(toDelete.ToString() + " deleted."))
w.Write([]byte("Node ID: " + toDelete.ToString() + " deleted."))
return
}

View File

@ -218,7 +218,10 @@ func Super(configPath string, useUAPI bool, printExample bool, bindmode string)
}
for _, peerconf := range sconfig.Peers {
super_peeradd(peerconf)
err := super_peeradd(peerconf)
if err != nil {
return err
}
}
logger4.Verbosef("Device4 started")
logger6.Verbosef("Device6 started")
@ -258,38 +261,42 @@ func Super(configPath string, useUAPI bool, printExample bool, bindmode string)
func super_peeradd(peerconf config.SuperPeerInfo) error {
pk, err := device.Str2PubKey(peerconf.PubKey)
if err != nil {
fmt.Println("Error decode base64 ", err)
return err
return fmt.Errorf("Error decode base64 :%v", err)
}
if peerconf.AdditionalCost < 0 {
return fmt.Errorf("AdditionalCost can't smaller than zero!")
}
if http_sconfig.PrivKeyV4 != "" {
var psk device.NoisePresharedKey
if peerconf.PSKey != "" {
psk, err = device.Str2PSKey(peerconf.PSKey)
if err != nil {
return fmt.Errorf("Error decode base64 :%v", err)
}
}
peer4, err := http_device4.NewPeer(pk, peerconf.NodeID, false)
if err != nil {
fmt.Printf("Error create peer id %v\n", peerconf.NodeID)
return err
return fmt.Errorf("Error create peer id :%v", err)
}
peer4.StaticConn = false
if peerconf.PSKey != "" {
psk, err := device.Str2PSKey(peerconf.PSKey)
if err != nil {
fmt.Println("Error decode base64 ", err)
return err
}
peer4.SetPSK(psk)
}
}
if http_sconfig.PrivKeyV6 != "" {
var psk device.NoisePresharedKey
if peerconf.PSKey != "" {
psk, err = device.Str2PSKey(peerconf.PSKey)
if err != nil {
return fmt.Errorf("Error decode base64 :%v", err)
}
}
peer6, err := http_device6.NewPeer(pk, peerconf.NodeID, false)
if err != nil {
fmt.Printf("Error create peer id %v\n", peerconf.NodeID)
return err
return fmt.Errorf("Error create peer id :%v", err)
}
peer6.StaticConn = false
if peerconf.PSKey != "" {
psk, err := device.Str2PSKey(peerconf.PSKey)
if err != nil {
fmt.Println("Error decode base64 ", err)
return err
}
peer6.SetPSK(psk)
}
}

View File

@ -173,6 +173,9 @@ func (g *IG) RemoveVirt(v config.Vertex, recalculate bool, checkchange bool) (ch
}
func (g *IG) UpdateLatency(u, v config.Vertex, dt time.Duration, additionalCost float64, recalculate bool, checkchange bool) (changed bool) {
if additionalCost < 0 {
additionalCost = 0
}
g.edgelock.Lock()
g.Vert[u] = true
g.Vert[v] = true