2017-05-30 22:36:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-01 21:31:30 +02:00
|
|
|
"net"
|
2017-05-30 22:36:49 +02:00
|
|
|
"sync"
|
2017-06-04 21:48:15 +02:00
|
|
|
"time"
|
2017-05-30 22:36:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Peer struct {
|
2017-06-04 21:48:15 +02:00
|
|
|
mutex sync.RWMutex
|
2017-06-24 15:34:17 +02:00
|
|
|
endpointIP net.IP //
|
|
|
|
endpointPort uint16 //
|
|
|
|
persistentKeepaliveInterval time.Duration // 0 = disabled
|
|
|
|
handshake Handshake
|
|
|
|
device *Device
|
|
|
|
}
|
|
|
|
|
|
|
|
func (device *Device) NewPeer(pk NoisePublicKey) *Peer {
|
|
|
|
var peer Peer
|
|
|
|
|
|
|
|
// map public key
|
|
|
|
|
|
|
|
device.mutex.Lock()
|
|
|
|
device.peers[pk] = &peer
|
|
|
|
device.mutex.Unlock()
|
|
|
|
|
|
|
|
// precompute
|
|
|
|
|
|
|
|
peer.mutex.Lock()
|
|
|
|
peer.device = device
|
|
|
|
func(h *Handshake) {
|
|
|
|
h.mutex.Lock()
|
|
|
|
h.remoteStatic = pk
|
|
|
|
h.precomputedStaticStatic = device.privateKey.sharedSecret(h.remoteStatic)
|
|
|
|
h.mutex.Unlock()
|
|
|
|
}(&peer.handshake)
|
|
|
|
peer.mutex.Unlock()
|
|
|
|
|
|
|
|
return &peer
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|