Added factory MakeCodec function to onions
This commit is contained in:
@@ -119,6 +119,7 @@ func (x *IntroAd) Handle(s *splice.Splice, p Onion, ng Ngin) (e error) {
|
||||
}
|
||||
|
||||
func (x *IntroAd) Len() int { return IntroLen }
|
||||
|
||||
func (x *IntroAd) Magic() string { return IntroMagic }
|
||||
|
||||
func (x *IntroAd) Splice(s *splice.Splice) {
|
||||
@@ -196,4 +197,5 @@ func NewIntroAd(
|
||||
}
|
||||
|
||||
func init() { Register(IntroMagic, introGen) }
|
||||
|
||||
func introGen() coding.Codec { return &IntroAd{} }
|
||||
|
||||
@@ -37,8 +37,38 @@ type ServiceAd struct {
|
||||
Sig crypto.SigBytes
|
||||
}
|
||||
|
||||
func (x *ServiceAd) Account(res *sess.Data, sm *sess.Manager,
|
||||
s *sessions.Data, last bool) (skip bool, sd *sessions.Data) {
|
||||
func NewServiceAd(
|
||||
id nonce.ID,
|
||||
key *crypto.Prv,
|
||||
relayRate uint32,
|
||||
port uint16,
|
||||
) (sv *ServiceAd) {
|
||||
|
||||
s := splice.New(IntroLen)
|
||||
k := crypto.DerivePub(key)
|
||||
ServiceSplice(s, id, k, relayRate, port)
|
||||
hash := sha256.Single(s.GetUntil(s.GetCursor()))
|
||||
var e error
|
||||
var sign crypto.SigBytes
|
||||
if sign, e = crypto.Sign(key, hash); fails(e) {
|
||||
return nil
|
||||
}
|
||||
sv = &ServiceAd{
|
||||
ID: id,
|
||||
Key: k,
|
||||
RelayRate: relayRate,
|
||||
Port: port,
|
||||
Sig: sign,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (x *ServiceAd) Account(
|
||||
res *sess.Data,
|
||||
sm *sess.Manager,
|
||||
s *sessions.Data,
|
||||
last bool,
|
||||
) (skip bool, sd *sessions.Data) {
|
||||
|
||||
return false, nil
|
||||
}
|
||||
@@ -51,6 +81,7 @@ func (x *ServiceAd) Decode(s *splice.Splice) (e error) {
|
||||
ReadSignature(&x.Sig)
|
||||
return
|
||||
}
|
||||
|
||||
func (x *ServiceAd) Encode(s *splice.Splice) (e error) {
|
||||
x.Splice(s)
|
||||
return
|
||||
@@ -126,31 +157,6 @@ func ServiceSplice(
|
||||
Uint32(relayRate)
|
||||
}
|
||||
|
||||
func NewServiceAd(
|
||||
id nonce.ID,
|
||||
key *crypto.Prv,
|
||||
relayRate uint32,
|
||||
port uint16,
|
||||
) (sv *ServiceAd) {
|
||||
|
||||
s := splice.New(IntroLen)
|
||||
k := crypto.DerivePub(key)
|
||||
ServiceSplice(s, id, k, relayRate, port)
|
||||
hash := sha256.Single(s.GetUntil(s.GetCursor()))
|
||||
var e error
|
||||
var sign crypto.SigBytes
|
||||
if sign, e = crypto.Sign(key, hash); fails(e) {
|
||||
return nil
|
||||
}
|
||||
sv = &ServiceAd{
|
||||
ID: id,
|
||||
Key: k,
|
||||
RelayRate: relayRate,
|
||||
Port: port,
|
||||
Sig: sign,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func init() { Register(ServiceAdMagic, serviceAdGen) }
|
||||
|
||||
func serviceAdGen() coding.Codec { return &ServiceAd{} }
|
||||
|
||||
@@ -23,18 +23,23 @@ func NewRegistry() *Registry {
|
||||
return &Registry{CodecGenerators: make(CodecGenerators)}
|
||||
}
|
||||
|
||||
func MakeCodec(magic string) (cdc coding.Codec) {
|
||||
var in func() coding.Codec
|
||||
var ok bool
|
||||
if in, ok = registry.CodecGenerators[magic]; ok {
|
||||
cdc = in()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Recognise(s *splice.Splice) (cdc coding.Codec) {
|
||||
registry.Lock()
|
||||
defer registry.Unlock()
|
||||
var magic string
|
||||
// log.D.S("splice", s.GetAll().ToBytes())
|
||||
s.ReadMagic(&magic)
|
||||
var ok bool
|
||||
var in func() coding.Codec
|
||||
if in, ok = registry.CodecGenerators[magic]; ok {
|
||||
cdc = in()
|
||||
}
|
||||
if !ok || cdc == nil {
|
||||
cdc = MakeCodec(magic)
|
||||
if cdc == nil {
|
||||
log.D.F("unrecognised magic %s ignoring message",
|
||||
color.Red.Sprint(magic),
|
||||
spew.Sdump(s.GetUntil(s.GetCursor()).ToBytes()),
|
||||
|
||||
Reference in New Issue
Block a user