mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
fix code after moving address/crypto/util
This commit is contained in:
parent
2c68d41409
commit
ea4ca02681
18 changed files with 469 additions and 421 deletions
|
@ -8,6 +8,9 @@ import (
|
|||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
||||
)
|
||||
|
||||
// The peers struct represents peers with an active connection.
|
||||
|
@ -19,7 +22,7 @@ type peers struct {
|
|||
mutex sync.Mutex // Synchronize writes to atomic
|
||||
ports atomic.Value //map[switchPort]*peer, use CoW semantics
|
||||
authMutex sync.RWMutex
|
||||
allowedEncryptionPublicKeys map[boxPubKey]struct{}
|
||||
allowedEncryptionPublicKeys map[crypto.BoxPubKey]struct{}
|
||||
}
|
||||
|
||||
// Initializes the peers struct.
|
||||
|
@ -28,11 +31,11 @@ func (ps *peers) init(c *Core) {
|
|||
defer ps.mutex.Unlock()
|
||||
ps.putPorts(make(map[switchPort]*peer))
|
||||
ps.core = c
|
||||
ps.allowedEncryptionPublicKeys = make(map[boxPubKey]struct{})
|
||||
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.
|
||||
func (ps *peers) isAllowedEncryptionPublicKey(box *boxPubKey) bool {
|
||||
func (ps *peers) isAllowedEncryptionPublicKey(box *crypto.BoxPubKey) bool {
|
||||
ps.authMutex.RLock()
|
||||
defer ps.authMutex.RUnlock()
|
||||
_, isIn := ps.allowedEncryptionPublicKeys[*box]
|
||||
|
@ -40,24 +43,24 @@ func (ps *peers) isAllowedEncryptionPublicKey(box *boxPubKey) bool {
|
|||
}
|
||||
|
||||
// Adds a key to the whitelist.
|
||||
func (ps *peers) addAllowedEncryptionPublicKey(box *boxPubKey) {
|
||||
func (ps *peers) addAllowedEncryptionPublicKey(box *crypto.BoxPubKey) {
|
||||
ps.authMutex.Lock()
|
||||
defer ps.authMutex.Unlock()
|
||||
ps.allowedEncryptionPublicKeys[*box] = struct{}{}
|
||||
}
|
||||
|
||||
// Removes a key from the whitelist.
|
||||
func (ps *peers) removeAllowedEncryptionPublicKey(box *boxPubKey) {
|
||||
func (ps *peers) removeAllowedEncryptionPublicKey(box *crypto.BoxPubKey) {
|
||||
ps.authMutex.Lock()
|
||||
defer ps.authMutex.Unlock()
|
||||
delete(ps.allowedEncryptionPublicKeys, *box)
|
||||
}
|
||||
|
||||
// Gets the whitelist of allowed keys for incoming connections.
|
||||
func (ps *peers) getAllowedEncryptionPublicKeys() []boxPubKey {
|
||||
func (ps *peers) getAllowedEncryptionPublicKeys() []crypto.BoxPubKey {
|
||||
ps.authMutex.RLock()
|
||||
defer ps.authMutex.RUnlock()
|
||||
keys := make([]boxPubKey, 0, len(ps.allowedEncryptionPublicKeys))
|
||||
keys := make([]crypto.BoxPubKey, 0, len(ps.allowedEncryptionPublicKeys))
|
||||
for key := range ps.allowedEncryptionPublicKeys {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
@ -81,10 +84,10 @@ type peer struct {
|
|||
// BUG: sync/atomic, 32 bit platforms need the above to be the first element
|
||||
core *Core
|
||||
port switchPort
|
||||
box boxPubKey
|
||||
sig sigPubKey
|
||||
shared boxSharedKey
|
||||
linkShared boxSharedKey
|
||||
box crypto.BoxPubKey
|
||||
sig crypto.SigPubKey
|
||||
shared crypto.BoxSharedKey
|
||||
linkShared crypto.BoxSharedKey
|
||||
endpoint string
|
||||
friendlyName string
|
||||
firstSeen time.Time // To track uptime for getPeers
|
||||
|
@ -96,11 +99,11 @@ type peer struct {
|
|||
}
|
||||
|
||||
// Creates a new peer with the specified box, sig, and linkShared keys, using the lowest unocupied port number.
|
||||
func (ps *peers) newPeer(box *boxPubKey, sig *sigPubKey, linkShared *boxSharedKey, endpoint string) *peer {
|
||||
func (ps *peers) newPeer(box *crypto.BoxPubKey, sig *crypto.SigPubKey, linkShared *crypto.BoxSharedKey, endpoint string) *peer {
|
||||
now := time.Now()
|
||||
p := peer{box: *box,
|
||||
sig: *sig,
|
||||
shared: *getSharedKey(&ps.core.boxPriv, box),
|
||||
shared: *crypto.GetSharedKey(&ps.core.boxPriv, box),
|
||||
linkShared: *linkShared,
|
||||
endpoint: endpoint,
|
||||
firstSeen: now,
|
||||
|
@ -212,7 +215,7 @@ func (p *peer) handlePacket(packet []byte) {
|
|||
case wire_LinkProtocolTraffic:
|
||||
p.handleLinkTraffic(packet)
|
||||
default:
|
||||
util_putBytes(packet)
|
||||
util.PutBytes(packet)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -236,13 +239,13 @@ func (p *peer) sendPacket(packet []byte) {
|
|||
// This wraps the packet in the inner (ephemeral) and outer (permanent) crypto layers.
|
||||
// It sends it to p.linkOut, which bypasses the usual packet queues.
|
||||
func (p *peer) sendLinkPacket(packet []byte) {
|
||||
innerPayload, innerNonce := boxSeal(&p.linkShared, packet, nil)
|
||||
innerPayload, innerNonce := crypto.BoxSeal(&p.linkShared, packet, nil)
|
||||
innerLinkPacket := wire_linkProtoTrafficPacket{
|
||||
Nonce: *innerNonce,
|
||||
Payload: innerPayload,
|
||||
}
|
||||
outerPayload := innerLinkPacket.encode()
|
||||
bs, nonce := boxSeal(&p.shared, outerPayload, nil)
|
||||
bs, nonce := crypto.BoxSeal(&p.shared, outerPayload, nil)
|
||||
linkPacket := wire_linkProtoTrafficPacket{
|
||||
Nonce: *nonce,
|
||||
Payload: bs,
|
||||
|
@ -258,7 +261,7 @@ func (p *peer) handleLinkTraffic(bs []byte) {
|
|||
if !packet.decode(bs) {
|
||||
return
|
||||
}
|
||||
outerPayload, isOK := boxOpen(&p.shared, packet.Payload, &packet.Nonce)
|
||||
outerPayload, isOK := crypto.BoxOpen(&p.shared, packet.Payload, &packet.Nonce)
|
||||
if !isOK {
|
||||
return
|
||||
}
|
||||
|
@ -266,7 +269,7 @@ func (p *peer) handleLinkTraffic(bs []byte) {
|
|||
if !innerPacket.decode(outerPayload) {
|
||||
return
|
||||
}
|
||||
payload, isOK := boxOpen(&p.linkShared, innerPacket.Payload, &innerPacket.Nonce)
|
||||
payload, isOK := crypto.BoxOpen(&p.linkShared, innerPacket.Payload, &innerPacket.Nonce)
|
||||
if !isOK {
|
||||
return
|
||||
}
|
||||
|
@ -278,7 +281,7 @@ func (p *peer) handleLinkTraffic(bs []byte) {
|
|||
case wire_SwitchMsg:
|
||||
p.handleSwitchMsg(payload)
|
||||
default:
|
||||
util_putBytes(bs)
|
||||
util.PutBytes(bs)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -292,7 +295,7 @@ func (p *peer) sendSwitchMsg() {
|
|||
msg.Hops = append(msg.Hops, switchMsgHop{
|
||||
Port: p.port,
|
||||
Next: p.sig,
|
||||
Sig: *sign(&p.core.sigPriv, bs),
|
||||
Sig: *crypto.Sign(&p.core.sigPriv, bs),
|
||||
})
|
||||
packet := msg.encode()
|
||||
p.sendLinkPacket(packet)
|
||||
|
@ -316,7 +319,7 @@ func (p *peer) handleSwitchMsg(packet []byte) {
|
|||
sigMsg.Hops = msg.Hops[:idx]
|
||||
loc.coords = append(loc.coords, hop.Port)
|
||||
bs := getBytesForSig(&hop.Next, &sigMsg)
|
||||
if !verify(&prevKey, bs, &hop.Sig) {
|
||||
if !crypto.Verify(&prevKey, bs, &hop.Sig) {
|
||||
p.core.peers.removePeer(p.port)
|
||||
}
|
||||
prevKey = hop.Next
|
||||
|
@ -341,7 +344,7 @@ func (p *peer) handleSwitchMsg(packet []byte) {
|
|||
|
||||
// This generates the bytes that we sign or check the signature of for a switchMsg.
|
||||
// It begins with the next node's key, followed by the root and the timetsamp, followed by coords being advertised to the next node.
|
||||
func getBytesForSig(next *sigPubKey, msg *switchMsg) []byte {
|
||||
func getBytesForSig(next *crypto.SigPubKey, msg *switchMsg) []byte {
|
||||
var loc switchLocator
|
||||
for _, hop := range msg.Hops {
|
||||
loc.coords = append(loc.coords, hop.Port)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue