mirror of
https://github.com/yggdrasil-network/yggdrasil-go
synced 2024-11-10 07:20:39 +03:00
Handle AllowedEncryptionPublicKeys internally
This commit is contained in:
parent
4b6c925cb4
commit
fdf300a1ff
@ -227,11 +227,6 @@ func main() {
|
|||||||
logger.Println("An error occurred during startup")
|
logger.Println("An error occurred during startup")
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
// Check to see if any allowed encryption keys were provided in the config.
|
|
||||||
// If they were then set them now.
|
|
||||||
for _, pBoxStr := range cfg.AllowedEncryptionPublicKeys {
|
|
||||||
n.core.AddAllowedEncryptionPublicKey(pBoxStr)
|
|
||||||
}
|
|
||||||
// The Stop function ensures that the TUN/TAP adapter is correctly shut down
|
// The Stop function ensures that the TUN/TAP adapter is correctly shut down
|
||||||
// before the program exits.
|
// before the program exits.
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -765,35 +765,20 @@ func (a *admin) getData_getSessions() []admin_nodeInfo {
|
|||||||
|
|
||||||
// getAllowedEncryptionPublicKeys returns the public keys permitted for incoming peer connections.
|
// getAllowedEncryptionPublicKeys returns the public keys permitted for incoming peer connections.
|
||||||
func (a *admin) getAllowedEncryptionPublicKeys() []string {
|
func (a *admin) getAllowedEncryptionPublicKeys() []string {
|
||||||
pubs := a.core.peers.getAllowedEncryptionPublicKeys()
|
return a.core.peers.getAllowedEncryptionPublicKeys()
|
||||||
var out []string
|
|
||||||
for _, pub := range pubs {
|
|
||||||
out = append(out, hex.EncodeToString(pub[:]))
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// addAllowedEncryptionPublicKey whitelists a key for incoming peer connections.
|
// addAllowedEncryptionPublicKey whitelists a key for incoming peer connections.
|
||||||
func (a *admin) addAllowedEncryptionPublicKey(bstr string) (err error) {
|
func (a *admin) addAllowedEncryptionPublicKey(bstr string) (err error) {
|
||||||
boxBytes, err := hex.DecodeString(bstr)
|
a.core.peers.addAllowedEncryptionPublicKey(bstr)
|
||||||
if err == nil {
|
return nil
|
||||||
var box crypto.BoxPubKey
|
|
||||||
copy(box[:], boxBytes)
|
|
||||||
a.core.peers.addAllowedEncryptionPublicKey(&box)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// removeAllowedEncryptionPublicKey removes a key from the whitelist for incoming peer connections.
|
// removeAllowedEncryptionPublicKey removes a key from the whitelist for incoming peer connections.
|
||||||
// If none are set, an empty list permits all incoming connections.
|
// If none are set, an empty list permits all incoming connections.
|
||||||
func (a *admin) removeAllowedEncryptionPublicKey(bstr string) (err error) {
|
func (a *admin) removeAllowedEncryptionPublicKey(bstr string) (err error) {
|
||||||
boxBytes, err := hex.DecodeString(bstr)
|
a.core.peers.removeAllowedEncryptionPublicKey(bstr)
|
||||||
if err == nil {
|
return nil
|
||||||
var box crypto.BoxPubKey
|
|
||||||
copy(box[:], boxBytes)
|
|
||||||
a.core.peers.removeAllowedEncryptionPublicKey(&box)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a DHT ping to the node with the provided key and coords, optionally looking up the specified target NodeID.
|
// Send a DHT ping to the node with the provided key and coords, optionally looking up the specified target NodeID.
|
||||||
|
@ -5,6 +5,7 @@ package yggdrasil
|
|||||||
// Live code should be better commented
|
// Live code should be better commented
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/hex"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@ -22,8 +23,6 @@ type peers struct {
|
|||||||
reconfigure chan chan error
|
reconfigure chan chan error
|
||||||
mutex sync.Mutex // Synchronize writes to atomic
|
mutex sync.Mutex // Synchronize writes to atomic
|
||||||
ports atomic.Value //map[switchPort]*peer, use CoW semantics
|
ports atomic.Value //map[switchPort]*peer, use CoW semantics
|
||||||
authMutex sync.RWMutex
|
|
||||||
allowedEncryptionPublicKeys map[crypto.BoxPubKey]struct{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initializes the peers struct.
|
// Initializes the peers struct.
|
||||||
@ -39,40 +38,48 @@ func (ps *peers) init(c *Core) {
|
|||||||
e <- nil
|
e <- nil
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
ps.allowedEncryptionPublicKeys = make(map[crypto.BoxPubKey]struct{})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if an incoming peer connection to a key is allowed, either because the key is in the whitelist or because the whitelist is empty.
|
// Returns true if an incoming peer connection to a key is allowed, either
|
||||||
|
// because the key is in the whitelist or because the whitelist is empty.
|
||||||
func (ps *peers) isAllowedEncryptionPublicKey(box *crypto.BoxPubKey) bool {
|
func (ps *peers) isAllowedEncryptionPublicKey(box *crypto.BoxPubKey) bool {
|
||||||
ps.authMutex.RLock()
|
boxstr := hex.EncodeToString(box[:])
|
||||||
defer ps.authMutex.RUnlock()
|
ps.core.configMutex.RLock()
|
||||||
_, isIn := ps.allowedEncryptionPublicKeys[*box]
|
defer ps.core.configMutex.RUnlock()
|
||||||
return isIn || len(ps.allowedEncryptionPublicKeys) == 0
|
for _, v := range ps.core.config.AllowedEncryptionPublicKeys {
|
||||||
|
if v == boxstr {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return len(ps.core.config.AllowedEncryptionPublicKeys) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a key to the whitelist.
|
// Adds a key to the whitelist.
|
||||||
func (ps *peers) addAllowedEncryptionPublicKey(box *crypto.BoxPubKey) {
|
func (ps *peers) addAllowedEncryptionPublicKey(box string) {
|
||||||
ps.authMutex.Lock()
|
ps.core.configMutex.RLock()
|
||||||
defer ps.authMutex.Unlock()
|
defer ps.core.configMutex.RUnlock()
|
||||||
ps.allowedEncryptionPublicKeys[*box] = struct{}{}
|
ps.core.config.AllowedEncryptionPublicKeys =
|
||||||
|
append(ps.core.config.AllowedEncryptionPublicKeys, box)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes a key from the whitelist.
|
// Removes a key from the whitelist.
|
||||||
func (ps *peers) removeAllowedEncryptionPublicKey(box *crypto.BoxPubKey) {
|
func (ps *peers) removeAllowedEncryptionPublicKey(box string) {
|
||||||
ps.authMutex.Lock()
|
ps.core.configMutex.RLock()
|
||||||
defer ps.authMutex.Unlock()
|
defer ps.core.configMutex.RUnlock()
|
||||||
delete(ps.allowedEncryptionPublicKeys, *box)
|
for k, v := range ps.core.config.AllowedEncryptionPublicKeys {
|
||||||
|
if v == box {
|
||||||
|
ps.core.config.AllowedEncryptionPublicKeys =
|
||||||
|
append(ps.core.config.AllowedEncryptionPublicKeys[:k],
|
||||||
|
ps.core.config.AllowedEncryptionPublicKeys[k+1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the whitelist of allowed keys for incoming connections.
|
// Gets the whitelist of allowed keys for incoming connections.
|
||||||
func (ps *peers) getAllowedEncryptionPublicKeys() []crypto.BoxPubKey {
|
func (ps *peers) getAllowedEncryptionPublicKeys() []string {
|
||||||
ps.authMutex.RLock()
|
ps.core.configMutex.RLock()
|
||||||
defer ps.authMutex.RUnlock()
|
defer ps.core.configMutex.RUnlock()
|
||||||
keys := make([]crypto.BoxPubKey, 0, len(ps.allowedEncryptionPublicKeys))
|
return ps.core.config.AllowedEncryptionPublicKeys
|
||||||
for key := range ps.allowedEncryptionPublicKeys {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
return keys
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Atomically gets a map[switchPort]*peer of known peers.
|
// Atomically gets a map[switchPort]*peer of known peers.
|
||||||
|
Loading…
Reference in New Issue
Block a user