sends out introduction to peers

This commit is contained in:
херетик
2023-02-27 09:00:51 +00:00
parent 2d91a0e7c0
commit 829d387b40
10 changed files with 45 additions and 23 deletions

View File

@@ -7,7 +7,7 @@ package sig
import ( import (
"github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa" "github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
"git-indra.lan/indra-labs/indra" "git-indra.lan/indra-labs/indra"
"git-indra.lan/indra-labs/indra/pkg/crypto/key/prv" "git-indra.lan/indra-labs/indra/pkg/crypto/key/prv"
"git-indra.lan/indra-labs/indra/pkg/crypto/key/pub" "git-indra.lan/indra-labs/indra/pkg/crypto/key/pub"
@@ -21,13 +21,11 @@ var (
) )
// Len is the length of the signatures used in Indra, compact keys that can have // Len is the length of the signatures used in Indra, compact keys that can have
// the public key extracted from them, thus eliminating the need to separately // the public key extracted from them.
// specify it in messages.
const Len = 65 const Len = 65
// Bytes is an ECDSA BIP62 formatted compact signature which allows the recovery // Bytes is an ECDSA BIP62 formatted compact signature which allows the recovery
// of the public key from the signature. This allows messages to avoid adding // of the public key from the signature.
// extra bytes to also specify the public key of the signer.
type Bytes [Len]byte type Bytes [Len]byte
// Sign produces an ECDSA BIP62 compact signature. // Sign produces an ECDSA BIP62 compact signature.

View File

@@ -65,9 +65,9 @@ func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
} }
splice.Splice(b, c). splice.Splice(b, c).
ReadID(&x.ID). ReadID(&x.ID).
ReadPubkey(&x.Key). ReadPubkey(&x.Layer.Key).
ReadAddrPort(&x.AddrPort). ReadAddrPort(&x.Layer.AddrPort).
ReadSignature(x.Bytes). ReadSignature(&x.Layer.Bytes).
ReadHash(&x.Ciphers[0]).ReadHash(&x.Ciphers[1]).ReadHash(&x.Ciphers[2]). ReadHash(&x.Ciphers[0]).ReadHash(&x.Ciphers[1]).ReadHash(&x.Ciphers[2]).
ReadIV(&x.Nonces[0]).ReadIV(&x.Nonces[1]).ReadIV(&x.Nonces[2]) ReadIV(&x.Nonces[0]).ReadIV(&x.Nonces[1]).ReadIV(&x.Nonces[2])
return return

View File

