Skip to content

Commit

Permalink
Optimized types
Browse files Browse the repository at this point in the history
  • Loading branch information
vikulin committed Dec 20, 2023
1 parent 7c85939 commit 74bf1f5
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 64 deletions.
2 changes: 1 addition & 1 deletion cmd/genkeys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
fmt.Println("Pub:", hex.EncodeToString(newKey.pub))
c := &core.Core{}
var bytes [ed25519.PublicKeySize]byte
addr := c.AddrForDomain(types.Domain{Key: bytes[:], Name: [32]byte(newKey.pub)})
addr := c.AddrForDomain(types.Domain{Key: bytes, Name: [32]byte(newKey.pub)})
fmt.Println("IP:", net.IP(addr[:]).String())
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/mesh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func run(sigCh chan os.Signal) {

getNodeKey := func() types.Domain {
name := cfg.Domain
return types.Domain{Key: publicKey, Name: [ed25519.PublicKeySize]byte([]byte(name))}
return types.Domain{Key: types.PublicKey(publicKey), Name: [ed25519.PublicKeySize]byte([]byte(name))}
}

switch {
Expand Down
2 changes: 1 addition & 1 deletion contrib/mobile/mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (m *Mesh) GetSubnetString() string {

// GetPublicKeyString gets the node's public key in hex form
func (m *Mesh) GetPublicKeyString() string {
return hex.EncodeToString(m.core.GetSelf().Domain.Key)
return hex.EncodeToString(m.core.GetSelf().Domain.Key.ToSlice())
}

// GetRoutingEntries gets the number of entries in the routing table
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/RiV-chain/RiV-mesh

go 1.21

replace github.com/Arceliar/ironwood => github.com/RiV-chain/ironwood v0.0.0-20231220110601-a30286562e6e
replace github.com/Arceliar/ironwood => github.com/RiV-chain/ironwood v0.0.0-20231220225542-c2451499a10f

replace github.com/mikispag/dns-over-tls-forwarder => github.com/RiV-chain/dns-over-tls-forwarder v0.0.0-20230828114909-c2cd9f8d79d3

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d h1:UK9fsWbWqwIQkMCz
github.com/Arceliar/phony v0.0.0-20220903101357-530938a4b13d/go.mod h1:BCnxhRf47C/dy/e/D2pmB8NkB3dQVIrkD98b220rx5Q=
github.com/RiV-chain/dns-over-tls-forwarder v0.0.0-20230828114909-c2cd9f8d79d3 h1:gz71d+oEAMXYUYw54JKT4A6CxniQx6B9J0G5CYJeLok=
github.com/RiV-chain/dns-over-tls-forwarder v0.0.0-20230828114909-c2cd9f8d79d3/go.mod h1:V2Irj3BjF2tLZ3xXp6TMQCu0I+eJWQPEktbLMHQN8XA=
github.com/RiV-chain/ironwood v0.0.0-20231220110601-a30286562e6e h1:JLsGxvmxpuijrprn5U7gdvS97ruA8WE4FkpRS6w+l50=
github.com/RiV-chain/ironwood v0.0.0-20231220110601-a30286562e6e/go.mod h1:O9iIMM9iVSXUIKNcrjossDuuXLwoGNuLSDXqjtTBHJk=
github.com/RiV-chain/ironwood v0.0.0-20231220225542-c2451499a10f h1:7RFUgliEL9+TTStt2apKHUqdN5+56kiuS0iLuMoSC/g=
github.com/RiV-chain/ironwood v0.0.0-20231220225542-c2451499a10f/go.mod h1:O9iIMM9iVSXUIKNcrjossDuuXLwoGNuLSDXqjtTBHJk=
github.com/apernet/quic-go v0.40.1-0.20231112225043-e7f3af208dee h1:S3r63crMLzbjjVJjiR+l6oS7Dzli1mYpxXNNlVc/qCc=
github.com/apernet/quic-go v0.40.1-0.20231112225043-e7f3af208dee/go.mod h1:9i0/jnY+4NvJA/wdatko2/I8iRf5R0bvZQ6fbk76tRA=
github.com/bits-and-blooms/bitset v1.3.1/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
Expand Down
2 changes: 1 addition & 1 deletion src/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (c *Core) CallPeer(u *url.URL, sintf string) error {
}

func (c *Core) PublicKey() ed25519.PublicKey {
return c.public.Key
return c.public.Key.ToEd()
}

// Hack to get the admin stuff working, TODO something cleaner
Expand Down
14 changes: 7 additions & 7 deletions src/core/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync/atomic"
"time"

"github.com/Arceliar/ironwood/types"
"github.com/Arceliar/phony"

"golang.org/x/crypto/blake2b"
Expand Down Expand Up @@ -67,7 +68,7 @@ type link struct {
}

type linkOptions struct {
pinnedEd25519Keys map[keyArray]struct{}
pinnedEd25519Keys map[types.PublicKey]struct{}
priority uint8
tlsSNI string
password []byte
Expand Down Expand Up @@ -160,10 +161,10 @@ func (l *links) add(u *url.URL, sintf string, linkType linkType) error {
retErr = ErrLinkPinnedKeyInvalid
return
}
var sigPubKey keyArray
var sigPubKey types.PublicKey
copy(sigPubKey[:], sigPub)
if options.pinnedEd25519Keys == nil {
options.pinnedEd25519Keys = map[keyArray]struct{}{}
options.pinnedEd25519Keys = map[types.PublicKey]struct{}{}
}
options.pinnedEd25519Keys[sigPubKey] = struct{}{}
}
Expand Down Expand Up @@ -604,8 +605,7 @@ func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, s
// Check if the remote side matches the keys we expected. This is a bit of a weak
// check - in future versions we really should check a signature or something like that.
if pinned := options.pinnedEd25519Keys; len(pinned) > 0 {
var key keyArray
copy(key[:], meta.domain.Key)
key := meta.domain.Key
if _, allowed := pinned[key]; !allowed {
return fmt.Errorf("node public key that does not match pinned keys")
}
Expand All @@ -617,13 +617,13 @@ func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, s
})
isallowed := len(allowed) == 0
for k := range allowed {
if bytes.Equal(k[:], meta.domain.Key) {
if bytes.Equal(k[:], meta.domain.Key.ToSlice()) {
isallowed = true
break
}
}
if linkType == linkTypeIncoming && !isallowed {
return fmt.Errorf("node public key %q is not in AllowedPublicKeys", hex.EncodeToString(meta.domain.Key))
return fmt.Errorf("node public key %q is not in AllowedPublicKeys", hex.EncodeToString(meta.domain.Key.ToSlice()))
}

dir := "outbound"
Expand Down
20 changes: 10 additions & 10 deletions src/core/nodeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"time"

"github.com/Arceliar/ironwood/types"
iwt "github.com/Arceliar/ironwood/types"
"github.com/Arceliar/phony"

Expand All @@ -19,7 +20,7 @@ type nodeinfo struct {
phony.Inbox
proto *protoHandler
myNodeInfo json.RawMessage
callbacks map[keyArray]nodeinfoCallback
callbacks map[types.PublicKey]nodeinfoCallback
}

type nodeinfoCallback struct {
Expand All @@ -37,7 +38,7 @@ func (m *nodeinfo) init(proto *protoHandler) {

func (m *nodeinfo) _init(proto *protoHandler) {
m.proto = proto
m.callbacks = make(map[keyArray]nodeinfoCallback)
m.callbacks = make(map[types.PublicKey]nodeinfoCallback)
m._cleanup()
}

Expand All @@ -52,15 +53,15 @@ func (m *nodeinfo) _cleanup() {
})
}

func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo json.RawMessage)) {
func (m *nodeinfo) _addCallback(sender types.PublicKey, call func(nodeinfo json.RawMessage)) {
m.callbacks[sender] = nodeinfoCallback{
created: time.Now(),
call: call,
}
}

// Handles the callback, if there is one
func (m *nodeinfo) _callback(sender keyArray, nodeinfo json.RawMessage) {
func (m *nodeinfo) _callback(sender types.PublicKey, nodeinfo json.RawMessage) {
if callback, ok := m.callbacks[sender]; ok {
callback.call(nodeinfo)
delete(m.callbacks, sender)
Expand Down Expand Up @@ -110,8 +111,7 @@ func (m *nodeinfo) sendReq(from phony.Actor, key iwt.Domain, callback func(nodei

func (m *nodeinfo) _sendReq(domain iwt.Domain, callback func(nodeinfo json.RawMessage)) {
if callback != nil {
var key keyArray
copy(key[:], domain.Key)
key := domain.Key
m._addCallback(key, callback)
}
_, _ = m.proto.core.PacketConn.WriteTo([]byte{typeSessionProto, typeProtoNodeInfoRequest}, iwt.Addr(domain))
Expand All @@ -125,8 +125,7 @@ func (m *nodeinfo) handleReq(from phony.Actor, key iwt.Domain) {

func (m *nodeinfo) handleRes(from phony.Actor, domain iwt.Domain, info json.RawMessage) {
m.Act(from, func() {
var key keyArray
copy(key[:], domain.Key)
key := domain.Key
m._callback(key, info)
})
}
Expand All @@ -148,7 +147,8 @@ func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error)
if err := json.Unmarshal(in, &req); err != nil {
return nil, err
}
if req.Key.Key.Equal([]byte("")) {
var zeros [32]byte
if req.Key.Key.Equal(zeros) {
return nil, fmt.Errorf("no remote public key supplied")
}
ch := make(chan []byte, 1)
Expand All @@ -165,7 +165,7 @@ func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error)
if err := msg.UnmarshalJSON(info); err != nil {
return nil, err
}
key := hex.EncodeToString(req.Key.Key)
key := hex.EncodeToString(req.Key.Key.ToSlice())
res := GetNodeInfoResponse{key: msg}
return res, nil
}
Expand Down
44 changes: 18 additions & 26 deletions src/core/proto.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package core

import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"time"

"github.com/Arceliar/ironwood/types"
iwt "github.com/Arceliar/ironwood/types"
"github.com/Arceliar/phony"
//"github.com/RiV-chain/RiV-mesh/src/address"
Expand All @@ -28,26 +28,26 @@ type reqInfo struct {
timer *time.Timer // time.AfterFunc cleanup
}

type keyArray [ed25519.PublicKeySize]byte
type keyArray types.PublicKey

Check failure on line 31 in src/core/proto.go

View workflow job for this annotation

GitHub Actions / Lint

type `keyArray` is unused (unused)

Check failure on line 31 in src/core/proto.go

View workflow job for this annotation

GitHub Actions / Lint

type `keyArray` is unused (unused)

type protoHandler struct {
phony.Inbox

core *Core
nodeinfo nodeinfo

selfRequests map[keyArray]*reqInfo
peersRequests map[keyArray]*reqInfo
dhtRequests map[keyArray]*reqInfo
selfRequests map[types.PublicKey]*reqInfo
peersRequests map[types.PublicKey]*reqInfo
dhtRequests map[types.PublicKey]*reqInfo
}

func (p *protoHandler) init(core *Core) {
p.core = core
p.nodeinfo.init(p)

p.selfRequests = make(map[keyArray]*reqInfo)
p.peersRequests = make(map[keyArray]*reqInfo)
p.dhtRequests = make(map[keyArray]*reqInfo)
p.selfRequests = make(map[types.PublicKey]*reqInfo)
p.peersRequests = make(map[types.PublicKey]*reqInfo)
p.dhtRequests = make(map[types.PublicKey]*reqInfo)
}

// Common functions
Expand Down Expand Up @@ -104,8 +104,7 @@ func (p *protoHandler) _sendDebug(key iwt.Domain, dType uint8, data []byte) {

func (p *protoHandler) sendGetSelfRequest(domain iwt.Domain, callback func([]byte)) {
p.Act(nil, func() {
var key keyArray
copy(key[:], domain.Key)
key := domain.Key
if info := p.selfRequests[key]; info != nil {
info.timer.Stop()
delete(p.selfRequests, key)
Expand All @@ -127,7 +126,7 @@ func (p *protoHandler) sendGetSelfRequest(domain iwt.Domain, callback func([]byt
func (p *protoHandler) _handleGetSelfRequest(key iwt.Domain) {
self := p.core.GetSelf()
res := map[string]string{
"key": hex.EncodeToString(self.Domain.Key),
"key": hex.EncodeToString(self.Domain.Key.ToSlice()),
"domain": string(self.Domain.GetNormalizedName()),
"tld": self.Tld,
"coords": fmt.Sprintf("%v", self.RoutingEntries),
Expand All @@ -140,8 +139,8 @@ func (p *protoHandler) _handleGetSelfRequest(key iwt.Domain) {
}

func (p *protoHandler) _handleGetSelfResponse(domain iwt.Domain, bs []byte) {
var key keyArray
copy(key[:], domain.Key)

key := domain.Key
if info := p.selfRequests[key]; info != nil {
info.timer.Stop()
info.callback(bs)
Expand All @@ -153,8 +152,7 @@ func (p *protoHandler) _handleGetSelfResponse(domain iwt.Domain, bs []byte) {

func (p *protoHandler) sendGetPeersRequest(domain iwt.Domain, callback func([]byte)) {
p.Act(nil, func() {
var key keyArray
copy(key[:], domain.Key)
key := domain.Key
if info := p.peersRequests[key]; info != nil {
info.timer.Stop()
delete(p.peersRequests, key)
Expand Down Expand Up @@ -188,8 +186,7 @@ func (p *protoHandler) _handleGetPeersRequest(domain iwt.Domain) {
}

func (p *protoHandler) _handleGetPeersResponse(domain iwt.Domain, bs []byte) {
var key keyArray
copy(key[:], domain.Key)
key := domain.Key
if info := p.peersRequests[key]; info != nil {
info.timer.Stop()
info.callback(bs)
Expand All @@ -201,8 +198,7 @@ func (p *protoHandler) _handleGetPeersResponse(domain iwt.Domain, bs []byte) {

func (p *protoHandler) sendGetDHTRequest(domain iwt.Domain, callback func([]byte)) {
p.Act(nil, func() {
var key keyArray
copy(key[:], domain.Key)
key := domain.Key
if info := p.dhtRequests[key]; info != nil {
info.timer.Stop()
delete(p.dhtRequests, key)
Expand Down Expand Up @@ -236,8 +232,7 @@ func (p *protoHandler) _handleGetTreeRequest(domain iwt.Domain) {
}

func (p *protoHandler) _handleGetTreeResponse(domain iwt.Domain, bs []byte) {
var key keyArray
copy(key[:], domain.Key)
key := domain.Key
if info := p.dhtRequests[key]; info != nil {
info.timer.Stop()
info.callback(bs)
Expand Down Expand Up @@ -291,9 +286,7 @@ func (p *protoHandler) getPeersHandler(in json.RawMessage) (interface{}, error)
if err := json.Unmarshal(in, &req); err != nil {
return nil, err
}
var key keyArray
kbs := req.Key.Key
copy(key[:], kbs)
key := req.Key.Key

Check failure on line 289 in src/core/proto.go

View workflow job for this annotation

GitHub Actions / Lint

SA4006: this value of `key` is never used (staticcheck)

Check failure on line 289 in src/core/proto.go

View workflow job for this annotation

GitHub Actions / Lint

SA4006: this value of `key` is never used (staticcheck)
ch := make(chan []byte, 1)
p.sendGetPeersRequest(req.Key, func(info []byte) {
ch <- info
Expand Down Expand Up @@ -337,8 +330,7 @@ func (p *protoHandler) getDHTHandler(in json.RawMessage) (interface{}, error) {
if err := json.Unmarshal(in, &req); err != nil {
return nil, err
}
var key keyArray
copy(key[:], req.Key.Key)
key := req.Key.Key

Check failure on line 333 in src/core/proto.go

View workflow job for this annotation

GitHub Actions / Lint

SA4006: this value of `key` is never used (staticcheck)

Check failure on line 333 in src/core/proto.go

View workflow job for this annotation

GitHub Actions / Lint

SA4006: this value of `key` is never used (staticcheck)
ch := make(chan []byte, 1)
p.sendGetDHTRequest(req.Key, func(info []byte) {
ch <- info
Expand Down
10 changes: 5 additions & 5 deletions src/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (m *version_metadata) encode(privateKey ed25519.PrivateKey, password []byte
if err != nil {
return nil, err
}
n, err := hasher.Write(m.domain.Key)
n, err := hasher.Write(m.domain.Key[:])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -128,8 +128,8 @@ func (m *version_metadata) decode(r io.Reader, password []byte) error {
m.minorVer = binary.BigEndian.Uint16(bs[:2])

case metaPublicKey:
m.domain.Key = make(ed25519.PublicKey, ed25519.PublicKeySize)
copy(m.domain.Key, bs[:ed25519.PublicKeySize])
//m.domain.Key = make(ed25519.PublicKey, ed25519.PublicKeySize)
copy(m.domain.Key[:], bs[:ed25519.PublicKeySize])

case metaDomainName:
var n [ed25519.PublicKeySize]byte
Expand All @@ -146,12 +146,12 @@ func (m *version_metadata) decode(r io.Reader, password []byte) error {
if err != nil {
return fmt.Errorf("invalid password supplied")
}
n, err := hasher.Write(m.domain.Key)
n, err := hasher.Write(m.domain.Key[:])
if err != nil || n != ed25519.PublicKeySize {
return fmt.Errorf("failed to generate hash")
}
hash := hasher.Sum(nil)
if !ed25519.Verify(m.domain.Key, hash, sig) {
if !ed25519.Verify(m.domain.Key[:], hash, sig) {
return fmt.Errorf("password is incorrect")
}
return nil
Expand Down

0 comments on commit 74bf1f5

Please sign in to comment.