From 92df1542e9795cc9f81fd3e9484878ccc995e4ea Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Wed, 3 Jul 2019 16:18:50 +0300 Subject: [PATCH 1/6] 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 96928b4660..e489c4606d 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 80148c00a7..e9172e0979 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 a920d79f91..0922dd803f 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 da31d7987f..7fe922d4bb 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 9e9f7edbe6..1d901a9d34 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 } From 6cf72c5626ef3eaee58cdd06ab64d6a41dc50730 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Wed, 3 Jul 2019 16:29:42 +0300 Subject: [PATCH 2/6] Remove noise res from `connectLoop`, `ConfirmLoop` signatures --- pkg/setup/node.go | 16 +++++++--------- pkg/setup/protocol.go | 11 ++++------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 1d901a9d34..67248b372b 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -122,13 +122,13 @@ func (sn *Node) createLoop(l *routing.Loop) error { responder := l.Responder() ldR := &LoopData{RemotePK: initiator, RemotePort: l.LocalPort, LocalPort: l.RemotePort, RouteID: rRouteID} - if _, err := sn.connectLoop(responder, ldR); err != nil { + 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} - if _, err := sn.connectLoop(initiator, ldI); err != nil { + 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 { sn.Logger.Warnf("Failed to close loop: %s", err) @@ -221,22 +221,20 @@ func (sn *Node) serveTransport(tr transport.Transport) error { return proto.WritePacket(RespSuccess, nil) } -func (sn *Node) connectLoop(on cipher.PubKey, ld *LoopData) (res []byte, err error) { +func (sn *Node) connectLoop(on cipher.PubKey, ld *LoopData) error { tr, err := sn.tm.CreateTransport(context.Background(), on, dmsg.Type, false) if err != nil { - err = fmt.Errorf("transport: %s", err) - return + return fmt.Errorf("transport: %s", err) } defer tr.Close() proto := NewSetupProtocol(tr) - res, err = ConfirmLoop(proto, ld) - if err != nil { - return nil, err + if err := ConfirmLoop(proto, ld); err != nil { + return err } sn.Logger.Infof("Confirmed loop on %s with %s. RemotePort: %d. LocalPort: %d", on, ld.RemotePK, ld.RemotePort, ld.LocalPort) - return res, nil + return nil } func (sn *Node) closeLoop(on cipher.PubKey, ld *LoopData) error { diff --git a/pkg/setup/protocol.go b/pkg/setup/protocol.go index c226cec4fc..bb59621aac 100644 --- a/pkg/setup/protocol.go +++ b/pkg/setup/protocol.go @@ -154,15 +154,12 @@ func CreateLoop(p *Protocol, l *routing.Loop) error { } // ConfirmLoop sends ConfirmLoop setup request. -func ConfirmLoop(p *Protocol, l *LoopData) (noiseRes []byte, err error) { - if err = p.WritePacket(PacketConfirmLoop, l); err != nil { - return +func ConfirmLoop(p *Protocol, l *LoopData) error { + if err := p.WritePacket(PacketConfirmLoop, l); err != nil { + return err } var res []byte - if err = readAndDecodePacket(p, &res); err != nil { - return - } - return res, nil + return readAndDecodePacket(p, &res) } // CloseLoop sends CloseLoop setup request. From 22dbdad782cd2b2a14c503c8ed9d4462c320e06b Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Wed, 3 Jul 2019 16:33:51 +0300 Subject: [PATCH 3/6] Adjust tests --- pkg/router/route_manager_test.go | 3 +-- pkg/router/router_test.go | 35 ++------------------------------ 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/pkg/router/route_manager_test.go b/pkg/router/route_manager_test.go index e9172e0979..9f3a1fb991 100644 --- a/pkg/router/route_manager_test.go +++ b/pkg/router/route_manager_test.go @@ -153,9 +153,8 @@ func TestRouteManagerConfirmLoop(t *testing.T) { RouteID: 1, NoiseMessage: []byte("bar"), } - noiseRes, err := setup.ConfirmLoop(proto, ld) + err := setup.ConfirmLoop(proto, ld) require.NoError(t, err) - assert.Equal(t, []byte("foo"), noiseRes) 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_test.go b/pkg/router/router_test.go index 508950ea1f..039c2b5883 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -343,24 +343,10 @@ func TestRouterSetup(t *testing.T) { }) t.Run("`confirm loop - responder", func(t *testing.T) { - confI := noise.Config{ - LocalSK: sk2, - LocalPK: pk2, - RemotePK: pk1, - Initiator: true, - } - - ni, err := noise.KKAndSecp256k1(confI) - require.NoError(t, err) - msg, err := ni.HandshakeMessage() - require.NoError(t, err) - - time.Sleep(100 * time.Millisecond) - appRouteID, err := setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 1, 2)) require.NoError(t, err) - noiseRes, err := setup.ConfirmLoop(sProto, &setup.LoopData{RemotePK: pk2, RemotePort: 1, LocalPort: 2, RouteID: routeID, NoiseMessage: msg}) + err = setup.ConfirmLoop(sProto, &setup.LoopData{RemotePK: pk2, RemotePort: 1, LocalPort: 2, RouteID: routeID}) require.NoError(t, err) rule, err := rt.Rule(appRouteID) @@ -381,8 +367,6 @@ func TestRouterSetup(t *testing.T) { assert.Equal(t, uint16(2), addrs[0].Port) assert.Equal(t, pk2, addrs[1].PubKey) assert.Equal(t, uint16(1), addrs[1].Port) - - require.NoError(t, ni.ProcessMessage(noiseRes)) }) t.Run("confirm loop - initiator", func(t *testing.T) { @@ -395,21 +379,6 @@ func TestRouterSetup(t *testing.T) { ni, err := noise.KKAndSecp256k1(confI) require.NoError(t, err) - msg, err := ni.HandshakeMessage() - require.NoError(t, err) - - confR := noise.Config{ - LocalSK: sk2, - LocalPK: pk2, - RemotePK: pk1, - Initiator: false, - } - - nr, err := noise.KKAndSecp256k1(confR) - require.NoError(t, err) - require.NoError(t, nr.ProcessMessage(msg)) - noiseRes, err := nr.HandshakeMessage() - require.NoError(t, err) time.Sleep(100 * time.Millisecond) @@ -418,7 +387,7 @@ func TestRouterSetup(t *testing.T) { appRouteID, err := setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 3, 4)) require.NoError(t, err) - _, err = setup.ConfirmLoop(sProto, &setup.LoopData{RemotePK: pk2, RemotePort: 3, LocalPort: 4, RouteID: routeID, NoiseMessage: noiseRes}) + err = setup.ConfirmLoop(sProto, &setup.LoopData{RemotePK: pk2, RemotePort: 3, LocalPort: 4, RouteID: routeID}) require.NoError(t, err) rule, err := rt.Rule(appRouteID) From 54c9e305f13ae1551f5c7537eedab1804619a65f Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Wed, 3 Jul 2019 19:54:23 +0300 Subject: [PATCH 4/6] Remove `NoiseMessage` from `LoopData` --- pkg/router/route_manager_test.go | 18 ++++++++---------- pkg/setup/protocol.go | 9 ++++----- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/pkg/router/route_manager_test.go b/pkg/router/route_manager_test.go index 9f3a1fb991..6edde1a356 100644 --- a/pkg/router/route_manager_test.go +++ b/pkg/router/route_manager_test.go @@ -147,11 +147,10 @@ func TestRouteManagerConfirmLoop(t *testing.T) { require.NoError(t, rt.SetRule(1, rule)) ld := &setup.LoopData{ - RemotePK: pk, - RemotePort: 3, - LocalPort: 2, - RouteID: 1, - NoiseMessage: []byte("bar"), + RemotePK: pk, + RemotePort: 3, + LocalPort: 2, + RouteID: 1, } err := setup.ConfirmLoop(proto, ld) require.NoError(t, err) @@ -192,11 +191,10 @@ func TestRouteManagerLoopClosed(t *testing.T) { require.NoError(t, rt.SetRule(1, rule)) ld := &setup.LoopData{ - RemotePK: pk, - RemotePort: 3, - LocalPort: 2, - RouteID: 1, - NoiseMessage: []byte("bar"), + RemotePK: pk, + RemotePort: 3, + LocalPort: 2, + RouteID: 1, } require.NoError(t, setup.LoopClosed(proto, ld)) assert.Equal(t, uint16(2), inAddr.Port) diff --git a/pkg/setup/protocol.go b/pkg/setup/protocol.go index bb59621aac..183574f406 100644 --- a/pkg/setup/protocol.go +++ b/pkg/setup/protocol.go @@ -57,11 +57,10 @@ const ( // LoopData stores loop confirmation request data. type LoopData struct { - RemotePK cipher.PubKey `json:"remote-pk"` - RemotePort uint16 `json:"remote-port"` - LocalPort uint16 `json:"local-port"` - RouteID routing.RouteID `json:"resp-rid,omitempty"` - NoiseMessage []byte `json:"noise-msg,omitempty"` + RemotePK cipher.PubKey `json:"remote-pk"` + RemotePort uint16 `json:"remote-port"` + LocalPort uint16 `json:"local-port"` + RouteID routing.RouteID `json:"resp-rid,omitempty"` } // Protocol defines routes setup protocol. From fa1f59fdc4c38460c1dd3ddbbe51bff953c8ab03 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Wed, 3 Jul 2019 20:22:59 +0300 Subject: [PATCH 5/6] Remove noise from `router` entirely --- pkg/router/loop_list.go | 2 -- pkg/router/router.go | 41 +++----------------------- pkg/router/router_test.go | 61 ++++----------------------------------- 3 files changed, 10 insertions(+), 94 deletions(-) diff --git a/pkg/router/loop_list.go b/pkg/router/loop_list.go index 7641f93dfc..c35881bad5 100644 --- a/pkg/router/loop_list.go +++ b/pkg/router/loop_list.go @@ -5,7 +5,6 @@ import ( "github.com/google/uuid" - "github.com/skycoin/skywire/internal/noise" "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" ) @@ -13,7 +12,6 @@ import ( type loop struct { trID uuid.UUID routeID routing.RouteID - noise *noise.Noise } type loopList struct { diff --git a/pkg/router/router.go b/pkg/router/router.go index 0922dd803f..da07ea20e1 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -14,7 +14,6 @@ import ( "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" - "github.com/skycoin/skywire/internal/noise" "github.com/skycoin/skywire/pkg/app" routeFinder "github.com/skycoin/skywire/pkg/route-finder/client" "github.com/skycoin/skywire/pkg/routing" @@ -214,17 +213,8 @@ func (r *Router) forwardPacket(payload []byte, rule routing.Rule) error { func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { raddr := &app.Addr{PubKey: rule.RemotePK(), Port: rule.RemotePort()} - l, err := r.pm.GetLoop(rule.LocalPort(), raddr) - if err != nil { - return errors.New("unknown loop") - } - data, err := l.noise.DecryptUnsafe(payload) - if err != nil { - return fmt.Errorf("noise: %s", err) - } - - p := &app.Packet{Addr: &app.LoopAddr{Port: rule.LocalPort(), Remote: *raddr}, Payload: data} + p := &app.Packet{Addr: &app.LoopAddr{Port: rule.LocalPort(), Remote: *raddr}, Payload: payload} b, _ := r.pm.Get(rule.LocalPort()) // nolint: errcheck if err := b.conn.Send(app.FrameSend, p, nil); err != nil { return err @@ -249,7 +239,7 @@ func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) err return errors.New("unknown transport") } - p := routing.MakePacket(l.routeID, l.noise.EncryptUnsafe(packet.Payload)) + p := routing.MakePacket(l.routeID, packet.Payload) r.Logger.Infof("Forwarded App packet from LocalPort %d using route ID %d", packet.Addr.Port, l.routeID) _, err = tr.Write(p) return err @@ -272,20 +262,8 @@ func (r *Router) forwardLocalAppPacket(packet *app.Packet) error { } func (r *Router) requestLoop(appConn *app.Protocol, raddr *app.Addr) (*app.Addr, error) { - r.Logger.Infof("Requesting new loop to %s", raddr) - nConf := noise.Config{ - LocalSK: r.config.SecKey, - LocalPK: r.config.PubKey, - RemotePK: raddr.PubKey, - Initiator: true, - } - ni, err := noise.KKAndSecp256k1(nConf) - if err != nil { - return nil, fmt.Errorf("noise: %s", err) - } - lport := r.pm.Alloc(appConn) - if err := r.pm.SetLoop(lport, raddr, &loop{noise: ni}); err != nil { + if err := r.pm.SetLoop(lport, raddr, &loop{}); err != nil { return nil, err } @@ -341,18 +319,7 @@ func (r *Router) confirmLoop(addr *app.LoopAddr, rule routing.Rule) error { return err } - 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 err - } - - if err := r.pm.SetLoop(addr.Port, &addr.Remote, &loop{rule.TransportID(), rule.RouteID(), ni}); err != nil { + if err := r.pm.SetLoop(addr.Port, &addr.Remote, &loop{rule.TransportID(), rule.RouteID()}); err != nil { return err } diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 039c2b5883..5ebe984860 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -16,7 +16,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/skycoin/skywire/internal/noise" "github.com/skycoin/skywire/pkg/app" routeFinder "github.com/skycoin/skywire/pkg/route-finder/client" "github.com/skycoin/skywire/pkg/routing" @@ -195,23 +194,20 @@ func TestRouterApp(t *testing.T) { routeID, err := rt.AddRule(rule) require.NoError(t, err) - ni1, ni2 := noiseInstances(t, pk1, pk2, sk1, sk2) raddr := &app.Addr{PubKey: pk2, Port: 5} - require.NoError(t, r.pm.SetLoop(6, raddr, &loop{tr.ID, 4, ni1})) + require.NoError(t, r.pm.SetLoop(6, raddr, &loop{tr.ID, 4})) tr2 := m2.Transport(tr.ID) go proto.Send(app.FrameSend, &app.Packet{Addr: &app.LoopAddr{Port: 6, Remote: *raddr}, Payload: []byte("bar")}, nil) // nolint: errcheck - packet := make(routing.Packet, 29) + packet := make(routing.Packet, 9) _, err = tr2.Read(packet) require.NoError(t, err) - assert.Equal(t, uint16(23), packet.Size()) + assert.Equal(t, uint16(3), packet.Size()) assert.Equal(t, routing.RouteID(4), packet.RouteID()) - decrypted, err := ni2.DecryptUnsafe(packet.Payload()) - require.NoError(t, err) - assert.Equal(t, []byte("bar"), decrypted) + assert.Equal(t, []byte("bar"), packet.Payload()) - _, err = tr2.Write(routing.MakePacket(routeID, ni2.EncryptUnsafe([]byte("foo")))) + _, err = tr2.Write(routing.MakePacket(routeID, []byte("foo"))) require.NoError(t, err) time.Sleep(100 * time.Millisecond) @@ -370,19 +366,9 @@ func TestRouterSetup(t *testing.T) { }) t.Run("confirm loop - initiator", func(t *testing.T) { - confI := noise.Config{ - LocalSK: sk1, - LocalPK: pk1, - RemotePK: pk2, - Initiator: true, - } - - ni, err := noise.KKAndSecp256k1(confI) - require.NoError(t, err) - time.Sleep(100 * time.Millisecond) - require.NoError(t, r.pm.SetLoop(4, &app.Addr{PubKey: pk2, Port: 3}, &loop{noise: ni})) + require.NoError(t, r.pm.SetLoop(4, &app.Addr{PubKey: pk2, Port: 3}, &loop{})) appRouteID, err := setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 3, 4)) require.NoError(t, err) @@ -511,7 +497,6 @@ func TestRouterSetupLoop(t *testing.T) { ll, err := r.pm.GetLoop(10, &app.Addr{PubKey: pk2, Port: 6}) require.NoError(t, err) require.NotNil(t, ll) - require.NotNil(t, ll.noise) assert.Equal(t, pk1, addr.PubKey) assert.Equal(t, uint16(10), addr.Port) @@ -755,37 +740,3 @@ func TestRouterRouteExpiration(t *testing.T) { assert.Equal(t, 0, rt.Count()) require.NoError(t, r.Close()) } - -func noiseInstances(t *testing.T, pkI, pkR cipher.PubKey, skI, skR cipher.SecKey) (ni, nr *noise.Noise) { - t.Helper() - - var err error - confI := noise.Config{ - LocalSK: skI, - LocalPK: pkI, - RemotePK: pkR, - Initiator: true, - } - - confR := noise.Config{ - LocalSK: skR, - LocalPK: pkR, - RemotePK: pkI, - Initiator: false, - } - - ni, err = noise.KKAndSecp256k1(confI) - require.NoError(t, err) - - nr, err = noise.KKAndSecp256k1(confR) - require.NoError(t, err) - - msg, err := ni.HandshakeMessage() - require.NoError(t, err) - require.NoError(t, nr.ProcessMessage(msg)) - - res, err := nr.HandshakeMessage() - require.NoError(t, err) - require.NoError(t, ni.ProcessMessage(res)) - return ni, nr -} From b51c007065653c20ade07e12b1b3656dfe71c844 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Thu, 4 Jul 2019 09:38:28 +0300 Subject: [PATCH 6/6] Refactor a bit --- pkg/setup/protocol.go | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/pkg/setup/protocol.go b/pkg/setup/protocol.go index 183574f406..cc3d1f0734 100644 --- a/pkg/setup/protocol.go +++ b/pkg/setup/protocol.go @@ -146,10 +146,7 @@ func CreateLoop(p *Protocol, l *routing.Loop) error { if err := p.WritePacket(PacketCreateLoop, l); err != nil { return err } - if err := readAndDecodePacket(p, nil); err != nil { // TODO: data race. - return err - } - return nil + return readAndDecodePacket(p, nil) // TODO: data race. } // ConfirmLoop sends ConfirmLoop setup request. @@ -157,8 +154,7 @@ func ConfirmLoop(p *Protocol, l *LoopData) error { if err := p.WritePacket(PacketConfirmLoop, l); err != nil { return err } - var res []byte - return readAndDecodePacket(p, &res) + return readAndDecodePacket(p, nil) } // CloseLoop sends CloseLoop setup request. @@ -166,10 +162,7 @@ func CloseLoop(p *Protocol, l *LoopData) error { if err := p.WritePacket(PacketCloseLoop, l); err != nil { return err } - if err := readAndDecodePacket(p, nil); err != nil { - return err - } - return nil + return readAndDecodePacket(p, nil) } // LoopClosed sends LoopClosed setup request. @@ -177,10 +170,7 @@ func LoopClosed(p *Protocol, l *LoopData) error { if err := p.WritePacket(PacketLoopClosed, l); err != nil { return err } - if err := readAndDecodePacket(p, nil); err != nil { - return err - } - return nil + return readAndDecodePacket(p, nil) } func readAndDecodePacket(p *Protocol, v interface{}) error { @@ -194,8 +184,5 @@ func readAndDecodePacket(p *Protocol, v interface{}) error { if v == nil { return nil } - if err = json.Unmarshal(raw, v); err != nil { - return err - } - return nil + return json.Unmarshal(raw, v) }