@@ -32,12 +32,12 @@ var (
) )
type Layer struct { type Layer struct {
*pub.Key Key *pub.Key
*netip.AddrPort AddrPort *netip.AddrPort
sig.Bytes Bytes sig.Bytes
} }
func New(key *prv.Key, ap *netip.AddrPort) (im *Layer) { func New(key *prv.Key, ap *netip.AddrPort) (in *Layer) {
pk := pub.Derive(key) pk := pub.Derive(key)
bap, _ := ap.MarshalBinary() bap, _ := ap.MarshalBinary()
pkb := pk.ToBytes() pkb := pk.ToBytes()
@@ -47,7 +47,7 @@ func New(key *prv.Key, ap *netip.AddrPort) (im *Layer) {
if sign, e = sig.Sign(key, hash); check(e) { if sign, e = sig.Sign(key, hash); check(e) {
return nil return nil
} }
im = &Layer{ in = &Layer{
Key: pk, Key: pk,
AddrPort: ap, AddrPort: ap,
Bytes: sign, Bytes: sign,
@@ -85,6 +85,7 @@ func (im *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
func (im *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) { func (im *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
splice.Splice(b, c). splice.Splice(b, c).
ReadPubkey(&im.Key). ReadPubkey(&im.Key).
ReadAddrPort(&im.AddrPort).ReadSignature(im.Bytes) ReadAddrPort(&im.AddrPort).
ReadSignature(&im.Bytes)
return return
} }

View File

@@ -0,0 +1,22 @@
package intro
import (
"testing"
"git-indra.lan/indra-labs/indra/pkg/crypto/key/prv"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
)
func TestLayer_Validate(t *testing.T) {
addr := slice.GenerateRandomAddrPortIPv4()
var e error
var idPrv *prv.Key
if idPrv, e = prv.GenerateKey(); check(e) {
return
}
im := New(idPrv, addr)
log.I.S(im)
if !im.Validate() {
t.Error("failed to validate")
}
}

View File

@@ -302,6 +302,7 @@ func TestClient_HiddenService(t *testing.T) {
v.Shutdown() v.Shutdown()
} }
} }
func TestClient_HiddenServiceBroadcast(t *testing.T) { func TestClient_HiddenServiceBroadcast(t *testing.T) {
log2.SetLogLevel(log2.Info) log2.SetLogLevel(log2.Info)
var clients []*Engine var clients []*Engine

View File

@@ -13,5 +13,5 @@ func (eng *Engine) hiddenservice(hs *hiddenservice.Layer, b slice.Bytes,
hs.Layer.Key.ToBase32()) hs.Layer.Key.ToBase32())
eng.Introductions.AddIntro(hs.Layer.Key, b[*c:]) eng.Introductions.AddIntro(hs.Layer.Key, b[*c:])
log.I.Ln("stored new introduction, starting broadcast") log.I.Ln("stored new introduction, starting broadcast")
go eng.hiddenserviceBroadcaster(hs.Layer.Key) go eng.hiddenserviceBroadcaster(&hs.Layer)
} }

View File

@@ -9,6 +9,8 @@ import (
func (eng *Engine) intro(intr *intro.Layer, b slice.Bytes, func (eng *Engine) intro(intr *intro.Layer, b slice.Bytes,
c *slice.Cursor, prev types.Onion) { c *slice.Cursor, prev types.Onion) {
log.D.F("sending out intro to %s at %s to all known peers", if intr.Validate() {
intr.Key.ToBase32(), intr.AddrPort.String()) log.D.F("sending out intro to %s at %s to all known peers",
intr.Key.ToBase32(), intr.AddrPort.String())
}
} }

View File

@@ -9,7 +9,6 @@ import (
func (eng *Engine) SendIntro(id nonce.ID, target *Session, intr *intro.Layer, func (eng *Engine) SendIntro(id nonce.ID, target *Session, intr *intro.Layer,
hook func(id nonce.ID, b slice.Bytes)) { hook func(id nonce.ID, b slice.Bytes)) {
log.I.Ln(target.Hop)
hops := []byte{0, 1, 2, 3, 4, 5} hops := []byte{0, 1, 2, 3, 4, 5}
s := make(Sessions, len(hops)) s := make(Sessions, len(hops))
s[2] = target s[2] = target

View File

@@ -13,12 +13,11 @@ import (
type Referrers map[pub.Bytes][]pub.Bytes type Referrers map[pub.Bytes][]pub.Bytes
func (eng *Engine) hiddenserviceBroadcaster(hsk *pub.Key) { func (eng *Engine) hiddenserviceBroadcaster(hs *intro.Layer) {
log.D.F("propagating hidden service introduction for %x", hsk.ToBytes()) log.D.F("propagating hidden service introduction for %x", hs.Key.ToBytes())
done := qu.T() done := qu.T()
me := eng.GetLocalNodeAddress()
intr := &intro.Layer{ intr := &intro.Layer{
Key: hsk, AddrPort: me, Key: hs.Key, AddrPort: hs.AddrPort, Bytes: hs.Bytes,
} }
msg := make(slice.Bytes, intro.Len) msg := make(slice.Bytes, intro.Len)
c := slice.NewCursor() c := slice.NewCursor()

View File

@@ -183,7 +183,7 @@ func (s *Splicer) Signature(sb sig.Bytes) *Splicer {
return s return s
} }
func (s *Splicer) ReadSignature(sb sig.Bytes) *Splicer { func (s *Splicer) ReadSignature(sb *sig.Bytes) *Splicer {
copy(sb[:], s.b[*s.c:s.c.Inc(sig.Len)]) copy(sb[:], s.b[*s.c:s.c.Inc(sig.Len)])
return s return s
} }