Skip to content

Commit

Permalink
Merge branch 'mainnet' into feature/remove-transport-manager-dependency
Browse files Browse the repository at this point in the history
# Conflicts:
#	pkg/router/router.go
#	pkg/setup/node.go
  • Loading branch information
nkryuchkov committed Jul 4, 2019
2 parents b07f24f + 6adc363 commit 3ff75ea
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 251 deletions.
3 changes: 0 additions & 3 deletions pkg/router/loop_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import (

"github.com/google/uuid"

"github.com/skycoin/dmsg/noise"

"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/routing"
)

type loop struct {
trID uuid.UUID
routeID routing.RouteID
noise *noise.Noise
}

type loopList struct {
Expand Down
36 changes: 14 additions & 22 deletions pkg/router/route_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -89,15 +89,14 @@ 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:
err = errors.New("unknown foundation packet")
}

if err != nil {

rm.Logger.Infof("Setup request with type %s failed: %s", t, err)
return proto.WritePacket(setup.RespFailure, err.Error())
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
28 changes: 11 additions & 17 deletions pkg/router/route_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -149,16 +147,13 @@ 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,
}
noiseRes, err := setup.ConfirmLoop(proto, ld)
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)
Expand Down Expand Up @@ -196,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)
Expand Down
81 changes: 9 additions & 72 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/dmsg/noise"

"github.com/skycoin/skywire/pkg/app"
routeFinder "github.com/skycoin/skywire/pkg/route-finder/client"
"github.com/skycoin/skywire/pkg/routing"
Expand Down Expand Up @@ -219,17 +217,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
Expand All @@ -254,7 +243,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
Expand All @@ -277,25 +266,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)
}

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 {
if err := r.pm.SetLoop(lport, raddr, &loop{}); err != nil {
return nil, err
}

Expand All @@ -314,7 +286,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())
Expand Down Expand Up @@ -345,27 +317,22 @@ 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
}

ni, msg, err := r.advanceNoiseHandshake(addr, noiseMsg)
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
if err := r.pm.SetLoop(addr.Port, &addr.Remote, &loop{rule.TransportID(), rule.RouteID()}); err != nil {
return err
}

addrs := [2]*app.Addr{&app.Addr{PubKey: r.config.PubKey, Port: addr.Port}, &addr.Remote}
if err = b.conn.Send(app.FrameConfirmLoop, addrs, nil); err != nil {
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 {
Expand Down Expand Up @@ -456,33 +423,3 @@ fetchRoutesAgain:
r.Logger.Infof("Found routes Forward: %s. Reverse %s", fwdRoutes, revRoutes)
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
}
Loading

0 comments on commit 3ff75ea

Please sign in to comment.