From 92df1542e9795cc9f81fd3e9484878ccc995e4ea Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Wed, 3 Jul 2019 16:18:50 +0300 Subject: [PATCH] Remove noise handshake from routing establishement --- pkg/router/route_manager.go | 36 ++++++++------------- pkg/router/route_manager_test.go | 7 ++-- pkg/router/router.go | 55 ++++++++------------------------ pkg/routing/loop.go | 11 +++---- pkg/setup/node.go | 11 +++---- 5 files changed, 39 insertions(+), 81 deletions(-) diff --git a/pkg/router/route_manager.go b/pkg/router/route_manager.go index 96928b466..e489c4606 100644 --- a/pkg/router/route_manager.go +++ b/pkg/router/route_manager.go @@ -15,7 +15,7 @@ import ( ) type setupCallbacks struct { - ConfirmLoop func(addr *app.LoopAddr, rule routing.Rule, noiseMsg []byte) (noiseRes []byte, err error) + ConfirmLoop func(addr *app.LoopAddr, rule routing.Rule) (err error) LoopClosed func(addr *app.LoopAddr) error } @@ -89,7 +89,7 @@ func (rm *routeManager) Serve(rw io.ReadWriter) error { case setup.PacketDeleteRules: respBody, err = rm.deleteRoutingRules(body) case setup.PacketConfirmLoop: - respBody, err = rm.confirmLoop(body) + err = rm.confirmLoop(body) case setup.PacketLoopClosed: err = rm.loopClosed(body) default: @@ -97,7 +97,6 @@ func (rm *routeManager) Serve(rw io.ReadWriter) error { } if err != nil { - rm.Logger.Infof("Setup request with type %s failed: %s", t, err) return proto.WritePacket(setup.RespFailure, err.Error()) } @@ -141,17 +140,17 @@ func (rm *routeManager) deleteRoutingRules(data []byte) ([]routing.RouteID, erro return ruleIDs, nil } -func (rm *routeManager) confirmLoop(data []byte) (noiseRes []byte, err error) { +func (rm *routeManager) confirmLoop(data []byte) error { ld := setup.LoopData{} - if err = json.Unmarshal(data, &ld); err != nil { - return + if err := json.Unmarshal(data, &ld); err != nil { + return err } raddr := &app.Addr{PubKey: ld.RemotePK, Port: ld.RemotePort} var appRouteID routing.RouteID var appRule routing.Rule - err = rm.rt.RangeRules(func(routeID routing.RouteID, rule routing.Rule) bool { + err := rm.rt.RangeRules(func(routeID routing.RouteID, rule routing.Rule) bool { if rule.Type() != routing.RuleApp || rule.RemotePK() != ld.RemotePK || rule.RemotePort() != ld.RemotePort || rule.LocalPort() != ld.LocalPort { return true @@ -163,41 +162,34 @@ func (rm *routeManager) confirmLoop(data []byte) (noiseRes []byte, err error) { return false }) if err != nil { - err = fmt.Errorf("routing table: %s", err) - return + return fmt.Errorf("routing table: %s", err) } if appRule == nil { - err = errors.New("unknown loop") - return + return errors.New("unknown loop") } rule, err := rm.rt.Rule(ld.RouteID) if err != nil { - err = fmt.Errorf("routing table: %s", err) - return + return fmt.Errorf("routing table: %s", err) } if rule.Type() != routing.RuleForward { - err = errors.New("reverse rule is not forward") - return + return errors.New("reverse rule is not forward") } - msg, err := rm.callbacks.ConfirmLoop(&app.LoopAddr{Port: ld.LocalPort, Remote: *raddr}, rule, ld.NoiseMessage) - if err != nil { - err = fmt.Errorf("confirm: %s", err) - return + if err = rm.callbacks.ConfirmLoop(&app.LoopAddr{Port: ld.LocalPort, Remote: *raddr}, rule); err != nil { + return fmt.Errorf("confirm: %s", err) } rm.Logger.Infof("Setting reverse route ID %d for rule with ID %d", ld.RouteID, appRouteID) appRule.SetRouteID(ld.RouteID) if rErr := rm.rt.SetRule(appRouteID, appRule); rErr != nil { - err = fmt.Errorf("routing table: %s", rErr) - return + return fmt.Errorf("routing table: %s", rErr) } rm.Logger.Infof("Confirmed loop with %s:%d", ld.RemotePK, ld.RemotePort) - return msg, nil + return nil } func (rm *routeManager) loopClosed(data []byte) error { diff --git a/pkg/router/route_manager_test.go b/pkg/router/route_manager_test.go index 80148c00a..e9172e097 100644 --- a/pkg/router/route_manager_test.go +++ b/pkg/router/route_manager_test.go @@ -123,13 +123,11 @@ func TestRouteManagerConfirmLoop(t *testing.T) { rt := manageRoutingTable(routing.InMemoryRoutingTable()) var inAddr *app.LoopAddr var inRule routing.Rule - var noiseMsg []byte callbacks := &setupCallbacks{ - ConfirmLoop: func(addr *app.LoopAddr, rule routing.Rule, nMsg []byte) (noiseRes []byte, err error) { + ConfirmLoop: func(addr *app.LoopAddr, rule routing.Rule) (err error) { inAddr = addr inRule = rule - noiseMsg = nMsg - return []byte("foo"), nil + return nil }, } rm := &routeManager{logging.MustGetLogger("routesetup"), rt, callbacks} @@ -158,7 +156,6 @@ func TestRouteManagerConfirmLoop(t *testing.T) { noiseRes, err := setup.ConfirmLoop(proto, ld) require.NoError(t, err) assert.Equal(t, []byte("foo"), noiseRes) - assert.Equal(t, []byte("bar"), noiseMsg) assert.Equal(t, rule, inRule) assert.Equal(t, uint16(2), inAddr.Port) assert.Equal(t, uint16(3), inAddr.Remote.Port) diff --git a/pkg/router/router.go b/pkg/router/router.go index a920d79f9..0922dd803 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -284,11 +284,6 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr *app.Addr) (*app.Addr, return nil, fmt.Errorf("noise: %s", err) } - msg, err := ni.HandshakeMessage() - if err != nil { - return nil, fmt.Errorf("noise handshake: %s", err) - } - lport := r.pm.Alloc(appConn) if err := r.pm.SetLoop(lport, raddr, &loop{noise: ni}); err != nil { return nil, err @@ -309,7 +304,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr *app.Addr) (*app.Addr, } l := &routing.Loop{LocalPort: laddr.Port, RemotePort: raddr.Port, - NoiseMessage: msg, Expiry: time.Now().Add(RouteTTL), + Expiry: time.Now().Add(RouteTTL), Forward: forwardRoute, Reverse: reverseRoute} proto, tr, err := r.setupProto(context.Background()) @@ -340,19 +335,25 @@ func (r *Router) confirmLocalLoop(laddr, raddr *app.Addr) error { return nil } -func (r *Router) confirmLoop(addr *app.LoopAddr, rule routing.Rule, noiseMsg []byte) ([]byte, error) { +func (r *Router) confirmLoop(addr *app.LoopAddr, rule routing.Rule) error { b, err := r.pm.Get(addr.Port) if err != nil { - return nil, err + return err } - ni, msg, err := r.advanceNoiseHandshake(addr, noiseMsg) + nConf := noise.Config{ + LocalSK: r.config.SecKey, + LocalPK: r.config.PubKey, + RemotePK: addr.Remote.PubKey, + Initiator: false, + } + ni, err := noise.KKAndSecp256k1(nConf) if err != nil { - return nil, fmt.Errorf("noise handshake: %s", err) + return err } if err := r.pm.SetLoop(addr.Port, &addr.Remote, &loop{rule.TransportID(), rule.RouteID(), ni}); err != nil { - return nil, err + return err } addrs := [2]*app.Addr{&app.Addr{PubKey: r.config.PubKey, Port: addr.Port}, &addr.Remote} @@ -360,7 +361,7 @@ func (r *Router) confirmLoop(addr *app.LoopAddr, rule routing.Rule, noiseMsg []b r.Logger.Warnf("Failed to notify App about new loop: %s", err) } - return msg, nil + return nil } func (r *Router) closeLoop(appConn *app.Protocol, addr *app.LoopAddr) error { @@ -451,36 +452,6 @@ fetchRoutesAgain: return fwdRoutes[0], revRoutes[0], nil } -func (r *Router) advanceNoiseHandshake(addr *app.LoopAddr, noiseMsg []byte) (ni *noise.Noise, noiseRes []byte, err error) { - var l *loop - l, _ = r.pm.GetLoop(addr.Port, &addr.Remote) // nolint: errcheck - - if l != nil && l.routeID != 0 { - err = errors.New("loop already exist") - return - } - - if l != nil && l.noise != nil { - return l.noise, nil, l.noise.ProcessMessage(noiseMsg) - } - - nConf := noise.Config{ - LocalSK: r.config.SecKey, - LocalPK: r.config.PubKey, - RemotePK: addr.Remote.PubKey, - Initiator: false, - } - ni, err = noise.KKAndSecp256k1(nConf) - if err != nil { - return - } - if err = ni.ProcessMessage(noiseMsg); err != nil { - return - } - noiseRes, err = ni.HandshakeMessage() - return -} - // IsSetupTransport checks whether `tr` is running in the `setup` mode. func (r *Router) IsSetupTransport(tr *transport.ManagedTransport) bool { for _, pk := range r.config.SetupNodes { diff --git a/pkg/routing/loop.go b/pkg/routing/loop.go index da31d7987..7fe922d4b 100644 --- a/pkg/routing/loop.go +++ b/pkg/routing/loop.go @@ -9,12 +9,11 @@ import ( // Loop defines a loop over a pair of routes. type Loop struct { - LocalPort uint16 - RemotePort uint16 - Forward Route - Reverse Route - Expiry time.Time - NoiseMessage []byte + LocalPort uint16 + RemotePort uint16 + Forward Route + Reverse Route + Expiry time.Time } // Initiator returns initiator of the Loop. diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 9e9f7edbe..1d901a9d3 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -121,14 +121,13 @@ func (sn *Node) createLoop(l *routing.Loop) error { initiator := l.Initiator() responder := l.Responder() - ldR := &LoopData{RemotePK: initiator, RemotePort: l.LocalPort, LocalPort: l.RemotePort, RouteID: rRouteID, NoiseMessage: l.NoiseMessage} - noiseRes, err := sn.connectLoop(responder, ldR) - if err != nil { + ldR := &LoopData{RemotePK: initiator, RemotePort: l.LocalPort, LocalPort: l.RemotePort, RouteID: rRouteID} + if _, err := sn.connectLoop(responder, ldR); err != nil { sn.Logger.Warnf("Failed to confirm loop with responder: %s", err) return fmt.Errorf("loop connect: %s", err) } - ldI := &LoopData{RemotePK: responder, RemotePort: l.RemotePort, LocalPort: l.LocalPort, RouteID: fRouteID, NoiseMessage: noiseRes} + ldI := &LoopData{RemotePK: responder, RemotePort: l.RemotePort, LocalPort: l.LocalPort, RouteID: fRouteID} if _, err := sn.connectLoop(initiator, ldI); err != nil { sn.Logger.Warnf("Failed to confirm loop with initiator: %s", err) if err := sn.closeLoop(responder, ldR); err != nil { @@ -222,7 +221,7 @@ func (sn *Node) serveTransport(tr transport.Transport) error { return proto.WritePacket(RespSuccess, nil) } -func (sn *Node) connectLoop(on cipher.PubKey, ld *LoopData) (noiseRes []byte, err error) { +func (sn *Node) connectLoop(on cipher.PubKey, ld *LoopData) (res []byte, err error) { tr, err := sn.tm.CreateTransport(context.Background(), on, dmsg.Type, false) if err != nil { err = fmt.Errorf("transport: %s", err) @@ -231,7 +230,7 @@ func (sn *Node) connectLoop(on cipher.PubKey, ld *LoopData) (noiseRes []byte, er defer tr.Close() proto := NewSetupProtocol(tr) - res, err := ConfirmLoop(proto, ld) + res, err = ConfirmLoop(proto, ld) if err != nil { return nil, err }