Files
indra/pkg/relay/introductions.go
2023-02-27 18:54:20 +00:00

174 lines
4.5 KiB
Go

package relay
import (
"sync"
"github.com/cybriq/qu"
"git-indra.lan/indra-labs/indra/pkg/crypto/key/pub"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/messages/intro"
"git-indra.lan/indra-labs/indra/pkg/types"
"git-indra.lan/indra-labs/indra/pkg/util/cryptorand"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
)
type Intros map[pub.Bytes]slice.Bytes
type NotifiedIntroducers map[pub.Bytes][]nonce.ID
type KnownIntros map[pub.Bytes]*intro.Layer
// Introductions is a map of existing known hidden service keys and the
// routing header for requesting a new one on behalf of the client.
//
// After a header is retrieved, the relay sends back a request to the hidden
// service using the headers in this store with the provided public key from the
// client which is then used to encrypt the provided header and prevent the
// introducing relay from also using the provided header.
type Introductions struct {
sync.Mutex
Intros
NotifiedIntroducers
KnownIntros
}
func NewIntroductions() *Introductions {
return &Introductions{Intros: make(Intros),
NotifiedIntroducers: make(NotifiedIntroducers),
KnownIntros: make(KnownIntros)}
}
func (in *Introductions) Find(key pub.Bytes) (header slice.Bytes) {
in.Lock()
var ok bool
if header, ok = in.Intros[key]; ok {
}
in.Unlock()
return
}
func (in *Introductions) Delete(key pub.Bytes) (header slice.Bytes) {
in.Lock()
var ok bool
if header, ok = in.Intros[key]; ok {
// If found, the header is not to be used again.
delete(in.Intros, key)
}
in.Unlock()
return
}
func (in *Introductions) AddIntro(pk *pub.Key, header slice.Bytes) {
in.Lock()
var ok bool
key := pk.ToBytes()
if _, ok = in.Intros[key]; ok {
log.D.Ln("entry already exists for key %x", key)
} else {
in.Intros[key] = header
in.NotifiedIntroducers[key] = []nonce.ID{}
}
in.Unlock()
}
func (in *Introductions) AddNotified(nodeID nonce.ID, ident pub.Bytes) {
in.Lock()
var ok bool
if _, ok = in.NotifiedIntroducers[ident]; ok {
in.NotifiedIntroducers[ident] = append(in.NotifiedIntroducers[ident],
nodeID)
} else {
in.NotifiedIntroducers[ident] = []nonce.ID{nodeID}
}
in.Unlock()
}
func (eng *Engine) SendIntro(id nonce.ID, target *Session, intr *intro.Layer,
hook func(id nonce.ID, b slice.Bytes)) {
hops := []byte{0, 1, 2, 3, 4, 5}
s := make(Sessions, len(hops))
s[2] = target
se := eng.SelectHops(hops, s)
var c Circuit
copy(c[:], se)
o := HiddenService(id, intr, se[len(se)-1], c, eng.KeySet)
log.D.Ln("sending out intro onion")
res := eng.PostAcctOnion(o)
eng.SendWithOneHook(c[0].AddrPort, res, hook)
}
func (eng *Engine) introductionBroadcaster(intr *intro.Layer) {
log.D.F("propagating hidden service introduction for %x", intr.Key.ToBytes())
done := qu.T()
msg := make(slice.Bytes, intro.Len)
c := slice.NewCursor()
intr.Encode(msg, c)
nPeers := eng.NodesLen()
peerIndices := make([]int, nPeers)
for i := 1; i < nPeers; i++ {
peerIndices[i] = i
}
cryptorand.Shuffle(nPeers, func(i, j int) {
peerIndices[i], peerIndices[j] = peerIndices[j], peerIndices[i]
})
// Since relays will also gossip this information, we will start a ticker
// that sends out the hidden service introduction once a second until it
// runs out of known relays to gossip to.
var cursor int
for {
select {
case <-eng.C.Wait():
return
case <-done:
return
default:
}
n := eng.FindNodeByIndex(peerIndices[cursor])
n.Transport.Send(msg)
cursor++
if cursor > len(peerIndices)-1 {
break
}
}
log.T.Ln("finished broadcasting intro")
}
func (eng *Engine) intro(intr *intro.Layer, b slice.Bytes,
c *slice.Cursor, prev types.Onion) {
eng.Introductions.Lock()
if intr.Validate() {
if _, ok := eng.Introductions.KnownIntros[intr.Key.ToBytes()]; ok {
log.T.Ln("received intro we already know about")
eng.Introductions.Unlock()
return
}
log.T.F("storing intro for %s", intr.Key.ToBase32())
eng.Introductions.KnownIntros[intr.Key.ToBytes()] = intr
log.D.F("%s sending out intro to %s at %s to all known peers",
eng.GetLocalNodeAddress(), intr.Key.ToBase32(),
intr.AddrPort.String())
sender := eng.SessionManager.FindNodeByAddrPort(intr.AddrPort)
nodes := make(map[nonce.ID]*Node)
eng.SessionManager.ForEachNode(func(n *Node) bool {
if n.ID != sender.ID {
nodes[n.ID] = n
}
return false
})
counter := 0
for i := range nodes {
log.T.F("sending intro to %s", nodes[i].AddrPort.String())
nodes[i].Transport.Send(b)
counter++
if counter < 2 {
continue
}
break
}
eng.Introductions.Unlock()
}
}