From 1857842e3d5862f21781e777303a5ac45ba37a6e Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Thu, 25 Jul 2019 21:49:13 +0300 Subject: [PATCH 01/26] Compiling. Passes tests --- pkg/setup/config.go | 2 ++ pkg/setup/node.go | 28 ++++++++++++++++++++-------- pkg/visor/config.go | 7 ++++--- pkg/visor/visor.go | 24 ++++++++++++++++++------ pkg/visor/visor_test.go | 1 + 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/pkg/setup/config.go b/pkg/setup/config.go index 65f8e2aba..f73552a99 100644 --- a/pkg/setup/config.go +++ b/pkg/setup/config.go @@ -16,5 +16,7 @@ type Config struct { TransportDiscovery string `json:"transport_discovery"` + TransportType string `json:TransportType` + LogLevel string `json:"log_level"` } diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 5ba649d83..6d2ff2900 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -30,7 +30,7 @@ type Node struct { Logger *logging.Logger tm *transport.Manager - messenger *dmsg.Client + messenger transport.Factory srvCount int metrics metrics.Recorder @@ -45,7 +45,15 @@ func NewNode(conf *Config, metrics metrics.Recorder) (*Node, error) { if lvl, err := logging.LevelFromString(conf.LogLevel); err == nil { logger.SetLevel(lvl) } - messenger := dmsg.NewClient(pk, sk, disc.NewHTTP(conf.Messaging.Discovery), dmsg.SetLogger(logger.PackageLogger(dmsg.Type))) + + var factory transport.Factory + switch conf.TransportType { + case "dmsg": + factory = dmsg.NewClient(pk, sk, disc.NewHTTP(conf.Messaging.Discovery), + dmsg.SetLogger(logger.PackageLogger(dmsg.Type))) + case "tcp-transport": + factory = transport.NewTCPFactory(pk, nil, nil) + } trDiscovery, err := trClient.NewHTTP(conf.TransportDiscovery, pk, sk) if err != nil { @@ -59,7 +67,7 @@ func NewNode(conf *Config, metrics metrics.Recorder) (*Node, error) { LogStore: transport.InMemoryTransportLogStore(), } - tm, err := transport.NewManager(tmConf, messenger) + tm, err := transport.NewManager(tmConf, factory) if err != nil { log.Fatal("Failed to setup Transport Manager: ", err) } @@ -69,18 +77,22 @@ func NewNode(conf *Config, metrics metrics.Recorder) (*Node, error) { Logger: logger.PackageLogger("routesetup"), metrics: metrics, tm: tm, - messenger: messenger, + messenger: factory, srvCount: conf.Messaging.ServerCount, }, nil } // Serve starts transport listening loop. func (sn *Node) Serve(ctx context.Context) error { - if sn.srvCount > 0 { - if err := sn.messenger.InitiateServerConnections(ctx, sn.srvCount); err != nil { - return fmt.Errorf("messaging: %s", err) + + switch factory := sn.messenger.(type) { + case *dmsg.Client: + if sn.srvCount > 0 { + if err := factory.InitiateServerConnections(ctx, sn.srvCount); err != nil { + return fmt.Errorf("messaging: %s", err) + } + sn.Logger.Info("Connected to messaging servers") } - sn.Logger.Info("Connected to messaging servers") } go func() { diff --git a/pkg/visor/config.go b/pkg/visor/config.go index bb8210424..2149e6761 100644 --- a/pkg/visor/config.go +++ b/pkg/visor/config.go @@ -57,10 +57,11 @@ type Config struct { AppsPath string `json:"apps_path"` LocalPath string `json:"local_path"` - LogLevel string `json:"log_level"` - ShutdownTimeout Duration `json:"shutdown_timeout"` // time value, examples: 10s, 1m, etc + TransportType string `json:"transport-type"` - Interfaces InterfaceConfig `json:"interfaces"` + ShutdownTimeout Duration `json:"shutdown_timeout"` // time value, examples: 10s, 1m, etc + Interfaces InterfaceConfig `json:"interfaces"` + LogLevel string `json:"log_level"` } // MessagingConfig returns config for dmsg client. diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index d726bc915..6ec1a1931 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -86,7 +86,7 @@ type PacketRouter interface { type Node struct { config *Config router PacketRouter - messenger *dmsg.Client + messenger transport.Factory tm *transport.Manager rt routing.Table executer appExecuter @@ -125,7 +125,13 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) return nil, fmt.Errorf("invalid Messaging config: %s", err) } - node.messenger = dmsg.NewClient(mConfig.PubKey, mConfig.SecKey, mConfig.Discovery, dmsg.SetLogger(node.Logger.PackageLogger(dmsg.Type))) + switch config.TransportType { + case "dmsg": + node.messenger = dmsg.NewClient(mConfig.PubKey, mConfig.SecKey, + mConfig.Discovery, dmsg.SetLogger(node.Logger.PackageLogger(dmsg.Type))) + case "tcp-transport": + node.messenger = transport.NewTCPFactory(mConfig.PubKey, nil, nil) + } trDiscovery, err := config.TransportDiscovery() if err != nil { @@ -205,11 +211,17 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) // Start spawns auto-started Apps, starts router and RPC interfaces . func (node *Node) Start() error { ctx := context.Background() - err := node.messenger.InitiateServerConnections(ctx, node.config.Messaging.ServerCount) - if err != nil { - return fmt.Errorf("%s: %s", dmsg.Type, err) + + switch factory := node.messenger.(type) { + case *dmsg.Client: + err := factory.InitiateServerConnections(ctx, node.config.Messaging.ServerCount) + if err != nil { + return fmt.Errorf("%s: %s", dmsg.Type, err) + } + node.logger.Info("Connected to messaging servers") + case *transport.TCPFactory: + node.logger.Info("TCPFactory: ignoring configured messaging servers") } - node.logger.Info("Connected to messaging servers") pathutil.EnsureDir(node.dir()) node.closePreviousApps() diff --git a/pkg/visor/visor_test.go b/pkg/visor/visor_test.go index a712e4fcb..4a4e0e3df 100644 --- a/pkg/visor/visor_test.go +++ b/pkg/visor/visor_test.go @@ -64,6 +64,7 @@ func TestNewNode(t *testing.T) { {App: "bar", AutoStart: true, Port: 2}, } + conf.TransportType = "dmsg" defer func() { require.NoError(t, os.RemoveAll("local")) }() From b81e03fa1c406731a05ef2dd9f30ec67a7b1e66a Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Thu, 25 Jul 2019 23:32:06 +0300 Subject: [PATCH 02/26] configurable pubkeys file, TCPFactory initialized with Listener --- cmd/skywire-cli/commands/node/gen-config.go | 5 ++ pkg/setup/config.go | 4 +- pkg/setup/node.go | 20 ++++- pkg/visor/config.go | 4 +- pkg/visor/visor.go | 19 ++++- pkg/visor/visor_test.go | 89 ++++++++++++--------- 6 files changed, 98 insertions(+), 43 deletions(-) diff --git a/cmd/skywire-cli/commands/node/gen-config.go b/cmd/skywire-cli/commands/node/gen-config.go index ab132ea19..9c3878855 100644 --- a/cmd/skywire-cli/commands/node/gen-config.go +++ b/cmd/skywire-cli/commands/node/gen-config.go @@ -100,10 +100,12 @@ func defaultConfig() *visor.Config { conf.Routing.RouteFinder = "https://routefinder.skywire.skycoin.net/" const defaultSetupNodePK = "0324579f003e6b4048bae2def4365e634d8e0e3054a20fc7af49daf2a179658557" + sPK := cipher.PubKey{} if err := sPK.UnmarshalText([]byte(defaultSetupNodePK)); err != nil { log.WithError(err).Warnf("Failed to unmarshal default setup node public key %s", defaultSetupNodePK) } + conf.Routing.SetupNodes = []cipher.PubKey{sPK} conf.Routing.Table.Type = "boltdb" conf.Routing.Table.Location = "./skywire/routing.db" @@ -120,5 +122,8 @@ func defaultConfig() *visor.Config { conf.Interfaces.RPCAddress = "localhost:3435" + conf.TransportType = "dmsg" + conf.PubKeysFile = "./local/pubkeys" + return conf } diff --git a/pkg/setup/config.go b/pkg/setup/config.go index f73552a99..9fe57d004 100644 --- a/pkg/setup/config.go +++ b/pkg/setup/config.go @@ -16,7 +16,9 @@ type Config struct { TransportDiscovery string `json:"transport_discovery"` - TransportType string `json:TransportType` + TransportType string `json:"transport_type"` + PubKeysFile string `json:"pubkeys_file"` + TCPTransportAddr string `json:"tcptransport_addr"` LogLevel string `json:"log_level"` } diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 6d2ff2900..b3623da96 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "log" + "net" "time" "github.com/skycoin/dmsg/cipher" @@ -49,10 +50,27 @@ func NewNode(conf *Config, metrics metrics.Recorder) (*Node, error) { var factory transport.Factory switch conf.TransportType { case "dmsg": + factory = dmsg.NewClient(pk, sk, disc.NewHTTP(conf.Messaging.Discovery), dmsg.SetLogger(logger.PackageLogger(dmsg.Type))) case "tcp-transport": - factory = transport.NewTCPFactory(pk, nil, nil) + + pkTbl, err := transport.FilePubKeyTable(conf.PubKeysFile) + if err != nil { + return nil, fmt.Errorf("error %v reading %v", err, conf.PubKeysFile) + } + + addr, err := net.ResolveTCPAddr("tcp", conf.TCPTransportAddr) + if err != nil { + return nil, fmt.Errorf("error %v resolving %v", err, conf.TCPTransportAddr) + } + + tcpListener, err := net.ListenTCP("tcp", addr) + if err != nil { + return nil, fmt.Errorf("error %v listening %v", err, conf.TCPTransportAddr) + } + + factory = transport.NewTCPFactory(pk, pkTbl, tcpListener) } trDiscovery, err := trClient.NewHTTP(conf.TransportDiscovery, pk, sk) diff --git a/pkg/visor/config.go b/pkg/visor/config.go index 2149e6761..db074b77d 100644 --- a/pkg/visor/config.go +++ b/pkg/visor/config.go @@ -57,7 +57,9 @@ type Config struct { AppsPath string `json:"apps_path"` LocalPath string `json:"local_path"` - TransportType string `json:"transport-type"` + TransportType string `json:"transport_type"` + PubKeysFile string `json:"pubkeys_file"` + TCPTransportAddr string `json:"tcptransport_addr"` ShutdownTimeout Duration `json:"shutdown_timeout"` // time value, examples: 10s, 1m, etc Interfaces InterfaceConfig `json:"interfaces"` diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 6ec1a1931..470203a83 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -130,7 +130,23 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) node.messenger = dmsg.NewClient(mConfig.PubKey, mConfig.SecKey, mConfig.Discovery, dmsg.SetLogger(node.Logger.PackageLogger(dmsg.Type))) case "tcp-transport": - node.messenger = transport.NewTCPFactory(mConfig.PubKey, nil, nil) + + pkTbl, err := transport.FilePubKeyTable(config.PubKeysFile) + if err != nil { + return nil, fmt.Errorf("error %v reading %v", err, config.PubKeysFile) + } + + addr, err := net.ResolveTCPAddr("tcp", config.TCPTransportAddr) + if err != nil { + return nil, fmt.Errorf("error %v resolving %v", err, config.TCPTransportAddr) + } + + tcpListener, err := net.ListenTCP("tcp", addr) + if err != nil { + return nil, fmt.Errorf("error %v listening %v", err, config.TCPTransportAddr) + } + + node.messenger = transport.NewTCPFactory(pk, pkTbl, tcpListener) } trDiscovery, err := config.TransportDiscovery() @@ -138,6 +154,7 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) return nil, fmt.Errorf("invalid MessagingConfig: %s", err) } logStore, err := config.TransportLogStore() + if err != nil { return nil, fmt.Errorf("invalid TransportLogStore: %s", err) } diff --git a/pkg/visor/visor_test.go b/pkg/visor/visor_test.go index 4a4e0e3df..9f44a9835 100644 --- a/pkg/visor/visor_test.go +++ b/pkg/visor/visor_test.go @@ -64,57 +64,68 @@ func TestNewNode(t *testing.T) { {App: "bar", AutoStart: true, Port: 2}, } - conf.TransportType = "dmsg" - defer func() { - require.NoError(t, os.RemoveAll("local")) - }() + for _, trType := range []string{"dmsg", "tcp-transport"} { + conf.TransportType = trType + + node, err := NewNode(&conf, masterLogger) + require.NoError(t, err) - node, err := NewNode(&conf, masterLogger) - require.NoError(t, err) + assert.NotNil(t, node.router) + assert.NotNil(t, node.appsConf) + assert.NotNil(t, node.appsPath) + assert.NotNil(t, node.localPath) + assert.NotNil(t, node.startedApps) - assert.NotNil(t, node.router) - assert.NotNil(t, node.appsConf) - assert.NotNil(t, node.appsPath) - assert.NotNil(t, node.localPath) - assert.NotNil(t, node.startedApps) + require.NoError(t, os.RemoveAll("local")) + } } func TestNodeStartClose(t *testing.T) { - r := new(mockRouter) - executer := &MockExecuter{} - conf := []AppConfig{ - {App: "skychat", Version: "1.0", AutoStart: true, Port: 1}, - {App: "foo", Version: "1.0", AutoStart: false}, - } - defer func() { - require.NoError(t, os.RemoveAll("skychat")) - }() + for _, trType := range []string{"dmsg", "tcp-transport"} { + t.Run(trType, func(t *testing.T) { + r := new(mockRouter) + executer := &MockExecuter{} + conf := []AppConfig{ + {App: "skychat", Version: "1.0", AutoStart: true, Port: 1}, + {App: "foo", Version: "1.0", AutoStart: false}, + } + + defer func() { + require.NoError(t, os.RemoveAll("skychat")) + }() - node := &Node{config: &Config{}, router: r, executer: executer, appsConf: conf, - startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test")} - mConf := &dmsg.Config{PubKey: cipher.PubKey{}, SecKey: cipher.SecKey{}, Discovery: disc.NewMock()} - node.messenger = dmsg.NewClient(mConf.PubKey, mConf.SecKey, mConf.Discovery) + node := &Node{config: &Config{}, router: r, executer: executer, appsConf: conf, + startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test")} + mConf := &dmsg.Config{PubKey: cipher.PubKey{}, SecKey: cipher.SecKey{}, Discovery: disc.NewMock()} - var err error + switch trType { + case "dmsg": + node.messenger = dmsg.NewClient(mConf.PubKey, mConf.SecKey, mConf.Discovery) + case "tcp-transport": + node.messenger = transport.NewTCPFactory(mConf.PubKey, nil, nil) + } - tmConf := &transport.ManagerConfig{PubKey: cipher.PubKey{}, DiscoveryClient: transport.NewDiscoveryMock()} - node.tm, err = transport.NewManager(tmConf, node.messenger) - require.NoError(t, err) + var err error + tmConf := &transport.ManagerConfig{PubKey: cipher.PubKey{}, DiscoveryClient: transport.NewDiscoveryMock()} + node.tm, err = transport.NewManager(tmConf, node.messenger) + require.NoError(t, err) - errCh := make(chan error) - go func() { - errCh <- node.Start() - }() + errCh := make(chan error) + go func() { + errCh <- node.Start() + }() - time.Sleep(100 * time.Millisecond) - require.NoError(t, node.Close()) - require.True(t, r.didClose) - require.NoError(t, <-errCh) + time.Sleep(100 * time.Millisecond) + require.NoError(t, node.Close()) + require.True(t, r.didClose) + require.NoError(t, <-errCh) - require.Len(t, executer.cmds, 1) - assert.Equal(t, "skychat.v1.0", executer.cmds[0].Path) - assert.Equal(t, "skychat/v1.0", executer.cmds[0].Dir) + require.Len(t, executer.cmds, 1) + assert.Equal(t, "skychat.v1.0", executer.cmds[0].Path) + assert.Equal(t, "skychat/v1.0", executer.cmds[0].Dir) + }) + } } func TestNodeSpawnApp(t *testing.T) { From 0a2297da501a93781d25e33471a2e67af23b3c77 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Fri, 26 Jul 2019 00:49:59 +0300 Subject: [PATCH 03/26] Deduplicated code --- go.mod | 6 ++-- go.sum | 8 ++++++ pkg/setup/node.go | 24 ++++------------ pkg/transport/tcp_transport.go | 43 ++++++++++++++++++++--------- pkg/transport/tcp_transport_test.go | 4 +-- pkg/visor/visor.go | 20 +++----------- pkg/visor/visor_test.go | 9 ++++-- 7 files changed, 60 insertions(+), 54 deletions(-) diff --git a/go.mod b/go.mod index 5db14d14e..87618660d 100644 --- a/go.mod +++ b/go.mod @@ -20,8 +20,10 @@ require ( github.com/spf13/cobra v0.0.5 github.com/stretchr/testify v1.3.0 go.etcd.io/bbolt v1.3.3 - golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 - golang.org/x/net v0.0.0-20190620200207-3b0461eec859 + golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 + golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 + golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 // indirect + golang.org/x/tools v0.0.0-20190725161231-2e34cfcb95cb // indirect ) // Uncomment for tests with alternate branches of 'dmsg' diff --git a/go.sum b/go.sum index 2508980af..628fac9fb 100644 --- a/go.sum +++ b/go.sum @@ -119,11 +119,15 @@ golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnf golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 h1:ydJNl0ENAG67pFbB+9tfhiL2pYqLhfoaZFw/cjLhY4A= golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc= +golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk= +golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -136,12 +140,16 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k= golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 h1:LepdCS8Gf/MVejFIt8lsiexZATdoGVyp5bcyS+rYoUI= +golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190627182818-9947fec5c3ab/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190725161231-2e34cfcb95cb h1:Zi4or4sGkVpI7V5TX4JDMHJAthfKDKg8WotOBKXqmTs= +golang.org/x/tools v0.0.0-20190725161231-2e34cfcb95cb/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/pkg/setup/node.go b/pkg/setup/node.go index b3623da96..8afea460b 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "log" - "net" "time" "github.com/skycoin/dmsg/cipher" @@ -47,30 +46,19 @@ func NewNode(conf *Config, metrics metrics.Recorder) (*Node, error) { logger.SetLevel(lvl) } - var factory transport.Factory + var ( + factory transport.Factory + err error + ) switch conf.TransportType { case "dmsg": - factory = dmsg.NewClient(pk, sk, disc.NewHTTP(conf.Messaging.Discovery), dmsg.SetLogger(logger.PackageLogger(dmsg.Type))) case "tcp-transport": - - pkTbl, err := transport.FilePubKeyTable(conf.PubKeysFile) - if err != nil { - return nil, fmt.Errorf("error %v reading %v", err, conf.PubKeysFile) - } - - addr, err := net.ResolveTCPAddr("tcp", conf.TCPTransportAddr) + factory, err = transport.NewTCPFactory(pk, conf.PubKeysFile, conf.TCPTransportAddr) if err != nil { - return nil, fmt.Errorf("error %v resolving %v", err, conf.TCPTransportAddr) + return nil, err } - - tcpListener, err := net.ListenTCP("tcp", addr) - if err != nil { - return nil, fmt.Errorf("error %v listening %v", err, conf.TCPTransportAddr) - } - - factory = transport.NewTCPFactory(pk, pkTbl, tcpListener) } trDiscovery, err := trClient.NewHTTP(conf.TransportDiscovery, pk, sk) diff --git a/pkg/transport/tcp_transport.go b/pkg/transport/tcp_transport.go index 3b5c01560..fff4107f4 100644 --- a/pkg/transport/tcp_transport.go +++ b/pkg/transport/tcp_transport.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "errors" + "fmt" "net" "os" "path/filepath" @@ -18,35 +19,51 @@ var ErrUnknownRemote = errors.New("unknown remote") // TCPFactory implements Factory over TCP connection. type TCPFactory struct { - l *net.TCPListener - lpk cipher.PubKey - pkt PubKeyTable + Pk cipher.PubKey + PkTable PubKeyTable + Lsr *net.TCPListener } -// NewTCPFactory constructs a new TCP Factory. -func NewTCPFactory(lpk cipher.PubKey, pkt PubKeyTable, l *net.TCPListener) Factory { - return &TCPFactory{l, lpk, pkt} +// NewTCPFactory constructs a new TCP Factory +func NewTCPFactory(pk cipher.PubKey, pubkeysFile string, tcpAddr string) (Factory, error) { + + pkTbl, err := FilePubKeyTable(pubkeysFile) + if err != nil { + return nil, fmt.Errorf("error %v reading %v", err, pubkeysFile) + } + + addr, err := net.ResolveTCPAddr("tcp", tcpAddr) + if err != nil { + return nil, fmt.Errorf("error %v resolving %v", err, tcpAddr) + } + + tcpListener, err := net.ListenTCP("tcp", addr) + if err != nil { + return nil, fmt.Errorf("error %v listening %v", err, tcpAddr) + } + + return &TCPFactory{pk, pkTbl, tcpListener}, nil } // Accept accepts a remotely-initiated Transport. func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { - conn, err := f.l.AcceptTCP() + conn, err := f.Lsr.AcceptTCP() if err != nil { return nil, err } raddr := conn.RemoteAddr().(*net.TCPAddr) - rpk := f.pkt.RemotePK(raddr.IP) + rpk := f.PkTable.RemotePK(raddr.IP) if rpk.Null() { return nil, ErrUnknownRemote } - return &TCPTransport{conn, [2]cipher.PubKey{f.lpk, rpk}}, nil + return &TCPTransport{conn, [2]cipher.PubKey{f.Pk, rpk}}, nil } // Dial initiates a Transport with a remote node. func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, error) { - raddr := f.pkt.RemoteAddr(remote) + raddr := f.PkTable.RemoteAddr(remote) if raddr == nil { return nil, ErrUnknownRemote } @@ -56,7 +73,7 @@ func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, return nil, err } - return &TCPTransport{conn, [2]cipher.PubKey{f.lpk, remote}}, nil + return &TCPTransport{conn, [2]cipher.PubKey{f.Pk, remote}}, nil } // Close implements io.Closer @@ -64,12 +81,12 @@ func (f *TCPFactory) Close() error { if f == nil { return nil } - return f.l.Close() + return f.Lsr.Close() } // Local returns the local public key. func (f *TCPFactory) Local() cipher.PubKey { - return f.lpk + return f.Pk } // Type returns the Transport type. diff --git a/pkg/transport/tcp_transport_test.go b/pkg/transport/tcp_transport_test.go index 97919889c..fecf3a227 100644 --- a/pkg/transport/tcp_transport_test.go +++ b/pkg/transport/tcp_transport_test.go @@ -32,7 +32,7 @@ func TestTCPFactory(t *testing.T) { pkt1 := transport.InMemoryPubKeyTable(map[cipher.PubKey]*net.TCPAddr{pk2: addr2}) pkt2 := transport.InMemoryPubKeyTable(map[cipher.PubKey]*net.TCPAddr{pk1: addr1}) - f1 := transport.NewTCPFactory(pk1, pkt1, l1) + f1 := &transport.TCPFactory{pk1, pkt1, l1} errCh := make(chan error) go func() { tr, err := f1.Accept(context.TODO()) @@ -49,7 +49,7 @@ func TestTCPFactory(t *testing.T) { errCh <- nil }() - f2 := transport.NewTCPFactory(pk2, pkt2, l2) + f2 := &transport.TCPFactory{pk2, pkt2, l2} assert.Equal(t, "tcp", f2.Type()) assert.Equal(t, pk2, f2.Local()) diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 470203a83..1854ce8a7 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -122,7 +122,7 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) sk := config.Node.StaticSecKey mConfig, err := config.MessagingConfig() if err != nil { - return nil, fmt.Errorf("invalid Messaging config: %s", err) + return nil, fmt.Errorf("invalid messaging config: %s", err) } switch config.TransportType { @@ -130,23 +130,11 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) node.messenger = dmsg.NewClient(mConfig.PubKey, mConfig.SecKey, mConfig.Discovery, dmsg.SetLogger(node.Logger.PackageLogger(dmsg.Type))) case "tcp-transport": - - pkTbl, err := transport.FilePubKeyTable(config.PubKeysFile) - if err != nil { - return nil, fmt.Errorf("error %v reading %v", err, config.PubKeysFile) - } - - addr, err := net.ResolveTCPAddr("tcp", config.TCPTransportAddr) + var err error + node.messenger, err = transport.NewTCPFactory(config.Node.StaticPubKey, config.PubKeysFile, config.TCPTransportAddr) if err != nil { - return nil, fmt.Errorf("error %v resolving %v", err, config.TCPTransportAddr) + return nil, err } - - tcpListener, err := net.ListenTCP("tcp", addr) - if err != nil { - return nil, fmt.Errorf("error %v listening %v", err, config.TCPTransportAddr) - } - - node.messenger = transport.NewTCPFactory(pk, pkTbl, tcpListener) } trDiscovery, err := config.TransportDiscovery() diff --git a/pkg/visor/visor_test.go b/pkg/visor/visor_test.go index 9f44a9835..de445e61b 100644 --- a/pkg/visor/visor_test.go +++ b/pkg/visor/visor_test.go @@ -95,15 +95,18 @@ func TestNodeStartClose(t *testing.T) { require.NoError(t, os.RemoveAll("skychat")) }() - node := &Node{config: &Config{}, router: r, executer: executer, appsConf: conf, + nodeCfg := &Config{} + node := &Node{config: nodeCfg, router: r, executer: executer, appsConf: conf, startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test")} - mConf := &dmsg.Config{PubKey: cipher.PubKey{}, SecKey: cipher.SecKey{}, Discovery: disc.NewMock()} switch trType { case "dmsg": + mConf := &dmsg.Config{PubKey: cipher.PubKey{}, SecKey: cipher.SecKey{}, Discovery: disc.NewMock()} node.messenger = dmsg.NewClient(mConf.PubKey, mConf.SecKey, mConf.Discovery) case "tcp-transport": - node.messenger = transport.NewTCPFactory(mConf.PubKey, nil, nil) + var err error + node.messenger, err = transport.NewTCPFactory(nodeCfg.Node.StaticPubKey, nodeCfg.PubKeysFile, nodeCfg.TCPTransportAddr) + require.NoError(t, err) } var err error From 6d9601737529b73a86a9fcfee81b1095ed435f16 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Mon, 29 Jul 2019 13:35:47 +0300 Subject: [PATCH 04/26] Commented out tcp-transport for setup-node --- go.mod | 4 +- go.sum | 4 ++ pkg/setup/node.go | 11 ++++-- vendor/golang.org/x/sys/unix/endian_little.go | 2 +- vendor/golang.org/x/sys/unix/mkerrors.sh | 3 ++ .../x/sys/unix/zerrors_linux_386.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_amd64.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_arm.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_arm64.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_mips.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_mips64.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_mips64le.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_mipsle.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_ppc64.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_ppc64le.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_riscv64.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_s390x.go | 34 +++++++++++++++++ .../x/sys/unix/zerrors_linux_sparc64.go | 34 +++++++++++++++++ .../x/sys/unix/zsysnum_linux_386.go | 6 +++ .../x/sys/unix/zsysnum_linux_amd64.go | 6 +++ .../x/sys/unix/zsysnum_linux_arm.go | 6 +++ .../x/sys/unix/zsysnum_linux_arm64.go | 6 +++ .../x/sys/unix/zsysnum_linux_mips.go | 6 +++ .../x/sys/unix/zsysnum_linux_mips64.go | 6 +++ .../x/sys/unix/zsysnum_linux_mips64le.go | 6 +++ .../x/sys/unix/zsysnum_linux_mipsle.go | 6 +++ .../x/sys/unix/zsysnum_linux_ppc64.go | 6 +++ .../x/sys/unix/zsysnum_linux_ppc64le.go | 6 +++ .../x/sys/unix/zsysnum_linux_riscv64.go | 6 +++ .../x/sys/unix/zsysnum_linux_s390x.go | 6 +++ .../x/sys/unix/zsysnum_linux_sparc64.go | 6 +++ .../golang.org/x/sys/unix/ztypes_linux_386.go | 37 ++++++++++++++++++ .../x/sys/unix/ztypes_linux_amd64.go | 38 +++++++++++++++++++ .../golang.org/x/sys/unix/ztypes_linux_arm.go | 37 ++++++++++++++++++ .../x/sys/unix/ztypes_linux_arm64.go | 38 +++++++++++++++++++ .../x/sys/unix/ztypes_linux_mips.go | 37 ++++++++++++++++++ .../x/sys/unix/ztypes_linux_mips64.go | 38 +++++++++++++++++++ .../x/sys/unix/ztypes_linux_mips64le.go | 38 +++++++++++++++++++ .../x/sys/unix/ztypes_linux_mipsle.go | 37 ++++++++++++++++++ .../x/sys/unix/ztypes_linux_ppc64.go | 38 +++++++++++++++++++ .../x/sys/unix/ztypes_linux_ppc64le.go | 38 +++++++++++++++++++ .../x/sys/unix/ztypes_linux_riscv64.go | 38 +++++++++++++++++++ .../x/sys/unix/ztypes_linux_s390x.go | 38 +++++++++++++++++++ .../x/sys/unix/ztypes_linux_sparc64.go | 38 +++++++++++++++++++ .../golang.org/x/sys/windows/types_windows.go | 7 +++- vendor/modules.txt | 6 +-- 46 files changed, 1035 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 87618660d..dfba6ab65 100644 --- a/go.mod +++ b/go.mod @@ -22,8 +22,8 @@ require ( go.etcd.io/bbolt v1.3.3 golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 - golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 // indirect - golang.org/x/tools v0.0.0-20190725161231-2e34cfcb95cb // indirect + golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e // indirect + golang.org/x/tools v0.0.0-20190728063539-fc6e2057e7f6 // indirect ) // Uncomment for tests with alternate branches of 'dmsg' diff --git a/go.sum b/go.sum index 628fac9fb..8d1653aa1 100644 --- a/go.sum +++ b/go.sum @@ -142,6 +142,8 @@ golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSF golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 h1:LepdCS8Gf/MVejFIt8lsiexZATdoGVyp5bcyS+rYoUI= golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e h1:D5TXcfTk7xF7hvieo4QErS3qqCB4teTffacDWr7CI+0= +golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= @@ -150,6 +152,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190627182818-9947fec5c3ab/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190725161231-2e34cfcb95cb h1:Zi4or4sGkVpI7V5TX4JDMHJAthfKDKg8WotOBKXqmTs= golang.org/x/tools v0.0.0-20190725161231-2e34cfcb95cb/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= +golang.org/x/tools v0.0.0-20190728063539-fc6e2057e7f6 h1:Ea0wSv+mYMnRqly3KGa0iFELsXbTI6NxhbFFG1qlo7Q= +golang.org/x/tools v0.0.0-20190728063539-fc6e2057e7f6/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 8afea460b..11ba9fbf4 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -55,10 +55,13 @@ func NewNode(conf *Config, metrics metrics.Recorder) (*Node, error) { factory = dmsg.NewClient(pk, sk, disc.NewHTTP(conf.Messaging.Discovery), dmsg.SetLogger(logger.PackageLogger(dmsg.Type))) case "tcp-transport": - factory, err = transport.NewTCPFactory(pk, conf.PubKeysFile, conf.TCPTransportAddr) - if err != nil { - return nil, err - } + return nil, fmt.Errorf("tcp-transport forbidden for setup-node") + + // TODO: remove it later + // factory, err = transport.NewTCPFactory(pk, conf.PubKeysFile, conf.TCPTransportAddr) + // if err != nil { + // return nil, err + // } } trDiscovery, err := trClient.NewHTTP(conf.TransportDiscovery, pk, sk) diff --git a/vendor/golang.org/x/sys/unix/endian_little.go b/vendor/golang.org/x/sys/unix/endian_little.go index 085df2d8d..bcdb5d30e 100644 --- a/vendor/golang.org/x/sys/unix/endian_little.go +++ b/vendor/golang.org/x/sys/unix/endian_little.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // -// +build 386 amd64 amd64p32 arm arm64 ppc64le mipsle mips64le +// +build 386 amd64 amd64p32 arm arm64 ppc64le mipsle mips64le riscv64 package unix diff --git a/vendor/golang.org/x/sys/unix/mkerrors.sh b/vendor/golang.org/x/sys/unix/mkerrors.sh index 3d85f2795..14624b953 100644 --- a/vendor/golang.org/x/sys/unix/mkerrors.sh +++ b/vendor/golang.org/x/sys/unix/mkerrors.sh @@ -199,6 +199,7 @@ struct ltchars { #include #include #include +#include #include #include #include @@ -435,6 +436,8 @@ ccflags="$@" $2 ~ /^TC[IO](ON|OFF)$/ || $2 ~ /^IN_/ || $2 ~ /^LOCK_(SH|EX|NB|UN)$/ || + $2 ~ /^LO_(KEY|NAME)_SIZE$/ || + $2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ || $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|MCAST|EVFILT|NOTE|EV|SHUT|PROT|MAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ || $2 ~ /^TP_STATUS_/ || $2 ~ /^FALLOC_/ || diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_386.go b/vendor/golang.org/x/sys/unix/zerrors_linux_386.go index 1db2f00de..5213d820a 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_386.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_386.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1135,6 +1149,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -1997,6 +2025,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x80108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x80108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x541b SIOCOUTQ = 0x5411 SIOCOUTQNSD = 0x894b @@ -2204,6 +2236,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2423,6 +2456,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x400854d5 TUNDETACHFILTER = 0x400854d6 + TUNGETDEVNETNS = 0x54e3 TUNGETFEATURES = 0x800454cf TUNGETFILTER = 0x800854db TUNGETIFF = 0x800454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_amd64.go b/vendor/golang.org/x/sys/unix/zerrors_linux_amd64.go index 8a9d2eadf..39b630cc5 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_amd64.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_amd64.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1135,6 +1149,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -1998,6 +2026,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x80108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x80108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x541b SIOCOUTQ = 0x5411 SIOCOUTQNSD = 0x894b @@ -2205,6 +2237,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2424,6 +2457,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x401054d5 TUNDETACHFILTER = 0x401054d6 + TUNGETDEVNETNS = 0x54e3 TUNGETFEATURES = 0x800454cf TUNGETFILTER = 0x801054db TUNGETIFF = 0x800454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_arm.go b/vendor/golang.org/x/sys/unix/zerrors_linux_arm.go index 2e7455814..c59a1beb3 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_arm.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_arm.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1134,6 +1148,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -2004,6 +2032,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x80108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x80108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x541b SIOCOUTQ = 0x5411 SIOCOUTQNSD = 0x894b @@ -2211,6 +2243,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2430,6 +2463,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x400854d5 TUNDETACHFILTER = 0x400854d6 + TUNGETDEVNETNS = 0x54e3 TUNGETFEATURES = 0x800454cf TUNGETFILTER = 0x800854db TUNGETIFF = 0x800454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_arm64.go b/vendor/golang.org/x/sys/unix/zerrors_linux_arm64.go index b1dc633a2..5f35c19d1 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_arm64.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_arm64.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -528,6 +541,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1137,6 +1151,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -1988,6 +2016,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x80108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x80108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x541b SIOCOUTQ = 0x5411 SIOCOUTQNSD = 0x894b @@ -2196,6 +2228,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2415,6 +2448,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x401054d5 TUNDETACHFILTER = 0x401054d6 + TUNGETDEVNETNS = 0x54e3 TUNGETFEATURES = 0x800454cf TUNGETFILTER = 0x801054db TUNGETIFF = 0x800454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_mips.go b/vendor/golang.org/x/sys/unix/zerrors_linux_mips.go index ad4d9afb6..7f1b7bef2 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_mips.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_mips.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1134,6 +1148,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -1997,6 +2025,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x40108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x40108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x467f SIOCOUTQ = 0x7472 SIOCOUTQNSD = 0x894b @@ -2205,6 +2237,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2425,6 +2458,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x800854d5 TUNDETACHFILTER = 0x800854d6 + TUNGETDEVNETNS = 0x200054e3 TUNGETFEATURES = 0x400454cf TUNGETFILTER = 0x400854db TUNGETIFF = 0x400454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_mips64.go b/vendor/golang.org/x/sys/unix/zerrors_linux_mips64.go index fe2965028..603d88b8b 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_mips64.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_mips64.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1134,6 +1148,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -1997,6 +2025,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x40108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x40108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x467f SIOCOUTQ = 0x7472 SIOCOUTQNSD = 0x894b @@ -2205,6 +2237,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2425,6 +2458,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x801054d5 TUNDETACHFILTER = 0x801054d6 + TUNGETDEVNETNS = 0x200054e3 TUNGETFEATURES = 0x400454cf TUNGETFILTER = 0x401054db TUNGETIFF = 0x400454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_mips64le.go b/vendor/golang.org/x/sys/unix/zerrors_linux_mips64le.go index 608878303..ed178f8a7 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_mips64le.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_mips64le.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1134,6 +1148,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -1997,6 +2025,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x40108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x40108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x467f SIOCOUTQ = 0x7472 SIOCOUTQNSD = 0x894b @@ -2205,6 +2237,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2425,6 +2458,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x801054d5 TUNDETACHFILTER = 0x801054d6 + TUNGETDEVNETNS = 0x200054e3 TUNGETFEATURES = 0x400454cf TUNGETFILTER = 0x401054db TUNGETIFF = 0x400454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_mipsle.go b/vendor/golang.org/x/sys/unix/zerrors_linux_mipsle.go index 4cf9ddfad..080b78933 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_mipsle.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_mipsle.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1134,6 +1148,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -1997,6 +2025,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x40108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x40108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x467f SIOCOUTQ = 0x7472 SIOCOUTQNSD = 0x894b @@ -2205,6 +2237,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2425,6 +2458,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x800854d5 TUNDETACHFILTER = 0x800854d6 + TUNGETDEVNETNS = 0x200054e3 TUNGETFEATURES = 0x400454cf TUNGETFILTER = 0x400854db TUNGETIFF = 0x400454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_ppc64.go b/vendor/golang.org/x/sys/unix/zerrors_linux_ppc64.go index 374e3007f..961e8eabe 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_ppc64.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_ppc64.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1134,6 +1148,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -2055,6 +2083,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x40108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x40108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x4004667f SIOCOUTQ = 0x40047473 SIOCOUTQNSD = 0x894b @@ -2262,6 +2294,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2485,6 +2518,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x801054d5 TUNDETACHFILTER = 0x801054d6 + TUNGETDEVNETNS = 0x200054e3 TUNGETFEATURES = 0x400454cf TUNGETFILTER = 0x401054db TUNGETIFF = 0x400454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_ppc64le.go b/vendor/golang.org/x/sys/unix/zerrors_linux_ppc64le.go index badf14102..6e0538f22 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_ppc64le.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_ppc64le.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1134,6 +1148,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -2055,6 +2083,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x40108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x40108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x4004667f SIOCOUTQ = 0x40047473 SIOCOUTQNSD = 0x894b @@ -2262,6 +2294,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2485,6 +2518,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x801054d5 TUNDETACHFILTER = 0x801054d6 + TUNGETDEVNETNS = 0x200054e3 TUNGETFEATURES = 0x400454cf TUNGETFILTER = 0x401054db TUNGETIFF = 0x400454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_riscv64.go b/vendor/golang.org/x/sys/unix/zerrors_linux_riscv64.go index 0ce8c7eff..06c0148c1 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_riscv64.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_riscv64.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1134,6 +1148,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -1985,6 +2013,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x80108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x80108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x541b SIOCOUTQ = 0x5411 SIOCOUTQNSD = 0x894b @@ -2192,6 +2224,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2411,6 +2444,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x401054d5 TUNDETACHFILTER = 0x401054d6 + TUNGETDEVNETNS = 0x54e3 TUNGETFEATURES = 0x800454cf TUNGETFILTER = 0x801054db TUNGETIFF = 0x800454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_s390x.go b/vendor/golang.org/x/sys/unix/zerrors_linux_s390x.go index 47675125a..39875095c 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_s390x.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_s390x.go @@ -196,6 +196,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -217,6 +219,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -238,16 +245,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -290,8 +300,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -411,6 +423,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -527,6 +540,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1134,6 +1148,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -2058,6 +2086,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x80108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x80108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x541b SIOCOUTQ = 0x5411 SIOCOUTQNSD = 0x894b @@ -2265,6 +2297,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2484,6 +2517,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x401054d5 TUNDETACHFILTER = 0x401054d6 + TUNGETDEVNETNS = 0x54e3 TUNGETFEATURES = 0x800454cf TUNGETFILTER = 0x801054db TUNGETIFF = 0x800454d2 diff --git a/vendor/golang.org/x/sys/unix/zerrors_linux_sparc64.go b/vendor/golang.org/x/sys/unix/zerrors_linux_sparc64.go index a46fc9b43..8d80f99bc 100644 --- a/vendor/golang.org/x/sys/unix/zerrors_linux_sparc64.go +++ b/vendor/golang.org/x/sys/unix/zerrors_linux_sparc64.go @@ -199,6 +199,8 @@ const ( BPF_A = 0x10 BPF_ABS = 0x20 BPF_ADD = 0x0 + BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff + BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 0x38 BPF_ALU = 0x4 BPF_ALU64 = 0x7 BPF_AND = 0x50 @@ -220,6 +222,11 @@ const ( BPF_FROM_BE = 0x8 BPF_FROM_LE = 0x0 BPF_FS_MAGIC = 0xcafe4a11 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = 0x2 + BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = 0x4 + BPF_F_ADJ_ROOM_ENCAP_L4_GRE = 0x8 + BPF_F_ADJ_ROOM_ENCAP_L4_UDP = 0x10 + BPF_F_ADJ_ROOM_FIXED_GSO = 0x1 BPF_F_ALLOW_MULTI = 0x2 BPF_F_ALLOW_OVERRIDE = 0x1 BPF_F_ANY_ALIGNMENT = 0x2 @@ -241,16 +248,19 @@ const ( BPF_F_PSEUDO_HDR = 0x10 BPF_F_QUERY_EFFECTIVE = 0x1 BPF_F_RDONLY = 0x8 + BPF_F_RDONLY_PROG = 0x80 BPF_F_RECOMPUTE_CSUM = 0x1 BPF_F_REUSE_STACKID = 0x400 BPF_F_SEQ_NUMBER = 0x8 BPF_F_SKIP_FIELD_MASK = 0xff BPF_F_STACK_BUILD_ID = 0x20 BPF_F_STRICT_ALIGNMENT = 0x1 + BPF_F_SYSCTL_BASE_NAME = 0x1 BPF_F_TUNINFO_IPV6 = 0x1 BPF_F_USER_BUILD_ID = 0x800 BPF_F_USER_STACK = 0x100 BPF_F_WRONLY = 0x10 + BPF_F_WRONLY_PROG = 0x100 BPF_F_ZERO_CSUM_TX = 0x2 BPF_F_ZERO_SEED = 0x40 BPF_H = 0x8 @@ -293,8 +303,10 @@ const ( BPF_OR = 0x40 BPF_PSEUDO_CALL = 0x1 BPF_PSEUDO_MAP_FD = 0x1 + BPF_PSEUDO_MAP_VALUE = 0x2 BPF_RET = 0x6 BPF_RSH = 0x70 + BPF_SK_STORAGE_GET_F_CREATE = 0x1 BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7 BPF_SOCK_OPS_RETRANS_CB_FLAG = 0x2 BPF_SOCK_OPS_RTO_CB_FLAG = 0x1 @@ -414,6 +426,7 @@ const ( CLONE_NEWUTS = 0x4000000 CLONE_PARENT = 0x8000 CLONE_PARENT_SETTID = 0x100000 + CLONE_PIDFD = 0x1000 CLONE_PTRACE = 0x2000 CLONE_SETTLS = 0x80000 CLONE_SIGHAND = 0x800 @@ -531,6 +544,7 @@ const ( ETH_P_DNA_RC = 0x6002 ETH_P_DNA_RT = 0x6003 ETH_P_DSA = 0x1b + ETH_P_DSA_8021Q = 0xdadb ETH_P_ECONET = 0x18 ETH_P_EDSA = 0xdada ETH_P_ERSPAN = 0x88be @@ -1138,6 +1152,20 @@ const ( LOCK_NB = 0x4 LOCK_SH = 0x1 LOCK_UN = 0x8 + LOOP_CLR_FD = 0x4c01 + LOOP_CTL_ADD = 0x4c80 + LOOP_CTL_GET_FREE = 0x4c82 + LOOP_CTL_REMOVE = 0x4c81 + LOOP_GET_STATUS = 0x4c03 + LOOP_GET_STATUS64 = 0x4c05 + LOOP_SET_BLOCK_SIZE = 0x4c09 + LOOP_SET_CAPACITY = 0x4c07 + LOOP_SET_DIRECT_IO = 0x4c08 + LOOP_SET_FD = 0x4c00 + LOOP_SET_STATUS = 0x4c02 + LOOP_SET_STATUS64 = 0x4c04 + LO_KEY_SIZE = 0x20 + LO_NAME_SIZE = 0x40 MADV_DODUMP = 0x11 MADV_DOFORK = 0xb MADV_DONTDUMP = 0x10 @@ -2050,6 +2078,10 @@ const ( SIOCGSKNS = 0x894c SIOCGSTAMP = 0x8906 SIOCGSTAMPNS = 0x8907 + SIOCGSTAMPNS_NEW = 0x40108907 + SIOCGSTAMPNS_OLD = 0x8907 + SIOCGSTAMP_NEW = 0x40108906 + SIOCGSTAMP_OLD = 0x8906 SIOCINQ = 0x4004667f SIOCOUTQ = 0x40047473 SIOCOUTQNSD = 0x894b @@ -2257,6 +2289,7 @@ const ( SYNC_FILE_RANGE_WAIT_AFTER = 0x4 SYNC_FILE_RANGE_WAIT_BEFORE = 0x1 SYNC_FILE_RANGE_WRITE = 0x2 + SYNC_FILE_RANGE_WRITE_AND_WAIT = 0x7 SYSFS_MAGIC = 0x62656572 S_BLKSIZE = 0x200 S_IEXEC = 0x40 @@ -2473,6 +2506,7 @@ const ( TS_COMM_LEN = 0x20 TUNATTACHFILTER = 0x801054d5 TUNDETACHFILTER = 0x801054d6 + TUNGETDEVNETNS = 0x200054e3 TUNGETFEATURES = 0x400454cf TUNGETFILTER = 0x401054db TUNGETIFF = 0x400454d2 diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_386.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_386.go index 33b6e4d1a..e869c0603 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_386.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_386.go @@ -423,4 +423,10 @@ const ( SYS_IO_URING_SETUP = 425 SYS_IO_URING_ENTER = 426 SYS_IO_URING_REGISTER = 427 + SYS_OPEN_TREE = 428 + SYS_MOVE_MOUNT = 429 + SYS_FSOPEN = 430 + SYS_FSCONFIG = 431 + SYS_FSMOUNT = 432 + SYS_FSPICK = 433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_amd64.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_amd64.go index 9ba207847..4917b8ab6 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_amd64.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_amd64.go @@ -345,4 +345,10 @@ const ( SYS_IO_URING_SETUP = 425 SYS_IO_URING_ENTER = 426 SYS_IO_URING_REGISTER = 427 + SYS_OPEN_TREE = 428 + SYS_MOVE_MOUNT = 429 + SYS_FSOPEN = 430 + SYS_FSCONFIG = 431 + SYS_FSMOUNT = 432 + SYS_FSPICK = 433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_arm.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_arm.go index 94f68f101..f85fcb4f8 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_arm.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_arm.go @@ -387,4 +387,10 @@ const ( SYS_IO_URING_SETUP = 425 SYS_IO_URING_ENTER = 426 SYS_IO_URING_REGISTER = 427 + SYS_OPEN_TREE = 428 + SYS_MOVE_MOUNT = 429 + SYS_FSOPEN = 430 + SYS_FSCONFIG = 431 + SYS_FSMOUNT = 432 + SYS_FSPICK = 433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_arm64.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_arm64.go index 15c413516..678a119bc 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_arm64.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_arm64.go @@ -290,4 +290,10 @@ const ( SYS_IO_URING_SETUP = 425 SYS_IO_URING_ENTER = 426 SYS_IO_URING_REGISTER = 427 + SYS_OPEN_TREE = 428 + SYS_MOVE_MOUNT = 429 + SYS_FSOPEN = 430 + SYS_FSCONFIG = 431 + SYS_FSMOUNT = 432 + SYS_FSPICK = 433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_mips.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_mips.go index 638465b14..222c9f9a2 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_mips.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_mips.go @@ -408,4 +408,10 @@ const ( SYS_IO_URING_SETUP = 4425 SYS_IO_URING_ENTER = 4426 SYS_IO_URING_REGISTER = 4427 + SYS_OPEN_TREE = 4428 + SYS_MOVE_MOUNT = 4429 + SYS_FSOPEN = 4430 + SYS_FSCONFIG = 4431 + SYS_FSMOUNT = 4432 + SYS_FSPICK = 4433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_mips64.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_mips64.go index 57ec82aac..28e6d0e9d 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_mips64.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_mips64.go @@ -338,4 +338,10 @@ const ( SYS_IO_URING_SETUP = 5425 SYS_IO_URING_ENTER = 5426 SYS_IO_URING_REGISTER = 5427 + SYS_OPEN_TREE = 5428 + SYS_MOVE_MOUNT = 5429 + SYS_FSOPEN = 5430 + SYS_FSCONFIG = 5431 + SYS_FSMOUNT = 5432 + SYS_FSPICK = 5433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_mips64le.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_mips64le.go index 825a3e3b0..e643c6f63 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_mips64le.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_mips64le.go @@ -338,4 +338,10 @@ const ( SYS_IO_URING_SETUP = 5425 SYS_IO_URING_ENTER = 5426 SYS_IO_URING_REGISTER = 5427 + SYS_OPEN_TREE = 5428 + SYS_MOVE_MOUNT = 5429 + SYS_FSOPEN = 5430 + SYS_FSCONFIG = 5431 + SYS_FSMOUNT = 5432 + SYS_FSPICK = 5433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_mipsle.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_mipsle.go index f152dfdd0..01d93c420 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_mipsle.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_mipsle.go @@ -408,4 +408,10 @@ const ( SYS_IO_URING_SETUP = 4425 SYS_IO_URING_ENTER = 4426 SYS_IO_URING_REGISTER = 4427 + SYS_OPEN_TREE = 4428 + SYS_MOVE_MOUNT = 4429 + SYS_FSOPEN = 4430 + SYS_FSCONFIG = 4431 + SYS_FSMOUNT = 4432 + SYS_FSPICK = 4433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_ppc64.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_ppc64.go index 7cbe78b19..5744149eb 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_ppc64.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_ppc64.go @@ -387,4 +387,10 @@ const ( SYS_IO_URING_SETUP = 425 SYS_IO_URING_ENTER = 426 SYS_IO_URING_REGISTER = 427 + SYS_OPEN_TREE = 428 + SYS_MOVE_MOUNT = 429 + SYS_FSOPEN = 430 + SYS_FSCONFIG = 431 + SYS_FSMOUNT = 432 + SYS_FSPICK = 433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_ppc64le.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_ppc64le.go index 51a2f1236..21c832042 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_ppc64le.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_ppc64le.go @@ -387,4 +387,10 @@ const ( SYS_IO_URING_SETUP = 425 SYS_IO_URING_ENTER = 426 SYS_IO_URING_REGISTER = 427 + SYS_OPEN_TREE = 428 + SYS_MOVE_MOUNT = 429 + SYS_FSOPEN = 430 + SYS_FSCONFIG = 431 + SYS_FSMOUNT = 432 + SYS_FSPICK = 433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_riscv64.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_riscv64.go index 323432ae3..c1bb6d8f2 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_riscv64.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_riscv64.go @@ -289,4 +289,10 @@ const ( SYS_IO_URING_SETUP = 425 SYS_IO_URING_ENTER = 426 SYS_IO_URING_REGISTER = 427 + SYS_OPEN_TREE = 428 + SYS_MOVE_MOUNT = 429 + SYS_FSOPEN = 430 + SYS_FSCONFIG = 431 + SYS_FSMOUNT = 432 + SYS_FSPICK = 433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_s390x.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_s390x.go index 9dca97484..bc3cc6b5b 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_s390x.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_s390x.go @@ -352,4 +352,10 @@ const ( SYS_IO_URING_SETUP = 425 SYS_IO_URING_ENTER = 426 SYS_IO_URING_REGISTER = 427 + SYS_OPEN_TREE = 428 + SYS_MOVE_MOUNT = 429 + SYS_FSOPEN = 430 + SYS_FSCONFIG = 431 + SYS_FSMOUNT = 432 + SYS_FSPICK = 433 ) diff --git a/vendor/golang.org/x/sys/unix/zsysnum_linux_sparc64.go b/vendor/golang.org/x/sys/unix/zsysnum_linux_sparc64.go index d3da46f0d..0a2841ba8 100644 --- a/vendor/golang.org/x/sys/unix/zsysnum_linux_sparc64.go +++ b/vendor/golang.org/x/sys/unix/zsysnum_linux_sparc64.go @@ -367,4 +367,10 @@ const ( SYS_IO_URING_SETUP = 425 SYS_IO_URING_ENTER = 426 SYS_IO_URING_REGISTER = 427 + SYS_OPEN_TREE = 428 + SYS_MOVE_MOUNT = 429 + SYS_FSOPEN = 430 + SYS_FSCONFIG = 431 + SYS_FSMOUNT = 432 + SYS_FSPICK = 433 ) diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_386.go b/vendor/golang.org/x/sys/unix/ztypes_linux_386.go index 5492b9666..50bc4128f 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_386.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_386.go @@ -2484,3 +2484,40 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint16 + Inode uint32 + Rdevice uint16 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]int8 + Encrypt_key [32]uint8 + Init [2]uint32 + Reserved [4]int8 +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_amd64.go b/vendor/golang.org/x/sys/unix/ztypes_linux_amd64.go index caf33b2c5..055eaa76a 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_amd64.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_amd64.go @@ -2497,3 +2497,41 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint64 + Inode uint64 + Rdevice uint64 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]int8 + Encrypt_key [32]uint8 + Init [2]uint64 + Reserved [4]int8 + _ [4]byte +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_arm.go b/vendor/golang.org/x/sys/unix/ztypes_linux_arm.go index 93aec7e22..66019c9cf 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_arm.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_arm.go @@ -2475,3 +2475,40 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint16 + Inode uint32 + Rdevice uint16 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint32 + Reserved [4]uint8 +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_arm64.go b/vendor/golang.org/x/sys/unix/ztypes_linux_arm64.go index 0a038436d..3104798c4 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_arm64.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_arm64.go @@ -2476,3 +2476,41 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint32 + Inode uint64 + Rdevice uint32 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]int8 + Encrypt_key [32]uint8 + Init [2]uint64 + Reserved [4]int8 + _ [4]byte +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_mips.go b/vendor/golang.org/x/sys/unix/ztypes_linux_mips.go index 2de0e5800..46c86021b 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_mips.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_mips.go @@ -2481,3 +2481,40 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint32 + Inode uint32 + Rdevice uint32 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]int8 + Encrypt_key [32]uint8 + Init [2]uint32 + Reserved [4]int8 +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_mips64.go b/vendor/golang.org/x/sys/unix/ztypes_linux_mips64.go index 3735eb42e..c2fe1a62a 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_mips64.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_mips64.go @@ -2478,3 +2478,41 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint32 + Inode uint64 + Rdevice uint32 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]int8 + Encrypt_key [32]uint8 + Init [2]uint64 + Reserved [4]int8 + _ [4]byte +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_mips64le.go b/vendor/golang.org/x/sys/unix/ztypes_linux_mips64le.go index 073c29939..f1eb0d397 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_mips64le.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_mips64le.go @@ -2478,3 +2478,41 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint32 + Inode uint64 + Rdevice uint32 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]int8 + Encrypt_key [32]uint8 + Init [2]uint64 + Reserved [4]int8 + _ [4]byte +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_mipsle.go b/vendor/golang.org/x/sys/unix/ztypes_linux_mipsle.go index 58d09f75e..8759bc36b 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_mipsle.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_mipsle.go @@ -2481,3 +2481,40 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint32 + Inode uint32 + Rdevice uint32 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]int8 + Encrypt_key [32]uint8 + Init [2]uint32 + Reserved [4]int8 +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_ppc64.go b/vendor/golang.org/x/sys/unix/ztypes_linux_ppc64.go index 3f1e62e03..a81200541 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_ppc64.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_ppc64.go @@ -2486,3 +2486,41 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint64 + Inode uint64 + Rdevice uint64 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 + Reserved [4]uint8 + _ [4]byte +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_ppc64le.go b/vendor/golang.org/x/sys/unix/ztypes_linux_ppc64le.go index e67be11eb..74b7a9199 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_ppc64le.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_ppc64le.go @@ -2486,3 +2486,41 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint64 + Inode uint64 + Rdevice uint64 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 + Reserved [4]uint8 + _ [4]byte +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_riscv64.go b/vendor/golang.org/x/sys/unix/ztypes_linux_riscv64.go index f44f29403..8344583e7 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_riscv64.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_riscv64.go @@ -2503,3 +2503,41 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint32 + Inode uint64 + Rdevice uint32 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 + Reserved [4]uint8 + _ [4]byte +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_s390x.go b/vendor/golang.org/x/sys/unix/ztypes_linux_s390x.go index 90bf5dcc7..d8fc0bc1c 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_s390x.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_s390x.go @@ -2500,3 +2500,41 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint16 + Inode uint64 + Rdevice uint16 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]int8 + Encrypt_key [32]uint8 + Init [2]uint64 + Reserved [4]int8 + _ [4]byte +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/unix/ztypes_linux_sparc64.go b/vendor/golang.org/x/sys/unix/ztypes_linux_sparc64.go index 4f054dcbb..5e0ab9329 100644 --- a/vendor/golang.org/x/sys/unix/ztypes_linux_sparc64.go +++ b/vendor/golang.org/x/sys/unix/ztypes_linux_sparc64.go @@ -2481,3 +2481,41 @@ const ( LINUX_CAPABILITY_VERSION_2 = 0x20071026 LINUX_CAPABILITY_VERSION_3 = 0x20080522 ) + +const ( + LO_FLAGS_READ_ONLY = 0x1 + LO_FLAGS_AUTOCLEAR = 0x4 + LO_FLAGS_PARTSCAN = 0x8 + LO_FLAGS_DIRECT_IO = 0x10 +) + +type LoopInfo struct { + Number int32 + Device uint32 + Inode uint64 + Rdevice uint32 + Offset int32 + Encrypt_type int32 + Encrypt_key_size int32 + Flags int32 + Name [64]int8 + Encrypt_key [32]uint8 + Init [2]uint64 + Reserved [4]int8 + _ [4]byte +} +type LoopInfo64 struct { + Device uint64 + Inode uint64 + Rdevice uint64 + Offset uint64 + Sizelimit uint64 + Number uint32 + Encrypt_type uint32 + Encrypt_key_size uint32 + Flags uint32 + File_name [64]uint8 + Crypt_name [64]uint8 + Encrypt_key [32]uint8 + Init [2]uint64 +} diff --git a/vendor/golang.org/x/sys/windows/types_windows.go b/vendor/golang.org/x/sys/windows/types_windows.go index 8a563f92b..1e3947f0f 100644 --- a/vendor/golang.org/x/sys/windows/types_windows.go +++ b/vendor/golang.org/x/sys/windows/types_windows.go @@ -197,8 +197,11 @@ const ( FILE_MAP_READ = 0x04 FILE_MAP_EXECUTE = 0x20 - CTRL_C_EVENT = 0 - CTRL_BREAK_EVENT = 1 + CTRL_C_EVENT = 0 + CTRL_BREAK_EVENT = 1 + CTRL_CLOSE_EVENT = 2 + CTRL_LOGOFF_EVENT = 5 + CTRL_SHUTDOWN_EVENT = 6 // Windows reserves errors >= 1<<29 for application use. APPLICATION_ERROR = 1 << 29 diff --git a/vendor/modules.txt b/vendor/modules.txt index ed363351b..998dec0bd 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -84,7 +84,7 @@ github.com/stretchr/testify/assert github.com/stretchr/testify/require # go.etcd.io/bbolt v1.3.3 go.etcd.io/bbolt -# golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 +# golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 golang.org/x/crypto/ssh/terminal golang.org/x/crypto/blake2b golang.org/x/crypto/blake2s @@ -93,12 +93,12 @@ golang.org/x/crypto/curve25519 golang.org/x/crypto/internal/chacha20 golang.org/x/crypto/internal/subtle golang.org/x/crypto/poly1305 -# golang.org/x/net v0.0.0-20190620200207-3b0461eec859 +# golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 golang.org/x/net/context golang.org/x/net/nettest golang.org/x/net/proxy golang.org/x/net/internal/socks -# golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb +# golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e golang.org/x/sys/unix golang.org/x/sys/windows golang.org/x/sys/windows/svc/eventlog From 56c74c54907e9e107640e8269f72c8120417cf37 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Wed, 31 Jul 2019 09:32:40 +0300 Subject: [PATCH 05/26] Changes: TCPFactory working correctly with predefined in/out ports 1. Rewritten with reverse index InMemoryTable 2. Fixed tests 3. Started integration environment for tcp-transport --- integration/check-delays.sh | 27 +++++ integration/tcp-tr/env-vars.sh | 0 integration/tcp-tr/hosts.pubkeys | 2 + integration/tcp-tr/nodeA.json | 67 +++++++++++ integration/tcp-tr/nodeB.json | 67 +++++++++++ pkg/transport/tcp_transport.go | 145 ++++++++++++++---------- pkg/transport/tcp_transport_test.go | 168 ++++++++++++++++++++++++---- 7 files changed, 397 insertions(+), 79 deletions(-) create mode 100644 integration/check-delays.sh create mode 100644 integration/tcp-tr/env-vars.sh create mode 100644 integration/tcp-tr/hosts.pubkeys create mode 100755 integration/tcp-tr/nodeA.json create mode 100755 integration/tcp-tr/nodeB.json diff --git a/integration/check-delays.sh b/integration/check-delays.sh new file mode 100644 index 000000000..56988cc79 --- /dev/null +++ b/integration/check-delays.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +MSGD=messaging.discovery.skywire.skycoin.net +MSGD_GET="https://"$MSGD"/messaging-discovery/available_servers" + +echo -e "\nTCP delays. Measuring by ping:" +ping $MSGD -c 10 -q + +if type mtr > /dev/null; then + echo -e "\nTCP delays. Measuring by mtr:" + mtr -y 2 --report --report-cycles=5 $MSGD > /tmp/msgd-out.txt + + cat /tmp/msgd-out.txt +else + echo -e "\nTCP delays. mtr not found. Install for detailed stats" +fi + +if type vegeta > /dev/null; then + echo -e "\nHTTP delays. Measuring by vegeta:" + echo "GET "$MSGD_GET \ + | vegeta attack -duration=10s |tee results.bin |vegeta report +else + echo -e "\nHTTP delays.vegeta not found\n. Install with \ngo get -u github.com/tsenart/vegeta\n for detailed stats" +fi + +echo -e "\nHTTP delays. Measuring by curl:" +curl $MSGD_GET >/dev/null diff --git a/integration/tcp-tr/env-vars.sh b/integration/tcp-tr/env-vars.sh new file mode 100644 index 000000000..e69de29bb diff --git a/integration/tcp-tr/hosts.pubkeys b/integration/tcp-tr/hosts.pubkeys new file mode 100644 index 000000000..04a7d4298 --- /dev/null +++ b/integration/tcp-tr/hosts.pubkeys @@ -0,0 +1,2 @@ +0322f66c5cac131376a2e6a20c6e31511baa7ec7bb0dccd26954c86c94af7d02b7 192.168.1.2:9119 +021adbbdf76f223f6e25ffe0b5626e600a1fcbb5fbbda833147a262a61b21312f3 192.168.1.3:9119 \ No newline at end of file diff --git a/integration/tcp-tr/nodeA.json b/integration/tcp-tr/nodeA.json new file mode 100755 index 000000000..bb37f70d9 --- /dev/null +++ b/integration/tcp-tr/nodeA.json @@ -0,0 +1,67 @@ +{ + "version": "1.0", + "node": { + "static_public_key": "0322f66c5cac131376a2e6a20c6e31511baa7ec7bb0dccd26954c86c94af7d02b7", + "static_secret_key": "8300741a43bcb60e44cc407980ff2bfecd762fd59b900c98f22e5cce77476e41" + }, + "messaging": { + "discovery": "https://messaging.discovery.skywire.skycoin.net", + "server_count": 1 + }, + "transport": { + "discovery": "https://transport.discovery.skywire.skycoin.net", + "log_store": { + "type": "file", + "location": "./skywire/transport_logs" + } + }, + "routing": { + "setup_nodes": [ + "0324579f003e6b4048bae2def4365e634d8e0e3054a20fc7af49daf2a179658557" + ], + "route_finder": "https://routefinder.skywire.skycoin.net/", + "route_finder_timeout": "10s", + "table": { + "type": "boltdb", + "location": "./skywire/routing.db" + } + }, + "apps": [ + { + "version": "1.0", + "app": "skychat", + "auto_start": true, + "port": 1, + "args": [] + }, + { + "version": "1.0", + "app": "SSH", + "auto_start": true, + "port": 2, + "args": [] + }, + { + "version": "1.0", + "app": "socksproxy", + "auto_start": true, + "port": 3, + "args": [ + "-passcode", + "1yHtvMg29Zs=" + ] + } + ], + "trusted_nodes": [], + "hypervisors": [], + "apps_path": "./apps", + "local_path": "./local", + "transport_type": "dmsg", + "pubkeys_file": "./local/pubkeys", + "tcptransport_addr": "", + "shutdown_timeout": "10s", + "interfaces": { + "rpc": "localhost:3435" + }, + "log_level": "info" +} \ No newline at end of file diff --git a/integration/tcp-tr/nodeB.json b/integration/tcp-tr/nodeB.json new file mode 100755 index 000000000..fc5d94d4b --- /dev/null +++ b/integration/tcp-tr/nodeB.json @@ -0,0 +1,67 @@ +{ + "version": "1.0", + "node": { + "static_public_key": "021adbbdf76f223f6e25ffe0b5626e600a1fcbb5fbbda833147a262a61b21312f3", + "static_secret_key": "ec1ba10659fca5e3847347b6978244013c651f6283f736c515f003407370649f" + }, + "messaging": { + "discovery": "https://messaging.discovery.skywire.skycoin.net", + "server_count": 1 + }, + "transport": { + "discovery": "https://transport.discovery.skywire.skycoin.net", + "log_store": { + "type": "file", + "location": "./skywire/transport_logs" + } + }, + "routing": { + "setup_nodes": [ + "0324579f003e6b4048bae2def4365e634d8e0e3054a20fc7af49daf2a179658557" + ], + "route_finder": "https://routefinder.skywire.skycoin.net/", + "route_finder_timeout": "10s", + "table": { + "type": "boltdb", + "location": "./skywire/routing.db" + } + }, + "apps": [ + { + "version": "1.0", + "app": "skychat", + "auto_start": true, + "port": 1, + "args": [] + }, + { + "version": "1.0", + "app": "SSH", + "auto_start": true, + "port": 2, + "args": [] + }, + { + "version": "1.0", + "app": "socksproxy", + "auto_start": true, + "port": 3, + "args": [ + "-passcode", + "uMsqIvgT9Cs=" + ] + } + ], + "trusted_nodes": [], + "hypervisors": [], + "apps_path": "./apps", + "local_path": "./local", + "transport_type": "dmsg", + "pubkeys_file": "./local/pubkeys", + "tcptransport_addr": "", + "shutdown_timeout": "10s", + "interfaces": { + "rpc": "localhost:3435" + }, + "log_level": "info" +} \ No newline at end of file diff --git a/pkg/transport/tcp_transport.go b/pkg/transport/tcp_transport.go index fff4107f4..69eb62540 100644 --- a/pkg/transport/tcp_transport.go +++ b/pkg/transport/tcp_transport.go @@ -53,9 +53,9 @@ func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { } raddr := conn.RemoteAddr().(*net.TCPAddr) - rpk := f.PkTable.RemotePK(raddr.IP) + rpk := f.PkTable.RemotePK(raddr.String()) if rpk.Null() { - return nil, ErrUnknownRemote + return nil, fmt.Errorf("Error: %v\n, raddr: %v\n, rpk: %v\n", ErrUnknownRemote, raddr, rpk) } return &TCPTransport{conn, [2]cipher.PubKey{f.Pk, rpk}}, nil @@ -63,12 +63,26 @@ func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { // Dial initiates a Transport with a remote node. func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, error) { - raddr := f.PkTable.RemoteAddr(remote) - if raddr == nil { + addr := f.PkTable.RemoteAddr(remote) + if addr == "" { return nil, ErrUnknownRemote } - conn, err := net.DialTCP("tcp", nil, raddr) + tcpAddr, err := net.ResolveTCPAddr("tcp", addr) + if err != nil { + return nil, err + } + + lsnAddr, err := net.ResolveTCPAddr("tcp", f.Lsr.Addr().String()) + if err != nil { + return nil, fmt.Errorf("error in resolving local address") + } + locAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%v:%v", lsnAddr.IP.String(), "9229")) + if err != nil { + return nil, fmt.Errorf("error in constructing local address ") + } + + conn, err := net.DialTCP("tcp", locAddr, tcpAddr) if err != nil { return nil, err } @@ -112,35 +126,44 @@ func (tr *TCPTransport) Type() string { // PubKeyTable provides translation between remote PubKey and TCPAddr. type PubKeyTable interface { - RemoteAddr(remotePK cipher.PubKey) *net.TCPAddr - RemotePK(remoteIP net.IP) cipher.PubKey + RemoteAddr(remotePK cipher.PubKey) string + RemotePK(address string) cipher.PubKey } type inMemoryPKTable struct { - entries map[cipher.PubKey]*net.TCPAddr + entries map[cipher.PubKey]string + reverse map[string]cipher.PubKey +} + +func inMemoryPubKeyTable(entries map[cipher.PubKey]string) *inMemoryPKTable { + reverse := make(map[string]cipher.PubKey) + for k, v := range entries { + addr, err := net.ResolveTCPAddr("tcp", v) + if err != nil { + panic("error in resolving address") + } + + reverse[fmt.Sprintf("%s:%s", addr.IP, "9229")] = k + } + return &inMemoryPKTable{entries, reverse} } // InMemoryPubKeyTable returns in memory implementation of the PubKeyTable. -func InMemoryPubKeyTable(entries map[cipher.PubKey]*net.TCPAddr) PubKeyTable { - return &inMemoryPKTable{entries} +func InMemoryPubKeyTable(entries map[cipher.PubKey]string) PubKeyTable { + return inMemoryPubKeyTable(entries) } -func (t *inMemoryPKTable) RemoteAddr(remotePK cipher.PubKey) *net.TCPAddr { +func (t *inMemoryPKTable) RemoteAddr(remotePK cipher.PubKey) string { return t.entries[remotePK] } -func (t *inMemoryPKTable) RemotePK(remoteIP net.IP) cipher.PubKey { - for pk, addr := range t.entries { - if addr.IP.String() == remoteIP.String() { - return pk - } - } - - return cipher.PubKey{} +func (t *inMemoryPKTable) RemotePK(address string) cipher.PubKey { + return t.reverse[address] } type filePKTable struct { - dbFile *os.File + dbFile string + *inMemoryPKTable } // FilePubKeyTable returns file based implementation of the PubKeyTable. @@ -155,43 +178,9 @@ func FilePubKeyTable(dbFile string) (PubKeyTable, error) { return nil, err } - return &filePKTable{f}, nil -} - -func (t *filePKTable) RemoteAddr(remotePK cipher.PubKey) *net.TCPAddr { - var raddr *net.TCPAddr - t.Seek(func(pk cipher.PubKey, addr *net.TCPAddr) bool { - if pk == remotePK { - raddr = addr - return true - } - - return false - }) - return raddr -} + entries := make(map[cipher.PubKey]string) -func (t *filePKTable) RemotePK(remoteIP net.IP) cipher.PubKey { - var rpk cipher.PubKey - t.Seek(func(pk cipher.PubKey, addr *net.TCPAddr) bool { - if remoteIP.String() == addr.IP.String() { - rpk = pk - return true - } - - return false - }) - return rpk -} - -func (t *filePKTable) Seek(seekFunc func(pk cipher.PubKey, addr *net.TCPAddr) bool) { - defer func() { - if _, err := t.dbFile.Seek(0, 0); err != nil { - log.WithError(err).Warn("Failed to seek to the beginning of DB") - } - }() - - scanner := bufio.NewScanner(t.dbFile) + scanner := bufio.NewScanner(f) for scanner.Scan() { components := strings.Fields(scanner.Text()) if len(components) != 2 { @@ -208,8 +197,46 @@ func (t *filePKTable) Seek(seekFunc func(pk cipher.PubKey, addr *net.TCPAddr) bo continue } - if seekFunc(pk, addr) { - return - } + entries[pk] = addr.String() } + + return &filePKTable{dbFile, inMemoryPubKeyTable(entries)}, nil } + +func (t *filePKTable) RemoteAddr(remotePK cipher.PubKey) string { + return t.entries[remotePK] +} + +func (t *filePKTable) RemotePK(address string) cipher.PubKey { + return t.reverse[address] +} + +// func (t *filePKTable) Seek(seekFunc func(pk cipher.PubKey, addr *net.TCPAddr) bool) { +// defer func() { +// if _, err := t.dbFile.Seek(0, 0); err != nil { +// log.WithError(err).Warn("Failed to seek to the beginning of DB") +// } +// }() + +// scanner := bufio.NewScanner(t.dbFile) +// for scanner.Scan() { +// components := strings.Fields(scanner.Text()) +// if len(components) != 2 { +// continue +// } + +// pk := cipher.PubKey{} +// if err := pk.UnmarshalText([]byte(components[0])); err != nil { +// continue +// } + +// addr, err := net.ResolveTCPAddr("tcp", components[1]) +// if err != nil { +// continue +// } + +// if seekFunc(pk, addr) { +// return +// } +// } +// } diff --git a/pkg/transport/tcp_transport_test.go b/pkg/transport/tcp_transport_test.go index fecf3a227..211213d2c 100644 --- a/pkg/transport/tcp_transport_test.go +++ b/pkg/transport/tcp_transport_test.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net" "os" + "sync" "testing" "github.com/skycoin/dmsg/cipher" @@ -15,6 +16,79 @@ import ( "github.com/skycoin/skywire/pkg/transport" ) +func Example_transport_InMemoryPubKeyTable() { + /* + This test requires IPs on host + sudo ip addr add 192.168.1.2 dev lo + sudo ip addr add 192.168.1.3 dev lo + */ + + if true { + return + } + + pk1, _ := cipher.GenerateKeyPair() + pk2, _ := cipher.GenerateKeyPair() + + addr1, err := net.ResolveTCPAddr("tcp", "192.168.1.2:9119") + l1, err := net.ListenTCP("tcp", addr1) + + addr2, err := net.ResolveTCPAddr("tcp", "192.168.1.3:9119") + l2, err := net.ListenTCP("tcp", addr2) + + entries := map[cipher.PubKey]string{ + pk2: addr2.String(), + pk1: addr1.String(), + } + + pkt1 := transport.InMemoryPubKeyTable(entries) + pkt2 := transport.InMemoryPubKeyTable(entries) + + f1 := &transport.TCPFactory{pk1, pkt1, l1} + f2 := &transport.TCPFactory{pk2, pkt2, l2} + + var wg sync.WaitGroup + + wg.Add(1) + go func() { + defer wg.Done() + tr, err := f1.Accept(context.TODO()) + if err != nil { + fmt.Printf("Accept err: %v\n", err) + return + } + fmt.Printf("Accept success: %v\n", err == nil) + + if _, err := tr.Write([]byte("Hallo!")); err != nil { + fmt.Printf("Write err: %v\n", err) + return + } + fmt.Printf("Write success: %v\n", err == nil) + return + }() + + tr, err := f2.Dial(context.TODO(), pk1) + if err != nil { + fmt.Printf("Dial err: %v\n", err) + } + fmt.Printf("Dial success: %v\n", err == nil) + + buf := make([]byte, 6) + _, err = tr.Read(buf) + if err != nil { + fmt.Printf("Read err: %v\n", err) + } + + fmt.Printf("Message recieved: %s\n", buf) + wg.Wait() + fmt.Println("Finish") + + // Output: Dial success: true + // Accept success: true + // Write success: true + // Message recieved: Hallo! +} + func TestTCPFactory(t *testing.T) { pk1, _ := cipher.GenerateKeyPair() pk2, _ := cipher.GenerateKeyPair() @@ -29,40 +103,65 @@ func TestTCPFactory(t *testing.T) { l2, err := net.ListenTCP("tcp", addr2) require.NoError(t, err) - pkt1 := transport.InMemoryPubKeyTable(map[cipher.PubKey]*net.TCPAddr{pk2: addr2}) - pkt2 := transport.InMemoryPubKeyTable(map[cipher.PubKey]*net.TCPAddr{pk1: addr1}) + pkt1 := transport.InMemoryPubKeyTable(map[cipher.PubKey]string{pk2: addr2.String()}) + pkt2 := transport.InMemoryPubKeyTable(map[cipher.PubKey]string{pk1: addr1.String()}) + + fmt.Println(addr1.String()) + fmt.Println(addr2.String()) f1 := &transport.TCPFactory{pk1, pkt1, l1} - errCh := make(chan error) + assert.Equal(t, "tcp", f1.Type()) + assert.Equal(t, pk1, f1.Local()) + + f2 := &transport.TCPFactory{pk2, pkt2, l2} + assert.Equal(t, "tcp", f2.Type()) + assert.Equal(t, pk2, f2.Local()) + + var wg sync.WaitGroup + + wg.Add(2) + errAcceptCh := make(chan error) go func() { tr, err := f1.Accept(context.TODO()) if err != nil { - errCh <- err + errAcceptCh <- err return } - if _, err := tr.Write([]byte("foo")); err != nil { - errCh <- err + if _, err := tr.Write([]byte("Hello!")); err != nil { + errAcceptCh <- err return } - errCh <- nil + require.NoError(t, tr.Close()) + close(errAcceptCh) + wg.Done() }() - f2 := &transport.TCPFactory{pk2, pkt2, l2} - assert.Equal(t, "tcp", f2.Type()) - assert.Equal(t, pk2, f2.Local()) + errDialCh := make(chan error) + go func() { + tr, err := f2.Dial(context.TODO(), pk1) + if err != nil { + errDialCh <- err + } - tr, err := f2.Dial(context.TODO(), pk1) - require.NoError(t, err) - assert.Equal(t, "tcp", tr.Type()) + buf := make([]byte, 6) + _, err = tr.Read(buf) + if err != nil { + errDialCh <- err + } - buf := make([]byte, 3) - _, err = tr.Read(buf) - require.NoError(t, err) - assert.Equal(t, []byte("foo"), buf) + assert.Equal(t, []byte("Hello!"), buf) + require.NoError(t, tr.Close()) + + close(errDialCh) + wg.Done() + }() - require.NoError(t, tr.Close()) + wg.Wait() + + require.NoError(t, <-errAcceptCh) + require.NoError(t, <-errDialCh) require.NoError(t, f2.Close()) require.NoError(t, f1.Close()) } @@ -86,8 +185,37 @@ func TestFilePKTable(t *testing.T) { require.NoError(t, err) raddr := pkt.RemoteAddr(pk) - assert.Equal(t, addr, raddr) + assert.Equal(t, addr.String(), raddr) - rpk := pkt.RemotePK(addr.IP) + rpk := pkt.RemotePK(addr.String()) assert.Equal(t, pk, rpk) } + +func Example_transport_FilePubKetTable() { + + pkLine := func(seed, addr string) string { + pk, _, _ := cipher.GenerateDeterministicKeyPair([]byte(seed)) + return fmt.Sprintf("%s\t%s\n", pk, addr) + } + + pkFileContent := + fmt.Sprintf("%v%v", pkLine("tcp-tr nodeA", "192.168.1.2:9119"), + pkLine("tcp-tr nodeB", "192.168.1.3:9119")) + fmt.Printf("pubkeys:\n%v", pkFileContent) + + tmpfile, _ := ioutil.TempFile("", "pktable") + defer os.Remove(tmpfile.Name()) + + _, _ = tmpfile.Write([]byte(pkFileContent)) + + pkt, err := transport.FilePubKeyTable(tmpfile.Name()) + fmt.Printf("Opening FilePubKeyTable success: %v\n", err == nil) + + fmt.Printf("PK for 192.168.1.2:9119: %v\n", pkt.RemotePK("192.168.1.2:9119")) + + // Output: pubkeys: + // 0322f66c5cac131376a2e6a20c6e31511baa7ec7bb0dccd26954c86c94af7d02b7 192.168.1.2:9119 + // 021adbbdf76f223f6e25ffe0b5626e600a1fcbb5fbbda833147a262a61b21312f3 192.168.1.3:9119 + // Opening FilePubKeyTable success: true + // PK for 192.168.1.2:9119 0322f66c5cac131376a2e6a20c6e31511baa7ec7bb0dccd26954c86c94af7d02b7 +} From 15226f07436b49bceb4667540225137b2b1a21e0 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Wed, 31 Jul 2019 12:27:47 +0300 Subject: [PATCH 06/26] Now works with dynamic ports from diallers --- pkg/transport/tcp_transport.go | 72 ++++--------- pkg/transport/tcp_transport_test.go | 153 ++++++++++++++++++---------- 2 files changed, 116 insertions(+), 109 deletions(-) diff --git a/pkg/transport/tcp_transport.go b/pkg/transport/tcp_transport.go index 69eb62540..854664398 100644 --- a/pkg/transport/tcp_transport.go +++ b/pkg/transport/tcp_transport.go @@ -55,7 +55,7 @@ func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { raddr := conn.RemoteAddr().(*net.TCPAddr) rpk := f.PkTable.RemotePK(raddr.String()) if rpk.Null() { - return nil, fmt.Errorf("Error: %v\n, raddr: %v\n, rpk: %v\n", ErrUnknownRemote, raddr, rpk) + return nil, fmt.Errorf("error: %v, raddr: %v, rpk: %v", ErrUnknownRemote, raddr.String(), rpk) } return &TCPTransport{conn, [2]cipher.PubKey{f.Pk, rpk}}, nil @@ -77,7 +77,7 @@ func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, if err != nil { return nil, fmt.Errorf("error in resolving local address") } - locAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%v:%v", lsnAddr.IP.String(), "9229")) + locAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%v:%v", lsnAddr.IP.String(), "0")) if err != nil { return nil, fmt.Errorf("error in constructing local address ") } @@ -130,40 +130,43 @@ type PubKeyTable interface { RemotePK(address string) cipher.PubKey } -type inMemoryPKTable struct { +type memPKTable struct { entries map[cipher.PubKey]string reverse map[string]cipher.PubKey } -func inMemoryPubKeyTable(entries map[cipher.PubKey]string) *inMemoryPKTable { +func memoryPubKeyTable(entries map[cipher.PubKey]string) *memPKTable { reverse := make(map[string]cipher.PubKey) for k, v := range entries { addr, err := net.ResolveTCPAddr("tcp", v) if err != nil { panic("error in resolving address") } - - reverse[fmt.Sprintf("%s:%s", addr.IP, "9229")] = k + reverse[addr.IP.String()] = k } - return &inMemoryPKTable{entries, reverse} + return &memPKTable{entries, reverse} } -// InMemoryPubKeyTable returns in memory implementation of the PubKeyTable. -func InMemoryPubKeyTable(entries map[cipher.PubKey]string) PubKeyTable { - return inMemoryPubKeyTable(entries) +// MemoryPKTable returns in memory implementation of the PubKeyTable. +func MemoryPubKeyTable(entries map[cipher.PubKey]string) PubKeyTable { + return memoryPubKeyTable(entries) } -func (t *inMemoryPKTable) RemoteAddr(remotePK cipher.PubKey) string { +func (t *memPKTable) RemoteAddr(remotePK cipher.PubKey) string { return t.entries[remotePK] } -func (t *inMemoryPKTable) RemotePK(address string) cipher.PubKey { - return t.reverse[address] +func (t *memPKTable) RemotePK(address string) cipher.PubKey { + addr, err := net.ResolveTCPAddr("tcp", address) + if err != nil { + panic("net.ResolveTCPAddr") + } + return t.reverse[addr.IP.String()] } type filePKTable struct { dbFile string - *inMemoryPKTable + *memPKTable } // FilePubKeyTable returns file based implementation of the PubKeyTable. @@ -179,7 +182,6 @@ func FilePubKeyTable(dbFile string) (PubKeyTable, error) { } entries := make(map[cipher.PubKey]string) - scanner := bufio.NewScanner(f) for scanner.Scan() { components := strings.Fields(scanner.Text()) @@ -200,43 +202,5 @@ func FilePubKeyTable(dbFile string) (PubKeyTable, error) { entries[pk] = addr.String() } - return &filePKTable{dbFile, inMemoryPubKeyTable(entries)}, nil -} - -func (t *filePKTable) RemoteAddr(remotePK cipher.PubKey) string { - return t.entries[remotePK] -} - -func (t *filePKTable) RemotePK(address string) cipher.PubKey { - return t.reverse[address] + return &filePKTable{dbFile, memoryPubKeyTable(entries)}, nil } - -// func (t *filePKTable) Seek(seekFunc func(pk cipher.PubKey, addr *net.TCPAddr) bool) { -// defer func() { -// if _, err := t.dbFile.Seek(0, 0); err != nil { -// log.WithError(err).Warn("Failed to seek to the beginning of DB") -// } -// }() - -// scanner := bufio.NewScanner(t.dbFile) -// for scanner.Scan() { -// components := strings.Fields(scanner.Text()) -// if len(components) != 2 { -// continue -// } - -// pk := cipher.PubKey{} -// if err := pk.UnmarshalText([]byte(components[0])); err != nil { -// continue -// } - -// addr, err := net.ResolveTCPAddr("tcp", components[1]) -// if err != nil { -// continue -// } - -// if seekFunc(pk, addr) { -// return -// } -// } -// } diff --git a/pkg/transport/tcp_transport_test.go b/pkg/transport/tcp_transport_test.go index 211213d2c..353ffe8d3 100644 --- a/pkg/transport/tcp_transport_test.go +++ b/pkg/transport/tcp_transport_test.go @@ -16,43 +16,44 @@ import ( "github.com/skycoin/skywire/pkg/transport" ) -func Example_transport_InMemoryPubKeyTable() { - /* - This test requires IPs on host - sudo ip addr add 192.168.1.2 dev lo - sudo ip addr add 192.168.1.3 dev lo - */ - - if true { - return +/* + This test requires IPs on host + sudo ip addr add 192.168.1.2 dev lo + sudo ip addr add 192.168.1.3 dev lo +*/ + +func Example_transport_TCPFactory() { + pkA := pkFromSeed("nodeA") + pkB := pkFromSeed("nodeB") + ipA := "192.168.1.2:9119" + ipB := "192.168.1.3:9119" + + addrA, _ := net.ResolveTCPAddr("tcp", ipA) + lsnA, err := net.ListenTCP("tcp", addrA) + if err != nil { + fmt.Println(err) } - pk1, _ := cipher.GenerateKeyPair() - pk2, _ := cipher.GenerateKeyPair() - - addr1, err := net.ResolveTCPAddr("tcp", "192.168.1.2:9119") - l1, err := net.ListenTCP("tcp", addr1) - - addr2, err := net.ResolveTCPAddr("tcp", "192.168.1.3:9119") - l2, err := net.ListenTCP("tcp", addr2) - - entries := map[cipher.PubKey]string{ - pk2: addr2.String(), - pk1: addr1.String(), + addrB, _ := net.ResolveTCPAddr("tcp", ipB) + lsnB, err := net.ListenTCP("tcp", addrB) + if err != nil { + fmt.Println(err) } - pkt1 := transport.InMemoryPubKeyTable(entries) - pkt2 := transport.InMemoryPubKeyTable(entries) - - f1 := &transport.TCPFactory{pk1, pkt1, l1} - f2 := &transport.TCPFactory{pk2, pkt2, l2} + pkt := transport.MemoryPubKeyTable( + map[cipher.PubKey]string{ + pkA: addrA.String(), + pkB: addrB.String(), + }) var wg sync.WaitGroup - wg.Add(1) + wg.Add(2) go func() { defer wg.Done() - tr, err := f1.Accept(context.TODO()) + + fA := &transport.TCPFactory{pkA, pkt, lsnA} + tr, err := fA.Accept(context.TODO()) if err != nil { fmt.Printf("Accept err: %v\n", err) return @@ -67,26 +68,32 @@ func Example_transport_InMemoryPubKeyTable() { return }() - tr, err := f2.Dial(context.TODO(), pk1) - if err != nil { - fmt.Printf("Dial err: %v\n", err) - } - fmt.Printf("Dial success: %v\n", err == nil) + go func() { + defer wg.Done() + fB := &transport.TCPFactory{pkB, pkt, lsnB} + tr, err := fB.Dial(context.TODO(), pkA) + if err != nil { + fmt.Printf("Dial err: %v\n", err) + } + fmt.Printf("Dial success: %v\n", err == nil) - buf := make([]byte, 6) - _, err = tr.Read(buf) - if err != nil { - fmt.Printf("Read err: %v\n", err) - } + buf := make([]byte, 6) + _, err = tr.Read(buf) + if err != nil { + fmt.Printf("Read err: %v\n", err) + } - fmt.Printf("Message recieved: %s\n", buf) + fmt.Printf("Message recieved: %s\n", buf) + }() wg.Wait() + fmt.Println("Finish") - // Output: Dial success: true - // Accept success: true + // Unordered output: Accept success: true // Write success: true + // Dial success: true // Message recieved: Hallo! + // Finish } func TestTCPFactory(t *testing.T) { @@ -103,8 +110,8 @@ func TestTCPFactory(t *testing.T) { l2, err := net.ListenTCP("tcp", addr2) require.NoError(t, err) - pkt1 := transport.InMemoryPubKeyTable(map[cipher.PubKey]string{pk2: addr2.String()}) - pkt2 := transport.InMemoryPubKeyTable(map[cipher.PubKey]string{pk1: addr1.String()}) + pkt1 := transport.MemoryPubKeyTable(map[cipher.PubKey]string{pk2: addr2.String()}) + pkt2 := transport.MemoryPubKeyTable(map[cipher.PubKey]string{pk1: addr1.String()}) fmt.Println(addr1.String()) fmt.Println(addr2.String()) @@ -191,31 +198,67 @@ func TestFilePKTable(t *testing.T) { assert.Equal(t, pk, rpk) } -func Example_transport_FilePubKetTable() { +func pkFromSeed(seed string) cipher.PubKey { + pk, _, err := cipher.GenerateDeterministicKeyPair([]byte(seed)) + if err != nil { + return cipher.PubKey{} + } + return pk +} - pkLine := func(seed, addr string) string { - pk, _, _ := cipher.GenerateDeterministicKeyPair([]byte(seed)) - return fmt.Sprintf("%s\t%s\n", pk, addr) +func Example_transport_MemoryPubKeyTable() { + pkA, pkB := pkFromSeed("nodeA"), pkFromSeed("nodeB") + ipA, ipB := "192.168.1.2:9119", "192.168.1.3:9119" + ipAA := "192.168.1.2:54312" + entries := map[cipher.PubKey]string{ + pkA: ipA, + pkB: ipB, } + pkt := transport.MemoryPubKeyTable(entries) + + fmt.Printf("ipA: %v\n", pkt.RemoteAddr(pkA)) + fmt.Printf("pkB in: %v\n", pkt.RemotePK(ipA)) + fmt.Printf("pkA out: %v\n", pkt.RemotePK(ipAA)) + + // Output: ipA: 192.168.1.2:9119 + // pkB in: 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab + // pkA out: 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab +} + +func Example_transport_FilePubKeyTable() { + pkA, pkB := pkFromSeed("nodeA"), pkFromSeed("nodeB") + ipA, ipB := "192.168.1.2:9119", "192.168.1.3:9119" + ipAA := "192.168.1.2:54312" pkFileContent := - fmt.Sprintf("%v%v", pkLine("tcp-tr nodeA", "192.168.1.2:9119"), - pkLine("tcp-tr nodeB", "192.168.1.3:9119")) + fmt.Sprintf("%v%v", + fmt.Sprintf("%s\t%s\n", pkA, ipA), + fmt.Sprintf("%s\t%s\n", pkB, ipB)) fmt.Printf("pubkeys:\n%v", pkFileContent) tmpfile, _ := ioutil.TempFile("", "pktable") - defer os.Remove(tmpfile.Name()) - _, _ = tmpfile.Write([]byte(pkFileContent)) + _, err := tmpfile.Write([]byte(pkFileContent)) + fmt.Printf("Write file success: %v\n", err == nil) pkt, err := transport.FilePubKeyTable(tmpfile.Name()) + // pkt.RemoteAddr(pkFromSeed("nodeA")) + // pkt.RemoteAddr() + fmt.Printf("Opening FilePubKeyTable success: %v\n", err == nil) - fmt.Printf("PK for 192.168.1.2:9119: %v\n", pkt.RemotePK("192.168.1.2:9119")) + fmt.Printf("ipA: %v\n", pkt.RemoteAddr(pkA)) + fmt.Printf("PK for ipA: %v\n", pkt.RemotePK(ipA)) + fmt.Printf("PK for ipAA: %v\n", pkt.RemotePK(ipAA)) + fmt.Printf("PK for ipB: %v\n", pkt.RemotePK(ipB)) // Output: pubkeys: - // 0322f66c5cac131376a2e6a20c6e31511baa7ec7bb0dccd26954c86c94af7d02b7 192.168.1.2:9119 - // 021adbbdf76f223f6e25ffe0b5626e600a1fcbb5fbbda833147a262a61b21312f3 192.168.1.3:9119 + // 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab 192.168.1.2:9119 + // 033978326862c191eaa39e33bb556a6296466facfe36bfb81e6b4c99d9c510e09f 192.168.1.3:9119 + // Write file success: true // Opening FilePubKeyTable success: true - // PK for 192.168.1.2:9119 0322f66c5cac131376a2e6a20c6e31511baa7ec7bb0dccd26954c86c94af7d02b7 + // ipA: 192.168.1.2:9119 + // PK for ipA: 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab + // PK for ipAA: 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab + // PK for ipB: 033978326862c191eaa39e33bb556a6296466facfe36bfb81e6b4c99d9c510e09f } From afbdd853bbdc76f73f1488bb6f714c884b97ae3a Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Wed, 31 Jul 2019 13:43:55 +0300 Subject: [PATCH 07/26] Integration environment ready. Use ``` $ source ./integration/tcp-tr/env-vars.sh ``` to init and everything as usual --- integration/tcp-tr/env-vars.sh | 13 +++++++++ integration/tcp-tr/hosts.pubkeys | 4 +-- integration/tcp-tr/nodeA.json | 43 ++++++++++------------------- integration/tcp-tr/nodeB.json | 42 ++++++++++------------------ pkg/transport/tcp_transport_test.go | 2 +- pkg/visor/visor.go | 16 +++++++---- 6 files changed, 56 insertions(+), 64 deletions(-) diff --git a/integration/tcp-tr/env-vars.sh b/integration/tcp-tr/env-vars.sh index e69de29bb..8fe8e0738 100644 --- a/integration/tcp-tr/env-vars.sh +++ b/integration/tcp-tr/env-vars.sh @@ -0,0 +1,13 @@ +# intended to be sourced `source ./integration/tcp-tr/env-vars.sh` + +export RPC_A=192.168.1.2:3435 +export RPC_C=192.168.1.3:3435 + +alias CLI_A='./skywire-cli --rpc $RPC_A' +alias CLI_C='./skywire-cli --rpc $RPC_C' + +export PK_A=$(./skywire-cli --rpc $RPC_A node pk) +export PK_C=$(./skywire-cli --rpc $RPC_C node pk) + +export CHAT_A=http://192.168.1.2:8001/message +export CHAT_C=http://192.168.1.3:8001/message \ No newline at end of file diff --git a/integration/tcp-tr/hosts.pubkeys b/integration/tcp-tr/hosts.pubkeys index 04a7d4298..035d10d44 100644 --- a/integration/tcp-tr/hosts.pubkeys +++ b/integration/tcp-tr/hosts.pubkeys @@ -1,2 +1,2 @@ -0322f66c5cac131376a2e6a20c6e31511baa7ec7bb0dccd26954c86c94af7d02b7 192.168.1.2:9119 -021adbbdf76f223f6e25ffe0b5626e600a1fcbb5fbbda833147a262a61b21312f3 192.168.1.3:9119 \ No newline at end of file +03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab 192.168.1.2:9119 +033978326862c191eaa39e33bb556a6296466facfe36bfb81e6b4c99d9c510e09f 192.168.1.3:9119 \ No newline at end of file diff --git a/integration/tcp-tr/nodeA.json b/integration/tcp-tr/nodeA.json index bb37f70d9..e048dd703 100755 --- a/integration/tcp-tr/nodeA.json +++ b/integration/tcp-tr/nodeA.json @@ -12,7 +12,7 @@ "discovery": "https://transport.discovery.skywire.skycoin.net", "log_store": { "type": "file", - "location": "./skywire/transport_logs" + "location": "./local/tcp-tr/nodeA/transport-logs" } }, "routing": { @@ -23,45 +23,32 @@ "route_finder_timeout": "10s", "table": { "type": "boltdb", - "location": "./skywire/routing.db" + "location": "./local/tcp-tr/nodeA/routing.db" } }, "apps": [ { - "version": "1.0", - "app": "skychat", - "auto_start": true, - "port": 1, - "args": [] - }, - { - "version": "1.0", - "app": "SSH", - "auto_start": true, - "port": 2, - "args": [] - }, - { - "version": "1.0", - "app": "socksproxy", - "auto_start": true, - "port": 3, - "args": [ - "-passcode", - "1yHtvMg29Zs=" - ] + "app": "skychat", + "version": "1.0", + "auto_start": true, + "port": 1, + "args": [ + "-addr", + "192.168.1.2:8001" + ] } ], + "trusted_nodes": [], "hypervisors": [], "apps_path": "./apps", "local_path": "./local", - "transport_type": "dmsg", - "pubkeys_file": "./local/pubkeys", - "tcptransport_addr": "", + "transport_type": "tcp-transport", + "pubkeys_file": "./integration/tcp-tr/hosts.pubkeys", + "tcptransport_addr": "192.168.1.2:9119", "shutdown_timeout": "10s", "interfaces": { - "rpc": "localhost:3435" + "rpc": "192.168.1.2:3435" }, "log_level": "info" } \ No newline at end of file diff --git a/integration/tcp-tr/nodeB.json b/integration/tcp-tr/nodeB.json index fc5d94d4b..c07e3dc16 100755 --- a/integration/tcp-tr/nodeB.json +++ b/integration/tcp-tr/nodeB.json @@ -12,7 +12,7 @@ "discovery": "https://transport.discovery.skywire.skycoin.net", "log_store": { "type": "file", - "location": "./skywire/transport_logs" + "location": "./local/tcp-tr/nodeB/transport-logs" } }, "routing": { @@ -23,45 +23,31 @@ "route_finder_timeout": "10s", "table": { "type": "boltdb", - "location": "./skywire/routing.db" + "location": "./local/tcp-tr/nodeB/routing.db" } }, "apps": [ { - "version": "1.0", - "app": "skychat", - "auto_start": true, - "port": 1, - "args": [] - }, - { - "version": "1.0", - "app": "SSH", - "auto_start": true, - "port": 2, - "args": [] - }, - { - "version": "1.0", - "app": "socksproxy", - "auto_start": true, - "port": 3, - "args": [ - "-passcode", - "uMsqIvgT9Cs=" - ] + "app": "skychat", + "version": "1.0", + "auto_start": true, + "port": 1, + "args": [ + "-addr", + "192.168.1.3:8001" + ] } ], "trusted_nodes": [], "hypervisors": [], "apps_path": "./apps", "local_path": "./local", - "transport_type": "dmsg", - "pubkeys_file": "./local/pubkeys", - "tcptransport_addr": "", + "transport_type": "tcp-transport", + "pubkeys_file": "./integration/tcp-tr/hosts.pubkeys", + "tcptransport_addr": "192.168.1.3:9119", "shutdown_timeout": "10s", "interfaces": { - "rpc": "localhost:3435" + "rpc": "192.168.1.3:3435" }, "log_level": "info" } \ No newline at end of file diff --git a/pkg/transport/tcp_transport_test.go b/pkg/transport/tcp_transport_test.go index 353ffe8d3..ee851ea46 100644 --- a/pkg/transport/tcp_transport_test.go +++ b/pkg/transport/tcp_transport_test.go @@ -17,7 +17,7 @@ import ( ) /* - This test requires IPs on host + This test requires configured IPs on host sudo ip addr add 192.168.1.2 dev lo sudo ip addr add 192.168.1.3 dev lo */ diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 1854ce8a7..20c823f2c 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -109,6 +109,8 @@ type Node struct { // NewNode constructs new Node. func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) { + + // Node init node := &Node{ config: config, executer: newOSExecuter(), @@ -120,15 +122,17 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) pk := config.Node.StaticPubKey sk := config.Node.StaticSecKey - mConfig, err := config.MessagingConfig() - if err != nil { - return nil, fmt.Errorf("invalid messaging config: %s", err) - } + // switch between transport types switch config.TransportType { case "dmsg": + mConfig, err := config.MessagingConfig() + if err != nil { + return nil, fmt.Errorf("invalid messaging config: %s", err) + } node.messenger = dmsg.NewClient(mConfig.PubKey, mConfig.SecKey, mConfig.Discovery, dmsg.SetLogger(node.Logger.PackageLogger(dmsg.Type))) + case "tcp-transport": var err error node.messenger, err = transport.NewTCPFactory(config.Node.StaticPubKey, config.PubKeysFile, config.TCPTransportAddr) @@ -139,7 +143,7 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) trDiscovery, err := config.TransportDiscovery() if err != nil { - return nil, fmt.Errorf("invalid MessagingConfig: %s", err) + return nil, fmt.Errorf("invalid TransportDiscoveryConfig: %s", err) } logStore, err := config.TransportLogStore() @@ -152,6 +156,7 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) LogStore: logStore, DefaultNodes: config.TrustedNodes, } + node.tm, err = transport.NewManager(tmConfig, node.messenger) if err != nil { return nil, fmt.Errorf("transport manager: %s", err) @@ -162,6 +167,7 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) if err != nil { return nil, fmt.Errorf("routing table: %s", err) } + rConfig := &router.Config{ Logger: node.Logger.PackageLogger("router"), PubKey: pk, From cb5bf317a53aa5199440fa9e65ed91d9b77dc862 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Thu, 1 Aug 2019 11:24:43 +0300 Subject: [PATCH 08/26] Proceeded to thorn-letter problem --- integration/tcp-tr/hosts.pubkeys | 5 +++-- integration/tcp-tr/nodeA.json | 7 +++---- integration/tcp-tr/nodeB.json | 4 ++-- pkg/router/router.go | 9 ++++++--- pkg/transport/manager.go | 11 +++++++--- pkg/transport/tcp_transport.go | 2 +- pkg/transport/tcp_transport_test.go | 10 ++++++++++ pkg/visor/visor.go | 31 ++++++++++++++++++++++------- 8 files changed, 57 insertions(+), 22 deletions(-) diff --git a/integration/tcp-tr/hosts.pubkeys b/integration/tcp-tr/hosts.pubkeys index 035d10d44..f273d92e8 100644 --- a/integration/tcp-tr/hosts.pubkeys +++ b/integration/tcp-tr/hosts.pubkeys @@ -1,2 +1,3 @@ -03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab 192.168.1.2:9119 -033978326862c191eaa39e33bb556a6296466facfe36bfb81e6b4c99d9c510e09f 192.168.1.3:9119 \ No newline at end of file +02fffa3ffd07630bf5565493a990f182ea7de56a9c14abbe041959a8e4667d3447 192.168.1.2:9119 +0315852b6dba67d16c5376b40e082a1036bd17b66c67573b242625f485f605756e 192.168.1.3:9119 + diff --git a/integration/tcp-tr/nodeA.json b/integration/tcp-tr/nodeA.json index e048dd703..1bb1b0f23 100755 --- a/integration/tcp-tr/nodeA.json +++ b/integration/tcp-tr/nodeA.json @@ -1,8 +1,8 @@ { "version": "1.0", "node": { - "static_public_key": "0322f66c5cac131376a2e6a20c6e31511baa7ec7bb0dccd26954c86c94af7d02b7", - "static_secret_key": "8300741a43bcb60e44cc407980ff2bfecd762fd59b900c98f22e5cce77476e41" + "static_public_key": "02fffa3ffd07630bf5565493a990f182ea7de56a9c14abbe041959a8e4667d3447", + "static_secret_key": "4be3f22d89d0bade8c5bf0084919f891e5e2dd0d4db0bc139318dfa576ca0237" }, "messaging": { "discovery": "https://messaging.discovery.skywire.skycoin.net", @@ -37,8 +37,7 @@ "192.168.1.2:8001" ] } - ], - + ], "trusted_nodes": [], "hypervisors": [], "apps_path": "./apps", diff --git a/integration/tcp-tr/nodeB.json b/integration/tcp-tr/nodeB.json index c07e3dc16..8ded67809 100755 --- a/integration/tcp-tr/nodeB.json +++ b/integration/tcp-tr/nodeB.json @@ -1,8 +1,8 @@ { "version": "1.0", "node": { - "static_public_key": "021adbbdf76f223f6e25ffe0b5626e600a1fcbb5fbbda833147a262a61b21312f3", - "static_secret_key": "ec1ba10659fca5e3847347b6978244013c651f6283f736c515f003407370649f" + "static_public_key": "0315852b6dba67d16c5376b40e082a1036bd17b66c67573b242625f485f605756e", + "static_secret_key": "55553e1fb464b557e47392caad59f94ca14dff872f43c4ee786a84df761e69e0" }, "messaging": { "discovery": "https://messaging.discovery.skywire.skycoin.net", diff --git a/pkg/router/router.go b/pkg/router/router.go index de9f36923..601c51fdf 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -10,7 +10,6 @@ import ( "sync" "time" - "github.com/skycoin/dmsg" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" @@ -39,6 +38,7 @@ type Config struct { RoutingTable routing.Table RouteFinder routeFinder.Client SetupNodes []cipher.PubKey + TransportType string } // Router implements node.PacketRouter. It manages routing table by @@ -281,6 +281,7 @@ func (r *Router) forwardLocalAppPacket(packet *app.Packet) error { } func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing.Addr, error) { + lport := r.pm.Alloc(appConn) if err := r.pm.SetLoop(lport, raddr, &loop{}); err != nil { return routing.Addr{}, err @@ -309,7 +310,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing Forward: forwardRoute, Reverse: reverseRoute, } - + r.Logger.Infof("Router.requestLoop 5\n") proto, tr, err := r.setupProto(context.Background()) if err != nil { return routing.Addr{}, err @@ -320,6 +321,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing } }() + r.Logger.Infof("Router.requestLoop 6\n") if err := setup.CreateLoop(proto, ld); err != nil { return routing.Addr{}, fmt.Errorf("route setup: %s", err) } @@ -423,8 +425,9 @@ func (r *Router) setupProto(ctx context.Context) (*setup.Protocol, transport.Tra return nil, nil, errors.New("route setup: no nodes") } + trType := r.config.TransportType // TODO(evanlinjin): need string constant for tp type. - tr, err := r.tm.CreateTransport(ctx, r.config.SetupNodes[0], dmsg.Type, false) + tr, err := r.tm.CreateTransport(ctx, r.config.SetupNodes[0], trType, false) if err != nil { return nil, nil, fmt.Errorf("setup transport: %s", err) } diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index d359fac1c..5e65bfd42 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -3,6 +3,7 @@ package transport import ( "context" "errors" + "fmt" "io" "strings" "sync" @@ -44,8 +45,10 @@ type Manager struct { func NewManager(config *ManagerConfig, factories ...Factory) (*Manager, error) { entries, err := config.DiscoveryClient.GetTransportsByEdge(context.Background(), config.PubKey) if err != nil { + entries = make([]*EntryWithStatus, 0) } + log.Infof("transport.NewManager. entries: v%\n", entries) mEntries := make(map[Entry]struct{}) for _, entry := range entries { @@ -275,7 +278,8 @@ func (tm *Manager) dialTransport(ctx context.Context, factory Factory, remote ci tr, err := factory.Dial(ctx, remote) if err != nil { - return nil, nil, err + + return nil, nil, fmt.Errorf("error %v on factory.Dial to remote %v", err, remote) } entry, err := settlementInitiatorHandshake(public).Do(tm, tr, time.Minute) @@ -294,12 +298,13 @@ func (tm *Manager) dialTransport(ctx context.Context, factory Factory, remote ci func (tm *Manager) createTransport(ctx context.Context, remote cipher.PubKey, tpType string, public bool) (*ManagedTransport, error) { factory := tm.factories[tpType] if factory == nil { - return nil, errors.New("unknown transport type") + + return nil, fmt.Errorf("unknown transport type %s", tpType) } tr, entry, err := tm.dialTransport(ctx, factory, remote, public) if err != nil { - return nil, err + return nil, fmt.Errorf("error on dialTransport: %v", err) } oldTr := tm.Transport(entry.ID) diff --git a/pkg/transport/tcp_transport.go b/pkg/transport/tcp_transport.go index 854664398..acbd53094 100644 --- a/pkg/transport/tcp_transport.go +++ b/pkg/transport/tcp_transport.go @@ -105,7 +105,7 @@ func (f *TCPFactory) Local() cipher.PubKey { // Type returns the Transport type. func (f *TCPFactory) Type() string { - return "tcp" + return "tcp-transport" } // TCPTransport implements Transport over TCP connection. diff --git a/pkg/transport/tcp_transport_test.go b/pkg/transport/tcp_transport_test.go index ee851ea46..28518750c 100644 --- a/pkg/transport/tcp_transport_test.go +++ b/pkg/transport/tcp_transport_test.go @@ -206,6 +206,16 @@ func pkFromSeed(seed string) cipher.PubKey { return pk } +func ExampleFile() { + pkA, skA, _ := cipher.GenerateDeterministicKeyPair([]byte("NodeA")) + pkB, skB, _ := cipher.GenerateDeterministicKeyPair([]byte("NodeB")) + + fmt.Printf("%v\n%v\n\n", pkA, skA) + fmt.Printf("%v\n%v\n\n", pkB, skB) + + // Output: AAAA +} + func Example_transport_MemoryPubKeyTable() { pkA, pkB := pkFromSeed("nodeA"), pkFromSeed("nodeB") ipA, ipB := "192.168.1.2:9119", "192.168.1.3:9119" diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 20c823f2c..82dba753e 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -19,6 +19,7 @@ import ( "syscall" "time" + "github.com/skycoin/dmsg/cipher" "github.com/skycoin/dmsg/noise" "github.com/skycoin/skycoin/src/util/logging" @@ -124,6 +125,11 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) sk := config.Node.StaticSecKey // switch between transport types + var ( + rtfClient routeFinder.Client + setupNodes []cipher.PubKey + trDiscovery transport.DiscoveryClient + ) switch config.TransportType { case "dmsg": mConfig, err := config.MessagingConfig() @@ -133,20 +139,29 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) node.messenger = dmsg.NewClient(mConfig.PubKey, mConfig.SecKey, mConfig.Discovery, dmsg.SetLogger(node.Logger.PackageLogger(dmsg.Type))) + rtfClient = routeFinder.NewHTTP(config.Routing.RouteFinder, time.Duration(config.Routing.RouteFinderTimeout)) + + setupNodes = config.Routing.SetupNodes + + trDiscovery, err = config.TransportDiscovery() + if err != nil { + return nil, fmt.Errorf("invalid TransportDiscoveryConfig: %s", err) + } + case "tcp-transport": var err error node.messenger, err = transport.NewTCPFactory(config.Node.StaticPubKey, config.PubKeysFile, config.TCPTransportAddr) if err != nil { return nil, err } - } - trDiscovery, err := config.TransportDiscovery() - if err != nil { - return nil, fmt.Errorf("invalid TransportDiscoveryConfig: %s", err) + rtfClient = routeFinder.NewMock() + setupNodes = []cipher.PubKey{config.Node.StaticPubKey} + + trDiscovery = transport.NewDiscoveryMock() } - logStore, err := config.TransportLogStore() + logStore, err := config.TransportLogStore() if err != nil { return nil, fmt.Errorf("invalid TransportLogStore: %s", err) } @@ -158,6 +173,7 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) } node.tm, err = transport.NewManager(tmConfig, node.messenger) + if err != nil { return nil, fmt.Errorf("transport manager: %s", err) } @@ -174,8 +190,9 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) SecKey: sk, TransportManager: node.tm, RoutingTable: node.rt, - RouteFinder: routeFinder.NewHTTP(config.Routing.RouteFinder, time.Duration(config.Routing.RouteFinderTimeout)), - SetupNodes: config.Routing.SetupNodes, + RouteFinder: rtfClient, + SetupNodes: setupNodes, + TransportType: config.TransportType, } r := router.New(rConfig) node.router = r From 4f56b615e816803216441fb7fe1dd2407b54cf4a Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Fri, 2 Aug 2019 07:25:08 +0300 Subject: [PATCH 09/26] Transport is accepted by nodeA. Still not working --- integration/tcp-tr/{nodeB.json => nodeC.json} | 0 integration/test-messaging.sh | 2 +- pkg/router/router.go | 48 +++++++++++++------ 3 files changed, 34 insertions(+), 16 deletions(-) rename integration/tcp-tr/{nodeB.json => nodeC.json} (100%) diff --git a/integration/tcp-tr/nodeB.json b/integration/tcp-tr/nodeC.json similarity index 100% rename from integration/tcp-tr/nodeB.json rename to integration/tcp-tr/nodeC.json diff --git a/integration/test-messaging.sh b/integration/test-messaging.sh index 6a5add8b7..d95ad085c 100755 --- a/integration/test-messaging.sh +++ b/integration/test-messaging.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash curl --data {'"recipient":"'$PK_A'", "message":"Hello Joe!"}' -X POST $CHAT_C -curl --data {'"recipient":"'$PK_C'", "message":"Hello Mike!"}' -X POST $CHAT_A +# curl --data {'"recipient":"'$PK_C'", "message":"Hello Mike!"}' -X POST $CHAT_A diff --git a/pkg/router/router.go b/pkg/router/router.go index 601c51fdf..621217ca5 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -244,10 +244,13 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { } func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) error { + + r.Logger.Info("Entering r.forwardAppPacket") + if packet.Loop.Remote.PubKey == r.config.PubKey { return r.forwardLocalAppPacket(packet) } - + r.Logger.Info("Entering r.forwardAppPacket GetLoop") l, err := r.pm.GetLoop(packet.Loop.Local.Port, packet.Loop.Remote) if err != nil { return err @@ -255,8 +258,9 @@ func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) err tr := r.tm.Transport(l.trID) if tr == nil { - return errors.New("unknown transport") + return fmt.Errorf("unknown transport id %v", l.trID) } + r.Logger.Info("r.forwardAppPacket enter routing.MakePacket") p := routing.MakePacket(l.routeID, packet.Payload) r.Logger.Infof("Forwarded App packet from LocalPort %d using route ID %d", packet.Loop.Local.Port, l.routeID) @@ -310,20 +314,34 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing Forward: forwardRoute, Reverse: reverseRoute, } - r.Logger.Infof("Router.requestLoop 5\n") - proto, tr, err := r.setupProto(context.Background()) - if err != nil { - return routing.Addr{}, err - } - defer func() { - if err := tr.Close(); err != nil { - r.Logger.Warnf("Failed to close transport: %s", err) + + r.Logger.Infof("Router.requestLoop\n") + r.Logger.Infof("laddr: %v\n, raddr: %v\n", laddr, raddr) + + r.Logger.Info("Attempt to r.setupProto from r.requestLoop") + switch r.config.TransportType { + case "dmsg": + proto, tr, err := r.setupProto(context.Background()) + if err != nil { + return routing.Addr{}, err + } + defer func() { + if err := tr.Close(); err != nil { + r.Logger.Warnf("Failed to close transport: %s", err) + } + }() + + r.Logger.Infof("Router.requestLoop 6\n") + if err := setup.CreateLoop(proto, ld); err != nil { + return routing.Addr{}, fmt.Errorf("route setup: %s", err) + } + case "tcp-transport": + r.Logger.Info("Skipping setup for tcp-transport") + _, err := r.tm.CreateTransport(context.Background(), raddr.PubKey, "tcp-transport", false) + if err != nil { + r.Logger.Warnf("error creating transport %s", err) } - }() - r.Logger.Infof("Router.requestLoop 6\n") - if err := setup.CreateLoop(proto, ld); err != nil { - return routing.Addr{}, fmt.Errorf("route setup: %s", err) } r.Logger.Infof("Created new loop to %s on port %d", raddr, laddr.Port) @@ -366,7 +384,7 @@ func (r *Router) closeLoop(appConn *app.Protocol, loop routing.Loop) error { if err := r.destroyLoop(loop); err != nil { r.Logger.Warnf("Failed to remove loop: %s", err) } - + r.Logger.Info("Attempt to r.setupProto from r.closeLoop") proto, tr, err := r.setupProto(context.Background()) if err != nil { return err From fca2d56e2ca80df746997e7f70fd8db17d05190a Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Mon, 12 Aug 2019 20:14:51 +0300 Subject: [PATCH 10/26] Start of multihead test --- go.mod | 3 + go.sum | 6 ++ pkg/visor/visor_multihead_test.go | 145 ++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 pkg/visor/visor_multihead_test.go diff --git a/go.mod b/go.mod index 4cc971b0f..42e8a9113 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,8 @@ go 1.12 require ( github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 + github.com/blang/semver v3.5.1+incompatible // indirect + github.com/boltdb/bolt v1.3.1 // indirect github.com/go-chi/chi v4.0.2+incompatible github.com/google/uuid v1.1.1 github.com/gorilla/handlers v1.4.0 @@ -14,6 +16,7 @@ require ( github.com/pkg/profile v1.3.0 github.com/prometheus/client_golang v1.0.0 github.com/prometheus/common v0.4.1 + github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 // indirect github.com/sirupsen/logrus v1.4.2 github.com/skycoin/dmsg v0.0.0-20190805065636-70f4c32a994f github.com/skycoin/skycoin v0.26.0 diff --git a/go.sum b/go.sum index 6459743fb..e53e2c2d2 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,10 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ= +github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= +github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= +github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -83,6 +87,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 h1:pntxY8Ary0t43dCZ5dqY4YTJCObLY1kIXl0uzMv+7DE= +github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= diff --git a/pkg/visor/visor_multihead_test.go b/pkg/visor/visor_multihead_test.go new file mode 100644 index 000000000..03e2bd209 --- /dev/null +++ b/pkg/visor/visor_multihead_test.go @@ -0,0 +1,145 @@ +// +build !no_ci + +package visor + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + + "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skywire/pkg/routing" +) + +func readConfig(cfgFile string) (Config, error) { + rdr, err := os.Open(filepath.Clean(cfgFile)) + if err != nil { + return Config{}, err + } + conf := Config{} + if err := json.NewDecoder(rdr).Decode(&conf); err != nil { + return Config{}, err + } + return conf, err +} + +func ipPool(addrTemplate string, n uint) []string { + ipAddrs := make([]string, n) + for i := uint(1); i < n+1; i++ { + ipAddrs[i-1] = fmt.Sprintf(addrTemplate, i) + } + return ipAddrs +} + +func Example_ipPool() { + + ipAddrs := ipPool("12.12.12.%d", 1) + fmt.Printf("%v\n", ipAddrs) + + // Output: ZZZZ +} + +func cfgPool(baseCfg Config, ipTmplt, localPathTmplt string, n uint) []Config { + nodeCfgs := make([]Config, n) + + for i := uint(0); i < n; i++ { + ip := fmt.Sprintf(ipTmplt, i+1) + localPath := fmt.Sprintf(localPathTmplt, i+1) + pk, sk, _ := cipher.GenerateDeterministicKeyPair([]byte(ip)) + + baseCfg.Node.StaticPubKey = pk + baseCfg.Node.StaticSecKey = sk + baseCfg.LocalPath = localPath + baseCfg.Interfaces.RPCAddress = fmt.Sprintf("%s:3435", ip) + baseCfg.Transport.LogStore.Location = fmt.Sprintf("%s/transport_logs", localPath) + baseCfg.Routing.Table.Location = fmt.Sprintf("%s/routing.db", localPath) + baseCfg.Apps = []AppConfig{ + AppConfig{ + App: "skychat", + AutoStart: true, + Port: routing.Port(1), + Args: []string{ + "-addr", + fmt.Sprintf("%v:8001", ip), + }, + }} + baseCfg.TCPTransportAddr = fmt.Sprintf("%v:9119", ip) + + nodeCfgs[i] = baseCfg + } + return nodeCfgs +} + +func Example_cfgPool() { + cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") + baseCfg, err := readConfig(cfgFile) + fmt.Printf("readConfig success: %v\n", err == nil) + + nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "./local/node_%03d", 3) + fmt.Printf("len(nodeCfgs): %v\n", len(nodeCfgs)) + + // Output: readConfig success: true + // len(nodeCfgs): 3 +} + +func nodePool(cfgs []Config) []*Node { + nodes := make([]*Node, len(cfgs)) + // loggers := make([]) + + var err error + for i := 0; i < len(cfgs); i++ { + nodes[i], err = NewNode(&cfgs[i], masterLogger) + if err != nil { + panic(err) + } + } + return nodes +} + +/* Run in shell: +$ for ((i=1; i<=16; i++)){_ ip addr add 12.12.12.$i/32 dev lo} +*/ + +func Example_nodePool() { + cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") + baseCfg, err := readConfig(cfgFile) + baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") + + fmt.Printf("readConfig success: %v\n", err == nil) + nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "./local/node_%03d", 16) + + nodes := nodePool(nodeCfgs) + fmt.Printf("Nodes: 12.12.12.1 - 12.12.12.%d", len(nodes)) + + // Output: readConfig success: true + // Nodes: 12.12.12.1 - 12.12.12.16 +} + +func startMultiHead(nodes []*Node) []error { + errs := []error{} + for i := 0; i < len(nodes); i++ { + if err := nodes[i].Start(); err != nil { + errs = append(errs, fmt.Errorf("error %v starting node %v", err, i)) + } + } + return errs +} + +func Example_startMultiHead() { + cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") + baseCfg, err := readConfig(cfgFile) + baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") + fmt.Printf("baseCfg success: %v\n", err == nil) + + nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "./local/node_%03d", 1) + nodes := nodePool(nodeCfgs) + + _ = nodes + var errs []error + errs = startMultiHead(nodes) + + fmt.Printf("errors: %v\n", errs) + + // Output: ZZZZ +} From 4d4b980871404944477cd6eb5444f6b217351daa Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Tue, 13 Aug 2019 10:18:32 +0300 Subject: [PATCH 11/26] Managed to run 128 nodes in Example_runMultihead ```zsh $ for ((i=1; i<=128; i++)){_ ip addr add 12.12.12.$i/32 dev lo} ``` then run Example_runMultihead ```zsh $ tree /tmp/multihead $ cat /tmp/multihead/multihead.log |sort $ ps aux |grep skychat |wc - l ``` You must see 128 instances of skywire-node with 128 skychats each on different IP --- pkg/visor/visor_multihead_test.go | 55 ++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/pkg/visor/visor_multihead_test.go b/pkg/visor/visor_multihead_test.go index 03e2bd209..cb63c4ea3 100644 --- a/pkg/visor/visor_multihead_test.go +++ b/pkg/visor/visor_multihead_test.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "path/filepath" + "time" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skywire/pkg/routing" @@ -85,7 +86,6 @@ func Example_cfgPool() { func nodePool(cfgs []Config) []*Node { nodes := make([]*Node, len(cfgs)) - // loggers := make([]) var err error for i := 0; i < len(cfgs); i++ { @@ -105,6 +105,7 @@ func Example_nodePool() { cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") baseCfg, err := readConfig(cfgFile) baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") + baseCfg.AppsPath, _ = filepath.Abs("../../apps") fmt.Printf("readConfig success: %v\n", err == nil) nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "./local/node_%03d", 16) @@ -116,30 +117,62 @@ func Example_nodePool() { // Nodes: 12.12.12.1 - 12.12.12.16 } -func startMultiHead(nodes []*Node) []error { - errs := []error{} +func startMultiHead(nodes []*Node) chan error { + errs := make(chan error, len(nodes)) for i := 0; i < len(nodes); i++ { - if err := nodes[i].Start(); err != nil { - errs = append(errs, fmt.Errorf("error %v starting node %v", err, i)) - } + go func(n int) { + if err := nodes[n].Start(); err != nil { + errs <- fmt.Errorf("error %v starting node %v", err, n) + } + }(i) + } + return errs +} + +func stopMultiHead(nodes []*Node) chan error { + errs := make(chan error, len(nodes)) + for i := 0; i < len(nodes); i++ { + go func(n int) { + if err := nodes[n].Close(); err != nil { + errs <- fmt.Errorf("error %v starting node %v", err, n) + } + }(i) } return errs } func Example_startMultiHead() { + + _ = os.MkdirAll("/tmp/multihead", 0777) + f, err := os.OpenFile("/tmp/multihead/multihead.log", os.O_RDWR|os.O_CREATE, 0666) + if err != nil { + log.Fatalf("error opening file: %v", err) + } + defer f.Close() + masterLogger.Out = f + cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") baseCfg, err := readConfig(cfgFile) baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") + baseCfg.AppsPath, _ = filepath.Abs("../../apps") fmt.Printf("baseCfg success: %v\n", err == nil) - nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "./local/node_%03d", 1) + nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "/tmp/multihead/node_%03d", 128) nodes := nodePool(nodeCfgs) - _ = nodes - var errs []error - errs = startMultiHead(nodes) + errsOnStart := startMultiHead(nodes) + + time.Sleep(time.Second * 5) + + errsOnStop := stopMultiHead(nodes) + + time.Sleep(time.Second * 15) + + close(errsOnStart) + close(errsOnStop) - fmt.Printf("errors: %v\n", errs) + fmt.Printf("errsOnStart: %v\n", len(errsOnStart)) + fmt.Printf("errsOnStop: %v\n", len(errsOnStop)) // Output: ZZZZ } From 8dec53df525b7843e631503a7f1bcd8c6ff052f3 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Tue, 13 Aug 2019 11:35:11 +0300 Subject: [PATCH 12/26] Logrus output accumulated in memory. Routing Tables are in memory too --- pkg/visor/visor_multihead_test.go | 32 ++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/pkg/visor/visor_multihead_test.go b/pkg/visor/visor_multihead_test.go index cb63c4ea3..327927876 100644 --- a/pkg/visor/visor_multihead_test.go +++ b/pkg/visor/visor_multihead_test.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "time" "github.com/skycoin/dmsg/cipher" @@ -54,7 +55,7 @@ func cfgPool(baseCfg Config, ipTmplt, localPathTmplt string, n uint) []Config { baseCfg.LocalPath = localPath baseCfg.Interfaces.RPCAddress = fmt.Sprintf("%s:3435", ip) baseCfg.Transport.LogStore.Location = fmt.Sprintf("%s/transport_logs", localPath) - baseCfg.Routing.Table.Location = fmt.Sprintf("%s/routing.db", localPath) + baseCfg.Apps = []AppConfig{ AppConfig{ App: "skychat", @@ -141,32 +142,34 @@ func stopMultiHead(nodes []*Node) chan error { return errs } +type multiheadWriter struct { + logRecords []string +} + +func (mhw *multiheadWriter) Write(p []byte) (n int, err error) { + mhw.logRecords = append(mhw.logRecords, string(p)) + return len(p), nil +} + func Example_startMultiHead() { - _ = os.MkdirAll("/tmp/multihead", 0777) - f, err := os.OpenFile("/tmp/multihead/multihead.log", os.O_RDWR|os.O_CREATE, 0666) - if err != nil { - log.Fatalf("error opening file: %v", err) - } - defer f.Close() - masterLogger.Out = f + mhw := multiheadWriter{logRecords: []string{}} + masterLogger.Out = &mhw cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") baseCfg, err := readConfig(cfgFile) baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") baseCfg.AppsPath, _ = filepath.Abs("../../apps") + baseCfg.Routing.Table.Type = "memory" fmt.Printf("baseCfg success: %v\n", err == nil) - nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "/tmp/multihead/node_%03d", 128) + nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "/tmp/multihead/node_%03d", 1) nodes := nodePool(nodeCfgs) errsOnStart := startMultiHead(nodes) - time.Sleep(time.Second * 5) - errsOnStop := stopMultiHead(nodes) - - time.Sleep(time.Second * 15) + time.Sleep(time.Second * 5) close(errsOnStart) close(errsOnStop) @@ -174,5 +177,8 @@ func Example_startMultiHead() { fmt.Printf("errsOnStart: %v\n", len(errsOnStart)) fmt.Printf("errsOnStop: %v\n", len(errsOnStop)) + fmt.Printf("log: %v\n", mhw.logRecords) + + strings.Contains(mhw.logRecords, "ZZZ") // Output: ZZZZ } From 5ae74d5cf45bc98b39e94abf5fa74e2acf89a526 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Wed, 14 Aug 2019 12:17:09 +0300 Subject: [PATCH 13/26] multihead environment enveloped in type MultiHead. Send message as MultiHead.sendMessage --- pkg/visor/visor_multihead_test.go | 290 +++++++++++++++++++++--------- 1 file changed, 206 insertions(+), 84 deletions(-) diff --git a/pkg/visor/visor_multihead_test.go b/pkg/visor/visor_multihead_test.go index 327927876..897fa84aa 100644 --- a/pkg/visor/visor_multihead_test.go +++ b/pkg/visor/visor_multihead_test.go @@ -3,50 +3,111 @@ package visor import ( + "bytes" "encoding/json" "fmt" + "net/http" "os" "path/filepath" - "strings" + "sync" "time" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skywire/pkg/routing" ) -func readConfig(cfgFile string) (Config, error) { +type multiheadLog struct { + mx sync.Mutex + records []string +} + +func (mhl *multiheadLog) Write(p []byte) (n int, err error) { + mhl.mx.Lock() + defer mhl.mx.Unlock() + mhl.records = append(mhl.records, string(p)) + return len(p), nil +} + +/* Prepare IP aliases: +$ for ((i=1; i<=16; i++)){_ ip addr add 12.12.12.$i/32 dev lo} +*/ + +type MultiHead struct { + baseCfg Config + ipTemplate string + ipPool []string + cfgPool []Config + nodes []*Node + + Log *multiheadLog +} + +func initMultiHead() *MultiHead { + mhLog := multiheadLog{records: []string{}} + masterLogger.Out = &mhLog + return &MultiHead{Log: &mhLog} +} + +func Example_initMultiHead() { + mh := initMultiHead() + mh.Log.Write([]byte("initMultiHead success")) + fmt.Printf("%v\n", mh.Log.records) + + // Output: [initMultiHead success] +} + +func (mh *MultiHead) readBaseConfig(cfgFile string) error { rdr, err := os.Open(filepath.Clean(cfgFile)) if err != nil { - return Config{}, err + return err } - conf := Config{} - if err := json.NewDecoder(rdr).Decode(&conf); err != nil { - return Config{}, err + baseCfg := Config{} + if err := json.NewDecoder(rdr).Decode(&baseCfg); err != nil { + return err } - return conf, err + baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") + baseCfg.AppsPath, _ = filepath.Abs("../../apps") + baseCfg.Routing.Table.Type = "memory" + + mh.baseCfg = baseCfg + return nil +} + +func Example_readBaseConfig() { + mh := initMultiHead() + cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") + err = mh.readBaseConfig(cfgFile) + fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) + + // Output: mh.ReadBaseConfig success: true } -func ipPool(addrTemplate string, n uint) []string { - ipAddrs := make([]string, n) +func (mh *MultiHead) initIPPool(ipTemplate string, n uint) { + ipPool := make([]string, n) for i := uint(1); i < n+1; i++ { - ipAddrs[i-1] = fmt.Sprintf(addrTemplate, i) + ipPool[i-1] = fmt.Sprintf(ipTemplate, i) } - return ipAddrs + mh.ipTemplate = ipTemplate + mh.ipPool = ipPool + } -func Example_ipPool() { +func ExampleMultiHead_initIPPool() { + mh := initMultiHead() + mh.initIPPool("12.12.12.%d", 16) - ipAddrs := ipPool("12.12.12.%d", 1) - fmt.Printf("%v\n", ipAddrs) + fmt.Printf("IP pool length: %v\n", len(mh.ipPool)) - // Output: ZZZZ + // Output: IP pool length: 16 } -func cfgPool(baseCfg Config, ipTmplt, localPathTmplt string, n uint) []Config { - nodeCfgs := make([]Config, n) +func (mh *MultiHead) initCfgPool() { + mh.cfgPool = make([]Config, len(mh.ipPool)) + localPathTmplt := "/tmp/multihead" + baseCfg := mh.baseCfg - for i := uint(0); i < n; i++ { - ip := fmt.Sprintf(ipTmplt, i+1) + for i := 0; i < len(mh.ipPool); i++ { + ip := fmt.Sprintf(mh.ipTemplate, i+1) localPath := fmt.Sprintf(localPathTmplt, i+1) pk, sk, _ := cipher.GenerateDeterministicKeyPair([]byte(ip)) @@ -68,61 +129,63 @@ func cfgPool(baseCfg Config, ipTmplt, localPathTmplt string, n uint) []Config { }} baseCfg.TCPTransportAddr = fmt.Sprintf("%v:9119", ip) - nodeCfgs[i] = baseCfg + mh.cfgPool[i] = baseCfg } - return nodeCfgs + } -func Example_cfgPool() { +func Example_initCfgPool() { + mh := initMultiHead() cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") - baseCfg, err := readConfig(cfgFile) - fmt.Printf("readConfig success: %v\n", err == nil) + err = mh.readBaseConfig(cfgFile) + fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) + mh.initIPPool("12.12.12.%d", 16) + mh.initCfgPool() - nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "./local/node_%03d", 3) - fmt.Printf("len(nodeCfgs): %v\n", len(nodeCfgs)) + fmt.Printf("len(mh.cfgPool): %v\n", len(mh.cfgPool)) - // Output: readConfig success: true - // len(nodeCfgs): 3 + // Output: mh.ReadBaseConfig success: true + // len(mh.cfgPool): 16 } -func nodePool(cfgs []Config) []*Node { - nodes := make([]*Node, len(cfgs)) +func (mh *MultiHead) initNodes() chan error { + mh.nodes = make([]*Node, len(mh.cfgPool)) + errs := make(chan error, len(mh.cfgPool)) var err error - for i := 0; i < len(cfgs); i++ { - nodes[i], err = NewNode(&cfgs[i], masterLogger) + for i := 0; i < len(mh.nodes); i++ { + mh.nodes[i], err = NewNode(&mh.cfgPool[i], masterLogger) if err != nil { - panic(err) + errs <- fmt.Errorf("error %v starting node %v", err, i) } } - return nodes + return errs } -/* Run in shell: -$ for ((i=1; i<=16; i++)){_ ip addr add 12.12.12.$i/32 dev lo} -*/ - -func Example_nodePool() { +func ExampleMultiHead_initNodes() { + mh := initMultiHead() cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") - baseCfg, err := readConfig(cfgFile) - baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") - baseCfg.AppsPath, _ = filepath.Abs("../../apps") - - fmt.Printf("readConfig success: %v\n", err == nil) - nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "./local/node_%03d", 16) - - nodes := nodePool(nodeCfgs) - fmt.Printf("Nodes: 12.12.12.1 - 12.12.12.%d", len(nodes)) + err = mh.readBaseConfig(cfgFile) + fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) + mh.initIPPool("12.12.12.%d", 16) + mh.initCfgPool() + initErrs := mh.initNodes() + close(initErrs) + + for err := range initErrs { + fmt.Printf("%v\n", err) + } + fmt.Printf("Errors on initNodes: %v\n", len(initErrs)) - // Output: readConfig success: true - // Nodes: 12.12.12.1 - 12.12.12.16 + // Output: mh.ReadBaseConfig success: true + // Errors on initNodes: 0 } -func startMultiHead(nodes []*Node) chan error { - errs := make(chan error, len(nodes)) - for i := 0; i < len(nodes); i++ { +func (mh *MultiHead) startNodes() chan error { + errs := make(chan error, len(mh.nodes)) + for i := 0; i < len(mh.nodes); i++ { go func(n int) { - if err := nodes[n].Start(); err != nil { + if err := mh.nodes[n].Start(); err != nil { errs <- fmt.Errorf("error %v starting node %v", err, n) } }(i) @@ -130,11 +193,11 @@ func startMultiHead(nodes []*Node) chan error { return errs } -func stopMultiHead(nodes []*Node) chan error { - errs := make(chan error, len(nodes)) - for i := 0; i < len(nodes); i++ { +func (mh *MultiHead) stopNodes() chan error { + errs := make(chan error, len(mh.nodes)) + for i := 0; i < len(mh.nodes); i++ { go func(n int) { - if err := nodes[n].Close(); err != nil { + if err := mh.nodes[n].Close(); err != nil { errs <- fmt.Errorf("error %v starting node %v", err, n) } }(i) @@ -142,43 +205,102 @@ func stopMultiHead(nodes []*Node) chan error { return errs } -type multiheadWriter struct { - logRecords []string +func makeMultiHeadN(n uint) *MultiHead { + mh := initMultiHead() + cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") + err = mh.readBaseConfig(cfgFile) + fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) + mh.initIPPool("12.12.12.%d", n) + mh.initCfgPool() + + return mh } -func (mhw *multiheadWriter) Write(p []byte) (n int, err error) { - mhw.logRecords = append(mhw.logRecords, string(p)) - return len(p), nil +func ExampleMultiHead_startNodes() { + mh := initMultiHead() + cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") + err = mh.readBaseConfig(cfgFile) + fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) + mh.initIPPool("12.12.12.%d", 16) + mh.initCfgPool() + initErrs := mh.initNodes() + close(initErrs) + + startErrs := mh.startNodes() + time.Sleep(time.Second * 5) + stopErrs := mh.stopNodes() + time.Sleep(time.Second * 5) + + close(startErrs) + close(stopErrs) + + for err := range initErrs { + fmt.Printf("%v\n", err) + } + fmt.Printf("Errors on initNodes: %v\n", len(initErrs)) + + for err := range startErrs { + fmt.Printf("%v\n", err) + } + fmt.Printf("Errors on startNodes: %v\n", len(startErrs)) + + for err := range stopErrs { + fmt.Printf("%v\n", err) + } + fmt.Printf("Errors on stopNodes: %v\n", len(stopErrs)) + + // Output: mh.ReadBaseConfig success: true + // Errors on initNodes: 0 + // Errors on startNodes: 0 + // Errors on stopNodes: 0 } -func Example_startMultiHead() { +func (mh *MultiHead) sendMessage(sender, reciever uint, message string) (*http.Response, error) { + url := fmt.Sprintf("http://%s:8001/message", mh.ipPool[sender]) + msgData := map[string]string{ + "recipient": fmt.Sprintf("%s", mh.cfgPool[reciever].Node.StaticPubKey), + "message": "Hello", + } + data, _ := json.Marshal(msgData) // nolint: errcheck + return http.Post(url, "application/json", bytes.NewBuffer(data)) // nolint: gosec - mhw := multiheadWriter{logRecords: []string{}} - masterLogger.Out = &mhw +} - cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") - baseCfg, err := readConfig(cfgFile) - baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") - baseCfg.AppsPath, _ = filepath.Abs("../../apps") - baseCfg.Routing.Table.Type = "memory" - fmt.Printf("baseCfg success: %v\n", err == nil) +func ExampleMultiHead_sendMessage() { + mh := makeMultiHeadN(2) - nodeCfgs := cfgPool(baseCfg, "12.12.12.%d", "/tmp/multihead/node_%03d", 1) - nodes := nodePool(nodeCfgs) + initErrs := mh.initNodes() + startErrs := mh.startNodes() + time.Sleep(time.Second * 2) - errsOnStart := startMultiHead(nodes) - time.Sleep(time.Second * 5) - errsOnStop := stopMultiHead(nodes) + // fmt.Printf("resp: %v, err: %v\n", resp, err) + + resp, err := mh.sendMessage(0, 1, "Hello") + fmt.Printf("resp: %v, err: %v\n ", resp, err) + + stopErrs := mh.stopNodes() time.Sleep(time.Second * 5) - close(errsOnStart) - close(errsOnStop) + close(initErrs) + close(startErrs) + close(stopErrs) + + for err := range initErrs { + fmt.Printf("%v\n", err) + } + fmt.Printf("Errors on initNodes: %v\n", len(initErrs)) - fmt.Printf("errsOnStart: %v\n", len(errsOnStart)) - fmt.Printf("errsOnStop: %v\n", len(errsOnStop)) + for err := range startErrs { + fmt.Printf("%v\n", err) + } + fmt.Printf("Errors on startNodes: %v\n", len(startErrs)) + + for err := range stopErrs { + fmt.Printf("%v\n", err) + } + fmt.Printf("Errors on stopNodes: %v\n", len(stopErrs)) - fmt.Printf("log: %v\n", mhw.logRecords) + fmt.Printf("%v\n", mh.Log.records) - strings.Contains(mhw.logRecords, "ZZZ") - // Output: ZZZZ + // Output: ZZZ } From 43d2611fed4d8c4d7b1ae16500d0c0588d233321 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Thu, 15 Aug 2019 14:09:16 +0300 Subject: [PATCH 14/26] Fixed logging everywhere. Multhead simplified --- internal/therealproxy/client.go | 10 +- internal/therealproxy/server.go | 4 +- internal/therealproxy/server_test.go | 2 +- internal/therealssh/auth.go | 2 +- internal/therealssh/channel.go | 32 ++--- internal/therealssh/client.go | 22 +-- internal/therealssh/server.go | 24 ++-- internal/therealssh/server_test.go | 2 +- internal/therealssh/session.go | 7 +- pkg/app/protocol.go | 11 ++ pkg/router/app_manager.go | 12 +- pkg/router/app_manager_test.go | 2 +- pkg/router/router.go | 17 ++- pkg/router/router_test.go | 30 ++-- pkg/transport/manager.go | 6 +- pkg/transport/manager_test.go | 12 +- pkg/transport/mock.go | 4 +- pkg/transport/tcp_transport_test.go | 28 ++-- pkg/visor/visor.go | 17 +++ pkg/visor/visor_multihead_test.go | 202 +++++++++++++++------------ 20 files changed, 252 insertions(+), 194 deletions(-) diff --git a/internal/therealproxy/client.go b/internal/therealproxy/client.go index c7f6efbf9..2a8b08e70 100644 --- a/internal/therealproxy/client.go +++ b/internal/therealproxy/client.go @@ -9,7 +9,7 @@ import ( "github.com/skycoin/skycoin/src/util/logging" ) -var log = logging.MustGetLogger("therealproxy") +var Logger = logging.MustGetLogger("therealproxy") // Client implement multiplexing proxy client using yamux. type Client struct { @@ -42,7 +42,7 @@ func (c *Client) ListenAndServe(addr string) error { for { conn, err := l.Accept() if err != nil { - return fmt.Errorf("accept: %s", err) + return fmt.Errorf("proxyclient Accept: %s", err) } stream, err = c.session.Open() @@ -64,14 +64,14 @@ func (c *Client) ListenAndServe(addr string) error { for err := range errCh { if err := conn.Close(); err != nil { - log.WithError(err).Warn("Failed to close connection") + Logger.WithError(err).Warn("Failed to close connection") } if err := stream.Close(); err != nil { - log.WithError(err).Warn("Failed to close stream") + Logger.WithError(err).Warn("Failed to close stream") } if err != nil { - log.Error("Copy error:", err) + Logger.Error("Copy error:", err) } } }() diff --git a/internal/therealproxy/server.go b/internal/therealproxy/server.go index 866abad52..b3869e883 100644 --- a/internal/therealproxy/server.go +++ b/internal/therealproxy/server.go @@ -36,7 +36,7 @@ func (s *Server) Serve(l net.Listener) error { for { conn, err := l.Accept() if err != nil { - return fmt.Errorf("accept: %s", err) + return fmt.Errorf("proxyserver Accept: %s", err) } session, err := yamux.Server(conn, nil) @@ -46,7 +46,7 @@ func (s *Server) Serve(l net.Listener) error { go func() { if err := s.socks.Serve(session); err != nil { - log.Error("Failed to start SOCKS5 server:", err) + Logger.Error("Failed to start SOCKS5 server:", err) } }() } diff --git a/internal/therealproxy/server_test.go b/internal/therealproxy/server_test.go index 1fd51ab46..690efc21b 100644 --- a/internal/therealproxy/server_test.go +++ b/internal/therealproxy/server_test.go @@ -22,7 +22,7 @@ func TestMain(m *testing.M) { if ok { lvl, err := logging.LevelFromString(loggingLevel) if err != nil { - log.Fatal(err) + Logger.Fatal(err) } logging.SetLevel(lvl) } else { diff --git a/internal/therealssh/auth.go b/internal/therealssh/auth.go index 45cfd8fde..9e370b610 100644 --- a/internal/therealssh/auth.go +++ b/internal/therealssh/auth.go @@ -72,7 +72,7 @@ func (auth *FileAuthorizer) Close() error { func (auth *FileAuthorizer) Authorize(remotePK cipher.PubKey) error { defer func() { if _, err := auth.authFile.Seek(0, 0); err != nil { - log.WithError(err).Warn("Failed to seek to the beginning of auth file") + Logger.WithError(err).Warn("Failed to seek to the beginning of auth file") } }() diff --git a/internal/therealssh/channel.go b/internal/therealssh/channel.go index 5e66e40fe..c4c8baffe 100644 --- a/internal/therealssh/channel.go +++ b/internal/therealssh/channel.go @@ -79,7 +79,7 @@ func (sshCh *SSHChannel) Write(p []byte) (n int, err error) { // Request sends request message and waits for response. func (sshCh *SSHChannel) Request(requestType RequestType, payload []byte) ([]byte, error) { - log.Debugf("sending request %x", requestType) + Logger.Debugf("sending request %x", requestType) req := append([]byte{byte(requestType)}, payload...) if err := sshCh.Send(CmdChannelRequest, req); err != nil { @@ -98,7 +98,7 @@ func (sshCh *SSHChannel) Request(requestType RequestType, payload []byte) ([]byt func (sshCh *SSHChannel) Serve() error { for data := range sshCh.msgCh { var err error - log.Debugf("new request %x", data[0]) + Logger.Debugf("new request %x", data[0]) switch RequestType(data[0]) { case RequestPTY: var u *user.User @@ -149,10 +149,10 @@ func (sshCh *SSHChannel) SocketPath() string { // ServeSocket starts socket handling loop. func (sshCh *SSHChannel) ServeSocket() error { if err := os.Remove(sshCh.SocketPath()); err != nil { - log.WithError(err).Warn("Failed to remove SSH channel socket file") + Logger.WithError(err).Warn("Failed to remove SSH channel socket file") } - log.Debugf("waiting for new socket connections on: %s", sshCh.SocketPath()) + Logger.Debugf("waiting for new socket connections on: %s", sshCh.SocketPath()) l, err := net.ListenUnix("unix", &net.UnixAddr{Name: sshCh.SocketPath(), Net: "unix"}) if err != nil { return fmt.Errorf("failed to open unix socket: %s", err) @@ -166,22 +166,22 @@ func (sshCh *SSHChannel) ServeSocket() error { return fmt.Errorf("failed to accept connection: %s", err) } - log.Debugln("got new socket connection") + Logger.Debugln("got new socket connection") defer func() { if err := conn.Close(); err != nil { - log.WithError(err).Warn("Failed to close connection") + Logger.WithError(err).Warn("Failed to close connection") } if err := sshCh.closeListener(); err != nil { - log.WithError(err).Warn("Failed to close listener") + Logger.WithError(err).Warn("Failed to close listener") } if err := os.Remove(sshCh.SocketPath()); err != nil { - log.WithError(err).Warn("Failed to close SSH channel socket file") + Logger.WithError(err).Warn("Failed to close SSH channel socket file") } }() go func() { - if _, err := io.Copy(sshCh, conn); err != nil && !strings.Contains(err.Error(), "use of closed network connection") { - log.Errorf("failed to write to server:", err) + if _, err := io.Copy(sshCh, conn); err != nil && !strings.Contains(err.Error(), "use of closzed network connection") { + Logger.Errorf("failed to write to server:", err) return } }() @@ -199,7 +199,7 @@ func (sshCh *SSHChannel) OpenPTY(user *user.User, sz *pty.Winsize) (err error) { return errors.New("session is already started") } - log.Debugf("starting new session for %s with %#v", user.Username, sz) + Logger.Debugf("starting new session for %s with %#v", user.Username, sz) sshCh.session, err = OpenSession(user, sz) if err != nil { sshCh.session = nil @@ -222,27 +222,27 @@ func (sshCh *SSHChannel) Start(command string) error { go func() { if err := sshCh.serveSession(); err != nil { - log.Error("Session failure:", err) + Logger.Error("Session failure:", err) } }() - log.Debugf("starting new pty process %s", command) + Logger.Debugf("starting new pty process %s", command) return sshCh.session.Start(command) } func (sshCh *SSHChannel) serveSession() error { defer func() { if err := sshCh.Send(CmdChannelServerClose, nil); err != nil { - log.WithError(err).Warn("Failed to send to SSH channel") + Logger.WithError(err).Warn("Failed to send to SSH channel") } if err := sshCh.Close(); err != nil { - log.WithError(err).Warn("Failed to close SSH channel") + Logger.WithError(err).Warn("Failed to close SSH channel") } }() go func() { if _, err := io.Copy(sshCh.session, sshCh); err != nil { - log.Error("PTY copy: ", err) + Logger.Error("PTY copy: ", err) return } }() diff --git a/internal/therealssh/client.go b/internal/therealssh/client.go index 8abeb805d..35d4fb713 100644 --- a/internal/therealssh/client.go +++ b/internal/therealssh/client.go @@ -57,7 +57,7 @@ func (c *Client) OpenChannel(remotePK cipher.PubKey) (localID uint32, sshCh *SSH } sshCh = OpenClientChannel(0, remotePK, conn) - log.Debugln("sending channel open command") + Logger.Debugln("sending channel open command") localID = c.chans.add(sshCh) req := appendU32([]byte{byte(CmdChannelOpen)}, localID) if _, err := conn.Write(req); err != nil { @@ -67,13 +67,13 @@ func (c *Client) OpenChannel(remotePK cipher.PubKey) (localID uint32, sshCh *SSH go func() { if err := c.serveConn(conn); err != nil { - log.Error(err) + Logger.Error(err) } }() - log.Debugln("waiting for channel open response") + Logger.Debugln("waiting for channel open response") data := <-sshCh.msgCh - log.Debugln("got channel open response") + Logger.Debugln("got channel open response") if data[0] == ResponseFail { cErr = fmt.Errorf("failed to open channel: %s", string(data[1:])) return @@ -121,7 +121,7 @@ func (c *Client) serveConn(conn net.Conn) error { } data := payload[5:] - log.Debugf("got new command: %x", payload[0]) + Logger.Debugf("got new command: %x", payload[0]) switch CommandType(payload[0]) { case CmdChannelOpenResponse, CmdChannelResponse: sshCh.msgCh <- data @@ -151,7 +151,7 @@ func (c *Client) Close() error { for _, sshCh := range c.chans.dropAll() { if err := sshCh.Close(); err != nil { - log.WithError(err).Warn("Failed to close SSH channel") + Logger.WithError(err).Warn("Failed to close SSH channel") } } @@ -165,13 +165,13 @@ type RPCClient struct { // RequestPTY defines RPC request for a new PTY session. func (rpc *RPCClient) RequestPTY(args *RequestPTYArgs, channelID *uint32) error { - log.Debugln("requesting SSH channel") + Logger.Debugln("requesting SSH channel") localID, channel, err := rpc.c.OpenChannel(args.RemotePK) if err != nil { return err } - log.Debugln("requesting PTY session") + Logger.Debugln("requesting PTY session") if _, err := channel.Request(RequestPTY, args.ToBinary()); err != nil { return fmt.Errorf("PTY request failure: %s", err) } @@ -187,7 +187,7 @@ func (rpc *RPCClient) Exec(args *ExecArgs, socketPath *string) error { return errors.New("unknown channel") } - log.Debugln("requesting shell process") + Logger.Debugln("requesting shell process") if args.CommandWithArgs == nil { if _, err := sshCh.Request(RequestShell, nil); err != nil { return fmt.Errorf("shell request failure: %s", err) @@ -200,10 +200,10 @@ func (rpc *RPCClient) Exec(args *ExecArgs, socketPath *string) error { waitCh := make(chan bool) go func() { - log.Debugln("starting socket listener") + Logger.Debugln("starting socket listener") waitCh <- true if err := sshCh.ServeSocket(); err != nil { - log.Error("Session failure:", err) + Logger.Error("Session failure:", err) } }() diff --git a/internal/therealssh/server.go b/internal/therealssh/server.go index 9c78169c3..747317e5d 100644 --- a/internal/therealssh/server.go +++ b/internal/therealssh/server.go @@ -56,19 +56,19 @@ var responseUnauthorized = append([]byte{ResponseFail}, []byte("unauthorized").. // Server handles remote PTY data exchange. type Server struct { - log *logging.Logger - auth Authorizer - chans *chanList + Logger *logging.Logger + auth Authorizer + chans *chanList } // NewServer constructs new Server. func NewServer(auth Authorizer) *Server { - return &Server{logging.MustGetLogger("therealssh_server"), auth, newChanList()} + return &Server{Logger, auth, newChanList()} } // OpenChannel opens new client channel. func (s *Server) OpenChannel(remoteAddr routing.Addr, remoteID uint32, conn net.Conn) error { - log.Debugln("opening new channel") + Logger.Debugln("opening new channel") channel := OpenChannel(remoteID, remoteAddr, conn) var res []byte @@ -78,18 +78,18 @@ func (s *Server) OpenChannel(remoteAddr routing.Addr, remoteID uint32, conn net. res = appendU32([]byte{ResponseConfirm}, s.chans.add(channel)) } - s.log.Debugln("sending response") + s.Logger.Debugln("sending response") if err := channel.Send(CmdChannelOpenResponse, res); err != nil { if err := channel.Close(); err != nil { - log.WithError(err).Warn("Failed to close channel") + Logger.WithError(err).Warn("Failed to close channel") } return fmt.Errorf("channel response failure: %s", err) } go func() { - s.log.Debugln("listening for channel requests") + s.Logger.Debugln("listening for channel requests") if err := channel.Serve(); err != nil { - log.Error("channel failure:", err) + Logger.Error("channel failure:", err) } }() @@ -105,7 +105,7 @@ func (s *Server) HandleRequest(remotePK cipher.PubKey, localID uint32, data []by if s.auth.Authorize(remotePK) != nil || channel.RemoteAddr.PubKey != remotePK { if err := channel.Send(CmdChannelResponse, responseUnauthorized); err != nil { - log.Error("failed to send response: ", err) + Logger.Error("failed to send response: ", err) } return nil } @@ -160,7 +160,7 @@ func (s *Server) Serve(conn net.Conn) error { payloadID := binary.BigEndian.Uint32(payload[1:]) data := payload[5:] - s.log.Debugf("got new command: %x", payload[0]) + s.Logger.Debugf("got new command: %x", payload[0]) switch CommandType(payload[0]) { case CmdChannelOpen: err = s.OpenChannel(raddr, payloadID, conn) @@ -186,7 +186,7 @@ func (s *Server) Close() error { for _, channel := range s.chans.dropAll() { if err := channel.Close(); err != nil { - log.WithError(err).Warn("Failed to close channel") + Logger.WithError(err).Warn("Failed to close channel") } } diff --git a/internal/therealssh/server_test.go b/internal/therealssh/server_test.go index 1c9f2b65e..58dccd162 100644 --- a/internal/therealssh/server_test.go +++ b/internal/therealssh/server_test.go @@ -19,7 +19,7 @@ func TestMain(m *testing.M) { if ok { lvl, err := logging.LevelFromString(loggingLevel) if err != nil { - log.Fatal(err) + Logger.Fatal(err) } logging.SetLevel(lvl) } else { diff --git a/internal/therealssh/session.go b/internal/therealssh/session.go index 752ee2d17..b5d577cd2 100644 --- a/internal/therealssh/session.go +++ b/internal/therealssh/session.go @@ -13,7 +13,8 @@ import ( "github.com/skycoin/skycoin/src/util/logging" ) -var log = logging.MustGetLogger("therealssh") +// Logger is PackageLogger for therealssh +var Logger = logging.MustGetLogger("therealssh") // Session represents PTY sessions. Channel normally handles Session's lifecycle. type Session struct { @@ -38,7 +39,7 @@ func OpenSession(user *user.User, sz *pty.Winsize) (s *Session, err error) { if err = pty.Setsize(s.pty, sz); err != nil { if closeErr := s.Close(); closeErr != nil { - log.WithError(closeErr).Warn("Failed to close session") + Logger.WithError(closeErr).Warn("Failed to close session") } err = fmt.Errorf("failed to set PTY size: %s", err) } @@ -50,7 +51,7 @@ func OpenSession(user *user.User, sz *pty.Winsize) (s *Session, err error) { func (s *Session) Start(command string) (err error) { defer func() { if err := s.tty.Close(); err != nil { - log.WithError(err).Warn("Failed to close TTY") + Logger.WithError(err).Warn("Failed to close TTY") } }() diff --git a/pkg/app/protocol.go b/pkg/app/protocol.go index 6c80784d2..bcc35cda6 100644 --- a/pkg/app/protocol.go +++ b/pkg/app/protocol.go @@ -8,6 +8,8 @@ import ( "io" "strings" "sync" + + "github.com/skycoin/skycoin/src/util/logging" ) // Frame defines type for all App frames. @@ -25,6 +27,10 @@ func (f Frame) String() string { return "Send" case FrameClose: return "Close" + case FrameFailure: + return "Failure" + case FrameSuccess: + return "Success" } return fmt.Sprintf("Unknown(%d)", f) @@ -48,6 +54,9 @@ const ( FrameSuccess = 0xff ) +// Logger is PackageLogger for app +var Logger = logging.MustGetLogger("app") + // Protocol implements full-duplex protocol for App to Node communication. type Protocol struct { rw io.ReadWriteCloser @@ -61,6 +70,7 @@ func NewProtocol(rw io.ReadWriteCloser) *Protocol { // Send sends command Frame with payload and awaits for response. func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { + Logger.WithField("frame", cmd).Info("Sending from Protocol.Send") id, resChan := p.chans.add() if err := p.writeFrame(cmd, id, payload); err != nil { return err @@ -140,6 +150,7 @@ func (p *Protocol) Close() error { } func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err error) { + Logger.WithField("tcp-transport", "debug").WithField("payload", payload).Info("writeFrame") var data []byte if err, ok := payload.(error); ok { data = []byte(err.Error()) diff --git a/pkg/router/app_manager.go b/pkg/router/app_manager.go index bd1cf1022..725369350 100644 --- a/pkg/router/app_manager.go +++ b/pkg/router/app_manager.go @@ -13,9 +13,9 @@ import ( const supportedProtocolVersion = "0.0.1" type appCallbacks struct { - CreateLoop func(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) - CloseLoop func(conn *app.Protocol, loop routing.Loop) error - Forward func(conn *app.Protocol, packet *app.Packet) error + CreateLoop func(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) + CloseLoop func(conn *app.Protocol, loop routing.Loop) error + ForwardAppPacket func(conn *app.Protocol, packet *app.Packet) error } type appManager struct { @@ -28,7 +28,8 @@ type appManager struct { func (am *appManager) Serve() error { return am.proto.Serve(func(frame app.Frame, payload []byte) (res interface{}, err error) { - am.Logger.Infof("Got new App request with type %s: %s", frame, string(payload)) + am.Logger.WithField("frame", frame).WithField("payload", payload).Infof("Got new request in appManager.Serve") + switch frame { case app.FrameInit: err = am.initApp(payload) @@ -96,5 +97,6 @@ func (am *appManager) forwardAppPacket(payload []byte) error { return err } - return am.callbacks.Forward(am.proto, packet) + am.Logger.WithField("packet", packet).Info("appMananger.forwardAppPacket") + return am.callbacks.ForwardAppPacket(am.proto, packet) } diff --git a/pkg/router/app_manager_test.go b/pkg/router/app_manager_test.go index 1f34d54fb..8b4a3b653 100644 --- a/pkg/router/app_manager_test.go +++ b/pkg/router/app_manager_test.go @@ -139,7 +139,7 @@ func TestAppManagerForward(t *testing.T) { app.NewProtocol(out), &app.Config{AppName: "foo", AppVersion: "0.0.1"}, &appCallbacks{ - Forward: func(conn *app.Protocol, packet *app.Packet) error { + ForwardAppPacket: func(conn *app.Protocol, packet *app.Packet) error { inPacket = packet return nil }, diff --git a/pkg/router/router.go b/pkg/router/router.go index 4926cda04..fbb10d111 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -155,9 +155,9 @@ func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) r.mu.Unlock() callbacks := &appCallbacks{ - CreateLoop: r.requestLoop, - CloseLoop: r.closeLoop, - Forward: r.forwardAppPacket, + CreateLoop: r.requestLoop, + CloseLoop: r.closeLoop, + ForwardAppPacket: r.forwardAppPacket, } am := &appManager{r.Logger, appProto, appConf, callbacks} err := am.Serve() @@ -257,7 +257,7 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) error { - r.Logger.Info("Entering r.forwardAppPacket") + r.Logger.WithField("packet.Loop", packet.Loop).Info("Entering r.forwardAppPacket") if packet.Loop.Remote.PubKey == r.config.PubKey { return r.forwardLocalAppPacket(packet) @@ -268,10 +268,12 @@ func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) err return err } + r.Logger.WithField("trID", l.trID).Infof("Entering r.forwardAppPacket r.tm.Transport(l.trID)") tr := r.tm.Transport(l.trID) if tr == nil { return fmt.Errorf("unknown transport id %v", l.trID) } + r.Logger.Info("r.forwardAppPacket enter routing.MakePacket") p := routing.MakePacket(l.routeID, packet.Payload) @@ -281,18 +283,21 @@ func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) err } func (r *Router) forwardLocalAppPacket(packet *app.Packet) error { + r.Logger.Info("entering r.forwardLocalAppPacket ") b, err := r.pm.Get(packet.Loop.Remote.Port) if err != nil { return nil } - + // r.Logger.WithField("Local", ).Info("entering r.forwardLocalAppPacket app.Packet") p := &app.Packet{ Loop: routing.Loop{ - Local: routing.Addr{Port: packet.Loop.Remote.Port}, + Local: routing.Addr{PubKey: packet.Loop.Remote.PubKey, Port: packet.Loop.Remote.Port}, Remote: routing.Addr{PubKey: packet.Loop.Remote.PubKey, Port: packet.Loop.Local.Port}, }, Payload: packet.Payload, } + + r.Logger.WithField("packet", p).Info("entering r.forwardLocalAppPacket Send") return b.conn.Send(app.FrameSend, p, nil) } diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index ce5f81f6d..4c61f8c86 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -48,9 +48,9 @@ func TestRouterForwarding(t *testing.T) { pk2, sk2 := cipher.GenerateKeyPair() pk3, sk3 := cipher.GenerateKeyPair() - c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore} - c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore} - c3 := &transport.ManagerConfig{PubKey: pk3, SecKey: sk3, DiscoveryClient: client, LogStore: logStore} + c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log} + c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log} + c3 := &transport.ManagerConfig{PubKey: pk3, SecKey: sk3, DiscoveryClient: client, LogStore: logStore, Logger: log} f1, f2 := transport.NewMockFactoryPair(pk1, pk2) f3, f4 := transport.NewMockFactoryPair(pk2, pk3) @@ -117,7 +117,7 @@ func TestRouterAppInit(t *testing.T) { logStore := transport.InMemoryTransportLogStore() pk1, sk1 := cipher.GenerateKeyPair() - c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore} + c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log} m1, err := transport.NewManager(c1) require.NoError(t, err) @@ -157,8 +157,8 @@ func TestRouterApp(t *testing.T) { pk1, sk1 := cipher.GenerateKeyPair() pk2, sk2 := cipher.GenerateKeyPair() - c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore} - c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore} + c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log} + c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log} f1, f2 := transport.NewMockFactoryPair(pk1, pk2) m1, err := transport.NewManager(c1, f1) @@ -254,7 +254,7 @@ func TestRouterLocalApp(t *testing.T) { logStore := transport.InMemoryTransportLogStore() pk, sk := cipher.GenerateKeyPair() - m, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk, SecKey: sk, DiscoveryClient: client, LogStore: logStore}) + m, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk, SecKey: sk, DiscoveryClient: client, LogStore: logStore, Logger: log}) require.NoError(t, err) conf := &Config{ @@ -332,8 +332,8 @@ func TestRouterSetup(t *testing.T) { pk1, sk1 := cipher.GenerateKeyPair() pk2, sk2 := cipher.GenerateKeyPair() - c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore} - c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore} + c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log} + c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log} f1, f2 := transport.NewMockFactoryPair(pk1, pk2) m1, err := transport.NewManager(c1, f1) @@ -538,11 +538,11 @@ func TestRouterSetupLoop(t *testing.T) { f1.SetType(dmsg.Type) f2.SetType(dmsg.Type) - m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, f1) + m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log}, f1) require.NoError(t, err) m1.SetSetupNodes([]cipher.PubKey{pk2}) - m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}, f2) + m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log}, f2) require.NoError(t, err) m2.SetSetupNodes([]cipher.PubKey{pk1}) @@ -663,11 +663,11 @@ func TestRouterCloseLoop(t *testing.T) { f1, f2 := transport.NewMockFactoryPair(pk1, pk2) f1.SetType(dmsg.Type) - m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, f1) + m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log}, f1) require.NoError(t, err) m1.SetSetupNodes([]cipher.PubKey{pk2}) - m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}, f2) + m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log}, f2) require.NoError(t, err) m2.SetSetupNodes([]cipher.PubKey{pk1}) @@ -766,11 +766,11 @@ func TestRouterCloseLoopOnAppClose(t *testing.T) { f1, f2 := transport.NewMockFactoryPair(pk1, pk2) f1.SetType(dmsg.Type) - m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore}, f1) + m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log}, f1) require.NoError(t, err) m1.SetSetupNodes([]cipher.PubKey{pk2}) - m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore}, f2) + m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log}, f2) require.NoError(t, err) m2.SetSetupNodes([]cipher.PubKey{pk1}) diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 6f397234f..cd9f72b5d 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -22,6 +22,7 @@ type ManagerConfig struct { DiscoveryClient DiscoveryClient LogStore LogStore DefaultNodes []cipher.PubKey // Nodes to automatically connect to + Logger *logging.Logger } // Manager manages Transports. @@ -51,7 +52,8 @@ func NewManager(config *ManagerConfig, factories ...Factory) (*Manager, error) { entries = make([]*EntryWithStatus, 0) } - log.Infof("transport.NewManager. entries: v%\n", entries) + + // log.Infof("transport.NewManager. entries: v%\n", entries) mEntries := make(map[Entry]struct{}) for _, entry := range entries { @@ -64,7 +66,7 @@ func NewManager(config *ManagerConfig, factories ...Factory) (*Manager, error) { } return &Manager{ - Logger: logging.MustGetLogger("tp_manager"), + Logger: config.Logger, config: config, factories: fMap, transports: make(map[uuid.UUID]*ManagedTransport), diff --git a/pkg/transport/manager_test.go b/pkg/transport/manager_test.go index f9031204d..9718052f3 100644 --- a/pkg/transport/manager_test.go +++ b/pkg/transport/manager_test.go @@ -40,8 +40,8 @@ func TestTransportManager(t *testing.T) { pk1, sk1 := cipher.GenerateKeyPair() pk2, sk2 := cipher.GenerateKeyPair() - c1 := &ManagerConfig{pk1, sk1, client, logStore, nil} - c2 := &ManagerConfig{pk2, sk2, client, logStore, nil} + c1 := &ManagerConfig{pk1, sk1, client, logStore, nil, log} + c2 := &ManagerConfig{pk2, sk2, client, logStore, nil, log} f1, f2 := NewMockFactoryPair(pk1, pk2) m1, err := NewManager(c1, f1) @@ -137,8 +137,8 @@ func TestTransportManagerReEstablishTransports(t *testing.T) { pk1, sk1 := cipher.GenerateKeyPair() pk2, sk2 := cipher.GenerateKeyPair() - c1 := &ManagerConfig{pk1, sk1, client, logStore, nil} - c2 := &ManagerConfig{pk2, sk2, client, logStore, nil} + c1 := &ManagerConfig{pk1, sk1, client, logStore, nil, log} + c2 := &ManagerConfig{pk2, sk2, client, logStore, nil, log} f1, f2 := NewMockFactoryPair(pk1, pk2) m1, err := NewManager(c1, f1) @@ -201,8 +201,8 @@ func TestTransportManagerLogs(t *testing.T) { pk1, sk1 := cipher.GenerateKeyPair() pk2, sk2 := cipher.GenerateKeyPair() - c1 := &ManagerConfig{pk1, sk1, client, logStore1, nil} - c2 := &ManagerConfig{pk2, sk2, client, logStore2, nil} + c1 := &ManagerConfig{pk1, sk1, client, logStore1, nil, log} + c2 := &ManagerConfig{pk2, sk2, client, logStore2, nil, log} f1, f2 := NewMockFactoryPair(pk1, pk2) m1, err := NewManager(c1, f1) diff --git a/pkg/transport/mock.go b/pkg/transport/mock.go index e9598e781..da97bf6c1 100644 --- a/pkg/transport/mock.go +++ b/pkg/transport/mock.go @@ -172,8 +172,8 @@ func MockTransportManagersPair() (pk1, pk2 cipher.PubKey, m1, m2 *Manager, errCh pk1, sk1 = cipher.GenerateKeyPair() pk2, sk2 = cipher.GenerateKeyPair() - c1 := &ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: discovery, LogStore: logs} - c2 := &ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: discovery, LogStore: logs} + c1 := &ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: discovery, LogStore: logs, Logger: log} + c2 := &ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: discovery, LogStore: logs, Logger: log} f1, f2 := NewMockFactoryPair(pk1, pk2) diff --git a/pkg/transport/tcp_transport_test.go b/pkg/transport/tcp_transport_test.go index 3bdc04134..b35585770 100644 --- a/pkg/transport/tcp_transport_test.go +++ b/pkg/transport/tcp_transport_test.go @@ -18,15 +18,15 @@ import ( /* This test requires configured IPs on host - sudo ip addr add 192.168.1.2 dev lo - sudo ip addr add 192.168.1.3 dev lo + sudo ip addr add 12.12.12.2 dev lo + sudo ip addr add 12.12.12.3 dev lo */ func Example_transport_TCPFactory() { - pkA := pkFromSeed("nodeA") - pkB := pkFromSeed("nodeB") - ipA := "192.168.1.2:9119" - ipB := "192.168.1.3:9119" + pkA := pkFromSeed("skyhost_001") + pkB := pkFromSeed("skyhost_002") + ipA := "skyhost_001:9119" + ipB := "skyhost_002:9119" addrA, _ := net.ResolveTCPAddr("tcp", ipA) lsnA, err := net.ListenTCP("tcp", addrA) @@ -208,8 +208,8 @@ func pkFromSeed(seed string) cipher.PubKey { func Example_transport_MemoryPubKeyTable() { pkA, pkB := pkFromSeed("nodeA"), pkFromSeed("nodeB") - ipA, ipB := "192.168.1.2:9119", "192.168.1.3:9119" - ipAA := "192.168.1.2:54312" + ipA, ipB := "skyhost_001:9119", "skyhost_003:9119" + ipAA := "skyhost_001:54312" entries := map[cipher.PubKey]string{ pkA: ipA, pkB: ipB, @@ -220,15 +220,15 @@ func Example_transport_MemoryPubKeyTable() { fmt.Printf("pkB in: %v\n", pkt.RemotePK(ipA)) fmt.Printf("pkA out: %v\n", pkt.RemotePK(ipAA)) - // Output: ipA: 192.168.1.2:9119 + // Output: ipA: skyhost_001:9119 // pkB in: 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab // pkA out: 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab } func Example_transport_FilePubKeyTable() { pkA, pkB := pkFromSeed("nodeA"), pkFromSeed("nodeB") - ipA, ipB := "192.168.1.2:9119", "192.168.1.3:9119" - ipAA := "192.168.1.2:54312" + ipA, ipB := "skyhost_001:9119", "skyhost_002:9119" + ipAA := "skyhost_001:54312" pkFileContent := fmt.Sprintf("%v%v", @@ -251,11 +251,11 @@ func Example_transport_FilePubKeyTable() { fmt.Printf("PK for ipB: %v\n", pkt.RemotePK(ipB)) // Output: pubkeys: - // 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab 192.168.1.2:9119 - // 033978326862c191eaa39e33bb556a6296466facfe36bfb81e6b4c99d9c510e09f 192.168.1.3:9119 + // 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab skyhost_001:9119 + // 033978326862c191eaa39e33bb556a6296466facfe36bfb81e6b4c99d9c510e09f skyhost_002:9119 // Write file success: true // Opening FilePubKeyTable success: true - // ipA: 192.168.1.2:9119 + // ipA: 12.12.12.1:9119 // PK for ipA: 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab // PK for ipAA: 03c8ab0302ecda8564df4bce595c456a03b64871caff699fcafaf24a93058474ab // PK for ipB: 033978326862c191eaa39e33bb556a6296466facfe36bfb81e6b4c99d9c510e09f diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 33fe2e672..78e1c3beb 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -23,7 +23,10 @@ import ( "github.com/skycoin/dmsg/noise" "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/internal/therealproxy" + "github.com/skycoin/skywire/internal/therealssh" "github.com/skycoin/skywire/pkg/app" + appProto "github.com/skycoin/skywire/pkg/app" routeFinder "github.com/skycoin/skywire/pkg/route-finder/client" "github.com/skycoin/skywire/pkg/router" "github.com/skycoin/skywire/pkg/routing" @@ -55,6 +58,15 @@ const supportedProtocolVersion = "0.0.1" var reservedPorts = map[routing.Port]string{0: "router", 1: "skychat", 2: "SSH", 3: "socksproxy"} +// MasterLogger interface +// type MasterLogger interface { +// PackageLogger(string) *logging.Logger + +// SetLevel(logrus.Level) +// WithError(err error) *logrus.Entry +// WithField(key string, value interface{}) *logrus.Entry +// } + // AppState defines state parameters for a registered App. type AppState struct { Name string `json:"name"` @@ -170,8 +182,13 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) DiscoveryClient: trDiscovery, LogStore: logStore, DefaultNodes: config.TrustedNodes, + Logger: masterLogger.PackageLogger("tr-manager"), } + appProto.Logger = masterLogger.PackageLogger("app") + therealssh.Logger = masterLogger.PackageLogger("therealssh") + therealproxy.Logger = masterLogger.PackageLogger("therealproxy") + node.tm, err = transport.NewManager(tmConfig, node.messenger) if err != nil { diff --git a/pkg/visor/visor_multihead_test.go b/pkg/visor/visor_multihead_test.go index 897fa84aa..d3d7ea513 100644 --- a/pkg/visor/visor_multihead_test.go +++ b/pkg/visor/visor_multihead_test.go @@ -9,9 +9,14 @@ import ( "net/http" "os" "path/filepath" + "strings" "sync" "time" + "github.com/skycoin/skycoin/src/util/logging" + + "github.com/sirupsen/logrus" + "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skywire/pkg/routing" ) @@ -39,16 +44,20 @@ type MultiHead struct { cfgPool []Config nodes []*Node - Log *multiheadLog + Log *multiheadLog + initErrs chan error + startErrs chan error + stopErrs chan error + sync.Once } func initMultiHead() *MultiHead { mhLog := multiheadLog{records: []string{}} - masterLogger.Out = &mhLog return &MultiHead{Log: &mhLog} } func Example_initMultiHead() { + mh := initMultiHead() mh.Log.Write([]byte("initMultiHead success")) fmt.Printf("%v\n", mh.Log.records) @@ -148,18 +157,52 @@ func Example_initCfgPool() { // len(mh.cfgPool): 16 } -func (mh *MultiHead) initNodes() chan error { +type TaggedFormatter struct { + tag []byte + *logging.TextFormatter +} + +func (tf *TaggedFormatter) Format(entry *logrus.Entry) ([]byte, error) { + data, err := tf.TextFormatter.Format(entry) + return append(tf.tag, data...), err +} + +// NewTaggedMasterLogger creates MasterLogger that prepends records with tag +func NewTaggedMasterLogger(tag string) *logging.MasterLogger { + hooks := make(logrus.LevelHooks) + return &logging.MasterLogger{ + Logger: &logrus.Logger{ + Out: os.Stdout, + Formatter: &TaggedFormatter{ + []byte(tag), + &logging.TextFormatter{ + FullTimestamp: true, + AlwaysQuoteStrings: true, + QuoteEmptyFields: true, + ForceFormatting: true, + DisableColors: false, + ForceColors: false, + }, + }, + Hooks: hooks, + Level: logrus.DebugLevel, + }, + } +} + +func (mh *MultiHead) initNodes() { mh.nodes = make([]*Node, len(mh.cfgPool)) - errs := make(chan error, len(mh.cfgPool)) + mh.initErrs = make(chan error, len(mh.cfgPool)) var err error for i := 0; i < len(mh.nodes); i++ { - mh.nodes[i], err = NewNode(&mh.cfgPool[i], masterLogger) + logger := NewTaggedMasterLogger(fmt.Sprintf("[node_%03d]", i+1)) + logger.Out = mh.Log + mh.nodes[i], err = NewNode(&mh.cfgPool[i], logger) if err != nil { - errs <- fmt.Errorf("error %v starting node %v", err, i) + mh.initErrs <- fmt.Errorf("error %v starting node %v", err, i) } } - return errs } func ExampleMultiHead_initNodes() { @@ -167,92 +210,90 @@ func ExampleMultiHead_initNodes() { cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") err = mh.readBaseConfig(cfgFile) fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) - mh.initIPPool("12.12.12.%d", 16) + mh.initIPPool("skyhost_%03d", 16) mh.initCfgPool() - initErrs := mh.initNodes() - close(initErrs) + mh.initNodes() - for err := range initErrs { + close(mh.initErrs) + for err := range mh.initErrs { fmt.Printf("%v\n", err) } - fmt.Printf("Errors on initNodes: %v\n", len(initErrs)) + fmt.Printf("Errors on initNodes: %v\n", len(mh.initErrs)) // Output: mh.ReadBaseConfig success: true // Errors on initNodes: 0 } -func (mh *MultiHead) startNodes() chan error { - errs := make(chan error, len(mh.nodes)) +func (mh *MultiHead) startNodes(postdelay time.Duration) { + mh.startErrs = make(chan error, len(mh.nodes)) for i := 0; i < len(mh.nodes); i++ { go func(n int) { if err := mh.nodes[n].Start(); err != nil { - errs <- fmt.Errorf("error %v starting node %v", err, n) + mh.startErrs <- fmt.Errorf("error %v starting node %v", err, n) } }(i) } - return errs + time.Sleep(postdelay) } -func (mh *MultiHead) stopNodes() chan error { - errs := make(chan error, len(mh.nodes)) +func (mh *MultiHead) stopNodes(predelay time.Duration) { + time.Sleep(predelay) + mh.stopErrs = make(chan error, len(mh.nodes)) for i := 0; i < len(mh.nodes); i++ { go func(n int) { if err := mh.nodes[n].Close(); err != nil { - errs <- fmt.Errorf("error %v starting node %v", err, n) + mh.stopErrs <- fmt.Errorf("error %v starting node %v", err, n) } }(i) } - return errs } func makeMultiHeadN(n uint) *MultiHead { mh := initMultiHead() cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") err = mh.readBaseConfig(cfgFile) - fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) - mh.initIPPool("12.12.12.%d", n) + if err != nil { + panic(err) + } + mh.initIPPool("skyhost_%03d", n) mh.initCfgPool() - + mh.initNodes() return mh } -func ExampleMultiHead_startNodes() { - mh := initMultiHead() - cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") - err = mh.readBaseConfig(cfgFile) - fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) - mh.initIPPool("12.12.12.%d", 16) - mh.initCfgPool() - initErrs := mh.initNodes() - close(initErrs) - - startErrs := mh.startNodes() - time.Sleep(time.Second * 5) - stopErrs := mh.stopNodes() - time.Sleep(time.Second * 5) - - close(startErrs) - close(stopErrs) - - for err := range initErrs { - fmt.Printf("%v\n", err) - } - fmt.Printf("Errors on initNodes: %v\n", len(initErrs)) - - for err := range startErrs { - fmt.Printf("%v\n", err) - } - fmt.Printf("Errors on startNodes: %v\n", len(startErrs)) - - for err := range stopErrs { - fmt.Printf("%v\n", err) +func (mh *MultiHead) errReport() string { + mh.Do(func() { + close(mh.initErrs) + close(mh.startErrs) + close(mh.stopErrs) + }) + collectErrs := func(tmplt string, errs chan error) string { + recs := make([]string, len(errs)+1) + for err := range errs { + recs = append(recs, fmt.Sprintf("%v", err)) + } + recs = append(recs, fmt.Sprintf(tmplt, len(errs))) + return strings.Join(recs, "\n") } - fmt.Printf("Errors on stopNodes: %v\n", len(stopErrs)) + return strings.Join([]string{ + collectErrs("init errors: %v", mh.initErrs), + collectErrs("start errors: %v", mh.startErrs), + collectErrs("stop errors: %v", mh.stopErrs), + }, "") +} - // Output: mh.ReadBaseConfig success: true - // Errors on initNodes: 0 - // Errors on startNodes: 0 - // Errors on stopNodes: 0 +func ExampleMultiHead_startNodes() { + delay := time.Second + n := uint(32) + mh := makeMultiHeadN(n) + mh.startNodes(delay) + + mh.stopNodes(delay) + fmt.Printf("%v\n", mh.errReport()) + fmt.Printf("records: %v, %v\n", len(mh.Log.records), len(mh.Log.records) >= int(n*12)) + // Output: init errors: 0 + // start errors: 0 + // stop errors: 0 } func (mh *MultiHead) sendMessage(sender, reciever uint, message string) (*http.Response, error) { @@ -266,41 +307,20 @@ func (mh *MultiHead) sendMessage(sender, reciever uint, message string) (*http.R } +// WIP func ExampleMultiHead_sendMessage() { - mh := makeMultiHeadN(2) - - initErrs := mh.initNodes() - startErrs := mh.startNodes() - time.Sleep(time.Second * 2) + mh := makeMultiHeadN(1) + mh.startNodes(time.Second) - // fmt.Printf("resp: %v, err: %v\n", resp, err) + _, err := mh.sendMessage(0, 0, "Hello") + fmt.Printf("err: %v", err) - resp, err := mh.sendMessage(0, 1, "Hello") - fmt.Printf("resp: %v, err: %v\n ", resp, err) - - stopErrs := mh.stopNodes() - time.Sleep(time.Second * 5) - - close(initErrs) - close(startErrs) - close(stopErrs) - - for err := range initErrs { - fmt.Printf("%v\n", err) - } - fmt.Printf("Errors on initNodes: %v\n", len(initErrs)) - - for err := range startErrs { - fmt.Printf("%v\n", err) - } - fmt.Printf("Errors on startNodes: %v\n", len(startErrs)) - - for err := range stopErrs { - fmt.Printf("%v\n", err) - } - fmt.Printf("Errors on stopNodes: %v\n", len(stopErrs)) - - fmt.Printf("%v\n", mh.Log.records) + mh.stopNodes(time.Second * 3) + fmt.Printf("%v\n", mh.errReport()) + // fmt.Printf("%v\n", mh.Log.records) - // Output: ZZZ + // Output: err: + // init errors: 0 + // start errors: 0 + // stop errors: 0 } From a8142bc8a30e57b30c7eb7fb74c21b8e75b0980a Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Fri, 16 Aug 2019 09:42:30 +0300 Subject: [PATCH 15/26] Restructured tests for router --- internal/testhelpers/testhelpers.go | 22 +- pkg/router/router_close_test.go | 224 +++++++++++ pkg/router/router_setup_test.go | 412 ++++++++++++++++++++ pkg/router/router_test.go | 576 +--------------------------- pkg/visor/taggedformatter.go | 57 +++ pkg/visor/visor_multihead_test.go | 49 +-- 6 files changed, 727 insertions(+), 613 deletions(-) create mode 100644 pkg/router/router_close_test.go create mode 100644 pkg/router/router_setup_test.go create mode 100644 pkg/visor/taggedformatter.go diff --git a/internal/testhelpers/testhelpers.go b/internal/testhelpers/testhelpers.go index 72f3f92fa..fcc9b91f9 100644 --- a/internal/testhelpers/testhelpers.go +++ b/internal/testhelpers/testhelpers.go @@ -5,7 +5,19 @@ import ( "time" ) -const timeout = 5 * time.Second +// Timeout defines timeout for NoErrorWithinTimeout +var Timeout = 5 * time.Second + +// joinErrChannels multiplexes several error channels +func joinErrChannels(errChans []<-chan error) chan error { + joinedCh := make(chan error) + for _, ch := range errChans { + go func(errCh <-chan error) { + joinedCh <- <-errCh + }(ch) + } + return joinedCh +} // NoErrorWithinTimeout tries to read an error from error channel within timeout and returns it. // If timeout exceeds, nil value is returned. @@ -13,7 +25,13 @@ func NoErrorWithinTimeout(ch <-chan error) error { select { case err := <-ch: return err - case <-time.After(timeout): + case <-time.After(Timeout): return nil } } + +// NoErrorWithinTimeoutN tries to read an error from error channels within timeout and returns it. +// If timeout exceeds, nil value is returned. +func NoErrorWithinTimeoutN(errChans ...<-chan error) error { + return NoErrorWithinTimeout(joinErrChannels(errChans)) +} diff --git a/pkg/router/router_close_test.go b/pkg/router/router_close_test.go new file mode 100644 index 000000000..c51dcad72 --- /dev/null +++ b/pkg/router/router_close_test.go @@ -0,0 +1,224 @@ +package router + +import ( + "context" + "encoding/json" + "errors" + "net" + "testing" + "time" + + "github.com/skycoin/dmsg" + "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" + "github.com/stretchr/testify/require" + + "github.com/skycoin/skywire/internal/testhelpers" + "github.com/skycoin/skywire/pkg/app" + "github.com/skycoin/skywire/pkg/routing" + "github.com/skycoin/skywire/pkg/setup" + "github.com/skycoin/skywire/pkg/transport" +) + +func TestRouterCloseLoop(t *testing.T) { + client := transport.NewDiscoveryMock() + logStore := transport.InMemoryTransportLogStore() + + pk1, sk1 := cipher.GenerateKeyPair() + pk2, sk2 := cipher.GenerateKeyPair() + pk3, _ := cipher.GenerateKeyPair() + + f1, f2 := transport.NewMockFactoryPair(pk1, pk2) + f1.SetType(dmsg.Type) + + m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log}, f1) + require.NoError(t, err) + m1.SetSetupNodes([]cipher.PubKey{pk2}) + + m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log}, f2) + require.NoError(t, err) + m2.SetSetupNodes([]cipher.PubKey{pk1}) + + serveErrCh := make(chan error, 1) + go func() { + serveErrCh <- m2.Serve(context.TODO()) + }() + + rt := routing.InMemoryRoutingTable() + rule := routing.AppRule(time.Now().Add(time.Hour), 4, pk3, 6, 5) + routeID, err := rt.AddRule(rule) + require.NoError(t, err) + + conf := &Config{ + Logger: logging.MustGetLogger("routesetup"), + PubKey: pk1, + SecKey: sk1, + TransportManager: m1, + RoutingTable: rt, + SetupNodes: []cipher.PubKey{pk2}, + } + r := New(conf) + errCh := make(chan error) + go func() { + tr := <-m2.SetupTpChan + + proto := setup.NewSetupProtocol(tr) + p, data, err := proto.ReadPacket() + if err != nil { + errCh <- err + return + } + + if p != setup.PacketCloseLoop { + errCh <- errors.New("unknown command") + return + } + + var ld routing.LoopData + if err := json.Unmarshal(data, &ld); err != nil { + errCh <- err + return + } + + if ld.Loop.Local.Port != 5 || ld.Loop.Remote.Port != 6 || ld.Loop.Remote.PubKey != pk3 { + errCh <- errors.New("invalid payload") + return + } + + errCh <- proto.WritePacket(setup.RespSuccess, []byte{}) + }() + + rw, rwIn := net.Pipe() + serveAppErrCh := make(chan error, 1) + go func() { + serveAppErrCh <- r.ServeApp(rwIn, 5, &app.Config{}) + }() + proto := app.NewProtocol(rw) + protoServeErrCh := make(chan error, 1) + go func() { + protoServeErrCh <- proto.Serve(nil) + }() + + time.Sleep(100 * time.Millisecond) + + raddr := routing.Addr{PubKey: pk3, Port: 6} + require.NoError(t, r.pm.SetLoop(5, raddr, &loop{})) + + require.NoError(t, proto.Send(app.FrameClose, routing.Loop{Local: routing.Addr{Port: 5}, Remote: raddr}, nil)) + + time.Sleep(100 * time.Millisecond) + + // Tests + _ = routeID + require.NoError(t, <-errCh) + _, err = r.pm.GetLoop(5, routing.Addr{PubKey: pk3, Port: 6}) + require.Error(t, err) + _, err = r.pm.Get(5) + require.NoError(t, err) + + rule, err = rt.Rule(routeID) + require.NoError(t, err) + require.Nil(t, rule) + + require.NoError(t, + testhelpers.NoErrorWithinTimeoutN(serveErrCh, serveAppErrCh, protoServeErrCh)) +} + +func TestRouterCloseLoopOnAppClose(t *testing.T) { + client := transport.NewDiscoveryMock() + logStore := transport.InMemoryTransportLogStore() + + pk1, sk1 := cipher.GenerateKeyPair() + pk2, sk2 := cipher.GenerateKeyPair() + pk3, _ := cipher.GenerateKeyPair() + + f1, f2 := transport.NewMockFactoryPair(pk1, pk2) + f1.SetType(dmsg.Type) + + m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log}, f1) + require.NoError(t, err) + m1.SetSetupNodes([]cipher.PubKey{pk2}) + + m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log}, f2) + require.NoError(t, err) + m2.SetSetupNodes([]cipher.PubKey{pk1}) + + serveErrCh := make(chan error, 1) + go func() { + serveErrCh <- m2.Serve(context.TODO()) + }() + + rt := routing.InMemoryRoutingTable() + rule := routing.AppRule(time.Now().Add(time.Hour), 4, pk3, 6, 5) + routeID, err := rt.AddRule(rule) + require.NoError(t, err) + + conf := &Config{ + Logger: logging.MustGetLogger("routesetup"), + PubKey: pk1, + SecKey: sk1, + TransportManager: m1, + RoutingTable: rt, + SetupNodes: []cipher.PubKey{pk2}, + } + r := New(conf) + errCh := make(chan error) + go func() { + tr := <-m2.SetupTpChan + + proto := setup.NewSetupProtocol(tr) + p, data, err := proto.ReadPacket() + if err != nil { + errCh <- err + return + } + + if p != setup.PacketCloseLoop { + errCh <- errors.New("unknown command") + return + } + + var ld routing.LoopData + if err := json.Unmarshal(data, &ld); err != nil { + errCh <- err + return + } + + if ld.Loop.Local.Port != 5 || ld.Loop.Remote.Port != 6 || ld.Loop.Remote.PubKey != pk3 { + errCh <- errors.New("invalid payload") + return + } + + errCh <- proto.WritePacket(setup.RespSuccess, []byte{}) + }() + + rw, rwIn := net.Pipe() + serveAppErrCh := make(chan error, 1) + go func() { + serveAppErrCh <- r.ServeApp(rwIn, 5, &app.Config{}) + }() + proto := app.NewProtocol(rw) + protoServeErrCh := make(chan error, 1) + go func() { + protoServeErrCh <- proto.Serve(nil) + }() + + time.Sleep(100 * time.Millisecond) + + raddr := routing.Addr{PubKey: pk3, Port: 6} + require.NoError(t, r.pm.SetLoop(5, raddr, &loop{})) + + require.NoError(t, rw.Close()) + + time.Sleep(100 * time.Millisecond) + + require.NoError(t, <-errCh) + _, err = r.pm.Get(5) + require.Error(t, err) + + rule, err = rt.Rule(routeID) + require.NoError(t, err) + require.Nil(t, rule) + + require.NoError(t, testhelpers.NoErrorWithinTimeoutN(serveErrCh, serveAppErrCh, protoServeErrCh)) +} diff --git a/pkg/router/router_setup_test.go b/pkg/router/router_setup_test.go new file mode 100644 index 000000000..d61aa52e0 --- /dev/null +++ b/pkg/router/router_setup_test.go @@ -0,0 +1,412 @@ +package router + +import ( + "context" + "encoding/json" + "errors" + "net" + "testing" + "time" + + "github.com/skycoin/dmsg" + "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/skycoin/skywire/internal/testhelpers" + "github.com/skycoin/skywire/pkg/app" + routeFinder "github.com/skycoin/skywire/pkg/route-finder/client" + "github.com/skycoin/skywire/pkg/routing" + "github.com/skycoin/skywire/pkg/setup" + "github.com/skycoin/skywire/pkg/transport" +) + +func TestRouterSetup(t *testing.T) { + // Environment + client := transport.NewDiscoveryMock() + logStore := transport.InMemoryTransportLogStore() + + pk1, sk1 := cipher.GenerateKeyPair() + pk2, sk2 := cipher.GenerateKeyPair() + + c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log} + c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log} + + f1, f2 := transport.NewMockFactoryPair(pk1, pk2) + m1, err := transport.NewManager(c1, f1) + require.NoError(t, err) + + m2, err := transport.NewManager(c2, f2) + require.NoError(t, err) + + m1.SetSetupNodes([]cipher.PubKey{pk2}) + + rt := routing.InMemoryRoutingTable() + conf := &Config{ + Logger: logging.MustGetLogger("routesetup"), + PubKey: pk1, + SecKey: sk1, + TransportManager: m1, + RoutingTable: rt, + SetupNodes: []cipher.PubKey{pk2}, + } + r := New(conf) + errCh := make(chan error) + go func() { + errCh <- r.Serve(context.TODO()) + }() + + tr, err := m2.CreateSetupTransport(context.TODO(), pk1, "mock") + require.NoError(t, err) + trID := transport.MakeTransportID(tr.LocalPK(), tr.RemotePK(), tr.Type(), false) + sProto := setup.NewSetupProtocol(tr) + + rw1, rwIn1 := net.Pipe() + serveAppErr1Ch := make(chan error, 1) + go func() { + serveAppErr1Ch <- r.ServeApp(rwIn1, 2, &app.Config{}) + }() + appProto1 := app.NewProtocol(rw1) + dataCh := make(chan []byte) + protoServeErr1Ch := make(chan error, 1) + go func() { + f := func(_ app.Frame, p []byte) (interface{}, error) { + go func() { dataCh <- p }() + return nil, nil + } + protoServeErr1Ch <- appProto1.Serve(f) + }() + + rw2, rwIn2 := net.Pipe() + serveAppErr2Ch := make(chan error, 1) + go func() { + serveAppErr2Ch <- r.ServeApp(rwIn2, 4, &app.Config{}) + }() + appProto2 := app.NewProtocol(rw2) + protoServeErr2Ch := make(chan error, 1) + go func() { + f := func(_ app.Frame, p []byte) (interface{}, error) { + go func() { dataCh <- p }() + return nil, nil + } + protoServeErr2Ch <- appProto2.Serve(f) + }() + + // Start of tests + + skiptests := 0 //| 1 | 2 | 4 | 8 | 16 + + var routeID routing.RouteID + t.Run("add rule", func(t *testing.T) { + if skiptests&1 == 1 { + t.Skip("skipping add rule") + } + routeID, err = setup.AddRule(sProto, routing.ForwardRule(time.Now().Add(time.Hour), 2, trID)) + require.NoError(t, err) + + rule, err := rt.Rule(routeID) + require.NoError(t, err) + assert.Equal(t, routing.RouteID(2), rule.RouteID()) + assert.Equal(t, trID, rule.TransportID()) + }) + + t.Run("confirm loop - responder", func(t *testing.T) { + if skiptests&2 == 2 { + t.Skip("skipping confirm loop - responder") + } + + appRouteID, err := setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 1, 2)) + require.NoError(t, err) + + err = setup.ConfirmLoop(sProto, routing.LoopData{ + Loop: routing.Loop{ + Remote: routing.Addr{ + PubKey: pk2, + Port: 1, + }, + Local: routing.Addr{ + Port: 2, + }, + }, + RouteID: routeID, + }) + require.NoError(t, err) + + rule, err := rt.Rule(appRouteID) + require.NoError(t, err) + assert.Equal(t, routeID, rule.RouteID()) + _, err = r.pm.Get(2) + require.NoError(t, err) + loop, err := r.pm.GetLoop(2, routing.Addr{PubKey: pk2, Port: 1}) + require.NoError(t, err) + require.NotNil(t, loop) + assert.Equal(t, trID, loop.trID) + assert.Equal(t, routing.RouteID(2), loop.routeID) + + var addrs [2]routing.Addr + require.NoError(t, json.Unmarshal(<-dataCh, &addrs)) + require.NoError(t, err) + assert.Equal(t, pk1, addrs[0].PubKey) + assert.Equal(t, routing.Port(2), addrs[0].Port) + assert.Equal(t, pk2, addrs[1].PubKey) + assert.Equal(t, routing.Port(1), addrs[1].Port) + }) + + t.Run("confirm loop - initiator", func(t *testing.T) { + if skiptests&4 == 4 { + t.Skip() + } + + time.Sleep(100 * time.Millisecond) + + require.NoError(t, r.pm.SetLoop(4, routing.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) + + err = setup.ConfirmLoop(sProto, routing.LoopData{ + Loop: routing.Loop{ + Remote: routing.Addr{ + PubKey: pk2, + Port: 3, + }, + Local: routing.Addr{ + Port: 4, + }, + }, + RouteID: routeID, + }) + require.NoError(t, err) + + rule, err := rt.Rule(appRouteID) + require.NoError(t, err) + assert.Equal(t, routeID, rule.RouteID()) + l, err := r.pm.GetLoop(2, routing.Addr{PubKey: pk2, Port: 1}) + require.NoError(t, err) + require.NotNil(t, l) + assert.Equal(t, trID, l.trID) + assert.Equal(t, routing.RouteID(2), l.routeID) + + var addrs [2]routing.Addr + require.NoError(t, json.Unmarshal(<-dataCh, &addrs)) + require.NoError(t, err) + assert.Equal(t, pk1, addrs[0].PubKey) + assert.Equal(t, routing.Port(4), addrs[0].Port) + assert.Equal(t, pk2, addrs[1].PubKey) + assert.Equal(t, routing.Port(3), addrs[1].Port) + }) + + t.Run("loop closed", func(t *testing.T) { + if skiptests&8 == 8 { + t.Skip("skipping loop closed") + } + + rule, err := rt.Rule(3) + require.NoError(t, err) + require.NotNil(t, rule) + assert.Equal(t, routing.RuleApp, rule.Type()) + + require.NoError(t, setup.LoopClosed(sProto, routing.LoopData{ + Loop: routing.Loop{ + Remote: routing.Addr{ + PubKey: pk2, + Port: 3, + }, + Local: routing.Addr{ + Port: 4, + }, + }, + })) + time.Sleep(100 * time.Millisecond) + + _, err = r.pm.GetLoop(4, routing.Addr{PubKey: pk2, Port: 3}) + require.Error(t, err) + _, err = r.pm.Get(4) + require.NoError(t, err) + + rule, err = rt.Rule(3) + require.NoError(t, err) + require.Nil(t, rule) + }) + + t.Run("delete rule", func(t *testing.T) { + if skiptests&16 == 16 { + t.Skip("skipping delete rule") + } + + require.NoError(t, setup.DeleteRule(sProto, routeID)) + + rule, err := rt.Rule(routeID) + require.NoError(t, err) + assert.Nil(t, rule) + }) + + // testhelpers.Timeout = time.Second * 5 + require.NoError(t, testhelpers.NoErrorWithinTimeoutN( + protoServeErr1Ch, + protoServeErr2Ch, + serveAppErr1Ch, + serveAppErr2Ch)) +} + +func TestRouterSetupLoop(t *testing.T) { + // Environment + client := transport.NewDiscoveryMock() + logStore := transport.InMemoryTransportLogStore() + + pk1, sk1 := cipher.GenerateKeyPair() + pk2, sk2 := cipher.GenerateKeyPair() + + f1, f2 := transport.NewMockFactoryPair(pk1, pk2) + f1.SetType(dmsg.Type) + f2.SetType(dmsg.Type) + + m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log}, f1) + require.NoError(t, err) + m1.SetSetupNodes([]cipher.PubKey{pk2}) + + m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log}, f2) + require.NoError(t, err) + m2.SetSetupNodes([]cipher.PubKey{pk1}) + + serveErrCh := make(chan error, 1) + go func() { + serveErrCh <- m2.Serve(context.TODO()) + }() + + conf := &Config{ + Logger: logging.MustGetLogger("routesetup"), + PubKey: pk1, + SecKey: sk1, + TransportManager: m1, + RoutingTable: routing.InMemoryRoutingTable(), + RouteFinder: routeFinder.NewMock(), + SetupNodes: []cipher.PubKey{pk2}, + } + r := New(conf) + errCh := make(chan error) + go func() { + tr := <-m2.SetupTpChan + + proto := setup.NewSetupProtocol(tr) + p, data, err := proto.ReadPacket() + if err != nil { + errCh <- err + return + } + + if p != setup.PacketCreateLoop { + errCh <- errors.New("unknown command") + return + } + + var ld routing.LoopDescriptor + if err := json.Unmarshal(data, &ld); err != nil { + errCh <- err + return + } + + if ld.Loop.Local.Port != 10 || ld.Loop.Remote.Port != 6 { + errCh <- errors.New("invalid payload") + return + } + + errCh <- proto.WritePacket(setup.RespSuccess, []byte{}) + }() + + rw, rwIn := net.Pipe() + serveAppErrCh := make(chan error, 1) + go func() { + serveAppErrCh <- r.ServeApp(rwIn, 5, &app.Config{}) + }() + appProto := app.NewProtocol(rw) + protoServeErrCh := make(chan error, 1) + go func() { + protoServeErrCh <- appProto.Serve(nil) + }() + + addr := routing.Addr{} + require.NoError(t, appProto.Send(app.FrameCreateLoop, routing.Addr{PubKey: pk2, Port: 6}, &addr)) + + require.NoError(t, <-errCh) + ll, err := r.pm.GetLoop(10, routing.Addr{PubKey: pk2, Port: 6}) + require.NoError(t, err) + require.NotNil(t, ll) + + assert.Equal(t, pk1, addr.PubKey) + assert.Equal(t, routing.Port(10), addr.Port) + + require.NoError(t, testhelpers.NoErrorWithinTimeoutN(serveErrCh, serveAppErrCh, protoServeErrCh)) +} + +func TestRouterSetupLoopLocal(t *testing.T) { + pk, sk := cipher.GenerateKeyPair() + conf := &Config{ + Logger: logging.MustGetLogger("routesetup"), + PubKey: pk, + SecKey: sk, + } + r := New(conf) + + rw, rwIn := net.Pipe() + serveAppErrCh := make(chan error, 1) + go func() { + serveAppErrCh <- r.ServeApp(rwIn, 5, &app.Config{}) + }() + proto := app.NewProtocol(rw) + protoServeErrCh := make(chan error, 1) + go func() { + protoServeErrCh <- proto.Serve(nil) + }() + + addr := routing.Addr{} + require.NoError(t, proto.Send(app.FrameCreateLoop, routing.Addr{PubKey: pk, Port: 5}, &addr)) + + ll, err := r.pm.GetLoop(10, routing.Addr{PubKey: pk, Port: 5}) + require.NoError(t, err) + require.NotNil(t, ll) + + assert.Equal(t, pk, addr.PubKey) + assert.Equal(t, routing.Port(10), addr.Port) + + require.NoError(t, testhelpers.NoErrorWithinTimeoutN(serveAppErrCh, protoServeErrCh)) + +} + +func TestRouterRouteExpiration(t *testing.T) { + client := transport.NewDiscoveryMock() + logStore := transport.InMemoryTransportLogStore() + + pk, sk := cipher.GenerateKeyPair() + m, err := transport.NewManager( + &transport.ManagerConfig{PubKey: pk, SecKey: sk, + DiscoveryClient: client, LogStore: logStore, Logger: log}) + require.NoError(t, err) + + rt := routing.InMemoryRoutingTable() + _, err = rt.AddRule(routing.AppRule(time.Now().Add(-time.Hour), 4, pk, 6, 5)) + require.NoError(t, err) + assert.Equal(t, 1, rt.Count()) + + conf := &Config{ + Logger: logging.MustGetLogger("routesetup"), + PubKey: pk, + SecKey: sk, + TransportManager: m, + RoutingTable: rt, + } + r := New(conf) + r.expiryTicker = time.NewTicker(100 * time.Millisecond) + serveErrCh := make(chan error, 1) + go func() { + serveErrCh <- r.Serve(context.TODO()) + }() + + time.Sleep(110 * time.Millisecond) + + assert.Equal(t, 0, rt.Count()) + require.NoError(t, r.Close()) + + require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) +} diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 4c61f8c86..a5c555947 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -3,14 +3,12 @@ package router import ( "context" "encoding/json" - "errors" "net" "os" "testing" "time" "github.com/sirupsen/logrus" - "github.com/skycoin/dmsg" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" "github.com/stretchr/testify/assert" @@ -18,9 +16,7 @@ import ( "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" - routeFinder "github.com/skycoin/skywire/pkg/route-finder/client" "github.com/skycoin/skywire/pkg/routing" - "github.com/skycoin/skywire/pkg/setup" "github.com/skycoin/skywire/pkg/transport" ) @@ -34,7 +30,7 @@ func TestMain(m *testing.M) { logging.SetLevel(lvl) } else { logging.SetLevel(logrus.TraceLevel) - //logging.Disable() + logging.Disable() } os.Exit(m.Run()) @@ -243,6 +239,10 @@ func TestRouterApp(t *testing.T) { require.NoError(t, <-errCh) require.NoError(t, m2.Close()) + + // require.NoError(t, + // testhelpers.NoErrorWithinTimeoutN(trServeErrCh, protoServeErrCh, sendErrCh, serveAppErrCh)) + require.NoError(t, testhelpers.NoErrorWithinTimeout(trServeErrCh)) require.NoError(t, testhelpers.NoErrorWithinTimeout(protoServeErrCh)) require.NoError(t, testhelpers.NoErrorWithinTimeout(sendErrCh)) @@ -324,569 +324,3 @@ func TestRouterLocalApp(t *testing.T) { require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErr1Ch)) require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErr2Ch)) } - -func TestRouterSetup(t *testing.T) { - client := transport.NewDiscoveryMock() - logStore := transport.InMemoryTransportLogStore() - - pk1, sk1 := cipher.GenerateKeyPair() - pk2, sk2 := cipher.GenerateKeyPair() - - c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log} - c2 := &transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log} - - f1, f2 := transport.NewMockFactoryPair(pk1, pk2) - m1, err := transport.NewManager(c1, f1) - require.NoError(t, err) - - m2, err := transport.NewManager(c2, f2) - require.NoError(t, err) - - m1.SetSetupNodes([]cipher.PubKey{pk2}) - - rt := routing.InMemoryRoutingTable() - conf := &Config{ - Logger: logging.MustGetLogger("routesetup"), - PubKey: pk1, - SecKey: sk1, - TransportManager: m1, - RoutingTable: rt, - SetupNodes: []cipher.PubKey{pk2}, - } - r := New(conf) - errCh := make(chan error) - go func() { - errCh <- r.Serve(context.TODO()) - }() - - tr, err := m2.CreateSetupTransport(context.TODO(), pk1, "mock") - require.NoError(t, err) - trID := transport.MakeTransportID(tr.LocalPK(), tr.RemotePK(), tr.Type(), false) - sProto := setup.NewSetupProtocol(tr) - - rw1, rwIn1 := net.Pipe() - serveAppErr1Ch := make(chan error, 1) - go func() { - serveAppErr1Ch <- r.ServeApp(rwIn1, 2, &app.Config{}) - }() - appProto1 := app.NewProtocol(rw1) - dataCh := make(chan []byte) - protoServeErr1Ch := make(chan error, 1) - go func() { - f := func(_ app.Frame, p []byte) (interface{}, error) { - go func() { dataCh <- p }() - return nil, nil - } - protoServeErr1Ch <- appProto1.Serve(f) - }() - - rw2, rwIn2 := net.Pipe() - serveAppErr2Ch := make(chan error, 1) - go func() { - serveAppErr2Ch <- r.ServeApp(rwIn2, 4, &app.Config{}) - }() - appProto2 := app.NewProtocol(rw2) - protoServeErr2Ch := make(chan error, 1) - go func() { - f := func(_ app.Frame, p []byte) (interface{}, error) { - go func() { dataCh <- p }() - return nil, nil - } - protoServeErr2Ch <- appProto2.Serve(f) - }() - - var routeID routing.RouteID - t.Run("add rule", func(t *testing.T) { - routeID, err = setup.AddRule(sProto, routing.ForwardRule(time.Now().Add(time.Hour), 2, trID)) - require.NoError(t, err) - - rule, err := rt.Rule(routeID) - require.NoError(t, err) - assert.Equal(t, routing.RouteID(2), rule.RouteID()) - assert.Equal(t, trID, rule.TransportID()) - }) - - t.Run("confirm loop - responder", func(t *testing.T) { - appRouteID, err := setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 1, 2)) - require.NoError(t, err) - - err = setup.ConfirmLoop(sProto, routing.LoopData{ - Loop: routing.Loop{ - Remote: routing.Addr{ - PubKey: pk2, - Port: 1, - }, - Local: routing.Addr{ - Port: 2, - }, - }, - RouteID: routeID, - }) - require.NoError(t, err) - - rule, err := rt.Rule(appRouteID) - require.NoError(t, err) - assert.Equal(t, routeID, rule.RouteID()) - _, err = r.pm.Get(2) - require.NoError(t, err) - loop, err := r.pm.GetLoop(2, routing.Addr{PubKey: pk2, Port: 1}) - require.NoError(t, err) - require.NotNil(t, loop) - assert.Equal(t, trID, loop.trID) - assert.Equal(t, routing.RouteID(2), loop.routeID) - - var addrs [2]routing.Addr - require.NoError(t, json.Unmarshal(<-dataCh, &addrs)) - require.NoError(t, err) - assert.Equal(t, pk1, addrs[0].PubKey) - assert.Equal(t, routing.Port(2), addrs[0].Port) - assert.Equal(t, pk2, addrs[1].PubKey) - assert.Equal(t, routing.Port(1), addrs[1].Port) - }) - - t.Run("confirm loop - initiator", func(t *testing.T) { - time.Sleep(100 * time.Millisecond) - - require.NoError(t, r.pm.SetLoop(4, routing.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) - - err = setup.ConfirmLoop(sProto, routing.LoopData{ - Loop: routing.Loop{ - Remote: routing.Addr{ - PubKey: pk2, - Port: 3, - }, - Local: routing.Addr{ - Port: 4, - }, - }, - RouteID: routeID, - }) - require.NoError(t, err) - - rule, err := rt.Rule(appRouteID) - require.NoError(t, err) - assert.Equal(t, routeID, rule.RouteID()) - l, err := r.pm.GetLoop(2, routing.Addr{PubKey: pk2, Port: 1}) - require.NoError(t, err) - require.NotNil(t, l) - assert.Equal(t, trID, l.trID) - assert.Equal(t, routing.RouteID(2), l.routeID) - - var addrs [2]routing.Addr - require.NoError(t, json.Unmarshal(<-dataCh, &addrs)) - require.NoError(t, err) - assert.Equal(t, pk1, addrs[0].PubKey) - assert.Equal(t, routing.Port(4), addrs[0].Port) - assert.Equal(t, pk2, addrs[1].PubKey) - assert.Equal(t, routing.Port(3), addrs[1].Port) - }) - - t.Run("loop closed", func(t *testing.T) { - rule, err := rt.Rule(3) - require.NoError(t, err) - require.NotNil(t, rule) - assert.Equal(t, routing.RuleApp, rule.Type()) - - require.NoError(t, setup.LoopClosed(sProto, routing.LoopData{ - Loop: routing.Loop{ - Remote: routing.Addr{ - PubKey: pk2, - Port: 3, - }, - Local: routing.Addr{ - Port: 4, - }, - }, - })) - time.Sleep(100 * time.Millisecond) - - _, err = r.pm.GetLoop(4, routing.Addr{PubKey: pk2, Port: 3}) - require.Error(t, err) - _, err = r.pm.Get(4) - require.NoError(t, err) - - rule, err = rt.Rule(3) - require.NoError(t, err) - require.Nil(t, rule) - }) - - t.Run("delete rule", func(t *testing.T) { - require.NoError(t, setup.DeleteRule(sProto, routeID)) - - rule, err := rt.Rule(routeID) - require.NoError(t, err) - assert.Nil(t, rule) - }) - - require.NoError(t, testhelpers.NoErrorWithinTimeout(protoServeErr1Ch)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(protoServeErr2Ch)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErr1Ch)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErr2Ch)) -} - -func TestRouterSetupLoop(t *testing.T) { - client := transport.NewDiscoveryMock() - logStore := transport.InMemoryTransportLogStore() - - pk1, sk1 := cipher.GenerateKeyPair() - pk2, sk2 := cipher.GenerateKeyPair() - - f1, f2 := transport.NewMockFactoryPair(pk1, pk2) - f1.SetType(dmsg.Type) - f2.SetType(dmsg.Type) - - m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log}, f1) - require.NoError(t, err) - m1.SetSetupNodes([]cipher.PubKey{pk2}) - - m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log}, f2) - require.NoError(t, err) - m2.SetSetupNodes([]cipher.PubKey{pk1}) - - serveErrCh := make(chan error, 1) - go func() { - serveErrCh <- m2.Serve(context.TODO()) - }() - - conf := &Config{ - Logger: logging.MustGetLogger("routesetup"), - PubKey: pk1, - SecKey: sk1, - TransportManager: m1, - RoutingTable: routing.InMemoryRoutingTable(), - RouteFinder: routeFinder.NewMock(), - SetupNodes: []cipher.PubKey{pk2}, - } - r := New(conf) - errCh := make(chan error) - go func() { - tr := <-m2.SetupTpChan - - proto := setup.NewSetupProtocol(tr) - p, data, err := proto.ReadPacket() - if err != nil { - errCh <- err - return - } - - if p != setup.PacketCreateLoop { - errCh <- errors.New("unknown command") - return - } - - var ld routing.LoopDescriptor - if err := json.Unmarshal(data, &ld); err != nil { - errCh <- err - return - } - - if ld.Loop.Local.Port != 10 || ld.Loop.Remote.Port != 6 { - errCh <- errors.New("invalid payload") - return - } - - errCh <- proto.WritePacket(setup.RespSuccess, []byte{}) - }() - - rw, rwIn := net.Pipe() - serveAppErrCh := make(chan error, 1) - go func() { - serveAppErrCh <- r.ServeApp(rwIn, 5, &app.Config{}) - }() - appProto := app.NewProtocol(rw) - protoServeErrCh := make(chan error, 1) - go func() { - protoServeErrCh <- appProto.Serve(nil) - }() - - addr := routing.Addr{} - require.NoError(t, appProto.Send(app.FrameCreateLoop, routing.Addr{PubKey: pk2, Port: 6}, &addr)) - - require.NoError(t, <-errCh) - ll, err := r.pm.GetLoop(10, routing.Addr{PubKey: pk2, Port: 6}) - require.NoError(t, err) - require.NotNil(t, ll) - - assert.Equal(t, pk1, addr.PubKey) - assert.Equal(t, routing.Port(10), addr.Port) - - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(protoServeErrCh)) -} - -func TestRouterSetupLoopLocal(t *testing.T) { - pk, sk := cipher.GenerateKeyPair() - conf := &Config{ - Logger: logging.MustGetLogger("routesetup"), - PubKey: pk, - SecKey: sk, - } - r := New(conf) - - rw, rwIn := net.Pipe() - serveAppErrCh := make(chan error, 1) - go func() { - serveAppErrCh <- r.ServeApp(rwIn, 5, &app.Config{}) - }() - proto := app.NewProtocol(rw) - protoServeErrCh := make(chan error, 1) - go func() { - protoServeErrCh <- proto.Serve(nil) - }() - - addr := routing.Addr{} - require.NoError(t, proto.Send(app.FrameCreateLoop, routing.Addr{PubKey: pk, Port: 5}, &addr)) - - ll, err := r.pm.GetLoop(10, routing.Addr{PubKey: pk, Port: 5}) - require.NoError(t, err) - require.NotNil(t, ll) - - assert.Equal(t, pk, addr.PubKey) - assert.Equal(t, routing.Port(10), addr.Port) - - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(protoServeErrCh)) -} - -func TestRouterCloseLoop(t *testing.T) { - client := transport.NewDiscoveryMock() - logStore := transport.InMemoryTransportLogStore() - - pk1, sk1 := cipher.GenerateKeyPair() - pk2, sk2 := cipher.GenerateKeyPair() - pk3, _ := cipher.GenerateKeyPair() - - f1, f2 := transport.NewMockFactoryPair(pk1, pk2) - f1.SetType(dmsg.Type) - - m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log}, f1) - require.NoError(t, err) - m1.SetSetupNodes([]cipher.PubKey{pk2}) - - m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log}, f2) - require.NoError(t, err) - m2.SetSetupNodes([]cipher.PubKey{pk1}) - - serveErrCh := make(chan error, 1) - go func() { - serveErrCh <- m2.Serve(context.TODO()) - }() - - rt := routing.InMemoryRoutingTable() - rule := routing.AppRule(time.Now().Add(time.Hour), 4, pk3, 6, 5) - routeID, err := rt.AddRule(rule) - require.NoError(t, err) - - conf := &Config{ - Logger: logging.MustGetLogger("routesetup"), - PubKey: pk1, - SecKey: sk1, - TransportManager: m1, - RoutingTable: rt, - SetupNodes: []cipher.PubKey{pk2}, - } - r := New(conf) - errCh := make(chan error) - go func() { - tr := <-m2.SetupTpChan - - proto := setup.NewSetupProtocol(tr) - p, data, err := proto.ReadPacket() - if err != nil { - errCh <- err - return - } - - if p != setup.PacketCloseLoop { - errCh <- errors.New("unknown command") - return - } - - var ld routing.LoopData - if err := json.Unmarshal(data, &ld); err != nil { - errCh <- err - return - } - - if ld.Loop.Local.Port != 5 || ld.Loop.Remote.Port != 6 || ld.Loop.Remote.PubKey != pk3 { - errCh <- errors.New("invalid payload") - return - } - - errCh <- proto.WritePacket(setup.RespSuccess, []byte{}) - }() - - rw, rwIn := net.Pipe() - serveAppErrCh := make(chan error, 1) - go func() { - serveAppErrCh <- r.ServeApp(rwIn, 5, &app.Config{}) - }() - proto := app.NewProtocol(rw) - protoServeErrCh := make(chan error, 1) - go func() { - protoServeErrCh <- proto.Serve(nil) - }() - - time.Sleep(100 * time.Millisecond) - - raddr := routing.Addr{PubKey: pk3, Port: 6} - require.NoError(t, r.pm.SetLoop(5, raddr, &loop{})) - - require.NoError(t, proto.Send(app.FrameClose, routing.Loop{Local: routing.Addr{Port: 5}, Remote: raddr}, nil)) - - time.Sleep(100 * time.Millisecond) - - require.NoError(t, <-errCh) - _, err = r.pm.GetLoop(5, routing.Addr{PubKey: pk3, Port: 6}) - require.Error(t, err) - _, err = r.pm.Get(5) - require.NoError(t, err) - - rule, err = rt.Rule(routeID) - require.NoError(t, err) - require.Nil(t, rule) - - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(protoServeErrCh)) -} - -func TestRouterCloseLoopOnAppClose(t *testing.T) { - client := transport.NewDiscoveryMock() - logStore := transport.InMemoryTransportLogStore() - - pk1, sk1 := cipher.GenerateKeyPair() - pk2, sk2 := cipher.GenerateKeyPair() - pk3, _ := cipher.GenerateKeyPair() - - f1, f2 := transport.NewMockFactoryPair(pk1, pk2) - f1.SetType(dmsg.Type) - - m1, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log}, f1) - require.NoError(t, err) - m1.SetSetupNodes([]cipher.PubKey{pk2}) - - m2, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk2, SecKey: sk2, DiscoveryClient: client, LogStore: logStore, Logger: log}, f2) - require.NoError(t, err) - m2.SetSetupNodes([]cipher.PubKey{pk1}) - - serveErrCh := make(chan error, 1) - go func() { - serveErrCh <- m2.Serve(context.TODO()) - }() - - rt := routing.InMemoryRoutingTable() - rule := routing.AppRule(time.Now().Add(time.Hour), 4, pk3, 6, 5) - routeID, err := rt.AddRule(rule) - require.NoError(t, err) - - conf := &Config{ - Logger: logging.MustGetLogger("routesetup"), - PubKey: pk1, - SecKey: sk1, - TransportManager: m1, - RoutingTable: rt, - SetupNodes: []cipher.PubKey{pk2}, - } - r := New(conf) - errCh := make(chan error) - go func() { - tr := <-m2.SetupTpChan - - proto := setup.NewSetupProtocol(tr) - p, data, err := proto.ReadPacket() - if err != nil { - errCh <- err - return - } - - if p != setup.PacketCloseLoop { - errCh <- errors.New("unknown command") - return - } - - var ld routing.LoopData - if err := json.Unmarshal(data, &ld); err != nil { - errCh <- err - return - } - - if ld.Loop.Local.Port != 5 || ld.Loop.Remote.Port != 6 || ld.Loop.Remote.PubKey != pk3 { - errCh <- errors.New("invalid payload") - return - } - - errCh <- proto.WritePacket(setup.RespSuccess, []byte{}) - }() - - rw, rwIn := net.Pipe() - serveAppErrCh := make(chan error, 1) - go func() { - serveAppErrCh <- r.ServeApp(rwIn, 5, &app.Config{}) - }() - proto := app.NewProtocol(rw) - protoServeErrCh := make(chan error, 1) - go func() { - protoServeErrCh <- proto.Serve(nil) - }() - - time.Sleep(100 * time.Millisecond) - - raddr := routing.Addr{PubKey: pk3, Port: 6} - require.NoError(t, r.pm.SetLoop(5, raddr, &loop{})) - - require.NoError(t, rw.Close()) - - time.Sleep(100 * time.Millisecond) - - require.NoError(t, <-errCh) - _, err = r.pm.Get(5) - require.Error(t, err) - - rule, err = rt.Rule(routeID) - require.NoError(t, err) - require.Nil(t, rule) - - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(protoServeErrCh)) -} - -func TestRouterRouteExpiration(t *testing.T) { - client := transport.NewDiscoveryMock() - logStore := transport.InMemoryTransportLogStore() - - pk, sk := cipher.GenerateKeyPair() - m, err := transport.NewManager(&transport.ManagerConfig{PubKey: pk, SecKey: sk, DiscoveryClient: client, LogStore: logStore}) - require.NoError(t, err) - - rt := routing.InMemoryRoutingTable() - _, err = rt.AddRule(routing.AppRule(time.Now().Add(-time.Hour), 4, pk, 6, 5)) - require.NoError(t, err) - assert.Equal(t, 1, rt.Count()) - - conf := &Config{ - Logger: logging.MustGetLogger("routesetup"), - PubKey: pk, - SecKey: sk, - TransportManager: m, - RoutingTable: rt, - } - r := New(conf) - r.expiryTicker = time.NewTicker(100 * time.Millisecond) - serveErrCh := make(chan error, 1) - go func() { - serveErrCh <- r.Serve(context.TODO()) - }() - - time.Sleep(110 * time.Millisecond) - - assert.Equal(t, 0, rt.Count()) - require.NoError(t, r.Close()) - - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) -} diff --git a/pkg/visor/taggedformatter.go b/pkg/visor/taggedformatter.go new file mode 100644 index 000000000..b1383cb48 --- /dev/null +++ b/pkg/visor/taggedformatter.go @@ -0,0 +1,57 @@ +package visor + +import ( + "bytes" + "os" + + "github.com/sirupsen/logrus" + "github.com/skycoin/skycoin/src/util/logging" +) + +// TaggedFormatter appends tag to log records and substitutes text +type TaggedFormatter struct { + tag []byte + subs []struct{ old, new []byte } + *logging.TextFormatter +} + +// Format executes formatting of TaggedFormatter +func (tf *TaggedFormatter) Format(entry *logrus.Entry) ([]byte, error) { + data, err := tf.TextFormatter.Format(entry) + for _, sub := range tf.subs { + data = bytes.ReplaceAll(data, sub.old, sub.new) + } + return append(tf.tag, data...), err +} + +// NewTaggedMasterLogger creates MasterLogger that prepends records with tag +func NewTaggedMasterLogger(tag string, ssubs []struct{ old, new string }) *logging.MasterLogger { + s2bsub := func(s struct{ old, new string }) struct{ old, new []byte } { + return struct{ old, new []byte }{[]byte(s.old), []byte(s.new)} + } + bsubs := make([]struct{ old, new []byte }, len(ssubs)) + for i := 0; i < len(ssubs); i++ { + bsubs[i] = s2bsub(ssubs[i]) + } + + hooks := make(logrus.LevelHooks) + return &logging.MasterLogger{ + Logger: &logrus.Logger{ + Out: os.Stdout, + Formatter: &TaggedFormatter{ + []byte(tag), + bsubs, + &logging.TextFormatter{ + FullTimestamp: true, + AlwaysQuoteStrings: true, + QuoteEmptyFields: true, + ForceFormatting: true, + DisableColors: false, + ForceColors: false, + }, + }, + Hooks: hooks, + Level: logrus.DebugLevel, + }, + } +} diff --git a/pkg/visor/visor_multihead_test.go b/pkg/visor/visor_multihead_test.go index d3d7ea513..1db8717fd 100644 --- a/pkg/visor/visor_multihead_test.go +++ b/pkg/visor/visor_multihead_test.go @@ -13,10 +13,6 @@ import ( "sync" "time" - "github.com/skycoin/skycoin/src/util/logging" - - "github.com/sirupsen/logrus" - "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skywire/pkg/routing" ) @@ -157,46 +153,18 @@ func Example_initCfgPool() { // len(mh.cfgPool): 16 } -type TaggedFormatter struct { - tag []byte - *logging.TextFormatter -} - -func (tf *TaggedFormatter) Format(entry *logrus.Entry) ([]byte, error) { - data, err := tf.TextFormatter.Format(entry) - return append(tf.tag, data...), err -} - -// NewTaggedMasterLogger creates MasterLogger that prepends records with tag -func NewTaggedMasterLogger(tag string) *logging.MasterLogger { - hooks := make(logrus.LevelHooks) - return &logging.MasterLogger{ - Logger: &logrus.Logger{ - Out: os.Stdout, - Formatter: &TaggedFormatter{ - []byte(tag), - &logging.TextFormatter{ - FullTimestamp: true, - AlwaysQuoteStrings: true, - QuoteEmptyFields: true, - ForceFormatting: true, - DisableColors: false, - ForceColors: false, - }, - }, - Hooks: hooks, - Level: logrus.DebugLevel, - }, - } -} - func (mh *MultiHead) initNodes() { mh.nodes = make([]*Node, len(mh.cfgPool)) mh.initErrs = make(chan error, len(mh.cfgPool)) + subs := []struct{ old, new string }{ + {"000000000000000000000000000000000000000000000000000000000000000000", "PubKey{}"}, + {"024195ae0d46eb0195c9ddabcaf62bc894316594ea2e92570f269238c5b5f817d1", "PK(skyhost_001)"}, + } + var err error for i := 0; i < len(mh.nodes); i++ { - logger := NewTaggedMasterLogger(fmt.Sprintf("[node_%03d]", i+1)) + logger := NewTaggedMasterLogger(fmt.Sprintf("[node_%03d]", i+1), subs) logger.Out = mh.Log mh.nodes[i], err = NewNode(&mh.cfgPool[i], logger) if err != nil { @@ -309,7 +277,7 @@ func (mh *MultiHead) sendMessage(sender, reciever uint, message string) (*http.R // WIP func ExampleMultiHead_sendMessage() { - mh := makeMultiHeadN(1) + mh := makeMultiHeadN(2) mh.startNodes(time.Second) _, err := mh.sendMessage(0, 0, "Hello") @@ -317,10 +285,11 @@ func ExampleMultiHead_sendMessage() { mh.stopNodes(time.Second * 3) fmt.Printf("%v\n", mh.errReport()) - // fmt.Printf("%v\n", mh.Log.records) + fmt.Printf("%v\n", mh.Log.records) // Output: err: // init errors: 0 // start errors: 0 // stop errors: 0 + } From 70b1779dc875cda46cde91daa747707a3f0c472a Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Sat, 17 Aug 2019 08:43:12 +0300 Subject: [PATCH 16/26] Changes in PacketRouter implementations: callbacks streamlined into regular calls. Debugging improvements --- internal/testhelpers/testhelpers.go | 5 +- pkg/app/payload.go | 56 ++++++++++++++ pkg/app/protocol.go | 4 +- pkg/router/app_manager.go | 18 ++--- pkg/router/app_manager_test.go | 64 ++++++++-------- pkg/router/mockrouter.go | 97 +++++++++++++++++++++++ pkg/router/router.go | 115 +++++++++++++++------------- pkg/router/router_close_test.go | 2 - pkg/router/router_setup_test.go | 36 --------- pkg/router/router_test.go | 62 +++++++++++---- pkg/visor/rpc_test.go | 7 +- pkg/visor/visor.go | 11 +-- pkg/visor/visor_multihead_test.go | 11 ++- pkg/visor/visor_test.go | 69 ++--------------- 14 files changed, 327 insertions(+), 230 deletions(-) create mode 100644 pkg/app/payload.go create mode 100644 pkg/router/mockrouter.go diff --git a/internal/testhelpers/testhelpers.go b/internal/testhelpers/testhelpers.go index fcc9b91f9..ebe67eaaa 100644 --- a/internal/testhelpers/testhelpers.go +++ b/internal/testhelpers/testhelpers.go @@ -13,7 +13,10 @@ func joinErrChannels(errChans []<-chan error) chan error { joinedCh := make(chan error) for _, ch := range errChans { go func(errCh <-chan error) { - joinedCh <- <-errCh + err := <-errCh + if err != nil { + joinedCh <- err + } }(ch) } return joinedCh diff --git a/pkg/app/payload.go b/pkg/app/payload.go new file mode 100644 index 000000000..cf9380093 --- /dev/null +++ b/pkg/app/payload.go @@ -0,0 +1,56 @@ +package app + +import ( + "encoding/json" + "fmt" + + "github.com/skycoin/skywire/pkg/routing" +) + +// Payload type encapsulates all possible payloads +type Payload struct { + Frame Frame + Data []byte +} + +func (p Payload) String() string { + switch p.Frame { + case FrameInit: + var config Config + if err := json.Unmarshal(p.Data, &config); err != nil { + return fmt.Sprintf("{FrameInit. Error: %v, Data: %v}", err, p.Data) + } + return fmt.Sprintf("{FrameInit: %v}", config) + case FrameConfirmLoop: + var addrs [2]routing.Addr + if err := json.Unmarshal(p.Data, &addrs); err != nil { + return fmt.Sprintf("{FrameConfirmLoop. Error: %v, Data: %v}", err, p.Data) + } + return fmt.Sprintf("{FrameConfirmLoop. %v}", addrs) + case FrameSend: + packet := &Packet{} + if err := json.Unmarshal(p.Data, packet); err != nil { + return fmt.Sprintf("{FrameSend. Error: %v, data: %v}", err, p.Data) + } + return fmt.Sprintf("{FrameSend: %v}", packet) + case FrameClose: + var loop routing.Loop + if err := json.Unmarshal(p.Data, &loop); err != nil { + return fmt.Sprintf("{FrameClose. Error: %v, data: %v}", err, p.Data) + } + return fmt.Sprintf("{FrameClose: %v}", p.Data) + case FrameFailure: + return fmt.Sprintf("{FrameFailure: %s}", string(p.Data)) + case FrameCreateLoop: + var raddr routing.Addr + if err := json.Unmarshal(p.Data, &raddr); err != nil { + return fmt.Sprintf("{FrameCreateLoop: Error: %v. Data: %v}", err, p.Data) + } + return fmt.Sprintf("{FrameCreateLoop: %s}", raddr) + case FrameSuccess: + return "{FrameSuccess}" + default: + return fmt.Sprintf("{Frame: %d. Data: %v}", p.Frame, p.Data) + } + +} diff --git a/pkg/app/protocol.go b/pkg/app/protocol.go index bcc35cda6..b37adc96e 100644 --- a/pkg/app/protocol.go +++ b/pkg/app/protocol.go @@ -70,7 +70,6 @@ func NewProtocol(rw io.ReadWriteCloser) *Protocol { // Send sends command Frame with payload and awaits for response. func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { - Logger.WithField("frame", cmd).Info("Sending from Protocol.Send") id, resChan := p.chans.add() if err := p.writeFrame(cmd, id, payload); err != nil { return err @@ -150,7 +149,7 @@ func (p *Protocol) Close() error { } func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err error) { - Logger.WithField("tcp-transport", "debug").WithField("payload", payload).Info("writeFrame") + var data []byte if err, ok := payload.(error); ok { data = []byte(err.Error()) @@ -160,6 +159,7 @@ func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err er return err } } + Logger.WithField("payload", Payload{frame, data}).Info("Protocol.writeFrame") packet := append([]byte{byte(frame), id}, data...) buf := make([]byte, 2) diff --git a/pkg/router/app_manager.go b/pkg/router/app_manager.go index 725369350..7e45a4686 100644 --- a/pkg/router/app_manager.go +++ b/pkg/router/app_manager.go @@ -19,16 +19,16 @@ type appCallbacks struct { } type appManager struct { - Logger *logging.Logger - - proto *app.Protocol - appConf *app.Config - callbacks *appCallbacks + Logger *logging.Logger + router PacketRouter + proto *app.Protocol + appConf *app.Config + // callbacks *appCallbacks } func (am *appManager) Serve() error { return am.proto.Serve(func(frame app.Frame, payload []byte) (res interface{}, err error) { - am.Logger.WithField("frame", frame).WithField("payload", payload).Infof("Got new request in appManager.Serve") + am.Logger.WithField("payload", app.Payload{frame, payload}).Infof("Got new request in appManager.Serve") switch frame { case app.FrameInit: @@ -79,7 +79,7 @@ func (am *appManager) setupLoop(payload []byte) (routing.Addr, error) { return routing.Addr{}, err } - return am.callbacks.CreateLoop(am.proto, raddr) + return am.router.CreateLoop(am.proto, raddr) } func (am *appManager) handleCloseLoop(payload []byte) error { @@ -88,7 +88,7 @@ func (am *appManager) handleCloseLoop(payload []byte) error { return err } - return am.callbacks.CloseLoop(am.proto, loop) + return am.router.CloseLoop(am.proto, loop) } func (am *appManager) forwardAppPacket(payload []byte) error { @@ -98,5 +98,5 @@ func (am *appManager) forwardAppPacket(payload []byte) error { } am.Logger.WithField("packet", packet).Info("appMananger.forwardAppPacket") - return am.callbacks.ForwardAppPacket(am.proto, packet) + return am.router.ForwardAppPacket(am.proto, packet) } diff --git a/pkg/router/app_manager_test.go b/pkg/router/app_manager_test.go index 8b4a3b653..122b7955f 100644 --- a/pkg/router/app_manager_test.go +++ b/pkg/router/app_manager_test.go @@ -18,9 +18,9 @@ func TestAppManagerInit(t *testing.T) { in, out := net.Pipe() am := &appManager{ logging.MustGetLogger("routesetup"), + nil, app.NewProtocol(out), &app.Config{AppName: "foo", AppVersion: "0.0.1"}, - nil, } srvCh := make(chan error) @@ -62,14 +62,10 @@ func TestAppManagerInit(t *testing.T) { func TestAppManagerSetupLoop(t *testing.T) { in, out := net.Pipe() am := &appManager{ - logging.MustGetLogger("routesetup"), - app.NewProtocol(out), - &app.Config{AppName: "foo", AppVersion: "0.0.1"}, - &appCallbacks{ - CreateLoop: func(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) { - return raddr, nil - }, - }, + Logger: logging.MustGetLogger("routesetup"), + router: new(MockRouter), + proto: app.NewProtocol(out), + appConf: &app.Config{AppName: "foo", AppVersion: "0.0.1"}, } srvCh := make(chan error) @@ -96,17 +92,19 @@ func TestAppManagerSetupLoop(t *testing.T) { func TestAppManagerCloseLoop(t *testing.T) { in, out := net.Pipe() - var inLoop routing.Loop + // var inLoop routing.Loop + r := new(MockRouter) am := &appManager{ - logging.MustGetLogger("routesetup"), - app.NewProtocol(out), - &app.Config{AppName: "foo", AppVersion: "0.0.1"}, - &appCallbacks{ - CloseLoop: func(conn *app.Protocol, loop routing.Loop) error { - inLoop = loop - return nil - }, - }, + Logger: logging.MustGetLogger("routesetup"), + router: r, + proto: app.NewProtocol(out), + appConf: &app.Config{AppName: "foo", AppVersion: "0.0.1"}, + // &appCallbacks{ + // CloseLoop: func(conn *app.Protocol, loop routing.Loop) error { + // inLoop = loop + // return nil + // }, + // }, } srvCh := make(chan error) @@ -120,10 +118,12 @@ func TestAppManagerCloseLoop(t *testing.T) { lpk, _ := cipher.GenerateKeyPair() rpk, _ := cipher.GenerateKeyPair() - loop := routing.Loop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}} + loop := routing.Loop{ + Local: routing.Addr{PubKey: lpk, Port: 2}, + Remote: routing.Addr{PubKey: rpk, Port: 3}} err := proto.Send(app.FrameClose, loop, nil) require.NoError(t, err) - assert.Equal(t, loop, inLoop) + assert.Equal(t, loop, r.inLoop) require.NoError(t, in.Close()) require.NoError(t, <-srvCh) @@ -133,17 +133,12 @@ func TestAppManagerCloseLoop(t *testing.T) { func TestAppManagerForward(t *testing.T) { in, out := net.Pipe() - var inPacket *app.Packet + r := new(MockRouter) am := &appManager{ - logging.MustGetLogger("routesetup"), - app.NewProtocol(out), - &app.Config{AppName: "foo", AppVersion: "0.0.1"}, - &appCallbacks{ - ForwardAppPacket: func(conn *app.Protocol, packet *app.Packet) error { - inPacket = packet - return nil - }, - }, + Logger: logging.MustGetLogger("routesetup"), + router: r, + proto: app.NewProtocol(out), + appConf: &app.Config{AppName: "foo", AppVersion: "0.0.1"}, } srvCh := make(chan error) @@ -157,10 +152,13 @@ func TestAppManagerForward(t *testing.T) { lpk, _ := cipher.GenerateKeyPair() rpk, _ := cipher.GenerateKeyPair() - packet := &app.Packet{Payload: []byte("foo"), Loop: routing.Loop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}} + packet := &app.Packet{Payload: []byte("foo"), + Loop: routing.Loop{ + Local: routing.Addr{PubKey: lpk, Port: 2}, + Remote: routing.Addr{PubKey: rpk, Port: 3}}} err := proto.Send(app.FrameSend, packet, nil) require.NoError(t, err) - assert.Equal(t, packet, inPacket) + assert.Equal(t, packet, r.inPacket) require.NoError(t, in.Close()) require.NoError(t, <-srvCh) diff --git a/pkg/router/mockrouter.go b/pkg/router/mockrouter.go new file mode 100644 index 000000000..d68698276 --- /dev/null +++ b/pkg/router/mockrouter.go @@ -0,0 +1,97 @@ +package router + +import ( + "context" + "net" + "sync" + + "github.com/skycoin/skywire/pkg/app" + "github.com/skycoin/skywire/pkg/routing" + "github.com/skycoin/skywire/pkg/transport" +) + +// MockRouter - test implementation of Router +type MockRouter struct { + sync.Mutex + + ports []routing.Port + + didStart bool + DidClose bool + + errChan chan error + + inPacket *app.Packet + inLoop routing.Loop +} + +var n *MockRouter + +// Ports implements PacketRouter.Ports +func (r *MockRouter) Ports() []routing.Port { + r.Lock() + p := r.ports + r.Unlock() + return p +} + +// Serve implements PacketRouter.Serve +func (r *MockRouter) Serve(_ context.Context) error { + r.didStart = true + return nil +} + +// ServeApp implements PacketRouter.ServeApp +func (r *MockRouter) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error { + r.Lock() + if r.ports == nil { + r.ports = []routing.Port{} + } + + r.ports = append(r.ports, port) + r.Unlock() + + if r.errChan == nil { + r.Lock() + r.errChan = make(chan error) + r.Unlock() + } + + return <-r.errChan +} + +// Close implements PacketRouter.Close +func (r *MockRouter) Close() error { + if r == nil { + return nil + } + r.DidClose = true + r.Lock() + if r.errChan != nil { + close(r.errChan) + } + r.Unlock() + return nil +} + +// IsSetupTransport implements PacketRouter.IsSetupTransport +func (r *MockRouter) IsSetupTransport(tr *transport.ManagedTransport) bool { + return false +} + +// CloseLoop implements PacketRouter.CloseLoop +func (r *MockRouter) CloseLoop(conn *app.Protocol, loop routing.Loop) error { + r.inLoop = loop + return nil +} + +// ForwardAppPacket implements PacketRouter.ForwardAppPacket +func (r *MockRouter) ForwardAppPacket(conn *app.Protocol, packet *app.Packet) error { + r.inPacket = packet + return nil +} + +// CreateLoop implements PacketRouter.CreateLoop +func (r *MockRouter) CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) { + return raddr, nil +} diff --git a/pkg/router/router.go b/pkg/router/router.go index fbb10d111..d8f541016 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -10,6 +10,7 @@ import ( "sync" "time" + "github.com/skycoin/dmsg" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" @@ -41,7 +42,18 @@ type Config struct { TransportType string } -// Router implements node.PacketRouter. It manages routing table by +// PacketRouter performs routing of the skywire packets. +type PacketRouter interface { + io.Closer + Serve(ctx context.Context) error + ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error + IsSetupTransport(tr *transport.ManagedTransport) bool + CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) + CloseLoop(conn *app.Protocol, loop routing.Loop) error + ForwardAppPacket(conn *app.Protocol, packet *app.Packet) error +} + +// Router implements PacketRouter. It manages routing table by // communicating with setup nodes, forward packets according to local // rules and manages loops for apps. type Router struct { @@ -154,12 +166,7 @@ func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) r.staticPorts[port] = struct{}{} r.mu.Unlock() - callbacks := &appCallbacks{ - CreateLoop: r.requestLoop, - CloseLoop: r.closeLoop, - ForwardAppPacket: r.forwardAppPacket, - } - am := &appManager{r.Logger, appProto, appConf, callbacks} + am := &appManager{r.Logger, r, appProto, appConf} err := am.Serve() for _, port := range r.pm.AppPorts(appProto) { @@ -180,6 +187,27 @@ func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) return err } +// callbacks := &appCallbacks{ +// CreateLoop: r.requestLoop, +// CloseLoop: r.closeLoop, +// ForwardAppPacket: r.forwardAppPacket, +// } + +// CreateLoop implements PactetRouter.CreateLoop +func (r *Router) CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) { + return r.requestLoop(conn, raddr) +} + +// CloseLoop implements PactetRouter.CloseLoop +func (r *Router) CloseLoop(conn *app.Protocol, loop routing.Loop) error { + return r.closeLoop(conn, loop) +} + +// ForwardAppPacket implements PactetRouter.ForwardAppPacket +func (r *Router) ForwardAppPacket(conn *app.Protocol, packet *app.Packet) error { + return r.forwardAppPacket(conn, packet) +} + // Close safely stops Router. func (r *Router) Close() error { if r == nil { @@ -217,13 +245,14 @@ func (r *Router) serveTransport(rw io.ReadWriter) error { r.Logger.Infof("Got new remote packet with route ID %d. Using rule: %s", packet.RouteID(), rule) if rule.Type() == routing.RuleForward { - return r.forwardPacket(payload, rule) + return r.ForwardPacket(payload, rule) } return r.consumePacket(payload, rule) } -func (r *Router) forwardPacket(payload []byte, rule routing.Rule) error { +// ForwardPacket forwards payload according to rule +func (r *Router) ForwardPacket(payload []byte, rule routing.Rule) error { packet := routing.MakePacket(rule.RouteID(), payload) tr := r.tm.Transport(rule.TransportID()) if tr == nil { @@ -256,26 +285,23 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { } func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) error { - - r.Logger.WithField("packet.Loop", packet.Loop).Info("Entering r.forwardAppPacket") - + if r == nil { + return nil + } if packet.Loop.Remote.PubKey == r.config.PubKey { return r.forwardLocalAppPacket(packet) } - r.Logger.Info("Entering r.forwardAppPacket GetLoop") + l, err := r.pm.GetLoop(packet.Loop.Local.Port, packet.Loop.Remote) if err != nil { return err } - r.Logger.WithField("trID", l.trID).Infof("Entering r.forwardAppPacket r.tm.Transport(l.trID)") tr := r.tm.Transport(l.trID) if tr == nil { - return fmt.Errorf("unknown transport id %v", l.trID) + return errors.New("unknown transport") } - r.Logger.Info("r.forwardAppPacket enter routing.MakePacket") - p := routing.MakePacket(l.routeID, packet.Payload) r.Logger.Infof("Forwarded App packet from LocalPort %d using route ID %d", packet.Loop.Local.Port, l.routeID) _, err = tr.Write(p) @@ -283,26 +309,25 @@ func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) err } func (r *Router) forwardLocalAppPacket(packet *app.Packet) error { - r.Logger.Info("entering r.forwardLocalAppPacket ") b, err := r.pm.Get(packet.Loop.Remote.Port) if err != nil { return nil } - // r.Logger.WithField("Local", ).Info("entering r.forwardLocalAppPacket app.Packet") + p := &app.Packet{ Loop: routing.Loop{ - Local: routing.Addr{PubKey: packet.Loop.Remote.PubKey, Port: packet.Loop.Remote.Port}, + Local: routing.Addr{Port: packet.Loop.Remote.Port}, Remote: routing.Addr{PubKey: packet.Loop.Remote.PubKey, Port: packet.Loop.Local.Port}, }, Payload: packet.Payload, } - - r.Logger.WithField("packet", p).Info("entering r.forwardLocalAppPacket Send") return b.conn.Send(app.FrameSend, p, nil) } func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing.Addr, error) { - + if r == nil { + return raddr, nil + } lport := r.pm.Alloc(appConn) if err := r.pm.SetLoop(lport, raddr, &loop{}); err != nil { return routing.Addr{}, err @@ -332,33 +357,18 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing Reverse: reverseRoute, } - r.Logger.Infof("Router.requestLoop\n") - r.Logger.Infof("laddr: %v\n, raddr: %v\n", laddr, raddr) - - r.Logger.Info("Attempt to r.setupProto from r.requestLoop") - switch r.config.TransportType { - case "dmsg": - proto, tr, err := r.setupProto(context.Background()) - if err != nil { - return routing.Addr{}, err - } - defer func() { - if err := tr.Close(); err != nil { - r.Logger.Warnf("Failed to close transport: %s", err) - } - }() - - r.Logger.Infof("Router.requestLoop 6\n") - if err := setup.CreateLoop(proto, ld); err != nil { - return routing.Addr{}, fmt.Errorf("route setup: %s", err) - } - case "tcp-transport": - r.Logger.Info("Skipping setup for tcp-transport") - _, err := r.tm.CreateSetupTransport(context.Background(), raddr.PubKey, "tcp-transport") - if err != nil { - r.Logger.Warnf("error creating transport %s", err) + proto, tr, err := r.setupProto(context.Background()) + if err != nil { + return routing.Addr{}, err + } + defer func() { + if err := tr.Close(); err != nil { + r.Logger.Warnf("Failed to close transport: %s", err) } + }() + if err := setup.CreateLoop(proto, ld); err != nil { + return routing.Addr{}, fmt.Errorf("route setup: %s", err) } r.Logger.Infof("Created new loop to %s on port %d", raddr, laddr.Port) @@ -401,7 +411,7 @@ func (r *Router) closeLoop(appConn *app.Protocol, loop routing.Loop) error { if err := r.destroyLoop(loop); err != nil { r.Logger.Warnf("Failed to remove loop: %s", err) } - r.Logger.Info("Attempt to r.setupProto from r.closeLoop") + proto, tr, err := r.setupProto(context.Background()) if err != nil { return err @@ -440,6 +450,9 @@ func (r *Router) loopClosed(loop routing.Loop) error { } func (r *Router) destroyLoop(loop routing.Loop) error { + if r == nil { + return nil + } r.mu.Lock() _, ok := r.staticPorts[loop.Local.Port] r.mu.Unlock() @@ -460,9 +473,7 @@ func (r *Router) setupProto(ctx context.Context) (*setup.Protocol, transport.Tra return nil, nil, errors.New("route setup: no nodes") } - trType := r.config.TransportType - // TODO(evanlinjin): need string constant for tp type. - tr, err := r.tm.CreateSetupTransport(ctx, r.config.SetupNodes[0], trType) + tr, err := r.tm.CreateSetupTransport(ctx, r.config.SetupNodes[0], dmsg.Type) if err != nil { return nil, nil, fmt.Errorf("setup transport: %s", err) } diff --git a/pkg/router/router_close_test.go b/pkg/router/router_close_test.go index c51dcad72..0710f6e6c 100644 --- a/pkg/router/router_close_test.go +++ b/pkg/router/router_close_test.go @@ -103,7 +103,6 @@ func TestRouterCloseLoop(t *testing.T) { raddr := routing.Addr{PubKey: pk3, Port: 6} require.NoError(t, r.pm.SetLoop(5, raddr, &loop{})) - require.NoError(t, proto.Send(app.FrameClose, routing.Loop{Local: routing.Addr{Port: 5}, Remote: raddr}, nil)) time.Sleep(100 * time.Millisecond) @@ -207,7 +206,6 @@ func TestRouterCloseLoopOnAppClose(t *testing.T) { raddr := routing.Addr{PubKey: pk3, Port: 6} require.NoError(t, r.pm.SetLoop(5, raddr, &loop{})) - require.NoError(t, rw.Close()) time.Sleep(100 * time.Millisecond) diff --git a/pkg/router/router_setup_test.go b/pkg/router/router_setup_test.go index d61aa52e0..033a669cf 100644 --- a/pkg/router/router_setup_test.go +++ b/pkg/router/router_setup_test.go @@ -374,39 +374,3 @@ func TestRouterSetupLoopLocal(t *testing.T) { } -func TestRouterRouteExpiration(t *testing.T) { - client := transport.NewDiscoveryMock() - logStore := transport.InMemoryTransportLogStore() - - pk, sk := cipher.GenerateKeyPair() - m, err := transport.NewManager( - &transport.ManagerConfig{PubKey: pk, SecKey: sk, - DiscoveryClient: client, LogStore: logStore, Logger: log}) - require.NoError(t, err) - - rt := routing.InMemoryRoutingTable() - _, err = rt.AddRule(routing.AppRule(time.Now().Add(-time.Hour), 4, pk, 6, 5)) - require.NoError(t, err) - assert.Equal(t, 1, rt.Count()) - - conf := &Config{ - Logger: logging.MustGetLogger("routesetup"), - PubKey: pk, - SecKey: sk, - TransportManager: m, - RoutingTable: rt, - } - r := New(conf) - r.expiryTicker = time.NewTicker(100 * time.Millisecond) - serveErrCh := make(chan error, 1) - go func() { - serveErrCh <- r.Serve(context.TODO()) - }() - - time.Sleep(110 * time.Millisecond) - - assert.Equal(t, 0, rt.Count()) - require.NoError(t, r.Close()) - - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) -} diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index a5c555947..cb96e1612 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -113,7 +113,12 @@ func TestRouterAppInit(t *testing.T) { logStore := transport.InMemoryTransportLogStore() pk1, sk1 := cipher.GenerateKeyPair() - c1 := &transport.ManagerConfig{PubKey: pk1, SecKey: sk1, DiscoveryClient: client, LogStore: logStore, Logger: log} + c1 := &transport.ManagerConfig{ + PubKey: pk1, + SecKey: sk1, + DiscoveryClient: client, + LogStore: logStore, + Logger: log} m1, err := transport.NewManager(c1) require.NoError(t, err) @@ -141,9 +146,8 @@ func TestRouterAppInit(t *testing.T) { require.Error(t, proto.Send(app.FrameInit, &app.Config{AppName: "foo1", AppVersion: "0.0.1", ProtocolVersion: "0.0.1"}, nil)) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) require.NoError(t, r.Close()) - require.NoError(t, <-errCh) + require.NoError(t, testhelpers.NoErrorWithinTimeoutN(serveErrCh, errCh)) } func TestRouterApp(t *testing.T) { @@ -240,13 +244,9 @@ func TestRouterApp(t *testing.T) { require.NoError(t, m2.Close()) - // require.NoError(t, - // testhelpers.NoErrorWithinTimeoutN(trServeErrCh, protoServeErrCh, sendErrCh, serveAppErrCh)) + require.NoError(t, + testhelpers.NoErrorWithinTimeoutN(trServeErrCh, protoServeErrCh, sendErrCh, serveAppErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(trServeErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(protoServeErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(sendErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErrCh)) } func TestRouterLocalApp(t *testing.T) { @@ -316,11 +316,43 @@ func TestRouterLocalApp(t *testing.T) { assert.Equal(t, []byte("foo"), packet.Payload) require.NoError(t, r.Close()) - require.NoError(t, <-errCh) + require.NoError(t, + testhelpers.NoErrorWithinTimeoutN(errCh, protoServeErr1Ch, protoServeErr2Ch, sendErrCh, serveAppErr1Ch, serveAppErr2Ch)) +} + +func TestRouterRouteExpiration(t *testing.T) { + client := transport.NewDiscoveryMock() + logStore := transport.InMemoryTransportLogStore() + + pk, sk := cipher.GenerateKeyPair() + m, err := transport.NewManager( + &transport.ManagerConfig{PubKey: pk, SecKey: sk, + DiscoveryClient: client, LogStore: logStore, Logger: log}) + require.NoError(t, err) + + rt := routing.InMemoryRoutingTable() + _, err = rt.AddRule(routing.AppRule(time.Now().Add(-time.Hour), 4, pk, 6, 5)) + require.NoError(t, err) + assert.Equal(t, 1, rt.Count()) + + conf := &Config{ + Logger: logging.MustGetLogger("routesetup"), + PubKey: pk, + SecKey: sk, + TransportManager: m, + RoutingTable: rt, + } + r := New(conf) + r.expiryTicker = time.NewTicker(100 * time.Millisecond) + serveErrCh := make(chan error, 1) + go func() { + serveErrCh <- r.Serve(context.TODO()) + }() + + time.Sleep(110 * time.Millisecond) - require.NoError(t, testhelpers.NoErrorWithinTimeout(protoServeErr1Ch)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(protoServeErr2Ch)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(sendErrCh)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErr1Ch)) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveAppErr2Ch)) + assert.Equal(t, 0, rt.Count()) + require.NoError(t, r.Close()) + + require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) } diff --git a/pkg/visor/rpc_test.go b/pkg/visor/rpc_test.go index c7503dbfd..3e167b995 100644 --- a/pkg/visor/rpc_test.go +++ b/pkg/visor/rpc_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/skycoin/skywire/pkg/router" "github.com/skycoin/skywire/pkg/routing" "github.com/skycoin/skywire/pkg/transport" "github.com/skycoin/skywire/pkg/util/pathutil" @@ -48,9 +49,11 @@ func TestListApps(t *testing.T) { assert.Equal(t, AppStatusRunning, app2.Status) } +var rtr router.Router + func TestStartStopApp(t *testing.T) { pk, _ := cipher.GenerateKeyPair() - router := new(mockRouter) + router := new(router.MockRouter) executer := new(MockExecuter) defer func() { require.NoError(t, os.RemoveAll("skychat")) @@ -97,7 +100,7 @@ func TestStartStopApp(t *testing.T) { } func TestRPC(t *testing.T) { - r := new(mockRouter) + r := new(router.MockRouter) executer := new(MockExecuter) defer func() { require.NoError(t, os.RemoveAll("skychat")) diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 78e1c3beb..ac870872b 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -6,7 +6,6 @@ import ( "context" "errors" "fmt" - "io" "net" "net/rpc" "os" @@ -86,19 +85,11 @@ type appBind struct { pid int } -// PacketRouter performs routing of the skywire packets. -type PacketRouter interface { - io.Closer - Serve(ctx context.Context) error - ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error - IsSetupTransport(tr *transport.ManagedTransport) bool -} - // Node provides messaging runtime for Apps by setting up all // necessary connections and performing messaging gateway functions. type Node struct { config *Config - router PacketRouter + router router.PacketRouter messenger transport.Factory tm *transport.Manager rt routing.Table diff --git a/pkg/visor/visor_multihead_test.go b/pkg/visor/visor_multihead_test.go index 1db8717fd..319de4530 100644 --- a/pkg/visor/visor_multihead_test.go +++ b/pkg/visor/visor_multihead_test.go @@ -158,8 +158,9 @@ func (mh *MultiHead) initNodes() { mh.initErrs = make(chan error, len(mh.cfgPool)) subs := []struct{ old, new string }{ - {"000000000000000000000000000000000000000000000000000000000000000000", "PubKey{}"}, + {"000000000000000000000000000000000000000000000000000000000000000000", "PK{NULL}"}, {"024195ae0d46eb0195c9ddabcaf62bc894316594ea2e92570f269238c5b5f817d1", "PK(skyhost_001)"}, + {"037dfc42ae5ca494f5646d09e6532ea39bea7954a915409925277f4604a34968b3", "PK(skyhost_002)"}, } var err error @@ -258,7 +259,7 @@ func ExampleMultiHead_startNodes() { mh.stopNodes(delay) fmt.Printf("%v\n", mh.errReport()) - fmt.Printf("records: %v, %v\n", len(mh.Log.records), len(mh.Log.records) >= int(n*12)) + // Output: init errors: 0 // start errors: 0 // stop errors: 0 @@ -280,12 +281,14 @@ func ExampleMultiHead_sendMessage() { mh := makeMultiHeadN(2) mh.startNodes(time.Second) - _, err := mh.sendMessage(0, 0, "Hello") + _, err := mh.sendMessage(0, 1, "Hello") fmt.Printf("err: %v", err) mh.stopNodes(time.Second * 3) fmt.Printf("%v\n", mh.errReport()) - fmt.Printf("%v\n", mh.Log.records) + // printLogger := logging.MustGetLogger("test") + // printLogger.Infof("%v\n", mh.Log.records) + // fmt.Printf("%v\n", mh.Log.records) // Output: err: // init errors: 0 diff --git a/pkg/visor/visor_test.go b/pkg/visor/visor_test.go index de445e61b..3fe4b8d73 100644 --- a/pkg/visor/visor_test.go +++ b/pkg/visor/visor_test.go @@ -1,7 +1,6 @@ package visor import ( - "context" "encoding/json" "errors" "io/ioutil" @@ -21,7 +20,7 @@ import ( "github.com/stretchr/testify/require" "github.com/skycoin/skywire/internal/httpauth" - "github.com/skycoin/skywire/pkg/app" + "github.com/skycoin/skywire/pkg/router" "github.com/skycoin/skywire/pkg/routing" "github.com/skycoin/skywire/pkg/transport" "github.com/skycoin/skywire/pkg/transport/dmsg" @@ -84,7 +83,7 @@ func TestNodeStartClose(t *testing.T) { for _, trType := range []string{"dmsg", "tcp-transport"} { t.Run(trType, func(t *testing.T) { - r := new(mockRouter) + r := new(router.MockRouter) executer := &MockExecuter{} conf := []AppConfig{ {App: "skychat", Version: "1.0", AutoStart: true, Port: 1}, @@ -121,7 +120,7 @@ func TestNodeStartClose(t *testing.T) { time.Sleep(100 * time.Millisecond) require.NoError(t, node.Close()) - require.True(t, r.didClose) + require.True(t, r.DidClose) require.NoError(t, <-errCh) require.Len(t, executer.cmds, 1) @@ -133,7 +132,7 @@ func TestNodeStartClose(t *testing.T) { func TestNodeSpawnApp(t *testing.T) { pk, _ := cipher.GenerateKeyPair() - r := new(mockRouter) + r := new(router.MockRouter) executer := &MockExecuter{} defer func() { require.NoError(t, os.RemoveAll("skychat")) @@ -168,7 +167,7 @@ func TestNodeSpawnApp(t *testing.T) { func TestNodeSpawnAppValidations(t *testing.T) { conn, _ := net.Pipe() - r := new(mockRouter) + r := new(router.MockRouter) executer := &MockExecuter{err: errors.New("foo")} defer func() { require.NoError(t, os.RemoveAll("skychat")) @@ -249,61 +248,3 @@ func (exc *MockExecuter) Wait(cmd *exec.Cmd) error { <-exc.stopCh return nil } - -type mockRouter struct { - sync.Mutex - - ports []routing.Port - - didStart bool - didClose bool - - errChan chan error -} - -func (r *mockRouter) Ports() []routing.Port { - r.Lock() - p := r.ports - r.Unlock() - return p -} - -func (r *mockRouter) Serve(_ context.Context) error { - r.didStart = true - return nil -} - -func (r *mockRouter) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error { - r.Lock() - if r.ports == nil { - r.ports = []routing.Port{} - } - - r.ports = append(r.ports, port) - r.Unlock() - - if r.errChan == nil { - r.Lock() - r.errChan = make(chan error) - r.Unlock() - } - - return <-r.errChan -} - -func (r *mockRouter) Close() error { - if r == nil { - return nil - } - r.didClose = true - r.Lock() - if r.errChan != nil { - close(r.errChan) - } - r.Unlock() - return nil -} - -func (r *mockRouter) IsSetupTransport(tr *transport.ManagedTransport) bool { - return false -} From 4a951c6ca3bda7c190624c753edb4db06373ff97 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Sat, 17 Aug 2019 14:50:36 +0300 Subject: [PATCH 17/26] a lot of debugging logs --- integration/tcp-tr/hosts.pubkeys | 131 +++++++++++++++++- pkg/app/payload.go | 4 +- pkg/app/protocol.go | 10 +- pkg/router/app_manager.go | 18 +-- pkg/router/router.go | 44 ++++-- pkg/transport/manager.go | 5 +- pkg/transport/tcp_transport.go | 26 +++- pkg/transport/tcp_transport_test.go | 11 +- ...or_multihead_test.go => multihead_test.go} | 54 +++++++- pkg/visor/visor.go | 8 +- pkg/visor/visor_test.go | 6 +- 11 files changed, 276 insertions(+), 41 deletions(-) rename pkg/visor/{visor_multihead_test.go => multihead_test.go} (84%) diff --git a/integration/tcp-tr/hosts.pubkeys b/integration/tcp-tr/hosts.pubkeys index f273d92e8..bc9205cbd 100644 --- a/integration/tcp-tr/hosts.pubkeys +++ b/integration/tcp-tr/hosts.pubkeys @@ -1,3 +1,128 @@ -02fffa3ffd07630bf5565493a990f182ea7de56a9c14abbe041959a8e4667d3447 192.168.1.2:9119 -0315852b6dba67d16c5376b40e082a1036bd17b66c67573b242625f485f605756e 192.168.1.3:9119 - +024195ae0d46eb0195c9ddabcaf62bc894316594ea2e92570f269238c5b5f817d1 skyhost_001:9119 +037dfc42ae5ca494f5646d09e6532ea39bea7954a915409925277f4604a34968b3 skyhost_002:9119 +0256f26f187b42c10c3a4b93453fd25add22dcfe07d4908a9ae4e7f0daf3b01086 skyhost_003:9119 +028941a0da17be26331db993584b83b83eb532b9486fd4cc6844a5fe4c79aa588d skyhost_004:9119 +03b4d7d5536b2310c369d6ce733221ab50f23a66b38ccc0f21557237cd93a86b41 skyhost_005:9119 +029524e7499335844e7f4cd238376c33562fce73ee549c4e7b1803e6a9324863f9 skyhost_006:9119 +03373c96227afbb25fcf8d8e5e057ed63e38b976abcd579611b38dc370cf9e23dd skyhost_007:9119 +032257488128d0bbe134fd4bc51d23189e9633fc9e3f31aaf366051b55c2024609 skyhost_008:9119 +0314bb3c470d207e1293535155040f48d25659c632c784185a0bb32550e9d80a52 skyhost_009:9119 +03891f6a2d8e9f23926d1ef0381285a411cbb7c1e62001ec08568a3dc034c50c34 skyhost_010:9119 +031b44a9d314abd95b83a9642b37dc8579cc7a4f95cf313091aa43986d6b38658c skyhost_011:9119 +0379194d7d7b94ce17219d2dc40a98cc2303e53148b15889acf83094f20afd75b3 skyhost_012:9119 +020397f54e134275aa3a7213857d43e6232a743ba48b021c07a7414654f974a20e skyhost_013:9119 +02619da7ef4bd2b3bc37960409b48769e61c4dbaaaf2a2ed1bb7980e321a10ac13 skyhost_014:9119 +0227f786985528ed4781d23d6fad50ca2f24f3b44581c58a38186a49d9a89fb4b3 skyhost_015:9119 +03ffc5618426cfa3e354dcc744e9128ac3aa69e56b9fef5e53aed6a4ddc85438ce skyhost_016:9119 +03989f5e40e44908185b4b58149f1bfdcb9ab2ef69c42a9f2305c20ea720aca6d6 skyhost_017:9119 +0239ca4ba0974c7855fe12e3d5c8e14336c927f8182fcfd1fd75405eb50de2ff7a skyhost_018:9119 +038f5afd9e68a75fca81e4eb14da081caf222816720cf3d456a7ea1fb2fbed9f65 skyhost_019:9119 +02a91ce458d2eae811b9925b7777c8511b136845a844577f10ae2b369f66107970 skyhost_020:9119 +0276e24892d65da1eb06b916da87e49eba27c90c4163040ceccae70749ff0a5b55 skyhost_021:9119 +0374f01d37952afbd3453e4b19cb99e360878e153e7c33cb10c98fd7c7079e7b0e skyhost_022:9119 +038e5ded2ef960ba405c62c306b1fa2d54802e6a213eb3bf822359997a9ac6f1c2 skyhost_023:9119 +02363f36a734aaafe7b0efebbbea7ac0bc3fa6231ba080cf97cb75399d511a3191 skyhost_024:9119 +020e38059b2c20de8487c1454cc66b2bbdc5af28c5776f9d58b2f74c936400f502 skyhost_025:9119 +03a9ae02bd27d9d21e5ad298ee5495fad68e94249882eb647fb703cbccd6853a0e skyhost_026:9119 +035a8f8cac27afa6299648d4e357b1a176f1d7e0a6af6847703360b8be300e21cf skyhost_027:9119 +024b516849bcacf045ab42344e355c07e4706d59d891682235e95fecc604a667c4 skyhost_028:9119 +02f6e9918cabe989e6b58436695370da50e0de56b9387e995b7880270aef9ba449 skyhost_029:9119 +039c3fa89c11e3382e66fe7906d25211f683e2fada29b190206656d29c972e748f skyhost_030:9119 +031c6b929639c5e12772539ae5d2a2b22603424ece435afaa50d9ef2fa112ac2a2 skyhost_031:9119 +0200e83d2655a38eef538a6171e5036e1b8f58da297047a81126c6a7915d836acb skyhost_032:9119 +031509ef53b1224ba5af2438d89049fe4869daf9d5e4e05eaab12c3320fbde6108 skyhost_033:9119 +035283050a5fd29dba173028a869f84ce843e97a9a6829851787486ee338c7ed7d skyhost_034:9119 +02701d64f1f0da77154510630a9403a97f036390e82cc807c2ef28820ce1982eee skyhost_035:9119 +0263a63a291442f0ca6caf0392daec59f6feeb3c96485a32e2874740b79536ca15 skyhost_036:9119 +02d6717b3ae11b7fd18f94a35018f682d020214ba48cedf5fd78d2204f0e90a913 skyhost_037:9119 +023b5024c8a3ef31efaecd446a98c54873fee41718f92c4228c51fe98402be8245 skyhost_038:9119 +03d3f5f2b745a3475cae14c9941b787def0e0a419091b012b08d6d6b535dca9ea3 skyhost_039:9119 +033de8084253e14c0e981cd23a6cf7dbe864f5c0cbadefa82146b3fe7b9b74f530 skyhost_040:9119 +0377c9c1f0bd94c02caea0dba1c3724daa8db31e0013b44c4922c5d922ceb880c2 skyhost_041:9119 +03755050134b5374bdac64551d139e008a2e574efec5a51eae0acda18c12ca80aa skyhost_042:9119 +036a27032f413fbc3564c064ed5d2a740c8bfb7ceb46f83820212f76ea68d92790 skyhost_043:9119 +02ba617ae92281f587836f36c13176dbea93bdbb8e100463d8606155fc3709fa49 skyhost_044:9119 +031d4719f4d5190fdee7b5daf92506cfc0391e2349ebdfaec6eb11c115672fbcee skyhost_045:9119 +03700dd365d20b5172b2c8884f3756ba2ffcab2447495a408aa672873ac436aa25 skyhost_046:9119 +034ff0e79d675b85cf0e7c7a53c46df3e86542d2102ad6561d88d5d04e924d010f skyhost_047:9119 +036f9b581ea0548d22126355338b8f469d73510f6099ff4ad4f60da947f63d39ed skyhost_048:9119 +022e1b56b7c6efc7e41390cefe585f37bf7f780de39ce9e15f40dd0ea1c3c4799a skyhost_049:9119 +022788a9dd824ff8beea80b35243c4507d02d5ea74eb3a93655746d820365fd581 skyhost_050:9119 +02c3a80bd7ded6c65e54332edf67f5c22f1f8536f207c4c3188656cb147814cd2d skyhost_051:9119 +027a09e20c425345286b04b0c2708dc206dce456d1031aa5573794c229e9973bf5 skyhost_052:9119 +029339bbc8719cd540b7f159ec1eda6921bcf91b78b3d9f8b1058dc7e44eab1ab8 skyhost_053:9119 +03ab1868f2a54ce41f689e81415ad51353395601d0af493298518015d7802396ca skyhost_054:9119 +02e5f274c4ceb87571bb84235708b5f879df5611c6a571e0f7107e50538f9774cc skyhost_055:9119 +024f1994a8e705a88ebfdf7dfeab6688806cdc7b3df34f1d67f69f63eec46df3b9 skyhost_056:9119 +030d50e27c94e5adde1e9427ed60417667f1dc228dbfcc70a0b9430d23a84afd25 skyhost_057:9119 +03836101fb58e3e1d2004aac1b0b9673a84101478aedbb1f7a01910e2d1cbdd6a1 skyhost_058:9119 +020999a7e05996483f8d9cd3bbfff15c6a8ab7742313c59e9a51cf9ffd6cccc8dd skyhost_059:9119 +02442601636264d662ca6030264f3f72480ea5f7ae30379d122563bff341dceef7 skyhost_060:9119 +032b4854d0ab1738d8b83729ab182906dadc6b5c07e01dc64927e94754609762fa skyhost_061:9119 +030cc432d13049fc8efca4de4232249078fad3569a30c231d30cee58aa72ba9204 skyhost_062:9119 +021fc87218b60dadf35b2628cf9becc65ab16aae5a24bc0f818b5d5b68c6c9b055 skyhost_063:9119 +038f6398f3cdaae818ad161c89565abbf4bd7f0f5f3e140dd181be7bcb5dfbdcfc skyhost_064:9119 +02593b933ee557e0062470dffc4bb1fa62da060ea71363737a156e070f5710251a skyhost_065:9119 +02a6c4f2c982621434ce0ae1064f52bfe88d0e16c28263567c36497a9f179f36b3 skyhost_066:9119 +0264b5070214ea86bb3a58eed38b227206c1757c9dd89c45d4484a46cbd6ae29a2 skyhost_067:9119 +0340bc08384cceddb27c5ea19186bc725c362576b5bf44eb44818260f2c418e076 skyhost_068:9119 +03bf5effc923dd0af5e87853c42c9e0348cfafd17987e7b6cd7477d804eab627fe skyhost_069:9119 +023e5d2e629381a788bcdeede3dede7f33157932c36a0749a8421afeb30b526e99 skyhost_070:9119 +036376d86e676b0601b95e33f459b5645eb791c7d40270850535066a1c8281a02c skyhost_071:9119 +0377a1d965886626d7407a48a2d92e3a142931bdf4f84f1a3c71cf20619ee047d0 skyhost_072:9119 +02314e3a5cf8bd8b3a1397cfb7423d91316fc1bd154adac2e1e5406a773963f583 skyhost_073:9119 +03d635622ba19390f9038994be5cac5aaaf83118577ca601f14b2273015e9741ed skyhost_074:9119 +03b3b0d95c26c152cc7e6641a905971d9f7742cc5f309dce599dba093452930a33 skyhost_075:9119 +031a9a280dbac05b98c2a5c71fc7da6e1f173449efb0b5028381bdd96d9fb36027 skyhost_076:9119 +02dc94d4b74319ccb744e79d6a3c2c3cccc5904d9df3ae3f6a1d170167f7d95e30 skyhost_077:9119 +035607e5f3028e43e89f612a2d5594f73876416413c3fdfa0fd69d5a23a841cc53 skyhost_078:9119 +02e8314a7e7dac102df36cf9a768b5cb2621ca7063f4767d966ca33cfef50f848c skyhost_079:9119 +03c3ae9aaa47c2bd2104562ec97fa0604137a72139837ebee2d16ec6f5a869bd3a skyhost_080:9119 +0282d04f44be258ced29a9d0ea219bfc848d6aed7ffe1d9132d421c71445e4f240 skyhost_081:9119 +028468d2cc703150a238319a0473f1255533065def8dd6bd9fd0612585697d89ef skyhost_082:9119 +0278223c57c34c36835da8291cb3cd5a4b11b044b9154d3103e7f8d9e5a06a4d0e skyhost_083:9119 +0348cd1be0137c38a01e306f7827f942b4607559a7273ee8c28db9a1d9a4700d69 skyhost_084:9119 +037beff859d8625dcd00f9f60c9aa46d4fbb0df11be0cbf833f56d107a09c9845f skyhost_085:9119 +020f2a586d17ee24b5db9e8f509f24f6d2c9e38435a970f9f0013cdad53d1ddfee skyhost_086:9119 +03a086a7a5713d00ee6334fbf0b6d39fe3f4a7f8644a16eded80a59682121b79fe skyhost_087:9119 +02de4c0e7ddf7a7b8bd9e49612ae61167d006440979c3a3c827257cb63ed6d5e62 skyhost_088:9119 +0242d56324e19e5f3d410c1445c59dea76222d00a1443ac80ab6b02cfb45a3fdb2 skyhost_089:9119 +02dabcba7c968331ffd4935e936aa3226b95c9288d4ea9df008ddd2a1de1c41d2c skyhost_090:9119 +0280eae0b6e242160eb0f630a8a8f98298eab7262a92296d7a097d6d0997d6bb14 skyhost_091:9119 +0398ef1a92cc17439410b97381724d798448f1023be5f9bd07c8dc64cce4f0b55a skyhost_092:9119 +038455d7d541f486eab847202b621a6d414877d02b3610bc3f5d70cf8e45a51dbb skyhost_093:9119 +02363018f6c6c2803e817151fa70f7899c225a9bba15a974704c70233cad96f07b skyhost_094:9119 +02f0dd73fe88a3faded006a9f6f5cb0452c165f67c480ab1f82504b76c66378152 skyhost_095:9119 +0383956d6a6be011c06fdec7e6e76a0499d62eda049cc187bb46a92fcc55c757ff skyhost_096:9119 +0278fc58753706286b8889f2b2f54cc63b36a905c31211315cc4c39803a92b614a skyhost_097:9119 +02d9f7bdf334a2019b7dee343d2473ac2319ee196d5c230353e50741208b9acba7 skyhost_098:9119 +02457ad23282c2b4e27127ce6f12778ae0720d176940608a7ca9d136f303ce70e9 skyhost_099:9119 +03fa1fca4491d8d7c145bc1301a2555efc0067d20797aa7d20b2307f3b57ce049a skyhost_100:9119 +030823b04f9c149a9121c104aee5657ebab931c52ff57eacbabbd0eda4469eca9e skyhost_101:9119 +036d883f7a0a164b8e672dd54a7781d0bb6128c2210a70599c306d408de57001b8 skyhost_102:9119 +036ba4d2f3a2ebff490f0b6e6ae7f8bac1fec88739b16eddb09cb103b6d4488dea skyhost_103:9119 +03a04fe8bb302b6a36f716dc66846b9ae8aafabe216254dc6852b4c31e0f67a3c5 skyhost_104:9119 +03b25ccefde473c68eb0f55e6210f17b108839867b5d14e27bdbb4258b12d7fb5f skyhost_105:9119 +0292ad1a2fcd3e7843411b21243538c330c60a9b31a25322e352a122dab3016d8f skyhost_106:9119 +02da5d9f9ab64360b855015d749ed74ec59e61c37ccd5f071cec5b2fa5067de7f8 skyhost_107:9119 +0333c80071daa716138725ee5204bc1f9e56e048bae1de4110865a633d02836323 skyhost_108:9119 +031e0f740c45ed6a5cc3295fa0c022507d35eb8fe5bb2862a7b604b3df497cfa7a skyhost_109:9119 +025a8088dc7c067f0c936ef7d4507ee71c9f42fdab4f299604612575ba7a48e444 skyhost_110:9119 +02816ce9cf3965698d301e4babb9fb17d691f34b66501de7034c7891320f2f5bcf skyhost_111:9119 +03a8d141c399d36111a5e9ba3778ba63905f5f153cf1d9c501d01c3ad8fea5758f skyhost_112:9119 +02ea963cc62b7722aead7142e2a171d24b20411c07eae7fdd6df863d7fffc0aace skyhost_113:9119 +0254ab50fa28894e1f67f32676b6c84c3ffad45d192dc69d30b2e068c90456eb17 skyhost_114:9119 +032c1dc027b7dc565b600c1dcad9ad73863a7fb3c0fff4f8a9fc6fb72a89d641a9 skyhost_115:9119 +0267c9824031a83bd52c7e02d6a6343caf8697e90e09a0d341bf4e578a1a98edaf skyhost_116:9119 +02e0531dea2b6c2b58fe0da35afb27b7936808de5d81c07f613d711883511729d4 skyhost_117:9119 +0287927ab07ad31a70fbafa9355dcaa43f95c4844fae5ef614dc3c0e51e338c7c0 skyhost_118:9119 +02dac42d560a4bab65c35f6b8f7ef71920f8c6bb3fcc55806308ef4275ab42125a skyhost_119:9119 +035bd1ecc493c1b79542447af3793ea5238ab46e36f1e6c8210ff39a472c5bd9d0 skyhost_120:9119 +037ef72928c7b23ebde3fbd07412430cee37489ba3410cb18b4024c321dcf28921 skyhost_121:9119 +031ddf1b3b5b2602008933775b2e256386610ce059fdae2048d993e726fe244522 skyhost_122:9119 +03759272e7425915148458e23eb34aa95705cf6225d8e18e4b884752aa2c63ae9a skyhost_123:9119 +029c9f50b8a2372baa951ed65b26b6019cc78707c0c29939ee5babd740c036f360 skyhost_124:9119 +02db0e1d76c11fee7dda0f1c3eb2d4ab4452255c67da0cf01cb08a26f81fba2ceb skyhost_125:9119 +0269679c529dc17efca197e667f1a4c1aa36c837097db5420356c84d32ce58c3d4 skyhost_126:9119 +0317dae55594f1c5c6e5105422853772755453894bc274af3267c42ede3be5e973 skyhost_127:9119 +0391684363b22b72b82e7419762dc33e0fec6ae1a2481ffd06e260d9e95214b473 skyhost_128:9119 \ No newline at end of file diff --git a/pkg/app/payload.go b/pkg/app/payload.go index cf9380093..508365d5d 100644 --- a/pkg/app/payload.go +++ b/pkg/app/payload.go @@ -30,9 +30,9 @@ func (p Payload) String() string { case FrameSend: packet := &Packet{} if err := json.Unmarshal(p.Data, packet); err != nil { - return fmt.Sprintf("{FrameSend. Error: %v, data: %v}", err, p.Data) + return fmt.Sprintf("{FrameSend. Error: %v, data: %s}", err, p.Data) } - return fmt.Sprintf("{FrameSend: %v}", packet) + return fmt.Sprintf("{FrameSend: %s}", packet) case FrameClose: var loop routing.Loop if err := json.Unmarshal(p.Data, &loop); err != nil { diff --git a/pkg/app/protocol.go b/pkg/app/protocol.go index b37adc96e..fd61ae74e 100644 --- a/pkg/app/protocol.go +++ b/pkg/app/protocol.go @@ -72,6 +72,7 @@ func NewProtocol(rw io.ReadWriteCloser) *Protocol { func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { id, resChan := p.chans.add() if err := p.writeFrame(cmd, id, payload); err != nil { + log.Warnf("Protocol.Send writeFrame err: %v\n", err) return err } @@ -149,7 +150,7 @@ func (p *Protocol) Close() error { } func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err error) { - + Logger.Info("[Protocol.writeFrame] enter") var data []byte if err, ok := payload.(error); ok { data = []byte(err.Error()) @@ -159,12 +160,17 @@ func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err er return err } } - Logger.WithField("payload", Payload{frame, data}).Info("Protocol.writeFrame") + Logger.WithField("payload", Payload{frame, data}).Info("[Protocol.writeFrame]") packet := append([]byte{byte(frame), id}, data...) buf := make([]byte, 2) binary.BigEndian.PutUint16(buf, uint16(len(packet))) _, err = p.rw.Write(append(buf, packet...)) + + if err != nil { + Logger.Warnf("[Protocol.writeFrame] p.rw.Write err: %v\n", err) + } + Logger.Infof("[Protocol.writeFrame] success: %v\n", err == nil) return err } diff --git a/pkg/router/app_manager.go b/pkg/router/app_manager.go index 7e45a4686..18b7b6f5b 100644 --- a/pkg/router/app_manager.go +++ b/pkg/router/app_manager.go @@ -12,11 +12,11 @@ import ( const supportedProtocolVersion = "0.0.1" -type appCallbacks struct { - CreateLoop func(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) - CloseLoop func(conn *app.Protocol, loop routing.Loop) error - ForwardAppPacket func(conn *app.Protocol, packet *app.Packet) error -} +// type appCallbacks struct { +// CreateLoop func(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) +// CloseLoop func(conn *app.Protocol, loop routing.Loop) error +// ForwardAppPacket func(conn *app.Protocol, packet *app.Packet) error +// } type appManager struct { Logger *logging.Logger @@ -28,7 +28,8 @@ type appManager struct { func (am *appManager) Serve() error { return am.proto.Serve(func(frame app.Frame, payload []byte) (res interface{}, err error) { - am.Logger.WithField("payload", app.Payload{frame, payload}).Infof("Got new request in appManager.Serve") + am.Logger.WithField("payload", app.Payload{frame, payload}). + Infof("[appManager.Serve] Got new request") switch frame { case app.FrameInit: @@ -44,7 +45,7 @@ func (am *appManager) Serve() error { } if err != nil { - am.Logger.Infof("App request with type %s failed: %s", frame, err) + am.Logger.Warnf("[appManager.Serve]: App request with type %s failed: %s", frame, err) } return res, err @@ -92,11 +93,12 @@ func (am *appManager) handleCloseLoop(payload []byte) error { } func (am *appManager) forwardAppPacket(payload []byte) error { + am.Logger.Info("[appMananger.forwardAppPacket] enter") packet := &app.Packet{} if err := json.Unmarshal(payload, packet); err != nil { return err } - am.Logger.WithField("packet", packet).Info("appMananger.forwardAppPacket") + am.Logger.WithField("packet", packet).Info("[appMananger.forwardAppPacket] exit") return am.router.ForwardAppPacket(am.proto, packet) } diff --git a/pkg/router/router.go b/pkg/router/router.go index d8f541016..e9ff8bcce 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -10,7 +10,6 @@ import ( "sync" "time" - "github.com/skycoin/dmsg" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" @@ -285,6 +284,7 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { } func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) error { + r.Logger.Info("[Router.forwardAppPacket] enter") if r == nil { return nil } @@ -294,23 +294,32 @@ func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) err l, err := r.pm.GetLoop(packet.Loop.Local.Port, packet.Loop.Remote) if err != nil { + r.Logger.Warnf("[Router.forwardAppPacket] r.pm.GetLoop: %v", err) return err } tr := r.tm.Transport(l.trID) if tr == nil { + r.Logger.Warnf("[Router.forwardAppPacket] r.tm.Transport: tr==nil") return errors.New("unknown transport") } p := routing.MakePacket(l.routeID, packet.Payload) - r.Logger.Infof("Forwarded App packet from LocalPort %d using route ID %d", packet.Loop.Local.Port, l.routeID) + r.Logger.Infof("[Router.forwardAppPacket] Forwarded App packet from LocalPort %d using route ID %d", packet.Loop.Local.Port, l.routeID) _, err = tr.Write(p) + + if err != nil { + r.Logger.Warnf("[Router.forwardAppPacket] tr.Write: %v", err) + } + r.Logger.Info("[Router.forwardAppPacket] success") return err } func (r *Router) forwardLocalAppPacket(packet *app.Packet) error { + r.Logger.Info("[Router.forwardLocalAppPacket] enter") b, err := r.pm.Get(packet.Loop.Remote.Port) if err != nil { + r.Logger.Warnf("[Router.forwardLocalAppPacket] r.pm.Get: %v\n", err) return nil } @@ -321,29 +330,39 @@ func (r *Router) forwardLocalAppPacket(packet *app.Packet) error { }, Payload: packet.Payload, } - return b.conn.Send(app.FrameSend, p, nil) + errSend := b.conn.Send(app.FrameSend, p, nil) + if errSend != nil { + r.Logger.Warnf("[Router.forwardLocalAppPacket] b.conn.Send: %v\n", err) + } + r.Logger.Warnf("[Router.forwardLocalAppPacket] success: %v\n", err == nil) + return errSend } func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing.Addr, error) { + r.Logger.Infof("[Router.requestLoop] enter") if r == nil { return raddr, nil } + lport := r.pm.Alloc(appConn) if err := r.pm.SetLoop(lport, raddr, &loop{}); err != nil { + r.Logger.Warnf("[Router.requestLoop] r.pm.SetLoop err: %v", err) return routing.Addr{}, err } laddr := routing.Addr{PubKey: r.config.PubKey, Port: lport} if raddr.PubKey == r.config.PubKey { if err := r.confirmLocalLoop(laddr, raddr); err != nil { + r.Logger.Warnf("[Router.requestLoop] r.ConfirmLocalLoop err: %v", err) return routing.Addr{}, fmt.Errorf("confirm: %s", err) } - r.Logger.Infof("Created local loop on port %d", laddr.Port) + r.Logger.Infof("[Router.requestLoop] r.confirmLocalLoop Created local loop on port %d", laddr.Port) return laddr, nil } forwardRoute, reverseRoute, err := r.fetchBestRoutes(laddr.PubKey, raddr.PubKey) if err != nil { + r.Logger.Warnf("[Router.requestLoop] r.fetchBestRoutes err: %v\n", err) return routing.Addr{}, fmt.Errorf("route finder: %s", err) } @@ -359,11 +378,13 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing proto, tr, err := r.setupProto(context.Background()) if err != nil { + r.Logger.Warnf("[Router.requestLoop] r.setupProto err: %v\n", err) return routing.Addr{}, err } + defer func() { if err := tr.Close(); err != nil { - r.Logger.Warnf("Failed to close transport: %s", err) + r.Logger.Warnf("[Router.requestLoop] Failed to close transport: %s", err) } }() @@ -371,7 +392,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing return routing.Addr{}, fmt.Errorf("route setup: %s", err) } - r.Logger.Infof("Created new loop to %s on port %d", raddr, laddr.Port) + r.Logger.Infof("[Router.requestLoop] success. Created new loop to %s on port %d", raddr, laddr.Port) return laddr, nil } @@ -469,21 +490,26 @@ func (r *Router) destroyLoop(loop routing.Loop) error { } func (r *Router) setupProto(ctx context.Context) (*setup.Protocol, transport.Transport, error) { + r.Logger.Infof("[Router.setupProto] for %v enter\n", r.config.TransportType) + if len(r.config.SetupNodes) == 0 { + r.Logger.Warn("[Router.setupProto] r.setupProto: route setup: no nodes ") return nil, nil, errors.New("route setup: no nodes") } - tr, err := r.tm.CreateSetupTransport(ctx, r.config.SetupNodes[0], dmsg.Type) + tr, err := r.tm.CreateSetupTransport(ctx, r.config.SetupNodes[0], r.config.TransportType) if err != nil { + r.Logger.Warnf("[Router.setupProto] r.setupProto r.tm.CreateSetupTransport error:%v\n", err) return nil, nil, fmt.Errorf("setup transport: %s", err) } sProto := setup.NewSetupProtocol(tr) + r.Logger.Infof("[Router.setupProto] success") return sProto, tr, nil } func (r *Router) fetchBestRoutes(source, destination cipher.PubKey) (fwd routing.Route, rev routing.Route, err error) { - r.Logger.Infof("Requesting new routes from %s to %s", source, destination) + r.Logger.Infof("[Router.fetchBestRoutes] Requesting new routes from %s to %s", source, destination) timer := time.NewTimer(time.Second * 10) defer timer.Stop() @@ -499,7 +525,7 @@ fetchRoutesAgain: } } - r.Logger.Infof("Found routes Forward: %s. Reverse %s", fwdRoutes, revRoutes) + r.Logger.Infof("[Router.fetchBestRoutes] Found routes Forward: %s. Reverse %s success", fwdRoutes, revRoutes) return fwdRoutes[0], revRoutes[0], nil } diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index cd9f72b5d..34610f4e7 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -193,7 +193,7 @@ func (tm *Manager) Serve(ctx context.Context) error { if strings.Contains(err.Error(), "closed") { return } - tm.Logger.Warnf("Failed to accept connection: %s", err) + tm.Logger.Warnf("Manager.Serve tm.acceptTransport failed to accept connection: %s", err) } } } @@ -209,10 +209,12 @@ func (tm *Manager) Serve(ctx context.Context) error { func (tm *Manager) CreateSetupTransport(ctx context.Context, remote cipher.PubKey, tpType string) (Transport, error) { factory, ok := tm.factories[tpType] if !ok { + tm.Logger.Warn("Manager.CreateSetupTransport err: unknown transport type") return nil, errors.New("unknown transport type") } tr, err := factory.Dial(ctx, remote) if err != nil { + tm.Logger.Warnf("Manager.CreateSetupTransport factory.Dial err: %v\n", err) return nil, err } tm.Logger.Infof("Dialed to setup node %s using %s factory.", remote, tpType) @@ -341,6 +343,7 @@ func (tm *Manager) dialTransport(ctx context.Context, factory Factory, remote ci func (tm *Manager) acceptTransport(ctx context.Context, factory Factory) (Transport, error) { tr, err := factory.Accept(ctx) if err != nil { + tm.Logger.Warnf("Manager.acceptTransport factory.Accept err: %s", err) return nil, err } diff --git a/pkg/transport/tcp_transport.go b/pkg/transport/tcp_transport.go index a139a1556..88a483ef8 100644 --- a/pkg/transport/tcp_transport.go +++ b/pkg/transport/tcp_transport.go @@ -11,6 +11,7 @@ import ( "strings" "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" ) // ErrUnknownRemote returned for connection attempts for remotes @@ -22,10 +23,11 @@ type TCPFactory struct { Pk cipher.PubKey PkTable PubKeyTable Lsr *net.TCPListener + Logger *logging.Logger } // NewTCPFactory constructs a new TCP Factory -func NewTCPFactory(pk cipher.PubKey, pubkeysFile string, tcpAddr string) (Factory, error) { +func NewTCPFactory(pk cipher.PubKey, pubkeysFile string, tcpAddr string, logger *logging.Logger) (Factory, error) { pkTbl, err := FilePubKeyTable(pubkeysFile) if err != nil { @@ -42,21 +44,26 @@ func NewTCPFactory(pk cipher.PubKey, pubkeysFile string, tcpAddr string) (Factor return nil, fmt.Errorf("error %v listening %v", err, tcpAddr) } - return &TCPFactory{pk, pkTbl, tcpListener}, nil + logger.Infof("NewTCPFactory: success, records in pubkeys file: %v\n", pkTbl.Count()) + return &TCPFactory{pk, pkTbl, tcpListener, logger}, nil } // Accept accepts a remotely-initiated Transport. func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { + f.Logger.Info("[TCPFactory.Accept] enter") conn, err := f.Lsr.AcceptTCP() if err != nil { + f.Logger.Warnf("[TCPFactory.Accept] f.Lsr.AcceptTCP err: %v\n", err) return nil, err } raddr := conn.RemoteAddr().(*net.TCPAddr) rpk := f.PkTable.RemotePK(raddr.String()) if rpk.Null() { + f.Logger.Infof("[TCPFactory.Accept] f.PkTable.RemotePK rpk.Null() for %v\n", raddr) return nil, fmt.Errorf("error: %v, raddr: %v, rpk: %v", ErrUnknownRemote, raddr.String(), rpk) } + f.Logger.Info("[TCPFactory.Accept] success") // return &TCPTransport{conn, [2]cipher.PubKey{f.Pk, rpk}}, nil return &TCPTransport{conn, f.Pk, rpk}, nil @@ -64,30 +71,37 @@ func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { // Dial initiates a Transport with a remote node. func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, error) { + f.Logger.Info("[TCPFactory.Dial] enter") addr := f.PkTable.RemoteAddr(remote) if addr == "" { + f.Logger.Warnf("[TCPFactory.Dial] f.PkTable.RemoteAddr(remote) is empty for %v\n", remote) return nil, ErrUnknownRemote } tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { + f.Logger.Warnf("[TCPFactory.Dial] net.ResolveTCPAddr_tcpAddr err: %v\n", err) return nil, err } lsnAddr, err := net.ResolveTCPAddr("tcp", f.Lsr.Addr().String()) if err != nil { + f.Logger.Warnf("[TCPFactory.Dial] net.ResolveTCPAddr_lsnAddr err: %v\n", err) return nil, fmt.Errorf("error in resolving local address") } locAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%v:%v", lsnAddr.IP.String(), "0")) if err != nil { + f.Logger.Warnf("[TCPFactory.Dial] net.ResolveTCPAddr_locAddr err: %v\n", err) + net.ResolveTCPAddr("tcp", f.Lsr.Addr().String()) return nil, fmt.Errorf("error in constructing local address ") } conn, err := net.DialTCP("tcp", locAddr, tcpAddr) if err != nil { + f.Logger.Warnf("[TCPFactory.Dial] net.DialTCP err: %v\n", err) return nil, err } - + f.Logger.Info("[TCPFactory.Dial] success") return &TCPTransport{conn, f.Pk, remote}, nil } @@ -135,6 +149,7 @@ func (tr *TCPTransport) Type() string { type PubKeyTable interface { RemoteAddr(remotePK cipher.PubKey) string RemotePK(address string) cipher.PubKey + Count() int } type memPKTable struct { @@ -159,6 +174,11 @@ func MemoryPubKeyTable(entries map[cipher.PubKey]string) PubKeyTable { return memoryPubKeyTable(entries) } +// Count returns number of Table records +func (t *memPKTable) Count() int { + return len(t.entries) +} + func (t *memPKTable) RemoteAddr(remotePK cipher.PubKey) string { return t.entries[remotePK] } diff --git a/pkg/transport/tcp_transport_test.go b/pkg/transport/tcp_transport_test.go index b35585770..1cc4c66b6 100644 --- a/pkg/transport/tcp_transport_test.go +++ b/pkg/transport/tcp_transport_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -21,6 +22,7 @@ import ( sudo ip addr add 12.12.12.2 dev lo sudo ip addr add 12.12.12.3 dev lo */ +var testLogger = logging.MustGetLogger("tcp-transport") func Example_transport_TCPFactory() { pkA := pkFromSeed("skyhost_001") @@ -52,7 +54,7 @@ func Example_transport_TCPFactory() { go func() { defer wg.Done() - fA := &transport.TCPFactory{pkA, pkt, lsnA} + fA := &transport.TCPFactory{pkA, pkt, lsnA, testLogger} tr, err := fA.Accept(context.TODO()) if err != nil { fmt.Printf("Accept err: %v\n", err) @@ -70,7 +72,7 @@ func Example_transport_TCPFactory() { go func() { defer wg.Done() - fB := &transport.TCPFactory{pkB, pkt, lsnB} + fB := &transport.TCPFactory{pkB, pkt, lsnB, testLogger} tr, err := fB.Dial(context.TODO(), pkA) if err != nil { fmt.Printf("Dial err: %v\n", err) @@ -116,11 +118,11 @@ func TestTCPFactory(t *testing.T) { fmt.Println(addr1.String()) fmt.Println(addr2.String()) - f1 := &transport.TCPFactory{pk1, pkt1, l1} + f1 := &transport.TCPFactory{pk1, pkt1, l1, testLogger} assert.Equal(t, "tcp-transport", f1.Type()) assert.Equal(t, pk1, f1.Local()) - f2 := &transport.TCPFactory{pk2, pkt2, l2} + f2 := &transport.TCPFactory{pk2, pkt2, l2, testLogger} assert.Equal(t, "tcp-transport", f2.Type()) assert.Equal(t, pk2, f2.Local()) @@ -190,6 +192,7 @@ func TestFilePKTable(t *testing.T) { pkt, err := transport.FilePubKeyTable(tmpfile.Name()) require.NoError(t, err) + require.Equal(t, pkt.Count(), 1) raddr := pkt.RemoteAddr(pk) assert.Equal(t, addr.String(), raddr) diff --git a/pkg/visor/visor_multihead_test.go b/pkg/visor/multihead_test.go similarity index 84% rename from pkg/visor/visor_multihead_test.go rename to pkg/visor/multihead_test.go index 319de4530..384932371 100644 --- a/pkg/visor/visor_multihead_test.go +++ b/pkg/visor/multihead_test.go @@ -157,11 +157,15 @@ func (mh *MultiHead) initNodes() { mh.nodes = make([]*Node, len(mh.cfgPool)) mh.initErrs = make(chan error, len(mh.cfgPool)) - subs := []struct{ old, new string }{ - {"000000000000000000000000000000000000000000000000000000000000000000", "PK{NULL}"}, - {"024195ae0d46eb0195c9ddabcaf62bc894316594ea2e92570f269238c5b5f817d1", "PK(skyhost_001)"}, - {"037dfc42ae5ca494f5646d09e6532ea39bea7954a915409925277f4604a34968b3", "PK(skyhost_002)"}, + subs := make([]struct{ old, new string }, len(mh.cfgPool)+1) + for i := 0; i < len(mh.cfgPool); i++ { + subs[i] = struct{ old, new string }{ + old: fmt.Sprintf("%v", mh.cfgPool[i].Node.StaticPubKey), + new: fmt.Sprintf("PK(%v)", mh.ipPool[i]), + } } + subs = append(subs, struct{ old, new string }{ + "000000000000000000000000000000000000000000000000000000000000000000", "PK{NULL}"}) var err error for i := 0; i < len(mh.nodes); i++ { @@ -276,19 +280,55 @@ func (mh *MultiHead) sendMessage(sender, reciever uint, message string) (*http.R } +func (mh *MultiHead) genPubKeysFile() string { + recs := make([]string, len(mh.cfgPool)) + for i := 0; i < len(mh.cfgPool); i++ { + recs[i] = fmt.Sprintf("%s\t%s\n", mh.cfgPool[i].Node.StaticPubKey, mh.ipPool[i]) + } + return strings.Join(recs, "") +} + +func Example_genPubKeysFile() { + + mh := makeMultiHeadN(128) + fmt.Printf("%v\n", mh.genPubKeysFile()) + + // Output: ZZZ +} + +func ExampleMultiHead_sendMessage_local() { + mh := makeMultiHeadN(1) + mh.startNodes(time.Second) + + _, err := mh.sendMessage(0, 0, "Hello") + fmt.Printf("err: %v", err) + + mh.stopNodes(time.Second * 3) + fmt.Printf("%v\n", mh.errReport()) + // printLogger := logging.MustGetLogger("test") + // printLogger.Infof("%v\n", mh.Log.records) + fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) + + // Output: err: + // init errors: 0 + // start errors: 0 + // stop errors: 0 + +} + // WIP -func ExampleMultiHead_sendMessage() { +func ExampleMultiHead_sendMessage_remote() { mh := makeMultiHeadN(2) mh.startNodes(time.Second) _, err := mh.sendMessage(0, 1, "Hello") fmt.Printf("err: %v", err) - mh.stopNodes(time.Second * 3) + mh.stopNodes(time.Second) fmt.Printf("%v\n", mh.errReport()) // printLogger := logging.MustGetLogger("test") // printLogger.Infof("%v\n", mh.Log.records) - // fmt.Printf("%v\n", mh.Log.records) + fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) // Output: err: // init errors: 0 diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index ac870872b..1be9f8a05 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -153,8 +153,14 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) case "tcp-transport": var err error - node.messenger, err = transport.NewTCPFactory(config.Node.StaticPubKey, config.PubKeysFile, config.TCPTransportAddr) + node.messenger, err = transport.NewTCPFactory( + config.Node.StaticPubKey, + config.PubKeysFile, + config.TCPTransportAddr, + masterLogger.PackageLogger("TCPFactory")) + if err != nil { + node.Logger.Warnf("NewNode transport.NewTCPFactory err: %v\n", err) return nil, err } diff --git a/pkg/visor/visor_test.go b/pkg/visor/visor_test.go index 3fe4b8d73..f7610e1ca 100644 --- a/pkg/visor/visor_test.go +++ b/pkg/visor/visor_test.go @@ -104,7 +104,11 @@ func TestNodeStartClose(t *testing.T) { node.messenger = dmsg.NewClient(mConf.PubKey, mConf.SecKey, mConf.Discovery) case "tcp-transport": var err error - node.messenger, err = transport.NewTCPFactory(nodeCfg.Node.StaticPubKey, nodeCfg.PubKeysFile, nodeCfg.TCPTransportAddr) + node.messenger, err = transport.NewTCPFactory( + nodeCfg.Node.StaticPubKey, + nodeCfg.PubKeysFile, + nodeCfg.TCPTransportAddr, + node.Logger.PackageLogger("TCPFactory.test")) require.NoError(t, err) } From 3da5dbe88466e4867d0c61ddb7372e8503dfd74a Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Mon, 19 Aug 2019 09:52:38 +0300 Subject: [PATCH 18/26] still working --- cmd/apps/skychat/chat.go | 10 ++ internal/testhelpers/testhelpers.go | 46 +++++++++ pkg/app/app.go | 17 +++- pkg/app/protocol.go | 24 ++++- pkg/router/app_manager.go | 21 +++- pkg/router/router.go | 150 ++++++++++++++++++++++++---- pkg/router/router_test.go | 3 + pkg/routing/packet.go | 5 + pkg/transport/managed_transport.go | 1 + pkg/transport/manager.go | 9 +- pkg/transport/manager_test.go | 6 +- pkg/transport/mock.go | 1 + pkg/transport/tcp_transport.go | 23 ++++- pkg/visor/multihead_test.go | 59 +++++++++-- pkg/visor/rpc.go | 2 +- pkg/visor/taggedformatter.go | 13 ++- pkg/visor/visor.go | 2 +- 17 files changed, 334 insertions(+), 58 deletions(-) diff --git a/cmd/apps/skychat/chat.go b/cmd/apps/skychat/chat.go index 4e8eb994b..5be306abc 100644 --- a/cmd/apps/skychat/chat.go +++ b/cmd/apps/skychat/chat.go @@ -13,11 +13,13 @@ import ( "net" "net/http" "sync" + "sync/atomic" "time" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skywire/internal/netutil" + "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" ) @@ -79,8 +81,15 @@ func listenLoop() { } func handleConn(conn net.Conn) { + log.Printf(testhelpers.Trace("ENTER")) + defer log.Printf(testhelpers.Trace("EXIT")) + var cntr uint64 + raddr := conn.RemoteAddr().(routing.Addr) + for { + atomic.AddUint64(&cntr, 1) + log.Printf(testhelpers.Trace(fmt.Sprintf("CYCLE %03d START", cntr))) buf := make([]byte, 32*1024) n, err := conn.Read(buf) if err != nil { @@ -98,6 +107,7 @@ func handleConn(conn net.Conn) { default: log.Printf("received and trashed: %s\n", clientMsg) } + log.Printf(testhelpers.Trace(fmt.Sprintf("CYCLE %03d END", cntr))) } } diff --git a/internal/testhelpers/testhelpers.go b/internal/testhelpers/testhelpers.go index ebe67eaaa..2760b96e4 100644 --- a/internal/testhelpers/testhelpers.go +++ b/internal/testhelpers/testhelpers.go @@ -2,6 +2,9 @@ package testhelpers import ( + "fmt" + "runtime" + "strings" "time" ) @@ -38,3 +41,46 @@ func NoErrorWithinTimeout(ch <-chan error) error { func NoErrorWithinTimeoutN(errChans ...<-chan error) error { return NoErrorWithinTimeout(joinErrChannels(errChans)) } + +func formatName(name string) string { + parts := strings.Split(name, "/") + return fmt.Sprintf("[%v]", parts[len(parts)-1]) + // return strings.Join(parts[len(parts)-3:], ".") +} + +// GetCallerN return name of nth-caller +func GetCallerN(skip int) string { + pc := make([]uintptr, 15) + n := runtime.Callers(skip, pc) + frames := runtime.CallersFrames(pc[:n]) + frame, _ := frames.Next() + return formatName(frame.Function) +} + +// GetCaller returns caller name +func GetCaller() string { + return GetCallerN(3) +} + +// Trace returns caller name with attached label +func Trace(label string) string { + return fmt.Sprintf("%v %v", GetCallerN(3), label) +} + +// CallerDepth return depth of function +func CallerDepth() int { + pc := make([]uintptr, 15) + return runtime.Callers(9, pc) +} + +// GetCallers return names of caller functions +func GetCallers(skip int) []string { + pc := make([]uintptr, 15) + n := runtime.Callers(skip, pc) + frames := runtime.CallersFrames(pc[:n]) + var callers []string + for frame, next := frames.Next(); next; frame, next = frames.Next() { + callers = append(callers, formatName(frame.Function)) + } + return callers +} diff --git a/pkg/app/app.go b/pkg/app/app.go index e2be01015..a58811694 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -16,6 +16,7 @@ import ( "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/routing" ) @@ -101,6 +102,9 @@ func Setup(config *Config) (*App, error) { // Close implements io.Closer for an App. func (app *App) Close() error { + log.Debug(testhelpers.Trace("ENTER")) + defer log.Debug(testhelpers.Trace("EXIT")) + if app == nil { return nil } @@ -129,11 +133,16 @@ func (app *App) Close() error { // Accept awaits for incoming loop confirmation request from a Node and // returns net.Conn for received loop. func (app *App) Accept() (net.Conn, error) { + log.Debug(testhelpers.Trace("ENTER")) + defer log.Debug(testhelpers.Trace("EXIT")) + addrs := <-app.acceptChan laddr := addrs[0] raddr := addrs[1] + // TODO: Why is that? loop := routing.Loop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} + // loop := routing.Loop{laddr, raddr} conn, out := net.Pipe() app.mu.Lock() app.conns[loop] = conn @@ -144,6 +153,9 @@ func (app *App) Accept() (net.Conn, error) { // Dial sends create loop request to a Node and returns net.Conn for created loop. func (app *App) Dial(raddr routing.Addr) (net.Conn, error) { + log.Debug(testhelpers.Trace("ENTER")) + defer log.Debug(testhelpers.Trace("EXIT")) + var laddr routing.Addr err := app.proto.Send(FrameCreateLoop, raddr, &laddr) if err != nil { @@ -175,10 +187,8 @@ func (app *App) handleProto() { default: err = errors.New("unexpected frame") } - return res, err }) - if err != nil { return } @@ -250,6 +260,9 @@ func (app *App) closeConn(data []byte) error { } func (app *App) confirmLoop(data []byte) error { + log.Debug(testhelpers.Trace("ENTER")) + defer log.Debug(testhelpers.Trace("EXIT")) + var addrs [2]routing.Addr if err := json.Unmarshal(data, &addrs); err != nil { return err diff --git a/pkg/app/protocol.go b/pkg/app/protocol.go index fd61ae74e..d044123f0 100644 --- a/pkg/app/protocol.go +++ b/pkg/app/protocol.go @@ -8,8 +8,10 @@ import ( "io" "strings" "sync" + "sync/atomic" "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/internal/testhelpers" ) // Frame defines type for all App frames. @@ -70,6 +72,9 @@ func NewProtocol(rw io.ReadWriteCloser) *Protocol { // Send sends command Frame with payload and awaits for response. func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { + Logger.Info(testhelpers.Trace("ENTER")) + defer Logger.Infof(testhelpers.Trace("EXIT")) + id, resChan := p.chans.add() if err := p.writeFrame(cmd, id, payload); err != nil { log.Warnf("Protocol.Send writeFrame err: %v\n", err) @@ -94,7 +99,13 @@ func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { // Serve reads incoming frame, passes it to the handleFunc and writes results. func (p *Protocol) Serve(handleFunc func(Frame, []byte) (interface{}, error)) error { + Logger.Info(testhelpers.Trace("ENTER")) + defer Logger.Infof(testhelpers.Trace("EXIT")) + var cntr uint64 + for { + atomic.AddUint64(&cntr, 1) + Logger.Debugf("%v CYCLE %03d START", testhelpers.GetCaller(), cntr) frame, err := p.readFrame() if err != nil { if err == io.EOF || strings.Contains(err.Error(), "closed") { @@ -142,6 +153,9 @@ func (p *Protocol) Serve(handleFunc func(Frame, []byte) (interface{}, error)) er // Close closes underlying ReadWriter. func (p *Protocol) Close() error { + Logger.Info(testhelpers.Trace("ENTER")) + defer Logger.Infof(testhelpers.Trace("EXIT")) + if p == nil { return nil } @@ -150,7 +164,9 @@ func (p *Protocol) Close() error { } func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err error) { - Logger.Info("[Protocol.writeFrame] enter") + Logger.Info(testhelpers.Trace("ENTER")) + defer Logger.Infof(testhelpers.Trace("EXIT")) + var data []byte if err, ok := payload.(error); ok { data = []byte(err.Error()) @@ -160,7 +176,7 @@ func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err er return err } } - Logger.WithField("payload", Payload{frame, data}).Info("[Protocol.writeFrame]") + Logger.WithField("payload", Payload{frame, data}).Info(testhelpers.GetCaller()) packet := append([]byte{byte(frame), id}, data...) buf := make([]byte, 2) @@ -168,9 +184,9 @@ func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err er _, err = p.rw.Write(append(buf, packet...)) if err != nil { - Logger.Warnf("[Protocol.writeFrame] p.rw.Write err: %v\n", err) + Logger.Warnf("% p.rw.Write err: %v\n", testhelpers.GetCaller(), err) } - Logger.Infof("[Protocol.writeFrame] success: %v\n", err == nil) + return err } diff --git a/pkg/router/app_manager.go b/pkg/router/app_manager.go index 18b7b6f5b..afd72b800 100644 --- a/pkg/router/app_manager.go +++ b/pkg/router/app_manager.go @@ -6,6 +6,7 @@ import ( "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" ) @@ -27,9 +28,14 @@ type appManager struct { } func (am *appManager) Serve() error { + am.Logger.Info(testhelpers.Trace("EXIT")) + defer am.Logger.Info(testhelpers.Trace("EXIT")) + + // Protocol.Serve loop start return am.proto.Serve(func(frame app.Frame, payload []byte) (res interface{}, err error) { am.Logger.WithField("payload", app.Payload{frame, payload}). - Infof("[appManager.Serve] Got new request") + Info(testhelpers.Trace("ENTER")) + defer am.Logger.Info(testhelpers.Trace("EXIT")) switch frame { case app.FrameInit: @@ -45,7 +51,7 @@ func (am *appManager) Serve() error { } if err != nil { - am.Logger.Warnf("[appManager.Serve]: App request with type %s failed: %s", frame, err) + am.Logger.Warnf("%v: App request with type %s failed: %+v\n", testhelpers.GetCaller(), frame, err) } return res, err @@ -75,6 +81,9 @@ func (am *appManager) initApp(payload []byte) error { } func (am *appManager) setupLoop(payload []byte) (routing.Addr, error) { + am.Logger.Info(testhelpers.Trace("ENTER")) + defer am.Logger.Info(testhelpers.Trace("EXIT")) + var raddr routing.Addr if err := json.Unmarshal(payload, &raddr); err != nil { return routing.Addr{}, err @@ -84,6 +93,9 @@ func (am *appManager) setupLoop(payload []byte) (routing.Addr, error) { } func (am *appManager) handleCloseLoop(payload []byte) error { + am.Logger.Info(testhelpers.Trace("ENTER")) + defer am.Logger.Info(testhelpers.Trace("EXIT")) + var loop routing.Loop if err := json.Unmarshal(payload, &loop); err != nil { return err @@ -93,12 +105,13 @@ func (am *appManager) handleCloseLoop(payload []byte) error { } func (am *appManager) forwardAppPacket(payload []byte) error { - am.Logger.Info("[appMananger.forwardAppPacket] enter") + am.Logger.Info(testhelpers.Trace("ENTER")) + defer am.Logger.Info(testhelpers.Trace("EXIT")) + packet := &app.Packet{} if err := json.Unmarshal(payload, packet); err != nil { return err } - am.Logger.WithField("packet", packet).Info("[appMananger.forwardAppPacket] exit") return am.router.ForwardAppPacket(am.proto, packet) } diff --git a/pkg/router/router.go b/pkg/router/router.go index e9ff8bcce..370ccbd9d 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -13,11 +13,13 @@ import ( "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" routeFinder "github.com/skycoin/skywire/pkg/route-finder/client" "github.com/skycoin/skywire/pkg/routing" "github.com/skycoin/skywire/pkg/setup" "github.com/skycoin/skywire/pkg/transport" + "github.com/skycoin/skywire/pkg/transport/dmsg" ) const ( @@ -31,14 +33,15 @@ var log = logging.MustGetLogger("router") // Config configures Router. type Config struct { - Logger *logging.Logger - PubKey cipher.PubKey - SecKey cipher.SecKey + Logger *logging.Logger + PubKey cipher.PubKey + SecKey cipher.SecKey + TransportManager *transport.Manager RoutingTable routing.Table RouteFinder routeFinder.Client SetupNodes []cipher.PubKey - TransportType string + // TransportType string } // PacketRouter performs routing of the skywire packets. @@ -62,6 +65,7 @@ type Router struct { tm *transport.Manager pm *portManager rm *routeManager + msgr transport.Factory expiryTicker *time.Ticker wg sync.WaitGroup @@ -91,6 +95,8 @@ func New(config *Config) *Router { // Serve starts transport listening loop. func (r *Router) Serve(ctx context.Context) error { r.Logger.Info("Starting router") + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) go func() { for { @@ -153,6 +159,9 @@ func (r *Router) handleTransport(tp transport.Transport, isAccepted, isSetup boo // ServeApp handles App packets from the App connection on provided port. func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error { + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) + r.wg.Add(1) defer r.wg.Done() @@ -186,12 +195,6 @@ func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) return err } -// callbacks := &appCallbacks{ -// CreateLoop: r.requestLoop, -// CloseLoop: r.closeLoop, -// ForwardAppPacket: r.forwardAppPacket, -// } - // CreateLoop implements PactetRouter.CreateLoop func (r *Router) CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) { return r.requestLoop(conn, raddr) @@ -209,6 +212,9 @@ func (r *Router) ForwardAppPacket(conn *app.Protocol, packet *app.Packet) error // Close safely stops Router. func (r *Router) Close() error { + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) + if r == nil { return nil } @@ -227,6 +233,9 @@ func (r *Router) Close() error { } func (r *Router) serveTransport(rw io.ReadWriter) error { + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) + packet := make(routing.Packet, 6) if _, err := io.ReadFull(rw, packet); err != nil { return err @@ -252,6 +261,9 @@ func (r *Router) serveTransport(rw io.ReadWriter) error { // ForwardPacket forwards payload according to rule func (r *Router) ForwardPacket(payload []byte, rule routing.Rule) error { + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) + packet := routing.MakePacket(rule.RouteID(), payload) tr := r.tm.Transport(rule.TransportID()) if tr == nil { @@ -267,6 +279,9 @@ func (r *Router) ForwardPacket(payload []byte, rule routing.Rule) error { } func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) + laddr := routing.Addr{Port: rule.LocalPort()} raddr := routing.Addr{PubKey: rule.RemotePK(), Port: rule.RemotePort()} @@ -284,14 +299,32 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { } func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) error { - r.Logger.Info("[Router.forwardAppPacket] enter") + r.Logger.Info(testhelpers.Trace("ENTER")) + defer r.Logger.Info(testhelpers.Trace("EXIT")) + if r == nil { - return nil + return errors.New("router not initialized") } + if packet.Loop.Remote.PubKey == r.config.PubKey { return r.forwardLocalAppPacket(packet) } + // TODO: Check if exists TCPFactory in r.tm.Factories() and if raddr.PubKey exists in TCPFactory.PKTable + + r.Logger.Infof("%v ATTEMPT TO FIND %v", testhelpers.GetCaller(), packet.Loop.Remote.PubKey) + + for _, fct := range r.tm.Factories() { + switch tfct := fct.(type) { + case *transport.TCPFactory: + if ipAddr := tfct.PkTable.RemoteAddr(packet.Loop.Remote.PubKey); ipAddr != "" { + r.Logger.Infof("%v PK %v FOUND", testhelpers.GetCaller(), packet.Loop.Remote.PubKey) + return r.forwardTCPAppPacket(packet) + } + + } + } + l, err := r.pm.GetLoop(packet.Loop.Local.Port, packet.Loop.Remote) if err != nil { r.Logger.Warnf("[Router.forwardAppPacket] r.pm.GetLoop: %v", err) @@ -311,15 +344,41 @@ func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) err if err != nil { r.Logger.Warnf("[Router.forwardAppPacket] tr.Write: %v", err) } - r.Logger.Info("[Router.forwardAppPacket] success") + return err } func (r *Router) forwardLocalAppPacket(packet *app.Packet) error { - r.Logger.Info("[Router.forwardLocalAppPacket] enter") + r.Logger.Info(testhelpers.Trace("ENTER")) + defer r.Logger.Info(testhelpers.Trace("EXIT")) + + b, err := r.pm.Get(packet.Loop.Remote.Port) + if err != nil { + r.Logger.Warnf("%v r.pm.Get: %v\n", testhelpers.GetCaller(), err) + return nil + } + + p := &app.Packet{ + Loop: routing.Loop{ + Local: routing.Addr{Port: packet.Loop.Remote.Port}, + Remote: routing.Addr{PubKey: packet.Loop.Remote.PubKey, Port: packet.Loop.Local.Port}, + }, + Payload: packet.Payload, + } + errSend := b.conn.Send(app.FrameSend, p, nil) + if errSend != nil { + r.Logger.Warnf("%v b.conn.Send: %v\n", testhelpers.GetCaller(), err) + } + return errSend +} + +func (r *Router) forwardTCPAppPacket(packet *app.Packet) error { + r.Logger.Info(testhelpers.Trace("ENTER")) + defer r.Logger.Info(testhelpers.Trace("EXIT")) + b, err := r.pm.Get(packet.Loop.Remote.Port) if err != nil { - r.Logger.Warnf("[Router.forwardLocalAppPacket] r.pm.Get: %v\n", err) + r.Logger.Warnf("%v r.pm.Get: %v\n", testhelpers.GetCaller(), err) return nil } @@ -332,14 +391,15 @@ func (r *Router) forwardLocalAppPacket(packet *app.Packet) error { } errSend := b.conn.Send(app.FrameSend, p, nil) if errSend != nil { - r.Logger.Warnf("[Router.forwardLocalAppPacket] b.conn.Send: %v\n", err) + r.Logger.Info("%v b.conn.Send: %v\n", testhelpers.GetCaller(), err) } - r.Logger.Warnf("[Router.forwardLocalAppPacket] success: %v\n", err == nil) return errSend } func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing.Addr, error) { - r.Logger.Infof("[Router.requestLoop] enter") + r.Logger.Info(testhelpers.Trace("ENTER")) + defer r.Logger.Info(testhelpers.Trace("EXIT")) + if r == nil { return raddr, nil } @@ -376,6 +436,20 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing Reverse: reverseRoute, } + r.Logger.Infof("%v ATTEMPT TO FIND %v", testhelpers.GetCaller(), raddr.PubKey) + for _, fct := range r.tm.Factories() { + switch tfct := fct.(type) { + case *transport.TCPFactory: + if ipAddr := tfct.PkTable.RemoteAddr(raddr.PubKey); ipAddr != "" { + r.Logger.Infof("%v PK %v FOUND", testhelpers.GetCaller(), raddr.PubKey) + if err := r.confirmTCPLoop(laddr, raddr); err != nil { + r.Logger.Warnf("%v r.ConfirmTCPLoop err: %v", testhelpers.GetCaller(), err) + } + } + return laddr, nil + } + } + proto, tr, err := r.setupProto(context.Background()) if err != nil { r.Logger.Warnf("[Router.requestLoop] r.setupProto err: %v\n", err) @@ -392,11 +466,31 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing return routing.Addr{}, fmt.Errorf("route setup: %s", err) } - r.Logger.Infof("[Router.requestLoop] success. Created new loop to %s on port %d", raddr, laddr.Port) + r.Logger.Infof("%v success. Created new loop to %s on port %d", testhelpers.GetCaller(), raddr, laddr.Port) return laddr, nil } func (r *Router) confirmLocalLoop(laddr, raddr routing.Addr) error { + r.Logger.Info(testhelpers.Trace("ENTER")) + defer r.Logger.Info(testhelpers.Trace("EXIT")) + + b, err := r.pm.Get(raddr.Port) + if err != nil { + return err + } + + addrs := [2]routing.Addr{raddr, laddr} + if err = b.conn.Send(app.FrameConfirmLoop, addrs, nil); err != nil { + return err + } + + return nil +} + +func (r *Router) confirmTCPLoop(laddr, raddr routing.Addr) error { + r.Logger.Info(testhelpers.Trace("ENTER")) + defer r.Logger.Info(testhelpers.Trace("EXIT")) + b, err := r.pm.Get(raddr.Port) if err != nil { return err @@ -411,6 +505,9 @@ func (r *Router) confirmLocalLoop(laddr, raddr routing.Addr) error { } func (r *Router) confirmLoop(l routing.Loop, rule routing.Rule) error { + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) + b, err := r.pm.Get(l.Local.Port) if err != nil { return err @@ -429,6 +526,9 @@ func (r *Router) confirmLoop(l routing.Loop, rule routing.Rule) error { } func (r *Router) closeLoop(appConn *app.Protocol, loop routing.Loop) error { + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) + if err := r.destroyLoop(loop); err != nil { r.Logger.Warnf("Failed to remove loop: %s", err) } @@ -453,6 +553,9 @@ func (r *Router) closeLoop(appConn *app.Protocol, loop routing.Loop) error { } func (r *Router) loopClosed(loop routing.Loop) error { + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) + b, err := r.pm.Get(loop.Local.Port) if err != nil { return nil @@ -471,6 +574,9 @@ func (r *Router) loopClosed(loop routing.Loop) error { } func (r *Router) destroyLoop(loop routing.Loop) error { + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) + if r == nil { return nil } @@ -490,21 +596,21 @@ func (r *Router) destroyLoop(loop routing.Loop) error { } func (r *Router) setupProto(ctx context.Context) (*setup.Protocol, transport.Transport, error) { - r.Logger.Infof("[Router.setupProto] for %v enter\n", r.config.TransportType) + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) if len(r.config.SetupNodes) == 0 { r.Logger.Warn("[Router.setupProto] r.setupProto: route setup: no nodes ") return nil, nil, errors.New("route setup: no nodes") } - tr, err := r.tm.CreateSetupTransport(ctx, r.config.SetupNodes[0], r.config.TransportType) + tr, err := r.tm.CreateSetupTransport(ctx, r.config.SetupNodes[0], dmsg.Type) if err != nil { r.Logger.Warnf("[Router.setupProto] r.setupProto r.tm.CreateSetupTransport error:%v\n", err) return nil, nil, fmt.Errorf("setup transport: %s", err) } sProto := setup.NewSetupProtocol(tr) - r.Logger.Infof("[Router.setupProto] success") return sProto, tr, nil } diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index cb96e1612..6721f4d52 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -108,6 +108,9 @@ func TestRouterForwarding(t *testing.T) { require.NoError(t, <-errCh) } + + + func TestRouterAppInit(t *testing.T) { client := transport.NewDiscoveryMock() logStore := transport.InMemoryTransportLogStore() diff --git a/pkg/routing/packet.go b/pkg/routing/packet.go index 589120f08..16c2a5c36 100644 --- a/pkg/routing/packet.go +++ b/pkg/routing/packet.go @@ -3,6 +3,8 @@ package routing import ( "encoding/binary" "math" + + "github.com/skycoin/skywire/internal/testhelpers" ) // RouteID represents ID of a Route in a Packet. @@ -14,6 +16,9 @@ type Packet []byte // MakePacket constructs a new Packet. If payload size is more than // uint16, MakePacket will panic. func MakePacket(id RouteID, payload []byte) Packet { + log.Debug(testhelpers.Trace("ENTER")) + defer log.Debug(testhelpers.Trace("ENTER")) + if len(payload) > math.MaxUint16 { panic("packet size exceeded") } diff --git a/pkg/transport/managed_transport.go b/pkg/transport/managed_transport.go index 66a675fa5..6692c14d2 100644 --- a/pkg/transport/managed_transport.go +++ b/pkg/transport/managed_transport.go @@ -53,6 +53,7 @@ func (tr *ManagedTransport) Read(p []byte) (n int, err error) { // Write writes to an underlying transport. func (tr *ManagedTransport) Write(p []byte) (n int, err error) { + tr.mu.RLock() n, err = tr.Transport.Write(p) if n > 0 { diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 34610f4e7..87a8b243e 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -93,8 +93,13 @@ func (tm *Manager) SetSetupNodes(nodes []cipher.PubKey) { tm.setupNodes = nodes } -// Factories returns all the factory types contained within the TransportManager. -func (tm *Manager) Factories() []string { +// Factories returns all factories +func (tm *Manager) Factories() map[string]Factory { + return tm.factories +} + +// FactoryTypes returns all the factory types contained within the TransportManager. +func (tm *Manager) FactoryTypes() []string { fTypes, i := make([]string, len(tm.factories)), 0 for _, f := range tm.factories { fTypes[i], i = f.Type(), i+1 diff --git a/pkg/transport/manager_test.go b/pkg/transport/manager_test.go index 9718052f3..eeff2643e 100644 --- a/pkg/transport/manager_test.go +++ b/pkg/transport/manager_test.go @@ -47,7 +47,7 @@ func TestTransportManager(t *testing.T) { m1, err := NewManager(c1, f1) require.NoError(t, err) - assert.Equal(t, []string{"mock"}, m1.Factories()) + assert.Equal(t, []string{"mock"}, m1.FactoryTypes()) errCh := make(chan error) go func() { @@ -143,7 +143,7 @@ func TestTransportManagerReEstablishTransports(t *testing.T) { f1, f2 := NewMockFactoryPair(pk1, pk2) m1, err := NewManager(c1, f1) require.NoError(t, err) - assert.Equal(t, []string{"mock"}, m1.Factories()) + assert.Equal(t, []string{"mock"}, m1.FactoryTypes()) m1.reconnectTransports(context.TODO()) @@ -208,7 +208,7 @@ func TestTransportManagerLogs(t *testing.T) { m1, err := NewManager(c1, f1) require.NoError(t, err) - assert.Equal(t, []string{"mock"}, m1.Factories()) + assert.Equal(t, []string{"mock"}, m1.FactoryTypes()) errCh := make(chan error) go func() { diff --git a/pkg/transport/mock.go b/pkg/transport/mock.go index da97bf6c1..bdc4820fb 100644 --- a/pkg/transport/mock.go +++ b/pkg/transport/mock.go @@ -28,6 +28,7 @@ type MockFactory struct { fType string } + // NewMockFactoryPair constructs a pair of MockFactories. func NewMockFactoryPair(local, remote cipher.PubKey) (*MockFactory, *MockFactory) { var ( diff --git a/pkg/transport/tcp_transport.go b/pkg/transport/tcp_transport.go index 88a483ef8..fd03ac2ae 100644 --- a/pkg/transport/tcp_transport.go +++ b/pkg/transport/tcp_transport.go @@ -12,6 +12,7 @@ import ( "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/internal/testhelpers" ) // ErrUnknownRemote returned for connection attempts for remotes @@ -28,6 +29,8 @@ type TCPFactory struct { // NewTCPFactory constructs a new TCP Factory func NewTCPFactory(pk cipher.PubKey, pubkeysFile string, tcpAddr string, logger *logging.Logger) (Factory, error) { + logger.Debug(testhelpers.Trace("ENTER")) + defer logger.Debug(testhelpers.Trace("EXIT")) pkTbl, err := FilePubKeyTable(pubkeysFile) if err != nil { @@ -48,9 +51,13 @@ func NewTCPFactory(pk cipher.PubKey, pubkeysFile string, tcpAddr string, logger return &TCPFactory{pk, pkTbl, tcpListener, logger}, nil } +var _TR = testhelpers.Trace + // Accept accepts a remotely-initiated Transport. func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { - f.Logger.Info("[TCPFactory.Accept] enter") + f.Logger.Info(_TR("ENTER")) + defer f.Logger.Info(_TR("EXIT")) + conn, err := f.Lsr.AcceptTCP() if err != nil { f.Logger.Warnf("[TCPFactory.Accept] f.Lsr.AcceptTCP err: %v\n", err) @@ -71,7 +78,9 @@ func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { // Dial initiates a Transport with a remote node. func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, error) { - f.Logger.Info("[TCPFactory.Dial] enter") + f.Logger.Info(_TR("ENTER")) + defer f.Logger.Info(_TR("EXIT")) + addr := f.PkTable.RemoteAddr(remote) if addr == "" { f.Logger.Warnf("[TCPFactory.Dial] f.PkTable.RemoteAddr(remote) is empty for %v\n", remote) @@ -101,12 +110,15 @@ func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, f.Logger.Warnf("[TCPFactory.Dial] net.DialTCP err: %v\n", err) return nil, err } - f.Logger.Info("[TCPFactory.Dial] success") + return &TCPTransport{conn, f.Pk, remote}, nil } // Close implements io.Closer func (f *TCPFactory) Close() error { + f.Logger.Debug(testhelpers.Trace("ENTER")) + defer f.Logger.Debug(testhelpers.Trace("EXIT")) + if f == nil { return nil } @@ -132,6 +144,7 @@ type TCPTransport struct { // LocalPK returns the TCPTransport local public key. func (tr *TCPTransport) LocalPK() cipher.PubKey { + return tr.localKey } @@ -142,7 +155,7 @@ func (tr *TCPTransport) RemotePK() cipher.PubKey { // Type returns the string representation of the transport type. func (tr *TCPTransport) Type() string { - return "tcp" + return "tcp-transport" } // PubKeyTable provides translation between remote PubKey and TCPAddr. @@ -169,7 +182,7 @@ func memoryPubKeyTable(entries map[cipher.PubKey]string) *memPKTable { return &memPKTable{entries, reverse} } -// MemoryPKTable returns in memory implementation of the PubKeyTable. +// MemoryPubKeyTable returns in memory implementation of the PubKeyTable. func MemoryPubKeyTable(entries map[cipher.PubKey]string) PubKeyTable { return memoryPubKeyTable(entries) } diff --git a/pkg/visor/multihead_test.go b/pkg/visor/multihead_test.go index 384932371..c034e3880 100644 --- a/pkg/visor/multihead_test.go +++ b/pkg/visor/multihead_test.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "path/filepath" + "regexp" "strings" "sync" "time" @@ -169,8 +170,10 @@ func (mh *MultiHead) initNodes() { var err error for i := 0; i < len(mh.nodes); i++ { - logger := NewTaggedMasterLogger(fmt.Sprintf("[node_%03d]", i+1), subs) + logger := NewTaggedMasterLogger(mh.ipPool[i], subs) logger.Out = mh.Log + logger.SetReportCaller(true) + // logger.SetFormatter(new(logrus.JSONFormatter)) mh.nodes[i], err = NewNode(&mh.cfgPool[i], logger) if err != nil { mh.initErrs <- fmt.Errorf("error %v starting node %v", err, i) @@ -296,38 +299,74 @@ func Example_genPubKeysFile() { // Output: ZZZ } -func ExampleMultiHead_sendMessage_local() { +func (mhl *multiheadLog) filter(pattern string, match bool) []string { + var result []string + r := regexp.MustCompile(pattern) + + for _, line := range mhl.records { + matched := r.MatchString(line) + if (matched && match) || (!matched && !match) { + result = append(result, line) + } + } + return result +} + +func ExampleMultiHead_sendMessage_msg001() { mh := makeMultiHeadN(1) mh.startNodes(time.Second) - _, err := mh.sendMessage(0, 0, "Hello") + _, err := mh.sendMessage(0, 0, "Hello001") fmt.Printf("err: %v", err) mh.stopNodes(time.Second * 3) fmt.Printf("%v\n", mh.errReport()) - // printLogger := logging.MustGetLogger("test") - // printLogger.Infof("%v\n", mh.Log.records) - fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) + + recs := mh.Log.filter(`skyhost_001.*received.*"message"`, true) + if len(recs) == 1 { + fmt.Println("skyhost_001 recieved message") + } // Output: err: // init errors: 0 // start errors: 0 // stop errors: 0 + // skyhost_001 recieved message } // WIP -func ExampleMultiHead_sendMessage_remote() { +func ExampleMultiHead_sendMessage_msg002() { mh := makeMultiHeadN(2) mh.startNodes(time.Second) - _, err := mh.sendMessage(0, 1, "Hello") + _, err := mh.sendMessage(0, 1, "Hello002") + fmt.Printf("err: %v", err) + + mh.stopNodes(time.Second) + fmt.Printf("%v\n", mh.errReport()) + + fmt.Printf("%v", mh.Log.filter(`skychat`, false)) + // fmt.Printf("%v\n", strings.Join(mh.Log.records[20:], "")) + + // fmt.Printf("%v", mh.Log.filter(`skychat`, true)) + + // Output: err: + // init errors: 0 + // start errors: 0 + // stop errors: 0 + +} + +func ExampleMultiHead_sendMessage_msg003() { + mh := makeMultiHeadN(1) + mh.startNodes(time.Second) + + _, err := mh.sendMessage(0, 0, "Hello003") fmt.Printf("err: %v", err) mh.stopNodes(time.Second) fmt.Printf("%v\n", mh.errReport()) - // printLogger := logging.MustGetLogger("test") - // printLogger.Infof("%v\n", mh.Log.records) fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) // Output: err: diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index bf16a4ea1..e1e336c4f 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -129,7 +129,7 @@ func (r *RPC) SetAutoStart(in *SetAutoStartIn, _ *struct{}) error { // TransportTypes lists all transport types supported by the Node. func (r *RPC) TransportTypes(_ *struct{}, out *[]string) error { - *out = r.node.tm.Factories() + *out = r.node.tm.FactoryTypes() return nil } diff --git a/pkg/visor/taggedformatter.go b/pkg/visor/taggedformatter.go index b1383cb48..090f24285 100644 --- a/pkg/visor/taggedformatter.go +++ b/pkg/visor/taggedformatter.go @@ -3,9 +3,11 @@ package visor import ( "bytes" "os" + "time" "github.com/sirupsen/logrus" "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/internal/testhelpers" ) // TaggedFormatter appends tag to log records and substitutes text @@ -21,7 +23,8 @@ func (tf *TaggedFormatter) Format(entry *logrus.Entry) ([]byte, error) { for _, sub := range tf.subs { data = bytes.ReplaceAll(data, sub.old, sub.new) } - return append(tf.tag, data...), err + prepend := bytes.Repeat([]byte(" "), testhelpers.CallerDepth()) + return bytes.Join([][]byte{tf.tag, prepend, data}, []byte(" ")), err } // NewTaggedMasterLogger creates MasterLogger that prepends records with tag @@ -42,16 +45,18 @@ func NewTaggedMasterLogger(tag string, ssubs []struct{ old, new string }) *loggi []byte(tag), bsubs, &logging.TextFormatter{ - FullTimestamp: true, AlwaysQuoteStrings: true, QuoteEmptyFields: true, + FullTimestamp: true, ForceFormatting: true, DisableColors: false, ForceColors: false, + TimestampFormat: time.StampMicro, }, }, - Hooks: hooks, - Level: logrus.DebugLevel, + Hooks: hooks, + Level: logrus.DebugLevel, + ReportCaller: true, }, } } diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 1be9f8a05..40e3881fe 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -207,7 +207,7 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) RoutingTable: node.rt, RouteFinder: rtfClient, SetupNodes: setupNodes, - TransportType: config.TransportType, + // TransportType: config.TransportType, } r := router.New(rConfig) node.router = r From 1d31171077e3df26c874f417c6098aec4aff07e1 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Mon, 19 Aug 2019 15:05:52 +0300 Subject: [PATCH 19/26] yet another step forward --- Makefile | 4 + pkg/app/app.go | 66 ++++++++++------ pkg/app/app_test.go | 22 +++--- pkg/app/packet.go | 2 +- pkg/app/packet_test.go | 2 +- pkg/app/payload.go | 2 +- pkg/app/protocol.go | 1 + pkg/router/app_manager.go | 60 +++++++-------- pkg/router/app_manager_test.go | 8 +- pkg/router/loop_list.go | 1 - pkg/router/mockrouter.go | 4 +- pkg/router/route_manager.go | 6 +- pkg/router/route_manager_test.go | 16 ++-- pkg/router/router.go | 124 ++++++++++++++++--------------- pkg/router/router_close_test.go | 2 +- pkg/router/router_setup_test.go | 6 +- pkg/router/router_test.go | 7 +- pkg/routing/loop.go | 12 +-- pkg/setup/node.go | 6 +- pkg/setup/node_test.go | 4 +- pkg/visor/taggedformatter.go | 3 +- pkg/visor/visor.go | 4 + 22 files changed, 195 insertions(+), 167 deletions(-) diff --git a/Makefile b/Makefile index 1e2f303b1..a0bd3e185 100644 --- a/Makefile +++ b/Makefile @@ -98,6 +98,10 @@ bin: ## Build `skywire-visor`, `skywire-cli`, `hypervisor`, `SSH-cli` ${OPTS} go build ${BUILD_OPTS} -o ./hypervisor ./cmd/hypervisor ${OPTS} go build ${BUILD_OPTS} -o ./SSH-cli ./cmd/therealssh-cli +minibuild: + ${OPTS} go build ${BUILD_OPTS} -o ./apps/skychat.v1.0 ./cmd/apps/skychat + ${OPTS} go build ${BUILD_OPTS} -o ./skywire-visor ./cmd/skywire-visor + release: ## Build `skywire-visor`, `skywire-cli`, `hypervisor`, `SSH-cli` and apps without -race flag ${OPTS} go build -o ./skywire-visor ./cmd/skywire-visor diff --git a/pkg/app/app.go b/pkg/app/app.go index a58811694..3f5bf747f 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -13,6 +13,7 @@ import ( "os/exec" "path/filepath" "sync" + "sync/atomic" "github.com/skycoin/skycoin/src/util/logging" @@ -48,7 +49,7 @@ type App struct { acceptChan chan [2]routing.Addr doneChan chan struct{} - conns map[routing.Loop]io.ReadWriteCloser + conns map[routing.AddrLoop]io.ReadWriteCloser mu sync.Mutex } @@ -80,7 +81,7 @@ func SetupFromPipe(config *Config, inFD, outFD uintptr) (*App, error) { proto: NewProtocol(pipeConn), acceptChan: make(chan [2]routing.Addr), doneChan: make(chan struct{}), - conns: make(map[routing.Loop]io.ReadWriteCloser), + conns: make(map[routing.AddrLoop]io.ReadWriteCloser), } go app.handleProto() @@ -141,8 +142,8 @@ func (app *App) Accept() (net.Conn, error) { raddr := addrs[1] // TODO: Why is that? - loop := routing.Loop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} - // loop := routing.Loop{laddr, raddr} + loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} + // loop := routing.AddrLoop{Local: laddr, Remote: raddr} conn, out := net.Pipe() app.mu.Lock() app.conns[loop] = conn @@ -161,7 +162,11 @@ func (app *App) Dial(raddr routing.Addr) (net.Conn, error) { if err != nil { return nil, err } - loop := routing.Loop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} + + // TODO: Why is that way? + loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} + // loop := routing.AddrLoop{Local: laddr, Remote: raddr} + conn, out := net.Pipe() app.mu.Lock() app.conns[loop] = conn @@ -175,26 +180,36 @@ func (app *App) Addr() net.Addr { return routing.Addr{} } +func (app *App) protoHandler(frame Frame, payload []byte) (res interface{}, err error) { + log.Debug(testhelpers.Trace("ENTER")) + defer log.Debug(testhelpers.Trace("EXIT")) + + switch frame { + case FrameConfirmLoop: + err = app.confirmLoop(payload) + case FrameSend: + err = app.forwardPacket(payload) + case FrameClose: + err = app.closeConn(payload) + default: + err = errors.New("unexpected frame") + } + return res, err +} + func (app *App) handleProto() { - err := app.proto.Serve(func(frame Frame, payload []byte) (res interface{}, err error) { - switch frame { - case FrameConfirmLoop: - err = app.confirmLoop(payload) - case FrameSend: - err = app.forwardPacket(payload) - case FrameClose: - err = app.closeConn(payload) - default: - err = errors.New("unexpected frame") - } - return res, err - }) + err := app.proto.Serve(app.protoHandler) if err != nil { return } } -func (app *App) serveConn(loop routing.Loop, conn io.ReadWriteCloser) { +func (app *App) serveConn(loop routing.AddrLoop, conn io.ReadWriteCloser) { + log.Debug(testhelpers.Trace("ENTER")) + defer log.Debug(testhelpers.Trace("EXIT")) + + var cntr uint64 + defer func() { if err := conn.Close(); err != nil { log.WithError(err).Warn("failed to close connection") @@ -202,6 +217,9 @@ func (app *App) serveConn(loop routing.Loop, conn io.ReadWriteCloser) { }() for { + atomic.AddUint64(&cntr, 1) + log.Debugf("%v CYCLE %03d START", testhelpers.GetCaller(), cntr) + buf := make([]byte, 32*1024) n, err := conn.Read(buf) if err != nil { @@ -225,6 +243,9 @@ func (app *App) serveConn(loop routing.Loop, conn io.ReadWriteCloser) { } func (app *App) forwardPacket(data []byte) error { + log.Debug(testhelpers.Trace("ENTER")) + defer log.Debug(testhelpers.Trace("EXIT")) + packet := &Packet{} if err := json.Unmarshal(data, packet); err != nil { return err @@ -243,7 +264,10 @@ func (app *App) forwardPacket(data []byte) error { } func (app *App) closeConn(data []byte) error { - var loop routing.Loop + log.Debug(testhelpers.Trace("ENTER")) + defer log.Debug(testhelpers.Trace("EXIT")) + + var loop routing.AddrLoop if err := json.Unmarshal(data, &loop); err != nil { return err } @@ -272,7 +296,7 @@ func (app *App) confirmLoop(data []byte) error { raddr := addrs[1] app.mu.Lock() - conn := app.conns[routing.Loop{Local: laddr, Remote: raddr}] + conn := app.conns[routing.AddrLoop{Local: laddr, Remote: raddr}] app.mu.Unlock() if conn != nil { diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 54759563a..99b3d2146 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -39,7 +39,7 @@ func TestAppDial(t *testing.T) { in, out := net.Pipe() proto := NewProtocol(out) - app := &App{proto: NewProtocol(in), conns: make(map[routing.Loop]io.ReadWriteCloser)} + app := &App{proto: NewProtocol(in), conns: make(map[routing.AddrLoop]io.ReadWriteCloser)} go app.handleProto() dataCh := make(chan []byte) @@ -65,13 +65,13 @@ func TestAppDial(t *testing.T) { assert.Equal(t, rpk.Hex()+":3", conn.RemoteAddr().String()) assert.Equal(t, lpk.Hex()+":2", conn.LocalAddr().String()) - require.NotNil(t, app.conns[routing.Loop{Local: routing.Addr{Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}]) + require.NotNil(t, app.conns[routing.AddrLoop{Local: routing.Addr{Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}]) require.NoError(t, conn.Close()) // Justified. Attempt to remove produces: FAIL time.Sleep(100 * time.Millisecond) - var loop routing.Loop + var loop routing.AddrLoop require.NoError(t, json.Unmarshal(<-dataCh, &loop)) assert.Equal(t, routing.Port(2), loop.Local.Port) assert.Equal(t, rpk, loop.Remote.PubKey) @@ -88,7 +88,7 @@ func TestAppAccept(t *testing.T) { lpk, _ := cipher.GenerateKeyPair() rpk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() - app := &App{proto: NewProtocol(in), acceptChan: make(chan [2]routing.Addr, 2), conns: make(map[routing.Loop]io.ReadWriteCloser)} + app := &App{proto: NewProtocol(in), acceptChan: make(chan [2]routing.Addr, 2), conns: make(map[routing.AddrLoop]io.ReadWriteCloser)} go app.handleProto() proto := NewProtocol(out) @@ -139,7 +139,7 @@ func TestAppWrite(t *testing.T) { appIn, appOut := net.Pipe() app := &App{proto: NewProtocol(in)} go app.handleProto() - go app.serveConn(routing.Loop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}, appIn) + go app.serveConn(routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}, appIn) proto := NewProtocol(out) dataCh := make(chan []byte) @@ -178,7 +178,7 @@ func TestAppRead(t *testing.T) { pk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() appIn, appOut := net.Pipe() - app := &App{proto: NewProtocol(in), conns: map[routing.Loop]io.ReadWriteCloser{routing.Loop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}}: appIn}} + app := &App{proto: NewProtocol(in), conns: map[routing.AddrLoop]io.ReadWriteCloser{routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}}: appIn}} go app.handleProto() proto := NewProtocol(out) @@ -189,7 +189,7 @@ func TestAppRead(t *testing.T) { errCh := make(chan error) go func() { - errCh <- proto.Send(FrameSend, &Packet{routing.Loop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}}, []byte("foo")}, nil) + errCh <- proto.Send(FrameSend, &Packet{routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}}, []byte("foo")}, nil) }() buf := make([]byte, 3) @@ -246,7 +246,7 @@ func TestAppCloseConn(t *testing.T) { rpk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() appIn, appOut := net.Pipe() - app := &App{proto: NewProtocol(in), conns: map[routing.Loop]io.ReadWriteCloser{routing.Loop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}: appIn}} + app := &App{proto: NewProtocol(in), conns: map[routing.AddrLoop]io.ReadWriteCloser{routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}: appIn}} go app.handleProto() proto := NewProtocol(out) @@ -257,7 +257,7 @@ func TestAppCloseConn(t *testing.T) { errCh := make(chan error) go func() { - errCh <- proto.Send(FrameClose, routing.Loop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}, nil) + errCh <- proto.Send(FrameClose, routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}, nil) }() _, err := appOut.Read(make([]byte, 3)) @@ -273,7 +273,7 @@ func TestAppClose(t *testing.T) { rpk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() appIn, appOut := net.Pipe() - app := &App{proto: NewProtocol(in), conns: map[routing.Loop]io.ReadWriteCloser{routing.Loop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}: appIn}, doneChan: make(chan struct{})} + app := &App{proto: NewProtocol(in), conns: map[routing.AddrLoop]io.ReadWriteCloser{routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}: appIn}, doneChan: make(chan struct{})} go app.handleProto() proto := NewProtocol(out) @@ -296,7 +296,7 @@ func TestAppClose(t *testing.T) { _, err := appOut.Read(make([]byte, 3)) require.Equal(t, io.EOF, err) - var loop routing.Loop + var loop routing.AddrLoop require.NoError(t, json.Unmarshal(<-dataCh, &loop)) assert.Equal(t, lpk, loop.Local.PubKey) assert.Equal(t, routing.Port(2), loop.Local.Port) diff --git a/pkg/app/packet.go b/pkg/app/packet.go index f77ada615..7d0aa8702 100644 --- a/pkg/app/packet.go +++ b/pkg/app/packet.go @@ -4,6 +4,6 @@ import "github.com/skycoin/skywire/pkg/routing" // Packet represents message exchanged between App and Node. type Packet struct { - Loop routing.Loop `json:"loop"` + Loop routing.AddrLoop `json:"loop"` Payload []byte `json:"payload"` } diff --git a/pkg/app/packet_test.go b/pkg/app/packet_test.go index 7990a166b..4a40b5708 100644 --- a/pkg/app/packet_test.go +++ b/pkg/app/packet_test.go @@ -12,7 +12,7 @@ func ExamplePacket() { var lpk, rpk cipher.PubKey laddr := routing.Addr{Port: 0, PubKey: lpk} raddr := routing.Addr{Port: 0, PubKey: rpk} - loop := routing.Loop{Local: laddr, Remote: raddr} + loop := routing.AddrLoop{Local: laddr, Remote: raddr} fmt.Println(raddr.Network()) fmt.Printf("%v\n", raddr) diff --git a/pkg/app/payload.go b/pkg/app/payload.go index 508365d5d..cb1972d65 100644 --- a/pkg/app/payload.go +++ b/pkg/app/payload.go @@ -34,7 +34,7 @@ func (p Payload) String() string { } return fmt.Sprintf("{FrameSend: %s}", packet) case FrameClose: - var loop routing.Loop + var loop routing.AddrLoop if err := json.Unmarshal(p.Data, &loop); err != nil { return fmt.Sprintf("{FrameClose. Error: %v, data: %v}", err, p.Data) } diff --git a/pkg/app/protocol.go b/pkg/app/protocol.go index d044123f0..ee903e35a 100644 --- a/pkg/app/protocol.go +++ b/pkg/app/protocol.go @@ -87,6 +87,7 @@ func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { } if Frame(frame[0]) == FrameFailure { + log.Warnf("%v writeFrame err: %v\n", testhelpers.GetCaller(), string(frame[2:])) return errors.New(string(frame[2:])) } diff --git a/pkg/router/app_manager.go b/pkg/router/app_manager.go index afd72b800..ea4a6a1ec 100644 --- a/pkg/router/app_manager.go +++ b/pkg/router/app_manager.go @@ -13,12 +13,6 @@ import ( const supportedProtocolVersion = "0.0.1" -// type appCallbacks struct { -// CreateLoop func(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) -// CloseLoop func(conn *app.Protocol, loop routing.Loop) error -// ForwardAppPacket func(conn *app.Protocol, packet *app.Packet) error -// } - type appManager struct { Logger *logging.Logger router PacketRouter @@ -27,35 +21,35 @@ type appManager struct { // callbacks *appCallbacks } +func (am *appManager) serveHandler(frame app.Frame, payload []byte) (res interface{}, err error) { + am.Logger.WithField("payload", app.Payload{frame, payload}). + Info(testhelpers.Trace("ENTER")) + defer am.Logger.Info(testhelpers.Trace("EXIT")) + + switch frame { + case app.FrameInit: + err = am.initApp(payload) + case app.FrameCreateLoop: + res, err = am.setupLoop(payload) + case app.FrameClose: + err = am.handleCloseLoop(payload) + case app.FrameSend: + err = am.forwardAppPacket(payload) + default: + err = errors.New("unexpected frame") + } + + if err != nil { + am.Logger.Warnf("%v: App request with type %s failed: %+v\n", testhelpers.GetCaller(), frame, err) + } + + return res, err +} + func (am *appManager) Serve() error { am.Logger.Info(testhelpers.Trace("EXIT")) defer am.Logger.Info(testhelpers.Trace("EXIT")) - - // Protocol.Serve loop start - return am.proto.Serve(func(frame app.Frame, payload []byte) (res interface{}, err error) { - am.Logger.WithField("payload", app.Payload{frame, payload}). - Info(testhelpers.Trace("ENTER")) - defer am.Logger.Info(testhelpers.Trace("EXIT")) - - switch frame { - case app.FrameInit: - err = am.initApp(payload) - case app.FrameCreateLoop: - res, err = am.setupLoop(payload) - case app.FrameClose: - err = am.handleCloseLoop(payload) - case app.FrameSend: - err = am.forwardAppPacket(payload) - default: - err = errors.New("unexpected frame") - } - - if err != nil { - am.Logger.Warnf("%v: App request with type %s failed: %+v\n", testhelpers.GetCaller(), frame, err) - } - - return res, err - }) + return am.proto.Serve(am.serveHandler) } func (am *appManager) initApp(payload []byte) error { @@ -96,7 +90,7 @@ func (am *appManager) handleCloseLoop(payload []byte) error { am.Logger.Info(testhelpers.Trace("ENTER")) defer am.Logger.Info(testhelpers.Trace("EXIT")) - var loop routing.Loop + var loop routing.AddrLoop if err := json.Unmarshal(payload, &loop); err != nil { return err } diff --git a/pkg/router/app_manager_test.go b/pkg/router/app_manager_test.go index 122b7955f..e34d188f5 100644 --- a/pkg/router/app_manager_test.go +++ b/pkg/router/app_manager_test.go @@ -92,7 +92,7 @@ func TestAppManagerSetupLoop(t *testing.T) { func TestAppManagerCloseLoop(t *testing.T) { in, out := net.Pipe() - // var inLoop routing.Loop + // var inLoop routing.AddrLoop r := new(MockRouter) am := &appManager{ Logger: logging.MustGetLogger("routesetup"), @@ -100,7 +100,7 @@ func TestAppManagerCloseLoop(t *testing.T) { proto: app.NewProtocol(out), appConf: &app.Config{AppName: "foo", AppVersion: "0.0.1"}, // &appCallbacks{ - // CloseLoop: func(conn *app.Protocol, loop routing.Loop) error { + // CloseLoop: func(conn *app.Protocol, loop routing.AddrLoop) error { // inLoop = loop // return nil // }, @@ -118,7 +118,7 @@ func TestAppManagerCloseLoop(t *testing.T) { lpk, _ := cipher.GenerateKeyPair() rpk, _ := cipher.GenerateKeyPair() - loop := routing.Loop{ + loop := routing.AddrLoop{ Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}} err := proto.Send(app.FrameClose, loop, nil) @@ -153,7 +153,7 @@ func TestAppManagerForward(t *testing.T) { lpk, _ := cipher.GenerateKeyPair() rpk, _ := cipher.GenerateKeyPair() packet := &app.Packet{Payload: []byte("foo"), - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}} err := proto.Send(app.FrameSend, packet, nil) diff --git a/pkg/router/loop_list.go b/pkg/router/loop_list.go index 4a4ad08ba..e0a2a3b8c 100644 --- a/pkg/router/loop_list.go +++ b/pkg/router/loop_list.go @@ -4,7 +4,6 @@ import ( "sync" "github.com/google/uuid" - "github.com/skycoin/skywire/pkg/routing" ) diff --git a/pkg/router/mockrouter.go b/pkg/router/mockrouter.go index d68698276..d6fefbfaf 100644 --- a/pkg/router/mockrouter.go +++ b/pkg/router/mockrouter.go @@ -22,7 +22,7 @@ type MockRouter struct { errChan chan error inPacket *app.Packet - inLoop routing.Loop + inLoop routing.AddrLoop } var n *MockRouter @@ -80,7 +80,7 @@ func (r *MockRouter) IsSetupTransport(tr *transport.ManagedTransport) bool { } // CloseLoop implements PacketRouter.CloseLoop -func (r *MockRouter) CloseLoop(conn *app.Protocol, loop routing.Loop) error { +func (r *MockRouter) CloseLoop(conn *app.Protocol, loop routing.AddrLoop) error { r.inLoop = loop return nil } diff --git a/pkg/router/route_manager.go b/pkg/router/route_manager.go index d944c56ef..d68182177 100644 --- a/pkg/router/route_manager.go +++ b/pkg/router/route_manager.go @@ -14,8 +14,8 @@ import ( ) type setupCallbacks struct { - ConfirmLoop func(loop routing.Loop, rule routing.Rule) (err error) - LoopClosed func(loop routing.Loop) error + ConfirmLoop func(loop routing.AddrLoop, rule routing.Rule) (err error) + LoopClosed func(loop routing.AddrLoop) error } type routeManager struct { @@ -42,7 +42,7 @@ func (rm *routeManager) GetRule(routeID routing.RouteID) (routing.Rule, error) { return rule, nil } -func (rm *routeManager) RemoveLoopRule(loop routing.Loop) error { +func (rm *routeManager) RemoveLoopRule(loop routing.AddrLoop) error { var appRouteID routing.RouteID var appRule routing.Rule err := rm.rt.RangeRules(func(routeID routing.RouteID, rule routing.Rule) bool { diff --git a/pkg/router/route_manager_test.go b/pkg/router/route_manager_test.go index 981f41967..200a1cca5 100644 --- a/pkg/router/route_manager_test.go +++ b/pkg/router/route_manager_test.go @@ -47,11 +47,11 @@ func TestRouteManagerRemoveLoopRule(t *testing.T) { _, err := rt.AddRule(rule) require.NoError(t, err) - loop := routing.Loop{Local: routing.Addr{Port: 3}, Remote: routing.Addr{PubKey: pk, Port: 3}} + loop := routing.AddrLoop{Local: routing.Addr{Port: 3}, Remote: routing.Addr{PubKey: pk, Port: 3}} require.NoError(t, rm.RemoveLoopRule(loop)) assert.Equal(t, 1, rt.Count()) - loop = routing.Loop{Local: routing.Addr{Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}} + loop = routing.AddrLoop{Local: routing.Addr{Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}} require.NoError(t, rm.RemoveLoopRule(loop)) assert.Equal(t, 0, rt.Count()) } @@ -120,10 +120,10 @@ func TestRouteManagerDeleteRules(t *testing.T) { func TestRouteManagerConfirmLoop(t *testing.T) { rt := manageRoutingTable(routing.InMemoryRoutingTable()) - var inLoop routing.Loop + var inLoop routing.AddrLoop var inRule routing.Rule callbacks := &setupCallbacks{ - ConfirmLoop: func(loop routing.Loop, rule routing.Rule) (err error) { + ConfirmLoop: func(loop routing.AddrLoop, rule routing.Rule) (err error) { inLoop = loop inRule = rule return nil @@ -146,7 +146,7 @@ func TestRouteManagerConfirmLoop(t *testing.T) { require.NoError(t, rt.SetRule(1, rule)) ld := routing.LoopData{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Remote: routing.Addr{ PubKey: pk, Port: 3, @@ -170,9 +170,9 @@ func TestRouteManagerConfirmLoop(t *testing.T) { func TestRouteManagerLoopClosed(t *testing.T) { rt := manageRoutingTable(routing.InMemoryRoutingTable()) - var inLoop routing.Loop + var inLoop routing.AddrLoop callbacks := &setupCallbacks{ - LoopClosed: func(loop routing.Loop) error { + LoopClosed: func(loop routing.AddrLoop) error { inLoop = loop return nil }, @@ -196,7 +196,7 @@ func TestRouteManagerLoopClosed(t *testing.T) { require.NoError(t, rt.SetRule(1, rule)) ld := routing.LoopData{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Remote: routing.Addr{ PubKey: pk, Port: 3, diff --git a/pkg/router/router.go b/pkg/router/router.go index 370ccbd9d..d6c5ff9d8 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -51,7 +51,7 @@ type PacketRouter interface { ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error IsSetupTransport(tr *transport.ManagedTransport) bool CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) - CloseLoop(conn *app.Protocol, loop routing.Loop) error + CloseLoop(conn *app.Protocol, loop routing.AddrLoop) error ForwardAppPacket(conn *app.Protocol, packet *app.Packet) error } @@ -179,7 +179,7 @@ func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) for _, port := range r.pm.AppPorts(appProto) { for _, addr := range r.pm.Close(port) { - if err := r.closeLoop(appProto, routing.Loop{Local: routing.Addr{Port: port}, Remote: addr}); err != nil { + if err := r.closeLoop(appProto, routing.AddrLoop{Local: routing.Addr{Port: port}, Remote: addr}); err != nil { log.WithError(err).Warn("Failed to close loop") } } @@ -201,7 +201,7 @@ func (r *Router) CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routi } // CloseLoop implements PactetRouter.CloseLoop -func (r *Router) CloseLoop(conn *app.Protocol, loop routing.Loop) error { +func (r *Router) CloseLoop(conn *app.Protocol, loop routing.AddrLoop) error { return r.closeLoop(conn, loop) } @@ -285,7 +285,7 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { laddr := routing.Addr{Port: rule.LocalPort()} raddr := routing.Addr{PubKey: rule.RemotePK(), Port: rule.RemotePort()} - p := &app.Packet{Loop: routing.Loop{Local: laddr, Remote: raddr}, Payload: payload} + p := &app.Packet{Loop: routing.AddrLoop{Local: laddr, Remote: raddr}, Payload: payload} b, err := r.pm.Get(rule.LocalPort()) if err != nil { return err @@ -298,7 +298,7 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { return nil } -func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) error { +func (r *Router) forwardAppPacket(appConn *app.Protocol, appPacket *app.Packet) error { r.Logger.Info(testhelpers.Trace("ENTER")) defer r.Logger.Info(testhelpers.Trace("EXIT")) @@ -306,40 +306,33 @@ func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) err return errors.New("router not initialized") } - if packet.Loop.Remote.PubKey == r.config.PubKey { - return r.forwardLocalAppPacket(packet) + if appPacket.Loop.Remote.PubKey == r.config.PubKey { + return r.forwardLocalAppPacket(appPacket) } - // TODO: Check if exists TCPFactory in r.tm.Factories() and if raddr.PubKey exists in TCPFactory.PKTable - - r.Logger.Infof("%v ATTEMPT TO FIND %v", testhelpers.GetCaller(), packet.Loop.Remote.PubKey) - - for _, fct := range r.tm.Factories() { - switch tfct := fct.(type) { - case *transport.TCPFactory: - if ipAddr := tfct.PkTable.RemoteAddr(packet.Loop.Remote.PubKey); ipAddr != "" { - r.Logger.Infof("%v PK %v FOUND", testhelpers.GetCaller(), packet.Loop.Remote.PubKey) - return r.forwardTCPAppPacket(packet) - } - - } - } - - l, err := r.pm.GetLoop(packet.Loop.Local.Port, packet.Loop.Remote) + loop, err := r.pm.GetLoop(appPacket.Loop.Local.Port, appPacket.Loop.Remote) if err != nil { r.Logger.Warnf("[Router.forwardAppPacket] r.pm.GetLoop: %v", err) return err } - tr := r.tm.Transport(l.trID) + r.Logger.Debugf("%v found loop: %v", testhelpers.GetCaller(), loop) + + tr := r.tm.Transport(loop.trID) if tr == nil { r.Logger.Warnf("[Router.forwardAppPacket] r.tm.Transport: tr==nil") return errors.New("unknown transport") } - p := routing.MakePacket(l.routeID, packet.Payload) - r.Logger.Infof("[Router.forwardAppPacket] Forwarded App packet from LocalPort %d using route ID %d", packet.Loop.Local.Port, l.routeID) - _, err = tr.Write(p) + r.Logger.Debugf("%v ATTEMPT TO FIND %v", testhelpers.GetCaller(), appPacket.Loop.Remote.PubKey) + if r.isPubKeyLocal(appPacket.Loop.Remote.PubKey) { + r.Logger.Infof("%v isPubKeyLocal = true", testhelpers.GetCaller()) + // return r.forwardTCPAppPacket(packet) + } + + rPacket := routing.MakePacket(loop.routeID, appPacket.Payload) + r.Logger.Infof("[Router.forwardAppPacket] Forwarded App packet from LocalPort %d using route ID %d", appPacket.Loop.Local.Port, loop.routeID) + _, err = tr.Write(rPacket) if err != nil { r.Logger.Warnf("[Router.forwardAppPacket] tr.Write: %v", err) @@ -348,22 +341,22 @@ func (r *Router) forwardAppPacket(appConn *app.Protocol, packet *app.Packet) err return err } -func (r *Router) forwardLocalAppPacket(packet *app.Packet) error { +func (r *Router) forwardLocalAppPacket(appPacket *app.Packet) error { r.Logger.Info(testhelpers.Trace("ENTER")) defer r.Logger.Info(testhelpers.Trace("EXIT")) - b, err := r.pm.Get(packet.Loop.Remote.Port) + b, err := r.pm.Get(appPacket.Loop.Remote.Port) if err != nil { r.Logger.Warnf("%v r.pm.Get: %v\n", testhelpers.GetCaller(), err) return nil } p := &app.Packet{ - Loop: routing.Loop{ - Local: routing.Addr{Port: packet.Loop.Remote.Port}, - Remote: routing.Addr{PubKey: packet.Loop.Remote.PubKey, Port: packet.Loop.Local.Port}, + Loop: routing.AddrLoop{ + Local: routing.Addr{Port: appPacket.Loop.Remote.Port}, + Remote: routing.Addr{PubKey: appPacket.Loop.Remote.PubKey, Port: appPacket.Loop.Local.Port}, }, - Payload: packet.Payload, + Payload: appPacket.Payload, } errSend := b.conn.Send(app.FrameSend, p, nil) if errSend != nil { @@ -372,30 +365,45 @@ func (r *Router) forwardLocalAppPacket(packet *app.Packet) error { return errSend } -func (r *Router) forwardTCPAppPacket(packet *app.Packet) error { +func (r *Router) forwardTCPAppPacket(appPacket *app.Packet) error { r.Logger.Info(testhelpers.Trace("ENTER")) defer r.Logger.Info(testhelpers.Trace("EXIT")) - b, err := r.pm.Get(packet.Loop.Remote.Port) + b, err := r.pm.Get(appPacket.Loop.Remote.Port) if err != nil { r.Logger.Warnf("%v r.pm.Get: %v\n", testhelpers.GetCaller(), err) return nil } p := &app.Packet{ - Loop: routing.Loop{ - Local: routing.Addr{Port: packet.Loop.Remote.Port}, - Remote: routing.Addr{PubKey: packet.Loop.Remote.PubKey, Port: packet.Loop.Local.Port}, + Loop: routing.AddrLoop{ + Local: routing.Addr{Port: appPacket.Loop.Remote.Port}, + Remote: routing.Addr{PubKey: appPacket.Loop.Remote.PubKey, Port: appPacket.Loop.Local.Port}, }, - Payload: packet.Payload, + Payload: appPacket.Payload, } errSend := b.conn.Send(app.FrameSend, p, nil) if errSend != nil { - r.Logger.Info("%v b.conn.Send: %v\n", testhelpers.GetCaller(), err) + r.Logger.Warnf("%v b.conn.Send: %v\n", testhelpers.GetCaller(), err) } return errSend } +func (r *Router) isPubKeyLocal(pk cipher.PubKey) bool { + r.Logger.Debug(testhelpers.Trace("ENTER")) + defer r.Logger.Debug(testhelpers.Trace("EXIT")) + + for _, fct := range r.tm.Factories() { + switch tfct := fct.(type) { + case *transport.TCPFactory: + if ipAddr := tfct.PkTable.RemoteAddr(pk); ipAddr != "" { + return true + } + } + } + return false +} + func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing.Addr, error) { r.Logger.Info(testhelpers.Trace("ENTER")) defer r.Logger.Info(testhelpers.Trace("EXIT")) @@ -427,7 +435,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing } ld := routing.LoopDescriptor{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Local: laddr, Remote: raddr, }, @@ -436,18 +444,11 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing Reverse: reverseRoute, } - r.Logger.Infof("%v ATTEMPT TO FIND %v", testhelpers.GetCaller(), raddr.PubKey) - for _, fct := range r.tm.Factories() { - switch tfct := fct.(type) { - case *transport.TCPFactory: - if ipAddr := tfct.PkTable.RemoteAddr(raddr.PubKey); ipAddr != "" { - r.Logger.Infof("%v PK %v FOUND", testhelpers.GetCaller(), raddr.PubKey) - if err := r.confirmTCPLoop(laddr, raddr); err != nil { - r.Logger.Warnf("%v r.ConfirmTCPLoop err: %v", testhelpers.GetCaller(), err) - } - } - return laddr, nil + if r.isPubKeyLocal(raddr.PubKey) { + if err := r.confirmTCPLoop(laddr, raddr); err != nil { + r.Logger.Warnf("%v r.ConfirmTCPLoop err: %v", testhelpers.GetCaller(), err) } + return laddr, nil } proto, tr, err := r.setupProto(context.Background()) @@ -476,11 +477,13 @@ func (r *Router) confirmLocalLoop(laddr, raddr routing.Addr) error { b, err := r.pm.Get(raddr.Port) if err != nil { + r.Logger.Debugf("%v r.pm.Get %v", testhelpers.GetCaller(), err) return err } addrs := [2]routing.Addr{raddr, laddr} if err = b.conn.Send(app.FrameConfirmLoop, addrs, nil); err != nil { + r.Logger.Warnf("%v b.conn.Send %v", testhelpers.GetCaller(), err) return err } @@ -488,23 +491,26 @@ func (r *Router) confirmLocalLoop(laddr, raddr routing.Addr) error { } func (r *Router) confirmTCPLoop(laddr, raddr routing.Addr) error { - r.Logger.Info(testhelpers.Trace("ENTER")) + r.Logger.Debug(testhelpers.Trace("ENTER")) defer r.Logger.Info(testhelpers.Trace("EXIT")) - b, err := r.pm.Get(raddr.Port) + // portBind + pb, err := r.pm.Get(raddr.Port) if err != nil { + r.Logger.Warnf("%v r.pm.Get %v", testhelpers.GetCaller(), err) return err } addrs := [2]routing.Addr{raddr, laddr} - if err = b.conn.Send(app.FrameConfirmLoop, addrs, nil); err != nil { + if err = pb.conn.Send(app.FrameConfirmLoop, addrs, nil); err != nil { + r.Logger.Warnf("%v b.conn.Send %v", testhelpers.GetCaller(), err) return err } return nil } -func (r *Router) confirmLoop(l routing.Loop, rule routing.Rule) error { +func (r *Router) confirmLoop(l routing.AddrLoop, rule routing.Rule) error { r.Logger.Debug(testhelpers.Trace("ENTER")) defer r.Logger.Debug(testhelpers.Trace("EXIT")) @@ -525,7 +531,7 @@ func (r *Router) confirmLoop(l routing.Loop, rule routing.Rule) error { return nil } -func (r *Router) closeLoop(appConn *app.Protocol, loop routing.Loop) error { +func (r *Router) closeLoop(appConn *app.Protocol, loop routing.AddrLoop) error { r.Logger.Debug(testhelpers.Trace("ENTER")) defer r.Logger.Debug(testhelpers.Trace("EXIT")) @@ -552,7 +558,7 @@ func (r *Router) closeLoop(appConn *app.Protocol, loop routing.Loop) error { return nil } -func (r *Router) loopClosed(loop routing.Loop) error { +func (r *Router) loopClosed(loop routing.AddrLoop) error { r.Logger.Debug(testhelpers.Trace("ENTER")) defer r.Logger.Debug(testhelpers.Trace("EXIT")) @@ -573,7 +579,7 @@ func (r *Router) loopClosed(loop routing.Loop) error { return nil } -func (r *Router) destroyLoop(loop routing.Loop) error { +func (r *Router) destroyLoop(loop routing.AddrLoop) error { r.Logger.Debug(testhelpers.Trace("ENTER")) defer r.Logger.Debug(testhelpers.Trace("EXIT")) diff --git a/pkg/router/router_close_test.go b/pkg/router/router_close_test.go index 0710f6e6c..b62bd5150 100644 --- a/pkg/router/router_close_test.go +++ b/pkg/router/router_close_test.go @@ -103,7 +103,7 @@ func TestRouterCloseLoop(t *testing.T) { raddr := routing.Addr{PubKey: pk3, Port: 6} require.NoError(t, r.pm.SetLoop(5, raddr, &loop{})) - require.NoError(t, proto.Send(app.FrameClose, routing.Loop{Local: routing.Addr{Port: 5}, Remote: raddr}, nil)) + require.NoError(t, proto.Send(app.FrameClose, routing.AddrLoop{Local: routing.Addr{Port: 5}, Remote: raddr}, nil)) time.Sleep(100 * time.Millisecond) diff --git a/pkg/router/router_setup_test.go b/pkg/router/router_setup_test.go index 033a669cf..4cb6b7b79 100644 --- a/pkg/router/router_setup_test.go +++ b/pkg/router/router_setup_test.go @@ -120,7 +120,7 @@ func TestRouterSetup(t *testing.T) { require.NoError(t, err) err = setup.ConfirmLoop(sProto, routing.LoopData{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Remote: routing.Addr{ PubKey: pk2, Port: 1, @@ -166,7 +166,7 @@ func TestRouterSetup(t *testing.T) { require.NoError(t, err) err = setup.ConfirmLoop(sProto, routing.LoopData{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Remote: routing.Addr{ PubKey: pk2, Port: 3, @@ -208,7 +208,7 @@ func TestRouterSetup(t *testing.T) { assert.Equal(t, routing.RuleApp, rule.Type()) require.NoError(t, setup.LoopClosed(sProto, routing.LoopData{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Remote: routing.Addr{ PubKey: pk2, Port: 3, diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 6721f4d52..d5e414c51 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -108,9 +108,6 @@ func TestRouterForwarding(t *testing.T) { require.NoError(t, <-errCh) } - - - func TestRouterAppInit(t *testing.T) { client := transport.NewDiscoveryMock() logStore := transport.InMemoryTransportLogStore() @@ -220,7 +217,7 @@ func TestRouterApp(t *testing.T) { tr2 := m2.Transport(tr.Entry.ID) sendErrCh := make(chan error, 1) go func() { - sendErrCh <- proto.Send(app.FrameSend, &app.Packet{Loop: routing.Loop{Local: routing.Addr{Port: 6}, Remote: raddr}, Payload: []byte("bar")}, nil) + sendErrCh <- proto.Send(app.FrameSend, &app.Packet{Loop: routing.AddrLoop{Local: routing.Addr{Port: 6}, Remote: raddr}, Payload: []byte("bar")}, nil) }() packet := make(routing.Packet, 9) @@ -303,7 +300,7 @@ func TestRouterLocalApp(t *testing.T) { sendErrCh := make(chan error, 1) go func() { packet := &app.Packet{ - Loop: routing.Loop{Local: routing.Addr{Port: 5}, Remote: routing.Addr{PubKey: pk, Port: 6}}, Payload: []byte("foo"), + Loop: routing.AddrLoop{Local: routing.Addr{Port: 5}, Remote: routing.Addr{PubKey: pk, Port: 6}}, Payload: []byte("foo"), } sendErrCh <- proto1.Send(app.FrameSend, packet, nil) }() diff --git a/pkg/routing/loop.go b/pkg/routing/loop.go index 5fc8fc097..44717f78c 100644 --- a/pkg/routing/loop.go +++ b/pkg/routing/loop.go @@ -7,20 +7,20 @@ import ( "github.com/skycoin/dmsg/cipher" ) -// Loop defines a loop over a pair of addresses. -type Loop struct { +// AddrLoop defines a loop over a pair of addresses. +type AddrLoop struct { Local Addr Remote Addr } // TODO: discuss if we should add local PK to the output -func (l Loop) String() string { +func (l AddrLoop) String() string { return fmt.Sprintf("%s:%d <-> %s:%d", l.Local.PubKey, l.Local.Port, l.Remote.PubKey, l.Remote.Port) } // LoopDescriptor defines a loop over a pair of routes. type LoopDescriptor struct { - Loop Loop + Loop AddrLoop Forward Route Reverse Route Expiry time.Time @@ -51,6 +51,6 @@ func (l LoopDescriptor) String() string { // LoopData stores loop confirmation request data. type LoopData struct { - Loop Loop `json:"loop"` - RouteID RouteID `json:"resp-rid,omitempty"` + Loop AddrLoop `json:"loop"` + RouteID RouteID `json:"resp-rid,omitempty"` } diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 0c1df0087..4365fe612 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -95,7 +95,7 @@ func (sn *Node) serveTransport(tr transport.Transport) error { var ld routing.LoopData if err = json.Unmarshal(data, &ld); err == nil { err = sn.closeLoop(ld.Loop.Remote.PubKey, routing.LoopData{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Remote: ld.Loop.Local, Local: ld.Loop.Remote, }, @@ -134,7 +134,7 @@ func (sn *Node) createLoop(ld routing.LoopDescriptor) error { responder := ld.Responder() ldR := routing.LoopData{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Remote: routing.Addr{ PubKey: initiator, Port: ld.Loop.Local.Port, @@ -152,7 +152,7 @@ func (sn *Node) createLoop(ld routing.LoopDescriptor) error { } ldI := routing.LoopData{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Remote: routing.Addr{ PubKey: responder, Port: ld.Loop.Remote.Port, diff --git a/pkg/setup/node_test.go b/pkg/setup/node_test.go index 930b21f74..bf099fd57 100644 --- a/pkg/setup/node_test.go +++ b/pkg/setup/node_test.go @@ -97,7 +97,7 @@ func TestNode(t *testing.T) { // prepare loop creation (client_1 will use this to request loop creation with setup node). ld := routing.LoopDescriptor{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Local: routing.Addr{PubKey: clients[1].Local(), Port: 1}, Remote: routing.Addr{PubKey: clients[4].Local(), Port: 1}, }, @@ -214,7 +214,7 @@ func TestNode(t *testing.T) { // prepare loop data describing the loop that is to be closed. ld := routing.LoopData{ - Loop: routing.Loop{ + Loop: routing.AddrLoop{ Local: routing.Addr{ PubKey: clients[1].Local(), Port: 1, diff --git a/pkg/visor/taggedformatter.go b/pkg/visor/taggedformatter.go index 090f24285..edcfba76a 100644 --- a/pkg/visor/taggedformatter.go +++ b/pkg/visor/taggedformatter.go @@ -3,7 +3,6 @@ package visor import ( "bytes" "os" - "time" "github.com/sirupsen/logrus" "github.com/skycoin/skycoin/src/util/logging" @@ -51,7 +50,7 @@ func NewTaggedMasterLogger(tag string, ssubs []struct{ old, new string }) *loggi ForceFormatting: true, DisableColors: false, ForceColors: false, - TimestampFormat: time.StampMicro, + TimestampFormat: "05.000000", }, }, Hooks: hooks, diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 40e3881fe..f9c0a8434 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -22,6 +22,7 @@ import ( "github.com/skycoin/dmsg/noise" "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/internal/therealproxy" "github.com/skycoin/skywire/internal/therealssh" "github.com/skycoin/skywire/pkg/app" @@ -253,6 +254,9 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) // Start spawns auto-started Apps, starts router and RPC interfaces . func (node *Node) Start() error { + node.logger.Debug(testhelpers.Trace("ENTER")) + defer node.logger.Debug(testhelpers.Trace("ENTER")) + ctx := context.Background() switch factory := node.messenger.(type) { From 598e17ab72e1a3eea3d60a31ca2b2800418e7d27 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Wed, 21 Aug 2019 21:29:40 +0300 Subject: [PATCH 20/26] Yet another step --- cmd/apps/skychat/chat.go | 57 +++++--- internal/netutil/retrier.go | 1 + internal/therealproxy/client.go | 1 + internal/therealssh/channel_test.go | 4 +- pkg/app/app.go | 54 ++++--- pkg/app/app_test.go | 16 +- pkg/app/packet.go | 2 +- pkg/app/protocol.go | 38 +++-- pkg/router/app_manager.go | 32 ++-- pkg/router/app_manager_test.go | 11 +- pkg/router/loop_list.go | 6 + pkg/router/mockrouter.go | 2 - pkg/router/port_manager.go | 47 +++++- pkg/router/port_manager_test.go | 3 +- pkg/router/router.go | 195 +++++++++++++++---------- pkg/router/router_close_test.go | 10 +- pkg/router/router_setup_test.go | 11 +- pkg/router/router_test.go | 22 ++- pkg/routing/packet.go | 6 +- pkg/transport/managed_transport.go | 2 +- pkg/transport/manager.go | 19 ++- pkg/transport/manager_test.go | 4 +- pkg/transport/mock.go | 1 - pkg/transport/tcp_transport.go | 26 ++-- pkg/transport/tcp_transport_test.go | 11 +- pkg/visor/multihead_test.go | 219 ++++++++++++++++++---------- pkg/visor/rpc_test.go | 2 - pkg/visor/taggedformatter.go | 21 +-- pkg/visor/visor.go | 65 +++++---- 29 files changed, 561 insertions(+), 327 deletions(-) diff --git a/cmd/apps/skychat/chat.go b/cmd/apps/skychat/chat.go index 5be306abc..acc46d20f 100644 --- a/cmd/apps/skychat/chat.go +++ b/cmd/apps/skychat/chat.go @@ -17,33 +17,43 @@ import ( "time" "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" "github.com/skycoin/skywire/internal/netutil" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" ) -var addr = flag.String("addr", ":8000", "address to bind") -var r = netutil.NewRetrier(50*time.Millisecond, 5, 2) - var ( + addr = flag.String("addr", ":8000", "address to bind") + logger = logging.NewMasterLogger().PackageLogger("chat") + r = netutil.NewRetrier(50*time.Millisecond, 0, 2) + chatApp *app.App clientCh chan string chatConns map[cipher.PubKey]net.Conn connsMu sync.Mutex + + trcLog = logger.WithField("_module", th.GetCallerN(4)) ) +func trStart() error { // nolint:unparam + logger.Debug(th.Trace("ENTER")) + return nil +} +func trFinish(_ error) { logger.Debug(th.Trace("EXIT")) } + func main() { flag.Parse() a, err := app.Setup(&app.Config{AppName: "skychat", AppVersion: "1.0", ProtocolVersion: "0.0.1"}) if err != nil { - log.Fatal("Setup failure: ", err) + trcLog.Fatal("Setup failure", err) } defer func() { if err := a.Close(); err != nil { - log.Println("Failed to close app:", err) + trcLog.Info("Failed to close app:", err) } }() @@ -59,11 +69,13 @@ func main() { http.HandleFunc("/message", messageHandler) http.HandleFunc("/sse", sseHandler) - log.Println("Serving HTTP on", *addr) - log.Fatal(http.ListenAndServe(*addr, nil)) + trcLog.Info("Serving HTTP on", *addr) + trcLog.Info(http.ListenAndServe(*addr, nil)) } func listenLoop() { + defer trFinish(trStart()) + for { conn, err := chatApp.Accept() if err != nil { @@ -81,37 +93,38 @@ func listenLoop() { } func handleConn(conn net.Conn) { - log.Printf(testhelpers.Trace("ENTER")) - defer log.Printf(testhelpers.Trace("EXIT")) + defer trFinish(trStart()) var cntr uint64 - raddr := conn.RemoteAddr().(routing.Addr) for { atomic.AddUint64(&cntr, 1) - log.Printf(testhelpers.Trace(fmt.Sprintf("CYCLE %03d START", cntr))) + + trcLog.Debugf("CYCLE %03d START", cntr) buf := make([]byte, 32*1024) n, err := conn.Read(buf) if err != nil { - log.Println("failed to read packet:", err) + trcLog.Debug("failed to read packet:", err) return } clientMsg, err := json.Marshal(map[string]string{"sender": raddr.PubKey.Hex(), "message": string(buf[:n])}) if err != nil { - log.Printf("Failed to marshal json: %v", err) + trcLog.Debug("Failed to marshal json: ", err) } select { case clientCh <- string(clientMsg): - log.Printf("received and sent to ui: %s\n", clientMsg) + trcLog.Debugf("received and sent to ui: %s\n", clientMsg) default: - log.Printf("received and trashed: %s\n", clientMsg) + trcLog.Debugf("received and trashed: %s\n", clientMsg) } - log.Printf(testhelpers.Trace(fmt.Sprintf("CYCLE %03d END", cntr))) + trcLog.Debugf("CYCLE %03d END", cntr) } } func messageHandler(w http.ResponseWriter, req *http.Request) { + defer trFinish(trStart()) + data := map[string]string{} if err := json.NewDecoder(req.Body).Decode(&data); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -125,17 +138,25 @@ func messageHandler(w http.ResponseWriter, req *http.Request) { } addr := routing.Addr{PubKey: pk, Port: 1} + trcLog.Debug("addr: ", addr) + connsMu.Lock() conn, ok := chatConns[pk] connsMu.Unlock() + trcLog.Debugf("chatConn: %v pk:%v\n", chatConns, pk) + + var cntr uint64 if !ok { var err error err = r.Do(func() error { + atomic.AddUint64(&cntr, 1) + trcLog.Debugf("dial %v addr:%v\n", cntr, addr) conn, err = chatApp.Dial(addr) return err }) if err != nil { + trcLog.Debug("err: ", err) http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -159,6 +180,8 @@ func messageHandler(w http.ResponseWriter, req *http.Request) { } func sseHandler(w http.ResponseWriter, req *http.Request) { + defer trFinish(trStart()) + f, ok := w.(http.Flusher) if !ok { http.Error(w, "Streaming unsupported!", http.StatusBadRequest) diff --git a/internal/netutil/retrier.go b/internal/netutil/retrier.go index c4741ee44..827a3e49d 100644 --- a/internal/netutil/retrier.go +++ b/internal/netutil/retrier.go @@ -48,6 +48,7 @@ func (r *Retrier) WithErrWhitelist(errors ...error) *Retrier { // Do takes a RetryFunc and attempts to execute it, if it fails with an error it will be retried a maximum of given times with an exponentialBackoff, until it returns // nil or an error that is whitelisted func (r Retrier) Do(f RetryFunc) error { + if r.times == 0 { return r.retryUntilSuccess(f) } diff --git a/internal/therealproxy/client.go b/internal/therealproxy/client.go index 2a8b08e70..0896865b9 100644 --- a/internal/therealproxy/client.go +++ b/internal/therealproxy/client.go @@ -9,6 +9,7 @@ import ( "github.com/skycoin/skycoin/src/util/logging" ) +// Logger for therealproxy var Logger = logging.MustGetLogger("therealproxy") // Client implement multiplexing proxy client using yamux. diff --git a/internal/therealssh/channel_test.go b/internal/therealssh/channel_test.go index f05a7323b..09d73245a 100644 --- a/internal/therealssh/channel_test.go +++ b/internal/therealssh/channel_test.go @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/routing" ) @@ -126,5 +126,5 @@ func TestChannelServeSocket(t *testing.T) { assert.Equal(t, []byte("bar"), buf) require.NoError(t, ch.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErr)) + require.NoError(t, th.NoErrorWithinTimeout(serveErr)) } diff --git a/pkg/app/app.go b/pkg/app/app.go index 3f5bf747f..aa6023761 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -17,7 +17,7 @@ import ( "github.com/skycoin/skycoin/src/util/logging" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/routing" ) @@ -56,6 +56,9 @@ type App struct { // Command setups pipe connection and returns *exec.Cmd for an App // with initialized connection. func Command(config *Config, appsPath string, args []string) (net.Conn, *exec.Cmd, error) { + log.Printf(th.Trace("ENTER")) + defer log.Printf(th.Trace("EXIT")) + srvConn, clientConn, err := OpenPipeConn() if err != nil { return nil, nil, fmt.Errorf("failed to open piped connection: %s", err) @@ -103,8 +106,8 @@ func Setup(config *Config) (*App, error) { // Close implements io.Closer for an App. func (app *App) Close() error { - log.Debug(testhelpers.Trace("ENTER")) - defer log.Debug(testhelpers.Trace("EXIT")) + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("EXIT")) if app == nil { return nil @@ -134,16 +137,16 @@ func (app *App) Close() error { // Accept awaits for incoming loop confirmation request from a Node and // returns net.Conn for received loop. func (app *App) Accept() (net.Conn, error) { - log.Debug(testhelpers.Trace("ENTER")) - defer log.Debug(testhelpers.Trace("EXIT")) + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("EXIT")) addrs := <-app.acceptChan laddr := addrs[0] raddr := addrs[1] // TODO: Why is that? - loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} - // loop := routing.AddrLoop{Local: laddr, Remote: raddr} + // loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} + loop := routing.AddrLoop{Local: laddr, Remote: raddr} conn, out := net.Pipe() app.mu.Lock() app.conns[loop] = conn @@ -154,8 +157,10 @@ func (app *App) Accept() (net.Conn, error) { // Dial sends create loop request to a Node and returns net.Conn for created loop. func (app *App) Dial(raddr routing.Addr) (net.Conn, error) { - log.Debug(testhelpers.Trace("ENTER")) - defer log.Debug(testhelpers.Trace("EXIT")) + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("EXIT")) + + log.Debugf("%v CALLERS: %v\n", th.GetCaller(), th.GetCallers(3)) var laddr routing.Addr err := app.proto.Send(FrameCreateLoop, raddr, &laddr) @@ -164,8 +169,8 @@ func (app *App) Dial(raddr routing.Addr) (net.Conn, error) { } // TODO: Why is that way? - loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} - // loop := routing.AddrLoop{Local: laddr, Remote: raddr} + // loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} + loop := routing.AddrLoop{Local: laddr, Remote: raddr} conn, out := net.Pipe() app.mu.Lock() @@ -181,8 +186,8 @@ func (app *App) Addr() net.Addr { } func (app *App) protoHandler(frame Frame, payload []byte) (res interface{}, err error) { - log.Debug(testhelpers.Trace("ENTER")) - defer log.Debug(testhelpers.Trace("EXIT")) + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("EXIT")) switch frame { case FrameConfirmLoop: @@ -198,6 +203,9 @@ func (app *App) protoHandler(frame Frame, payload []byte) (res interface{}, err } func (app *App) handleProto() { + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("EXIT")) + err := app.proto.Serve(app.protoHandler) if err != nil { return @@ -205,8 +213,10 @@ func (app *App) handleProto() { } func (app *App) serveConn(loop routing.AddrLoop, conn io.ReadWriteCloser) { - log.Debug(testhelpers.Trace("ENTER")) - defer log.Debug(testhelpers.Trace("EXIT")) + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("EXIT")) + + log.Debugf("%v loop: %v conn: %T\n", th.GetCaller(), loop, conn) var cntr uint64 @@ -218,7 +228,7 @@ func (app *App) serveConn(loop routing.AddrLoop, conn io.ReadWriteCloser) { for { atomic.AddUint64(&cntr, 1) - log.Debugf("%v CYCLE %03d START", testhelpers.GetCaller(), cntr) + log.Debugf("%v CYCLE %03d START", th.GetCaller(), cntr) buf := make([]byte, 32*1024) n, err := conn.Read(buf) @@ -243,8 +253,8 @@ func (app *App) serveConn(loop routing.AddrLoop, conn io.ReadWriteCloser) { } func (app *App) forwardPacket(data []byte) error { - log.Debug(testhelpers.Trace("ENTER")) - defer log.Debug(testhelpers.Trace("EXIT")) + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("EXIT")) packet := &Packet{} if err := json.Unmarshal(data, packet); err != nil { @@ -264,8 +274,8 @@ func (app *App) forwardPacket(data []byte) error { } func (app *App) closeConn(data []byte) error { - log.Debug(testhelpers.Trace("ENTER")) - defer log.Debug(testhelpers.Trace("EXIT")) + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("EXIT")) var loop routing.AddrLoop if err := json.Unmarshal(data, &loop); err != nil { @@ -284,8 +294,8 @@ func (app *App) closeConn(data []byte) error { } func (app *App) confirmLoop(data []byte) error { - log.Debug(testhelpers.Trace("ENTER")) - defer log.Debug(testhelpers.Trace("EXIT")) + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("EXIT")) var addrs [2]routing.Addr if err := json.Unmarshal(data, &addrs); err != nil { diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 99b3d2146..ab97cefba 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -14,7 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/routing" ) @@ -81,7 +81,7 @@ func TestAppDial(t *testing.T) { require.Len(t, app.conns, 0) app.mu.Unlock() require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) } func TestAppAccept(t *testing.T) { @@ -129,7 +129,7 @@ func TestAppAccept(t *testing.T) { assert.Equal(t, lpk.Hex()+":2", conn.LocalAddr().String()) require.Len(t, app.conns, 2) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) } func TestAppWrite(t *testing.T) { @@ -169,7 +169,7 @@ func TestAppWrite(t *testing.T) { assert.Equal(t, []byte("foo"), packet.Payload) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) require.NoError(t, appOut.Close()) } @@ -201,7 +201,7 @@ func TestAppRead(t *testing.T) { require.NoError(t, <-errCh) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) require.NoError(t, appOut.Close()) } @@ -238,7 +238,7 @@ func TestAppSetup(t *testing.T) { assert.Equal(t, "0.0.1", config.ProtocolVersion) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) } func TestAppCloseConn(t *testing.T) { @@ -265,7 +265,7 @@ func TestAppCloseConn(t *testing.T) { require.Len(t, app.conns, 0) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) } func TestAppClose(t *testing.T) { @@ -304,7 +304,7 @@ func TestAppClose(t *testing.T) { assert.Equal(t, routing.Port(3), loop.Remote.Port) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) } func TestAppCommand(t *testing.T) { diff --git a/pkg/app/packet.go b/pkg/app/packet.go index 7d0aa8702..dd596a1e4 100644 --- a/pkg/app/packet.go +++ b/pkg/app/packet.go @@ -5,5 +5,5 @@ import "github.com/skycoin/skywire/pkg/routing" // Packet represents message exchanged between App and Node. type Packet struct { Loop routing.AddrLoop `json:"loop"` - Payload []byte `json:"payload"` + Payload []byte `json:"payload"` } diff --git a/pkg/app/protocol.go b/pkg/app/protocol.go index ee903e35a..ff617399c 100644 --- a/pkg/app/protocol.go +++ b/pkg/app/protocol.go @@ -11,7 +11,8 @@ import ( "sync/atomic" "github.com/skycoin/skycoin/src/util/logging" - "github.com/skycoin/skywire/internal/testhelpers" + + th "github.com/skycoin/skywire/internal/testhelpers" ) // Frame defines type for all App frames. @@ -57,7 +58,7 @@ const ( ) // Logger is PackageLogger for app -var Logger = logging.MustGetLogger("app") +var Logger = logging.MustGetLogger("Protocol") // Protocol implements full-duplex protocol for App to Node communication. type Protocol struct { @@ -72,22 +73,29 @@ func NewProtocol(rw io.ReadWriteCloser) *Protocol { // Send sends command Frame with payload and awaits for response. func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { - Logger.Info(testhelpers.Trace("ENTER")) - defer Logger.Infof(testhelpers.Trace("EXIT")) + Logger.Debug(th.Trace("ENTER")) + Logger.Debugf("%v cmd: %v, payload: %v\n", th.GetCaller(), cmd, payload) + + Logger.Debugf("%v CALLERS: %v\n", th.GetCaller(), th.GetCallers(3)) + + defer Logger.Debug(th.Trace("EXIT")) id, resChan := p.chans.add() if err := p.writeFrame(cmd, id, payload); err != nil { - log.Warnf("Protocol.Send writeFrame err: %v\n", err) + Logger.Warnf("%v p.writeFrame(%v, %v, %v) err: %v\n", th.GetCaller(), cmd, id, payload, err) + return err } + Logger.Debugf("%v waiting reply\n", th.GetCaller()) frame, more := <-resChan if !more { return io.EOF } + Logger.Debugf("%v received %v\n", th.GetCaller(), Frame(frame[0])) if Frame(frame[0]) == FrameFailure { - log.Warnf("%v writeFrame err: %v\n", testhelpers.GetCaller(), string(frame[2:])) + Logger.Warnf("%v writeFrame err: %v\n", th.GetCaller(), string(frame[2:])) return errors.New(string(frame[2:])) } @@ -100,13 +108,13 @@ func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { // Serve reads incoming frame, passes it to the handleFunc and writes results. func (p *Protocol) Serve(handleFunc func(Frame, []byte) (interface{}, error)) error { - Logger.Info(testhelpers.Trace("ENTER")) - defer Logger.Infof(testhelpers.Trace("EXIT")) + Logger.Debug(th.Trace("ENTER")) + defer Logger.Debug(th.Trace("EXIT")) var cntr uint64 for { atomic.AddUint64(&cntr, 1) - Logger.Debugf("%v CYCLE %03d START", testhelpers.GetCaller(), cntr) + Logger.Debugf("%v CYCLE %03d START", th.GetCaller(), cntr) frame, err := p.readFrame() if err != nil { if err == io.EOF || strings.Contains(err.Error(), "closed") { @@ -154,8 +162,8 @@ func (p *Protocol) Serve(handleFunc func(Frame, []byte) (interface{}, error)) er // Close closes underlying ReadWriter. func (p *Protocol) Close() error { - Logger.Info(testhelpers.Trace("ENTER")) - defer Logger.Infof(testhelpers.Trace("EXIT")) + Logger.Debug(th.Trace("ENTER")) + defer Logger.Debug(th.Trace("EXIT")) if p == nil { return nil @@ -165,8 +173,8 @@ func (p *Protocol) Close() error { } func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err error) { - Logger.Info(testhelpers.Trace("ENTER")) - defer Logger.Infof(testhelpers.Trace("EXIT")) + Logger.Debug(th.Trace("ENTER")) + defer Logger.Debug(th.Trace("EXIT")) var data []byte if err, ok := payload.(error); ok { @@ -177,7 +185,7 @@ func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err er return err } } - Logger.WithField("payload", Payload{frame, data}).Info(testhelpers.GetCaller()) + Logger.WithField("payload", Payload{frame, data}).Info(th.GetCaller()) packet := append([]byte{byte(frame), id}, data...) buf := make([]byte, 2) @@ -185,7 +193,7 @@ func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err er _, err = p.rw.Write(append(buf, packet...)) if err != nil { - Logger.Warnf("% p.rw.Write err: %v\n", testhelpers.GetCaller(), err) + Logger.Warnf("% p.rw.Write err: %v\n", th.GetCaller(), err) } return err diff --git a/pkg/router/app_manager.go b/pkg/router/app_manager.go index ea4a6a1ec..6809ccdfd 100644 --- a/pkg/router/app_manager.go +++ b/pkg/router/app_manager.go @@ -6,7 +6,7 @@ import ( "github.com/skycoin/skycoin/src/util/logging" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" ) @@ -18,13 +18,12 @@ type appManager struct { router PacketRouter proto *app.Protocol appConf *app.Config - // callbacks *appCallbacks } func (am *appManager) serveHandler(frame app.Frame, payload []byte) (res interface{}, err error) { - am.Logger.WithField("payload", app.Payload{frame, payload}). - Info(testhelpers.Trace("ENTER")) - defer am.Logger.Info(testhelpers.Trace("EXIT")) + am.Logger.WithField("payload", app.Payload{Frame: frame, Data: payload}). + Debug(th.Trace("ENTER")) + defer am.Logger.Debug(th.Trace("EXIT")) switch frame { case app.FrameInit: @@ -40,19 +39,23 @@ func (am *appManager) serveHandler(frame app.Frame, payload []byte) (res interfa } if err != nil { - am.Logger.Warnf("%v: App request with type %s failed: %+v\n", testhelpers.GetCaller(), frame, err) + am.Logger.Warnf("%v: App request with type %s failed: %+v\n", th.GetCaller(), frame, err) } return res, err } func (am *appManager) Serve() error { - am.Logger.Info(testhelpers.Trace("EXIT")) - defer am.Logger.Info(testhelpers.Trace("EXIT")) + am.Logger.Debug(th.Trace("ENTER")) + defer am.Logger.Debug(th.Trace("EXIT")) + return am.proto.Serve(am.serveHandler) } func (am *appManager) initApp(payload []byte) error { + am.Logger.Debug(th.Trace("ENTER")) + defer am.Logger.Debug(th.Trace("EXIT")) + var config app.Config if err := json.Unmarshal(payload, &config); err != nil { return errors.New("invalid Init payload") @@ -75,8 +78,8 @@ func (am *appManager) initApp(payload []byte) error { } func (am *appManager) setupLoop(payload []byte) (routing.Addr, error) { - am.Logger.Info(testhelpers.Trace("ENTER")) - defer am.Logger.Info(testhelpers.Trace("EXIT")) + am.Logger.Debug(th.Trace("ENTER")) + defer am.Logger.Debug(th.Trace("EXIT")) var raddr routing.Addr if err := json.Unmarshal(payload, &raddr); err != nil { @@ -87,8 +90,8 @@ func (am *appManager) setupLoop(payload []byte) (routing.Addr, error) { } func (am *appManager) handleCloseLoop(payload []byte) error { - am.Logger.Info(testhelpers.Trace("ENTER")) - defer am.Logger.Info(testhelpers.Trace("EXIT")) + am.Logger.Debug(th.Trace("ENTER")) + defer am.Logger.Debug(th.Trace("EXIT")) var loop routing.AddrLoop if err := json.Unmarshal(payload, &loop); err != nil { @@ -99,13 +102,14 @@ func (am *appManager) handleCloseLoop(payload []byte) error { } func (am *appManager) forwardAppPacket(payload []byte) error { - am.Logger.Info(testhelpers.Trace("ENTER")) - defer am.Logger.Info(testhelpers.Trace("EXIT")) + am.Logger.Debug(th.Trace("ENTER")) + defer am.Logger.Debug(th.Trace("EXIT")) packet := &app.Packet{} if err := json.Unmarshal(payload, packet); err != nil { return err } + am.Logger.Debugf("%v: %v\n", th.GetCaller(), packet) return am.router.ForwardAppPacket(am.proto, packet) } diff --git a/pkg/router/app_manager_test.go b/pkg/router/app_manager_test.go index e34d188f5..20ce050ca 100644 --- a/pkg/router/app_manager_test.go +++ b/pkg/router/app_manager_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" ) @@ -52,11 +52,10 @@ func TestAppManagerInit(t *testing.T) { err := proto.Send(app.FrameInit, &app.Config{AppName: "foo", AppVersion: "0.0.1", ProtocolVersion: "0.0.1"}, nil) require.NoError(t, err) - require.NoError(t, in.Close()) require.NoError(t, <-srvCh) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) } func TestAppManagerSetupLoop(t *testing.T) { @@ -87,7 +86,7 @@ func TestAppManagerSetupLoop(t *testing.T) { require.NoError(t, in.Close()) require.NoError(t, <-srvCh) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) } func TestAppManagerCloseLoop(t *testing.T) { @@ -128,7 +127,7 @@ func TestAppManagerCloseLoop(t *testing.T) { require.NoError(t, in.Close()) require.NoError(t, <-srvCh) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) } func TestAppManagerForward(t *testing.T) { @@ -163,5 +162,5 @@ func TestAppManagerForward(t *testing.T) { require.NoError(t, in.Close()) require.NoError(t, <-srvCh) require.NoError(t, proto.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) } diff --git a/pkg/router/loop_list.go b/pkg/router/loop_list.go index e0a2a3b8c..d89ce4ba4 100644 --- a/pkg/router/loop_list.go +++ b/pkg/router/loop_list.go @@ -1,9 +1,11 @@ package router import ( + "fmt" "sync" "github.com/google/uuid" + "github.com/skycoin/skywire/pkg/routing" ) @@ -12,6 +14,10 @@ type loop struct { routeID routing.RouteID } +func (l loop) String() string { + return fmt.Sprintf("{trId: %v, routeID: %v}", l.trID, l.routeID) +} + type loopList struct { sync.Mutex diff --git a/pkg/router/mockrouter.go b/pkg/router/mockrouter.go index d6fefbfaf..0d3f25a11 100644 --- a/pkg/router/mockrouter.go +++ b/pkg/router/mockrouter.go @@ -25,8 +25,6 @@ type MockRouter struct { inLoop routing.AddrLoop } -var n *MockRouter - // Ports implements PacketRouter.Ports func (r *MockRouter) Ports() []routing.Port { r.Lock() diff --git a/pkg/router/port_manager.go b/pkg/router/port_manager.go index 87dcab4eb..15bcd0565 100644 --- a/pkg/router/port_manager.go +++ b/pkg/router/port_manager.go @@ -4,24 +4,44 @@ import ( "errors" "fmt" + "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" + + th "github.com/skycoin/skywire/internal/testhelpers" ) +// var ( +// logger = logging.MustGetLogger("router") +// debug = logger.Debug +// ) + type portManager struct { - ports *portList + ports *portList + logger *logging.Logger } -func newPortManager(minPort routing.Port) *portManager { - return &portManager{newPortList(minPort)} +func (pm *portManager) debug(args ...interface{}) { + pm.logger.Debug(args) +} + +func newPortManager(minPort routing.Port, logger *logging.Logger) *portManager { + return &portManager{newPortList(minPort), logger} } func (pm *portManager) Alloc(conn *app.Protocol) routing.Port { + pm.debug(th.Trace("ENTER")) + defer pm.debug(th.Trace("EXIT")) + b := &portBind{conn, newLoopList()} return pm.ports.add(b) } func (pm *portManager) Open(port routing.Port, proto *app.Protocol) error { + pm.debug(th.Trace("ENTER")) + defer pm.debug(th.Trace("EXIT")) + if pm.ports.get(port) != nil { return fmt.Errorf("port %d is already bound", port) } @@ -31,6 +51,9 @@ func (pm *portManager) Open(port routing.Port, proto *app.Protocol) error { } func (pm *portManager) SetLoop(port routing.Port, raddr routing.Addr, l *loop) error { + pm.debug(th.Trace("ENTER")) + defer pm.debug(th.Trace("EXIT")) + b := pm.ports.get(port) if b == nil { return errors.New("port is not bound") @@ -41,6 +64,9 @@ func (pm *portManager) SetLoop(port routing.Port, raddr routing.Addr, l *loop) e } func (pm *portManager) AppConns() []*app.Protocol { + pm.debug(th.Trace("ENTER")) + defer pm.debug(th.Trace("EXIT")) + res := make([]*app.Protocol, 0) set := map[*app.Protocol]struct{}{} for _, bind := range pm.ports.all() { @@ -53,6 +79,9 @@ func (pm *portManager) AppConns() []*app.Protocol { } func (pm *portManager) AppPorts(appConn *app.Protocol) []routing.Port { + pm.debug(th.Trace("ENTER")) + defer pm.debug(th.Trace("EXIT")) + res := make([]routing.Port, 0) for port, bind := range pm.ports.all() { if bind.conn == appConn { @@ -63,6 +92,9 @@ func (pm *portManager) AppPorts(appConn *app.Protocol) []routing.Port { } func (pm *portManager) Close(port routing.Port) []routing.Addr { + pm.debug(th.Trace("ENTER")) + defer pm.debug(th.Trace("EXIT")) + if pm == nil { return nil } @@ -76,6 +108,9 @@ func (pm *portManager) Close(port routing.Port) []routing.Addr { } func (pm *portManager) RemoveLoop(port routing.Port, raddr routing.Addr) error { + pm.debug(th.Trace("ENTER")) + defer pm.debug(th.Trace("EXIT")) + b, err := pm.Get(port) if err != nil { return err @@ -86,6 +121,9 @@ func (pm *portManager) RemoveLoop(port routing.Port, raddr routing.Addr) error { } func (pm *portManager) Get(port routing.Port) (*portBind, error) { + pm.debug(th.Trace("ENTER")) + defer pm.debug(th.Trace("EXIT")) + b := pm.ports.get(port) if b == nil { return nil, errors.New("port is not bound") @@ -95,6 +133,9 @@ func (pm *portManager) Get(port routing.Port) (*portBind, error) { } func (pm *portManager) GetLoop(localPort routing.Port, remoteAddr routing.Addr) (*loop, error) { + pm.debug(th.Trace("ENTER")) + defer pm.debug(th.Trace("EXIT")) + b, err := pm.Get(localPort) if err != nil { return nil, err diff --git a/pkg/router/port_manager_test.go b/pkg/router/port_manager_test.go index 9a548ce99..b726a0e8d 100644 --- a/pkg/router/port_manager_test.go +++ b/pkg/router/port_manager_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -14,7 +15,7 @@ import ( ) func TestPortManager(t *testing.T) { - pm := newPortManager(10) + pm := newPortManager(10, logging.MustGetLogger("portmanager")) in, _ := net.Pipe() proto := app.NewProtocol(in) diff --git a/pkg/router/router.go b/pkg/router/router.go index d6c5ff9d8..f8159806d 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -10,10 +10,11 @@ import ( "sync" "time" + "github.com/sirupsen/logrus" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" routeFinder "github.com/skycoin/skywire/pkg/route-finder/client" "github.com/skycoin/skywire/pkg/routing" @@ -65,7 +66,6 @@ type Router struct { tm *transport.Manager pm *portManager rm *routeManager - msgr transport.Factory expiryTicker *time.Ticker wg sync.WaitGroup @@ -79,7 +79,7 @@ func New(config *Config) *Router { r := &Router{ Logger: config.Logger, tm: config.TransportManager, - pm: newPortManager(10), + pm: newPortManager(10, config.Logger), config: config, expiryTicker: time.NewTicker(10 * time.Minute), staticPorts: make(map[routing.Port]struct{}), @@ -92,11 +92,23 @@ func New(config *Config) *Router { return r } +func (r *Router) trStart() (_ error) { + r.trLog().Debug("ENTER") + return +} +func (r *Router) trFinish(_ error) { + r.trLog().Debug("EXIT") + return +} + +func (r *Router) trLog() *logrus.Entry { + return r.Logger.WithField("M", th.GetCallerN(3)) +} + // Serve starts transport listening loop. func (r *Router) Serve(ctx context.Context) error { + defer r.trFinish(r.trStart()) r.Logger.Info("Starting router") - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) go func() { for { @@ -130,6 +142,9 @@ func (r *Router) Serve(ctx context.Context) error { } func (r *Router) handleTransport(tp transport.Transport, isAccepted, isSetup bool) { + defer r.trFinish(r.trStart()) + r.Logger.Info("Starting router") + var serve func(io.ReadWriter) error switch { case isAccepted && isSetup: @@ -159,8 +174,8 @@ func (r *Router) handleTransport(tp transport.Transport, isAccepted, isSetup boo // ServeApp handles App packets from the App connection on provided port. func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) + defer r.trFinish(r.trStart()) + r.Logger.Info("Starting router") r.wg.Add(1) defer r.wg.Done() @@ -179,7 +194,7 @@ func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) for _, port := range r.pm.AppPorts(appProto) { for _, addr := range r.pm.Close(port) { - if err := r.closeLoop(appProto, routing.AddrLoop{Local: routing.Addr{Port: port}, Remote: addr}); err != nil { + if err := r.closeLoop(routing.AddrLoop{Local: routing.Addr{Port: port}, Remote: addr}); err != nil { log.WithError(err).Warn("Failed to close loop") } } @@ -201,19 +216,19 @@ func (r *Router) CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routi } // CloseLoop implements PactetRouter.CloseLoop -func (r *Router) CloseLoop(conn *app.Protocol, loop routing.AddrLoop) error { - return r.closeLoop(conn, loop) +func (r *Router) CloseLoop(_ *app.Protocol, loop routing.AddrLoop) error { + return r.closeLoop(loop) } // ForwardAppPacket implements PactetRouter.ForwardAppPacket -func (r *Router) ForwardAppPacket(conn *app.Protocol, packet *app.Packet) error { - return r.forwardAppPacket(conn, packet) +func (r *Router) ForwardAppPacket(_ *app.Protocol, packet *app.Packet) error { + return r.forwardAppPacket(packet) } // Close safely stops Router. func (r *Router) Close() error { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) if r == nil { return nil @@ -233,8 +248,8 @@ func (r *Router) Close() error { } func (r *Router) serveTransport(rw io.ReadWriter) error { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) packet := make(routing.Packet, 6) if _, err := io.ReadFull(rw, packet); err != nil { @@ -261,12 +276,16 @@ func (r *Router) serveTransport(rw io.ReadWriter) error { // ForwardPacket forwards payload according to rule func (r *Router) ForwardPacket(payload []byte, rule routing.Rule) error { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Debugf("%v %v %v\n", th.GetCaller(), payload, rule) packet := routing.MakePacket(rule.RouteID(), payload) tr := r.tm.Transport(rule.TransportID()) if tr == nil { + r.Logger.Debugf("%v: packet %v \n", th.GetCaller(), packet) + + // tr = r.tm.CreateDataTransport(context.Background(), ra) return errors.New("unknown transport") } @@ -279,8 +298,8 @@ func (r *Router) ForwardPacket(payload []byte, rule routing.Rule) error { } func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) laddr := routing.Addr{Port: rule.LocalPort()} raddr := routing.Addr{PubKey: rule.RemotePK(), Port: rule.RemotePort()} @@ -298,56 +317,64 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { return nil } -func (r *Router) forwardAppPacket(appConn *app.Protocol, appPacket *app.Packet) error { - r.Logger.Info(testhelpers.Trace("ENTER")) - defer r.Logger.Info(testhelpers.Trace("EXIT")) +func (r *Router) forwardAppPacket(appPacket *app.Packet) error { + defer r.trFinish(r.trStart()) + + r.trLog().Errorf("CALLERZZZ: %v\n", th.GetCallers(1)) + + r.trLog().Error("appPacket: ", appPacket) if r == nil { return errors.New("router not initialized") } if appPacket.Loop.Remote.PubKey == r.config.PubKey { + r.trLog().Infof("r.config.PubKey: ", r.config.PubKey) return r.forwardLocalAppPacket(appPacket) } loop, err := r.pm.GetLoop(appPacket.Loop.Local.Port, appPacket.Loop.Remote) + r.trLog().Error("r.pm.GetLoop: ", err) + if err != nil { - r.Logger.Warnf("[Router.forwardAppPacket] r.pm.GetLoop: %v", err) return err } - r.Logger.Debugf("%v found loop: %v", testhelpers.GetCaller(), loop) + // r.Logger.Debugf("%v found loop: %v", th.GetCaller(), loop) tr := r.tm.Transport(loop.trID) if tr == nil { - r.Logger.Warnf("[Router.forwardAppPacket] r.tm.Transport: tr==nil") + r.trLog().Error("Loop: ", loop) + r.trLog().Error("OOOOOOO") + r.Logger.Errorf("%v r.tm.Transport: tr==nil", th.GetCaller()) return errors.New("unknown transport") + } - r.Logger.Debugf("%v ATTEMPT TO FIND %v", testhelpers.GetCaller(), appPacket.Loop.Remote.PubKey) + r.Logger.Debugf("%v ATTEMPT TO FIND %v", th.GetCaller(), appPacket.Loop.Remote.PubKey) if r.isPubKeyLocal(appPacket.Loop.Remote.PubKey) { - r.Logger.Infof("%v isPubKeyLocal = true", testhelpers.GetCaller()) + r.Logger.Infof("%v isPubKeyLocal = true", th.GetCaller()) // return r.forwardTCPAppPacket(packet) } rPacket := routing.MakePacket(loop.routeID, appPacket.Payload) - r.Logger.Infof("[Router.forwardAppPacket] Forwarded App packet from LocalPort %d using route ID %d", appPacket.Loop.Local.Port, loop.routeID) + r.Logger.Infof("%v Forwarded App packet from LocalPort %d using route ID %d", th.GetCaller(), appPacket.Loop.Local.Port, loop.routeID) _, err = tr.Write(rPacket) if err != nil { - r.Logger.Warnf("[Router.forwardAppPacket] tr.Write: %v", err) + r.Logger.Warnf("%v tr.Write: %v", err) } return err } func (r *Router) forwardLocalAppPacket(appPacket *app.Packet) error { - r.Logger.Info(testhelpers.Trace("ENTER")) - defer r.Logger.Info(testhelpers.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) b, err := r.pm.Get(appPacket.Loop.Remote.Port) if err != nil { - r.Logger.Warnf("%v r.pm.Get: %v\n", testhelpers.GetCaller(), err) + r.Logger.Warnf("%v r.pm.Get: %v\n", th.GetCaller(), err) return nil } @@ -360,18 +387,18 @@ func (r *Router) forwardLocalAppPacket(appPacket *app.Packet) error { } errSend := b.conn.Send(app.FrameSend, p, nil) if errSend != nil { - r.Logger.Warnf("%v b.conn.Send: %v\n", testhelpers.GetCaller(), err) + r.Logger.Warnf("%v b.conn.Send: %v\n", th.GetCaller(), err) } return errSend } -func (r *Router) forwardTCPAppPacket(appPacket *app.Packet) error { - r.Logger.Info(testhelpers.Trace("ENTER")) - defer r.Logger.Info(testhelpers.Trace("EXIT")) +func (r *Router) forwardTCPAppPacket(appPacket *app.Packet) error { // nolint: unused + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) b, err := r.pm.Get(appPacket.Loop.Remote.Port) if err != nil { - r.Logger.Warnf("%v r.pm.Get: %v\n", testhelpers.GetCaller(), err) + r.Logger.Warnf("%v r.pm.Get: %v\n", th.GetCaller(), err) return nil } @@ -384,14 +411,14 @@ func (r *Router) forwardTCPAppPacket(appPacket *app.Packet) error { } errSend := b.conn.Send(app.FrameSend, p, nil) if errSend != nil { - r.Logger.Warnf("%v b.conn.Send: %v\n", testhelpers.GetCaller(), err) + r.Logger.Warnf("%v b.conn.Send: %v\n", th.GetCaller(), err) } return errSend } func (r *Router) isPubKeyLocal(pk cipher.PubKey) bool { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) for _, fct := range r.tm.Factories() { switch tfct := fct.(type) { @@ -405,8 +432,8 @@ func (r *Router) isPubKeyLocal(pk cipher.PubKey) bool { } func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing.Addr, error) { - r.Logger.Info(testhelpers.Trace("ENTER")) - defer r.Logger.Info(testhelpers.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) if r == nil { return raddr, nil @@ -414,23 +441,23 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing lport := r.pm.Alloc(appConn) if err := r.pm.SetLoop(lport, raddr, &loop{}); err != nil { - r.Logger.Warnf("[Router.requestLoop] r.pm.SetLoop err: %v", err) + r.Logger.Warnf("%v r.pm.SetLoop err: %v", err) return routing.Addr{}, err } laddr := routing.Addr{PubKey: r.config.PubKey, Port: lport} if raddr.PubKey == r.config.PubKey { if err := r.confirmLocalLoop(laddr, raddr); err != nil { - r.Logger.Warnf("[Router.requestLoop] r.ConfirmLocalLoop err: %v", err) + r.Logger.Warnf("%v r.ConfirmLocalLoop err: %v", err) return routing.Addr{}, fmt.Errorf("confirm: %s", err) } - r.Logger.Infof("[Router.requestLoop] r.confirmLocalLoop Created local loop on port %d", laddr.Port) + r.Logger.Infof("%v: r.confirmLocalLoop Created local loop on port %d", th.GetCaller(), laddr.Port) return laddr, nil } forwardRoute, reverseRoute, err := r.fetchBestRoutes(laddr.PubKey, raddr.PubKey) if err != nil { - r.Logger.Warnf("[Router.requestLoop] r.fetchBestRoutes err: %v\n", err) + r.Logger.Warnf("%v r.fetchBestRoutes err: %v\n", th.GetCaller(), err) return routing.Addr{}, fmt.Errorf("route finder: %s", err) } @@ -445,21 +472,23 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing } if r.isPubKeyLocal(raddr.PubKey) { + r.Logger.Debugf("%v laddr: %v raddr: %v \n", th.GetCaller(), laddr, raddr) if err := r.confirmTCPLoop(laddr, raddr); err != nil { - r.Logger.Warnf("%v r.ConfirmTCPLoop err: %v", testhelpers.GetCaller(), err) + r.Logger.Warnf("%v r.ConfirmTCPLoop err: %v", th.GetCaller(), err) } + r.Logger.Debugf("%v laddr: %v raddr: %v \n", th.GetCaller(), laddr, raddr) return laddr, nil } proto, tr, err := r.setupProto(context.Background()) if err != nil { - r.Logger.Warnf("[Router.requestLoop] r.setupProto err: %v\n", err) + r.Logger.Warnf("%v r.setupProto err: %v\n", err) return routing.Addr{}, err } defer func() { if err := tr.Close(); err != nil { - r.Logger.Warnf("[Router.requestLoop] Failed to close transport: %s", err) + r.Logger.Warnf("%v Failed to close transport: %s", err) } }() @@ -467,23 +496,23 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing return routing.Addr{}, fmt.Errorf("route setup: %s", err) } - r.Logger.Infof("%v success. Created new loop to %s on port %d", testhelpers.GetCaller(), raddr, laddr.Port) + r.Logger.Infof("%v success. Created new loop to %s on port %d", th.GetCaller(), raddr, laddr.Port) return laddr, nil } func (r *Router) confirmLocalLoop(laddr, raddr routing.Addr) error { - r.Logger.Info(testhelpers.Trace("ENTER")) - defer r.Logger.Info(testhelpers.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) b, err := r.pm.Get(raddr.Port) if err != nil { - r.Logger.Debugf("%v r.pm.Get %v", testhelpers.GetCaller(), err) + r.Logger.Debugf("%v r.pm.Get %v", th.GetCaller(), err) return err } addrs := [2]routing.Addr{raddr, laddr} if err = b.conn.Send(app.FrameConfirmLoop, addrs, nil); err != nil { - r.Logger.Warnf("%v b.conn.Send %v", testhelpers.GetCaller(), err) + r.Logger.Warnf("%v b.conn.Send %v", th.GetCaller(), err) return err } @@ -491,28 +520,37 @@ func (r *Router) confirmLocalLoop(laddr, raddr routing.Addr) error { } func (r *Router) confirmTCPLoop(laddr, raddr routing.Addr) error { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Info(testhelpers.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) + + r.Logger.Debugf("%v: laddr: %v, raddr: %v", th.GetCaller(), laddr, raddr) // portBind pb, err := r.pm.Get(raddr.Port) if err != nil { - r.Logger.Warnf("%v r.pm.Get %v", testhelpers.GetCaller(), err) + r.Logger.Warnf("%v r.pm.Get %v", th.GetCaller(), err) return err } addrs := [2]routing.Addr{raddr, laddr} if err = pb.conn.Send(app.FrameConfirmLoop, addrs, nil); err != nil { - r.Logger.Warnf("%v b.conn.Send %v", testhelpers.GetCaller(), err) + r.Logger.Warnf("%v b.conn.Send %v", th.GetCaller(), err) return err } + tr, err := r.tm.CreateDataTransport(context.Background(), raddr.PubKey, "tcp-transport", true) + if err != nil { + r.Logger.Warnf("%v r.tm.CreateDataTransport err: %v\n", th.GetCaller(), err) + } + r.Logger.Debugf("%v r.tm.CreateDataTransport tr: %T success: %v\n", th.GetCaller(), tr, err == nil) + return nil + } func (r *Router) confirmLoop(l routing.AddrLoop, rule routing.Rule) error { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) b, err := r.pm.Get(l.Local.Port) if err != nil { @@ -531,14 +569,18 @@ func (r *Router) confirmLoop(l routing.AddrLoop, rule routing.Rule) error { return nil } -func (r *Router) closeLoop(appConn *app.Protocol, loop routing.AddrLoop) error { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) +func (r *Router) closeLoop(loop routing.AddrLoop) error { + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) if err := r.destroyLoop(loop); err != nil { r.Logger.Warnf("Failed to remove loop: %s", err) } + if r.isPubKeyLocal(loop.Remote.PubKey) { + r.Logger.Debug("%v isPubKeyLocal() == true", th.GetCaller()) + return nil + } proto, tr, err := r.setupProto(context.Background()) if err != nil { return err @@ -559,8 +601,8 @@ func (r *Router) closeLoop(appConn *app.Protocol, loop routing.AddrLoop) error { } func (r *Router) loopClosed(loop routing.AddrLoop) error { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) b, err := r.pm.Get(loop.Local.Port) if err != nil { @@ -580,8 +622,8 @@ func (r *Router) loopClosed(loop routing.AddrLoop) error { } func (r *Router) destroyLoop(loop routing.AddrLoop) error { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) if r == nil { return nil @@ -602,17 +644,17 @@ func (r *Router) destroyLoop(loop routing.AddrLoop) error { } func (r *Router) setupProto(ctx context.Context) (*setup.Protocol, transport.Transport, error) { - r.Logger.Debug(testhelpers.Trace("ENTER")) - defer r.Logger.Debug(testhelpers.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) if len(r.config.SetupNodes) == 0 { - r.Logger.Warn("[Router.setupProto] r.setupProto: route setup: no nodes ") + r.Logger.Warnf("%v : route setup: no nodes", th.GetCaller()) return nil, nil, errors.New("route setup: no nodes") } tr, err := r.tm.CreateSetupTransport(ctx, r.config.SetupNodes[0], dmsg.Type) if err != nil { - r.Logger.Warnf("[Router.setupProto] r.setupProto r.tm.CreateSetupTransport error:%v\n", err) + r.Logger.Warnf("%v r.tm.CreateSetupTransport error:%v\n", th.GetCaller(), err) return nil, nil, fmt.Errorf("setup transport: %s", err) } @@ -621,8 +663,10 @@ func (r *Router) setupProto(ctx context.Context) (*setup.Protocol, transport.Tra } func (r *Router) fetchBestRoutes(source, destination cipher.PubKey) (fwd routing.Route, rev routing.Route, err error) { - r.Logger.Infof("[Router.fetchBestRoutes] Requesting new routes from %s to %s", source, destination) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Infof("[Router.fetchBestRoutes] Requesting new routes from %s to %s", source, destination) timer := time.NewTimer(time.Second * 10) defer timer.Stop() @@ -637,12 +681,15 @@ fetchRoutesAgain: } } - r.Logger.Infof("[Router.fetchBestRoutes] Found routes Forward: %s. Reverse %s success", fwdRoutes, revRoutes) + r.Logger.Infof("%v Found routes Forward: %s. Reverse %s success", th.GetCaller(), fwdRoutes, revRoutes) return fwdRoutes[0], revRoutes[0], nil } // IsSetupTransport checks whether `tr` is running in the `setup` mode. func (r *Router) IsSetupTransport(tr *transport.ManagedTransport) bool { + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) + for _, pk := range r.config.SetupNodes { if tr.RemotePK() == pk { return true diff --git a/pkg/router/router_close_test.go b/pkg/router/router_close_test.go index b62bd5150..2e018184e 100644 --- a/pkg/router/router_close_test.go +++ b/pkg/router/router_close_test.go @@ -13,7 +13,7 @@ import ( "github.com/skycoin/skycoin/src/util/logging" "github.com/stretchr/testify/require" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" "github.com/skycoin/skywire/pkg/setup" @@ -120,7 +120,7 @@ func TestRouterCloseLoop(t *testing.T) { require.Nil(t, rule) require.NoError(t, - testhelpers.NoErrorWithinTimeoutN(serveErrCh, serveAppErrCh, protoServeErrCh)) + th.NoErrorWithinTimeoutN(serveErrCh, serveAppErrCh, protoServeErrCh)) } func TestRouterCloseLoopOnAppClose(t *testing.T) { @@ -218,5 +218,9 @@ func TestRouterCloseLoopOnAppClose(t *testing.T) { require.NoError(t, err) require.Nil(t, rule) - require.NoError(t, testhelpers.NoErrorWithinTimeoutN(serveErrCh, serveAppErrCh, protoServeErrCh)) + require.NoError(t, + th.NoErrorWithinTimeoutN( + serveErrCh, + serveAppErrCh, + protoServeErrCh)) } diff --git a/pkg/router/router_setup_test.go b/pkg/router/router_setup_test.go index 4cb6b7b79..9a2b7f734 100644 --- a/pkg/router/router_setup_test.go +++ b/pkg/router/router_setup_test.go @@ -14,7 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" routeFinder "github.com/skycoin/skywire/pkg/route-finder/client" "github.com/skycoin/skywire/pkg/routing" @@ -242,8 +242,8 @@ func TestRouterSetup(t *testing.T) { assert.Nil(t, rule) }) - // testhelpers.Timeout = time.Second * 5 - require.NoError(t, testhelpers.NoErrorWithinTimeoutN( + // th.Timeout = time.Second * 5 + require.NoError(t, th.NoErrorWithinTimeoutN( protoServeErr1Ch, protoServeErr2Ch, serveAppErr1Ch, @@ -337,7 +337,7 @@ func TestRouterSetupLoop(t *testing.T) { assert.Equal(t, pk1, addr.PubKey) assert.Equal(t, routing.Port(10), addr.Port) - require.NoError(t, testhelpers.NoErrorWithinTimeoutN(serveErrCh, serveAppErrCh, protoServeErrCh)) + require.NoError(t, th.NoErrorWithinTimeoutN(serveErrCh, serveAppErrCh, protoServeErrCh)) } func TestRouterSetupLoopLocal(t *testing.T) { @@ -370,7 +370,6 @@ func TestRouterSetupLoopLocal(t *testing.T) { assert.Equal(t, pk, addr.PubKey) assert.Equal(t, routing.Port(10), addr.Port) - require.NoError(t, testhelpers.NoErrorWithinTimeoutN(serveAppErrCh, protoServeErrCh)) + require.NoError(t, th.NoErrorWithinTimeoutN(serveAppErrCh, protoServeErrCh)) } - diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index d5e414c51..a9d5bec52 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -14,7 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" "github.com/skycoin/skywire/pkg/transport" @@ -147,7 +147,7 @@ func TestRouterAppInit(t *testing.T) { require.NoError(t, proto.Close()) require.NoError(t, r.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeoutN(serveErrCh, errCh)) + require.NoError(t, th.NoErrorWithinTimeoutN(serveErrCh, errCh)) } func TestRouterApp(t *testing.T) { @@ -241,11 +241,13 @@ func TestRouterApp(t *testing.T) { require.NoError(t, r.Close()) require.NoError(t, <-errCh) - require.NoError(t, m2.Close()) - require.NoError(t, - testhelpers.NoErrorWithinTimeoutN(trServeErrCh, protoServeErrCh, sendErrCh, serveAppErrCh)) + th.NoErrorWithinTimeoutN( + trServeErrCh, + protoServeErrCh, + sendErrCh, + serveAppErrCh)) } @@ -317,7 +319,13 @@ func TestRouterLocalApp(t *testing.T) { require.NoError(t, r.Close()) require.NoError(t, - testhelpers.NoErrorWithinTimeoutN(errCh, protoServeErr1Ch, protoServeErr2Ch, sendErrCh, serveAppErr1Ch, serveAppErr2Ch)) + th.NoErrorWithinTimeoutN( + errCh, + protoServeErr1Ch, + protoServeErr2Ch, + sendErrCh, + serveAppErr1Ch, + serveAppErr2Ch)) } func TestRouterRouteExpiration(t *testing.T) { @@ -354,5 +362,5 @@ func TestRouterRouteExpiration(t *testing.T) { assert.Equal(t, 0, rt.Count()) require.NoError(t, r.Close()) - require.NoError(t, testhelpers.NoErrorWithinTimeout(serveErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(serveErrCh)) } diff --git a/pkg/routing/packet.go b/pkg/routing/packet.go index 16c2a5c36..6495ac743 100644 --- a/pkg/routing/packet.go +++ b/pkg/routing/packet.go @@ -4,7 +4,7 @@ import ( "encoding/binary" "math" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" ) // RouteID represents ID of a Route in a Packet. @@ -16,8 +16,8 @@ type Packet []byte // MakePacket constructs a new Packet. If payload size is more than // uint16, MakePacket will panic. func MakePacket(id RouteID, payload []byte) Packet { - log.Debug(testhelpers.Trace("ENTER")) - defer log.Debug(testhelpers.Trace("ENTER")) + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("ENTER")) if len(payload) > math.MaxUint16 { panic("packet size exceeded") diff --git a/pkg/transport/managed_transport.go b/pkg/transport/managed_transport.go index 6692c14d2..eb0bb0bec 100644 --- a/pkg/transport/managed_transport.go +++ b/pkg/transport/managed_transport.go @@ -53,7 +53,7 @@ func (tr *ManagedTransport) Read(p []byte) (n int, err error) { // Write writes to an underlying transport. func (tr *ManagedTransport) Write(p []byte) (n int, err error) { - + tr.mu.RLock() n, err = tr.Transport.Write(p) if n > 0 { diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 87a8b243e..0ced30c85 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -13,6 +13,8 @@ import ( "github.com/google/uuid" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" + + th "github.com/skycoin/skywire/internal/testhelpers" ) // ManagerConfig configures a Manager. @@ -212,14 +214,17 @@ func (tm *Manager) Serve(ctx context.Context) error { // CreateSetupTransport begins to attempt to establish setup transports to the given 'remote' node. func (tm *Manager) CreateSetupTransport(ctx context.Context, remote cipher.PubKey, tpType string) (Transport, error) { + tm.Logger.Debug(th.Trace("ENTER")) + defer tm.Logger.Debug(th.Trace("EXIT")) + factory, ok := tm.factories[tpType] if !ok { - tm.Logger.Warn("Manager.CreateSetupTransport err: unknown transport type") - return nil, errors.New("unknown transport type") + tm.Logger.Warnf("%v err: unknown transport type: %v", th.GetCaller(), tpType) + return nil, fmt.Errorf("unknown transport type %v", tpType) } tr, err := factory.Dial(ctx, remote) if err != nil { - tm.Logger.Warnf("Manager.CreateSetupTransport factory.Dial err: %v\n", err) + tm.Logger.Warnf("%v factory.Dial err: %v\n", th.GetCaller(), err) return nil, err } tm.Logger.Infof("Dialed to setup node %s using %s factory.", remote, tpType) @@ -228,6 +233,9 @@ func (tm *Manager) CreateSetupTransport(ctx context.Context, remote cipher.PubKe // CreateDataTransport begins to attempt to establish data transports to the given 'remote' node. func (tm *Manager) CreateDataTransport(ctx context.Context, remote cipher.PubKey, tpType string, public bool) (*ManagedTransport, error) { + tm.Logger.Debug(th.Trace("ENTER")) + defer tm.Logger.Debug(th.Trace("EXIT")) + factory, ok := tm.factories[tpType] if !ok { return nil, errors.New("unknown transport type") @@ -243,7 +251,7 @@ func (tm *Manager) CreateDataTransport(ctx context.Context, remote cipher.PubKey oldTr.killWorker() } - tm.Logger.Infof("Dialed to %s using %s factory. Transport ID: %s", remote, tpType, entry.ID) + tm.Logger.Infof("%v: Dialed to %s using %s factory. Transport ID: %s", th.GetCaller(), remote, tpType, entry.ID) mTr := newManagedTransport(tr, *entry, false) tm.mu.Lock() @@ -319,6 +327,9 @@ func (tm *Manager) Close() error { } func (tm *Manager) dialTransport(ctx context.Context, factory Factory, remote cipher.PubKey, public bool) (Transport, *Entry, error) { + tm.Logger.Debug(th.Trace("ENTER")) + defer tm.Logger.Debug(th.Trace("EXIT")) + if tm.isClosing() { return nil, nil, errors.New("transport.Manager is closing. Skipping dialing transport") } diff --git a/pkg/transport/manager_test.go b/pkg/transport/manager_test.go index eeff2643e..b4d8fc37e 100644 --- a/pkg/transport/manager_test.go +++ b/pkg/transport/manager_test.go @@ -15,7 +15,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" ) func TestMain(m *testing.M) { @@ -251,7 +251,7 @@ func TestTransportManagerLogs(t *testing.T) { require.NoError(t, m2.Close()) require.NoError(t, m1.Close()) require.NoError(t, <-errCh) - require.NoError(t, testhelpers.NoErrorWithinTimeout(writeErrCh)) + require.NoError(t, th.NoErrorWithinTimeout(writeErrCh)) } func ExampleSortEdges() { diff --git a/pkg/transport/mock.go b/pkg/transport/mock.go index bdc4820fb..da97bf6c1 100644 --- a/pkg/transport/mock.go +++ b/pkg/transport/mock.go @@ -28,7 +28,6 @@ type MockFactory struct { fType string } - // NewMockFactoryPair constructs a pair of MockFactories. func NewMockFactoryPair(local, remote cipher.PubKey) (*MockFactory, *MockFactory) { var ( diff --git a/pkg/transport/tcp_transport.go b/pkg/transport/tcp_transport.go index fd03ac2ae..6750c01ea 100644 --- a/pkg/transport/tcp_transport.go +++ b/pkg/transport/tcp_transport.go @@ -12,7 +12,8 @@ import ( "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" - "github.com/skycoin/skywire/internal/testhelpers" + + th "github.com/skycoin/skywire/internal/testhelpers" ) // ErrUnknownRemote returned for connection attempts for remotes @@ -29,8 +30,8 @@ type TCPFactory struct { // NewTCPFactory constructs a new TCP Factory func NewTCPFactory(pk cipher.PubKey, pubkeysFile string, tcpAddr string, logger *logging.Logger) (Factory, error) { - logger.Debug(testhelpers.Trace("ENTER")) - defer logger.Debug(testhelpers.Trace("EXIT")) + logger.Debug(th.Trace("ENTER")) + defer logger.Debug(th.Trace("EXIT")) pkTbl, err := FilePubKeyTable(pubkeysFile) if err != nil { @@ -51,12 +52,10 @@ func NewTCPFactory(pk cipher.PubKey, pubkeysFile string, tcpAddr string, logger return &TCPFactory{pk, pkTbl, tcpListener, logger}, nil } -var _TR = testhelpers.Trace - // Accept accepts a remotely-initiated Transport. func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { - f.Logger.Info(_TR("ENTER")) - defer f.Logger.Info(_TR("EXIT")) + f.Logger.Debug(th.Trace("ENTER")) + defer f.Logger.Debug(th.Trace("EXIT")) conn, err := f.Lsr.AcceptTCP() if err != nil { @@ -78,8 +77,8 @@ func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { // Dial initiates a Transport with a remote node. func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, error) { - f.Logger.Info(_TR("ENTER")) - defer f.Logger.Info(_TR("EXIT")) + f.Logger.Debug(th.Trace("ENTER")) + defer f.Logger.Debug(th.Trace("EXIT")) addr := f.PkTable.RemoteAddr(remote) if addr == "" { @@ -101,7 +100,7 @@ func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, locAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%v:%v", lsnAddr.IP.String(), "0")) if err != nil { f.Logger.Warnf("[TCPFactory.Dial] net.ResolveTCPAddr_locAddr err: %v\n", err) - net.ResolveTCPAddr("tcp", f.Lsr.Addr().String()) + // net.ResolveTCPAddr("tcp", f.Lsr.Addr().String()) return nil, fmt.Errorf("error in constructing local address ") } @@ -116,8 +115,8 @@ func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, // Close implements io.Closer func (f *TCPFactory) Close() error { - f.Logger.Debug(testhelpers.Trace("ENTER")) - defer f.Logger.Debug(testhelpers.Trace("EXIT")) + f.Logger.Debug(th.Trace("ENTER")) + defer f.Logger.Debug(th.Trace("EXIT")) if f == nil { return nil @@ -211,6 +210,9 @@ type filePKTable struct { // FilePubKeyTable returns file based implementation of the PubKeyTable. func FilePubKeyTable(dbFile string) (PubKeyTable, error) { + log.Debug(th.Trace("ENTER")) + defer log.Debug(th.Trace("EXIT")) + path, err := filepath.Abs(dbFile) if err != nil { return nil, err diff --git a/pkg/transport/tcp_transport_test.go b/pkg/transport/tcp_transport_test.go index 1cc4c66b6..528aa449f 100644 --- a/pkg/transport/tcp_transport_test.go +++ b/pkg/transport/tcp_transport_test.go @@ -30,13 +30,13 @@ func Example_transport_TCPFactory() { ipA := "skyhost_001:9119" ipB := "skyhost_002:9119" - addrA, _ := net.ResolveTCPAddr("tcp", ipA) + addrA, _ := net.ResolveTCPAddr("tcp", ipA) //nolint: errcheck lsnA, err := net.ListenTCP("tcp", addrA) if err != nil { fmt.Println(err) } - addrB, _ := net.ResolveTCPAddr("tcp", ipB) + addrB, _ := net.ResolveTCPAddr("tcp", ipB) //nolint: errcheck lsnB, err := net.ListenTCP("tcp", addrB) if err != nil { fmt.Println(err) @@ -67,7 +67,6 @@ func Example_transport_TCPFactory() { return } fmt.Printf("Write success: %v\n", err == nil) - return }() go func() { @@ -85,7 +84,7 @@ func Example_transport_TCPFactory() { fmt.Printf("Read err: %v\n", err) } - fmt.Printf("Message recieved: %s\n", buf) + fmt.Printf("Message received: %s\n", buf) }() wg.Wait() @@ -94,7 +93,7 @@ func Example_transport_TCPFactory() { // Unordered output: Accept success: true // Write success: true // Dial success: true - // Message recieved: Hallo! + // Message received: Hallo! // Finish } @@ -239,7 +238,7 @@ func Example_transport_FilePubKeyTable() { fmt.Sprintf("%s\t%s\n", pkB, ipB)) fmt.Printf("pubkeys:\n%v", pkFileContent) - tmpfile, _ := ioutil.TempFile("", "pktable") + tmpfile, _ := ioutil.TempFile("", "pktable") //nolint: errcheck _, err := tmpfile.Write([]byte(pkFileContent)) fmt.Printf("Write file success: %v\n", err == nil) diff --git a/pkg/visor/multihead_test.go b/pkg/visor/multihead_test.go index c034e3880..ee4eb3445 100644 --- a/pkg/visor/multihead_test.go +++ b/pkg/visor/multihead_test.go @@ -4,6 +4,7 @@ package visor import ( "bytes" + "context" "encoding/json" "fmt" "net/http" @@ -15,6 +16,8 @@ import ( "time" "github.com/skycoin/dmsg/cipher" + + "github.com/skycoin/skywire/pkg/router" "github.com/skycoin/skywire/pkg/routing" ) @@ -45,7 +48,10 @@ type MultiHead struct { initErrs chan error startErrs chan error stopErrs chan error - sync.Once + + repOnce sync.Once + startOnce sync.Once + stopOnce sync.Once } func initMultiHead() *MultiHead { @@ -56,7 +62,7 @@ func initMultiHead() *MultiHead { func Example_initMultiHead() { mh := initMultiHead() - mh.Log.Write([]byte("initMultiHead success")) + _, _ = mh.Log.Write([]byte("initMultiHead success")) //nolint: errcheck fmt.Printf("%v\n", mh.Log.records) // Output: [initMultiHead success] @@ -71,8 +77,8 @@ func (mh *MultiHead) readBaseConfig(cfgFile string) error { if err := json.NewDecoder(rdr).Decode(&baseCfg); err != nil { return err } - baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") - baseCfg.AppsPath, _ = filepath.Abs("../../apps") + baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") // nolint: errcheck + baseCfg.AppsPath, _ = filepath.Abs("../../apps") // nolint: errcheck baseCfg.Routing.Table.Type = "memory" mh.baseCfg = baseCfg @@ -81,14 +87,15 @@ func (mh *MultiHead) readBaseConfig(cfgFile string) error { func Example_readBaseConfig() { mh := initMultiHead() - cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") - err = mh.readBaseConfig(cfgFile) + cfgFile, _ := filepath.Abs("../../integration/tcp-tr/nodeA.json") //nolint: errcheck + err := mh.readBaseConfig(cfgFile) fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) // Output: mh.ReadBaseConfig success: true } func (mh *MultiHead) initIPPool(ipTemplate string, n uint) { + ipPool := make([]string, n) for i := uint(1); i < n+1; i++ { ipPool[i-1] = fmt.Sprintf(ipTemplate, i) @@ -101,7 +108,6 @@ func (mh *MultiHead) initIPPool(ipTemplate string, n uint) { func ExampleMultiHead_initIPPool() { mh := initMultiHead() mh.initIPPool("12.12.12.%d", 16) - fmt.Printf("IP pool length: %v\n", len(mh.ipPool)) // Output: IP pool length: 16 @@ -115,7 +121,7 @@ func (mh *MultiHead) initCfgPool() { for i := 0; i < len(mh.ipPool); i++ { ip := fmt.Sprintf(mh.ipTemplate, i+1) localPath := fmt.Sprintf(localPathTmplt, i+1) - pk, sk, _ := cipher.GenerateDeterministicKeyPair([]byte(ip)) + pk, sk, _ := cipher.GenerateDeterministicKeyPair([]byte(ip)) //nolint: errcheck baseCfg.Node.StaticPubKey = pk baseCfg.Node.StaticSecKey = sk @@ -142,38 +148,41 @@ func (mh *MultiHead) initCfgPool() { func Example_initCfgPool() { mh := initMultiHead() - cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") - err = mh.readBaseConfig(cfgFile) + cfgFile, _ := filepath.Abs("../../integration/tcp-tr/nodeA.json") //nolint: errcheck + err := mh.readBaseConfig(cfgFile) fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) - mh.initIPPool("12.12.12.%d", 16) + mh.initIPPool("skyhost%03d", 16) mh.initCfgPool() - fmt.Printf("len(mh.cfgPool): %v\n", len(mh.cfgPool)) // Output: mh.ReadBaseConfig success: true // len(mh.cfgPool): 16 } -func (mh *MultiHead) initNodes() { - mh.nodes = make([]*Node, len(mh.cfgPool)) - mh.initErrs = make(chan error, len(mh.cfgPool)) - - subs := make([]struct{ old, new string }, len(mh.cfgPool)+1) +func (mh *MultiHead) pkAliases() []strsub { + subs := make([]strsub, len(mh.cfgPool)+1) for i := 0; i < len(mh.cfgPool); i++ { - subs[i] = struct{ old, new string }{ + subs[i] = strsub{ old: fmt.Sprintf("%v", mh.cfgPool[i].Node.StaticPubKey), new: fmt.Sprintf("PK(%v)", mh.ipPool[i]), } } - subs = append(subs, struct{ old, new string }{ - "000000000000000000000000000000000000000000000000000000000000000000", "PK{NULL}"}) + return append(subs, strsub{ + fmt.Sprintf("%v", cipher.PubKey{}), "PK{NULL}"}) +} + +func (mh *MultiHead) initNodes() { + mh.nodes = make([]*Node, len(mh.cfgPool)) + mh.initErrs = make(chan error, len(mh.cfgPool)) + mh.startErrs = make(chan error, len(mh.cfgPool)) + mh.stopErrs = make(chan error, len(mh.cfgPool)) var err error for i := 0; i < len(mh.nodes); i++ { - logger := NewTaggedMasterLogger(mh.ipPool[i], subs) + logger := NewTaggedMasterLogger(mh.ipPool[i], mh.pkAliases()) logger.Out = mh.Log logger.SetReportCaller(true) - // logger.SetFormatter(new(logrus.JSONFormatter)) + mh.nodes[i], err = NewNode(&mh.cfgPool[i], logger) if err != nil { mh.initErrs <- fmt.Errorf("error %v starting node %v", err, i) @@ -183,51 +192,54 @@ func (mh *MultiHead) initNodes() { func ExampleMultiHead_initNodes() { mh := initMultiHead() - cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") - err = mh.readBaseConfig(cfgFile) + cfgFile, _ := filepath.Abs("../../integration/tcp-tr/nodeA.json") //nolint: errcheck + err := mh.readBaseConfig(cfgFile) fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) mh.initIPPool("skyhost_%03d", 16) mh.initCfgPool() mh.initNodes() - close(mh.initErrs) - for err := range mh.initErrs { - fmt.Printf("%v\n", err) - } - fmt.Printf("Errors on initNodes: %v\n", len(mh.initErrs)) + fmt.Print(mh.errReport()) // Output: mh.ReadBaseConfig success: true - // Errors on initNodes: 0 + + // init errors: 0 + // start errors: 0 + // stop errors: 0 } func (mh *MultiHead) startNodes(postdelay time.Duration) { - mh.startErrs = make(chan error, len(mh.nodes)) - for i := 0; i < len(mh.nodes); i++ { - go func(n int) { - if err := mh.nodes[n].Start(); err != nil { - mh.startErrs <- fmt.Errorf("error %v starting node %v", err, n) - } - }(i) - } - time.Sleep(postdelay) + mh.startOnce.Do(func() { + mh.startErrs = make(chan error, len(mh.nodes)) + for i := 0; i < len(mh.nodes); i++ { + go func(n int) { + if err := mh.nodes[n].Start(); err != nil { + mh.startErrs <- fmt.Errorf("error %v starting node %v", err, n) + } + }(i) + } + time.Sleep(postdelay) + }) } func (mh *MultiHead) stopNodes(predelay time.Duration) { - time.Sleep(predelay) - mh.stopErrs = make(chan error, len(mh.nodes)) - for i := 0; i < len(mh.nodes); i++ { - go func(n int) { - if err := mh.nodes[n].Close(); err != nil { - mh.stopErrs <- fmt.Errorf("error %v starting node %v", err, n) - } - }(i) - } + mh.stopOnce.Do(func() { + time.Sleep(predelay) + mh.stopErrs = make(chan error, len(mh.nodes)) + for i := 0; i < len(mh.nodes); i++ { + go func(n int) { + if err := mh.nodes[n].Close(); err != nil { + mh.stopErrs <- fmt.Errorf("error %v stopping node %v", err, n) + } + }(i) + } + }) } func makeMultiHeadN(n uint) *MultiHead { mh := initMultiHead() - cfgFile, err := filepath.Abs("../../integration/tcp-tr/nodeA.json") - err = mh.readBaseConfig(cfgFile) + cfgFile, _ := filepath.Abs("../../integration/tcp-tr/nodeA.json") //nolint: errcheck + err := mh.readBaseConfig(cfgFile) if err != nil { panic(err) } @@ -238,7 +250,7 @@ func makeMultiHeadN(n uint) *MultiHead { } func (mh *MultiHead) errReport() string { - mh.Do(func() { + mh.repOnce.Do(func() { close(mh.initErrs) close(mh.startErrs) close(mh.stopErrs) @@ -272,11 +284,11 @@ func ExampleMultiHead_startNodes() { // stop errors: 0 } -func (mh *MultiHead) sendMessage(sender, reciever uint, message string) (*http.Response, error) { +func (mh *MultiHead) sendMessage(sender, receiver uint, message string) (*http.Response, error) { url := fmt.Sprintf("http://%s:8001/message", mh.ipPool[sender]) msgData := map[string]string{ - "recipient": fmt.Sprintf("%s", mh.cfgPool[reciever].Node.StaticPubKey), - "message": "Hello", + "recipient": mh.cfgPool[receiver].Node.StaticPubKey.String(), + "message": message, } data, _ := json.Marshal(msgData) // nolint: errcheck return http.Post(url, "application/json", bytes.NewBuffer(data)) // nolint: gosec @@ -292,11 +304,12 @@ func (mh *MultiHead) genPubKeysFile() string { } func Example_genPubKeysFile() { - mh := makeMultiHeadN(128) - fmt.Printf("%v\n", mh.genPubKeysFile()) + pkFile := mh.genPubKeysFile() - // Output: ZZZ + fmt.Printf("pkFile generated with %v records\n", len(strings.Split(pkFile, "\n"))) + _, _ = os.Stderr.WriteString(pkFile) //nolint: errcheck + // Output: pkFile generated with 129 records } func (mhl *multiheadLog) filter(pattern string, match bool) []string { @@ -324,48 +337,105 @@ func ExampleMultiHead_sendMessage_msg001() { recs := mh.Log.filter(`skyhost_001.*received.*"message"`, true) if len(recs) == 1 { - fmt.Println("skyhost_001 recieved message") + fmt.Println("skyhost_001 received message") } // Output: err: // init errors: 0 // start errors: 0 // stop errors: 0 - // skyhost_001 recieved message + // skyhost_001 received message } -// WIP func ExampleMultiHead_sendMessage_msg002() { mh := makeMultiHeadN(2) - mh.startNodes(time.Second) + mh.RunExample(func(mh *MultiHead) { + _, err := mh.sendMessage(0, 1, "Hello002") + fmt.Printf("err: %v", err) + }) - _, err := mh.sendMessage(0, 1, "Hello002") - fmt.Printf("err: %v", err) + // fmt.Printf("%v\n", mh.errReport()) + // fmt.Printf("%v", mh.Log.filter(`skychat`, false)) + fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) + // fmt.Printf("%v", mh.Log.filter(`skychat`, false)) + // Output: err: + // init errors: 0 + // start errors: 0 + // stop errors: 0 + +} + +func (mh *MultiHead) RunExample(example func(mh *MultiHead)) { + mh.startNodes(time.Second) + example(mh) mh.stopNodes(time.Second) +} + +func ExampleMultiHead_confirmAppPacket() { + mh := makeMultiHeadN(2) + + mh.RunExample(func(mh *MultiHead) { + fmt.Printf("HHIIII!") + }) + fmt.Printf("%v\n", mh.errReport()) + fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) + + // Output: ZZZZ +} - fmt.Printf("%v", mh.Log.filter(`skychat`, false)) - // fmt.Printf("%v\n", strings.Join(mh.Log.records[20:], "")) +func ExampleMultihead_forwardAppPacket() { + mh := makeMultiHeadN(2) + mh.RunExample(func(mh *MultiHead) { + cfgA, _ := mh.cfgPool[0], mh.cfgPool[1] + nodeA, nodeB := mh.nodes[0], mh.nodes[1] + pkA, _ := nodeA.config.Node.StaticPubKey, nodeB.config.Node.StaticPubKey + tmA, _ := nodeA.tm, nodeB.tm - // fmt.Printf("%v", mh.Log.filter(`skychat`, true)) + rA, ok := nodeA.router.(*router.Router) + fmt.Printf("rA is %T %v\n", rA, ok) + rB, ok := nodeB.router.(*router.Router) + fmt.Printf("rB is %T %v\n", rB, ok) - // Output: err: - // init errors: 0 - // start errors: 0 - // stop errors: 0 + rtA, err := cfgA.RoutingTable() + fmt.Printf("rtA is %T %v\n", rtA, err) + rtB, err := cfgA.RoutingTable() + fmt.Printf("rtB is %T %v\n", rtB, err) + + trA, err := tmA.CreateDataTransport(context.TODO(), pkA, "tcp-transport", true) + fmt.Printf("CreateDataTransport trA %T success: %v\n", trA, err == nil) + + ruleA := routing.ForwardRule(time.Now().Add(time.Hour), 4, trA.Entry.ID) + fmt.Printf("ruleA: %v\n", ruleA) + + routeA, err := rtA.AddRule(ruleA) + fmt.Printf("routeA: %v, err: %v\n", routeA, err) + + // time.Sleep(100 * time.Millisecond) + packetA := routing.MakePacket(routeA, []byte("Hello")) + fmt.Printf("packetA: %v\n", packetA) + n, err := trA.Write(packetA) + fmt.Printf("trA.Write(packetA): %v %v\n", n, err) + }) + + _, _ = os.Stderr.WriteString(fmt.Sprintf("%v\n", mh.errReport())) //nolint: errcheck + _, _ = os.Stderr.WriteString(fmt.Sprintf("%v\n", strings.Join(mh.Log.records, ""))) //nolint: errcheck + // fmt.Printf("%v\n", mh.Log.filter(`skychat`, false)) + + // Output: ZZZZ } func ExampleMultiHead_sendMessage_msg003() { mh := makeMultiHeadN(1) - mh.startNodes(time.Second) + mh.RunExample(func(mh *MultiHead) { + _, err := mh.sendMessage(0, 0, "Hello003") + fmt.Printf("err: %v", err) - _, err := mh.sendMessage(0, 0, "Hello003") - fmt.Printf("err: %v", err) + }) - mh.stopNodes(time.Second) fmt.Printf("%v\n", mh.errReport()) fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) @@ -373,5 +443,4 @@ func ExampleMultiHead_sendMessage_msg003() { // init errors: 0 // start errors: 0 // stop errors: 0 - } diff --git a/pkg/visor/rpc_test.go b/pkg/visor/rpc_test.go index 3e167b995..d57052f27 100644 --- a/pkg/visor/rpc_test.go +++ b/pkg/visor/rpc_test.go @@ -49,8 +49,6 @@ func TestListApps(t *testing.T) { assert.Equal(t, AppStatusRunning, app2.Status) } -var rtr router.Router - func TestStartStopApp(t *testing.T) { pk, _ := cipher.GenerateKeyPair() router := new(router.MockRouter) diff --git a/pkg/visor/taggedformatter.go b/pkg/visor/taggedformatter.go index edcfba76a..3134ef8d9 100644 --- a/pkg/visor/taggedformatter.go +++ b/pkg/visor/taggedformatter.go @@ -6,15 +6,18 @@ import ( "github.com/sirupsen/logrus" "github.com/skycoin/skycoin/src/util/logging" - "github.com/skycoin/skywire/internal/testhelpers" + + th "github.com/skycoin/skywire/internal/testhelpers" ) // TaggedFormatter appends tag to log records and substitutes text type TaggedFormatter struct { tag []byte - subs []struct{ old, new []byte } + subs []bytesub *logging.TextFormatter } +type strsub struct{ old, new string } +type bytesub struct{ old, new []byte } // Format executes formatting of TaggedFormatter func (tf *TaggedFormatter) Format(entry *logrus.Entry) ([]byte, error) { @@ -22,18 +25,16 @@ func (tf *TaggedFormatter) Format(entry *logrus.Entry) ([]byte, error) { for _, sub := range tf.subs { data = bytes.ReplaceAll(data, sub.old, sub.new) } - prepend := bytes.Repeat([]byte(" "), testhelpers.CallerDepth()) + prepend := bytes.Repeat([]byte(" "), th.CallerDepth()) return bytes.Join([][]byte{tf.tag, prepend, data}, []byte(" ")), err } // NewTaggedMasterLogger creates MasterLogger that prepends records with tag -func NewTaggedMasterLogger(tag string, ssubs []struct{ old, new string }) *logging.MasterLogger { - s2bsub := func(s struct{ old, new string }) struct{ old, new []byte } { - return struct{ old, new []byte }{[]byte(s.old), []byte(s.new)} - } - bsubs := make([]struct{ old, new []byte }, len(ssubs)) - for i := 0; i < len(ssubs); i++ { - bsubs[i] = s2bsub(ssubs[i]) +func NewTaggedMasterLogger(tag string, subs []strsub) *logging.MasterLogger { + + bsubs := make([]bytesub, len(subs)) + for i := 0; i < len(subs); i++ { + bsubs[i] = bytesub{[]byte(subs[i].old), []byte(subs[i].new)} } hooks := make(logrus.LevelHooks) diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index f9c0a8434..7f4b7905d 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -22,7 +22,7 @@ import ( "github.com/skycoin/dmsg/noise" "github.com/skycoin/skycoin/src/util/logging" - "github.com/skycoin/skywire/internal/testhelpers" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/internal/therealproxy" "github.com/skycoin/skywire/internal/therealssh" "github.com/skycoin/skywire/pkg/app" @@ -107,6 +107,7 @@ type Node struct { startedApps map[string]*appBind pidMu sync.Mutex + once sync.Once rpcListener net.Listener rpcDialers []*noise.RPCClientDialer @@ -180,7 +181,7 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) DiscoveryClient: trDiscovery, LogStore: logStore, DefaultNodes: config.TrustedNodes, - Logger: masterLogger.PackageLogger("tr-manager"), + Logger: masterLogger.PackageLogger("trmanager"), } appProto.Logger = masterLogger.PackageLogger("app") @@ -192,7 +193,7 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) if err != nil { return nil, fmt.Errorf("transport manager: %s", err) } - node.tm.Logger = node.Logger.PackageLogger("trmanager") + // node.tm.Logger = node.Logger.PackageLogger("trmanager") node.tm.SetSetupNodes(config.Routing.SetupNodes) node.rt, err = config.RoutingTable() @@ -254,8 +255,8 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) // Start spawns auto-started Apps, starts router and RPC interfaces . func (node *Node) Start() error { - node.logger.Debug(testhelpers.Trace("ENTER")) - defer node.logger.Debug(testhelpers.Trace("ENTER")) + node.logger.Debug(th.Trace("ENTER")) + defer node.logger.Debug(th.Trace("ENTER")) ctx := context.Background() @@ -372,35 +373,39 @@ func (node *Node) Close() (err error) { if node == nil { return nil } - if node.rpcListener != nil { - if err = node.rpcListener.Close(); err != nil { - node.logger.WithError(err).Error("failed to stop RPC interface") - } else { - node.logger.Info("RPC interface stopped successfully") + var closeErr error + node.once.Do(func() { + if node.rpcListener != nil { + if err = node.rpcListener.Close(); err != nil { + node.logger.WithError(err).Error("failed to stop RPC interface") + } else { + node.logger.Info("RPC interface stopped successfully") + } } - } - for i, dialer := range node.rpcDialers { - if err = dialer.Close(); err != nil { - node.logger.WithError(err).Errorf("(%d) failed to stop RPC dialer", i) - } else { - node.logger.Infof("(%d) RPC dialer closed successfully", i) + for i, dialer := range node.rpcDialers { + if err = dialer.Close(); err != nil { + node.logger.WithError(err).Errorf("(%d) failed to stop RPC dialer", i) + } else { + node.logger.Infof("(%d) RPC dialer closed successfully", i) + } } - } - node.startedMu.Lock() - for a, bind := range node.startedApps { - if err = node.stopApp(a, bind); err != nil { - node.logger.WithError(err).Errorf("(%s) failed to stop app", a) + node.startedMu.Lock() + for a, bind := range node.startedApps { + if err = node.stopApp(a, bind); err != nil { + node.logger.WithError(err).Errorf("(%s) failed to stop app", a) + } else { + node.logger.Infof("(%s) app stopped successfully", a) + } + } + node.startedMu.Unlock() + if closeErr = node.router.Close(); closeErr != nil { + node.logger.WithError(err).Error("failed to stop router") } else { - node.logger.Infof("(%s) app stopped successfully", a) + node.logger.Info("router stopped successfully") } - } - node.startedMu.Unlock() - if err = node.router.Close(); err != nil { - node.logger.WithError(err).Error("failed to stop router") - } else { - node.logger.Info("router stopped successfully") - } - return err + + }) + return closeErr } // Apps returns list of AppStates for all registered apps. From 7572b29a053eaee63c8c669dffaac34c6d6733b4 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Thu, 22 Aug 2019 14:59:20 +0300 Subject: [PATCH 21/26] tcp-transport finally working --- .../therealproxy-client.go | 2 +- pkg/app/app.go | 8 +- pkg/app/protocol.go | 4 +- pkg/router/port_list.go | 10 + pkg/router/port_manager.go | 9 + pkg/router/router.go | 18 +- pkg/transport/manager.go | 5 + pkg/transport/tcp_transport.go | 2 - pkg/visor/multihead.go | 263 ++++++++++++ pkg/visor/multihead_base_test.go | 86 ++++ pkg/visor/multihead_test.go | 375 +++--------------- pkg/visor/testdata/hosts.pubkeys | 128 ++++++ pkg/visor/testdata/node.json | 53 +++ pkg/visor/visor.go | 11 +- 14 files changed, 637 insertions(+), 337 deletions(-) create mode 100644 pkg/visor/multihead.go create mode 100644 pkg/visor/multihead_base_test.go create mode 100644 pkg/visor/testdata/hosts.pubkeys create mode 100755 pkg/visor/testdata/node.json diff --git a/cmd/apps/therealproxy-client/therealproxy-client.go b/cmd/apps/therealproxy-client/therealproxy-client.go index 26366ea4f..57dca5da2 100644 --- a/cmd/apps/therealproxy-client/therealproxy-client.go +++ b/cmd/apps/therealproxy-client/therealproxy-client.go @@ -19,7 +19,7 @@ import ( const socksPort = 3 -var r = netutil.NewRetrier(time.Second, 0, 1) +var r = netutil.NewRetrier(time.Second, 5, 2) func main() { var addr = flag.String("addr", ":1080", "Client address to listen on") diff --git a/pkg/app/app.go b/pkg/app/app.go index aa6023761..0736db186 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -145,8 +145,8 @@ func (app *App) Accept() (net.Conn, error) { raddr := addrs[1] // TODO: Why is that? - // loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} - loop := routing.AddrLoop{Local: laddr, Remote: raddr} + loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} + // loop := routing.AddrLoop{Local: laddr, Remote: raddr} conn, out := net.Pipe() app.mu.Lock() app.conns[loop] = conn @@ -169,8 +169,8 @@ func (app *App) Dial(raddr routing.Addr) (net.Conn, error) { } // TODO: Why is that way? - // loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} - loop := routing.AddrLoop{Local: laddr, Remote: raddr} + loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} + // loop := routing.AddrLoop{Local: laddr, Remote: raddr} conn, out := net.Pipe() app.mu.Lock() diff --git a/pkg/app/protocol.go b/pkg/app/protocol.go index ff617399c..a7c31d717 100644 --- a/pkg/app/protocol.go +++ b/pkg/app/protocol.go @@ -75,11 +75,8 @@ func NewProtocol(rw io.ReadWriteCloser) *Protocol { func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { Logger.Debug(th.Trace("ENTER")) Logger.Debugf("%v cmd: %v, payload: %v\n", th.GetCaller(), cmd, payload) - Logger.Debugf("%v CALLERS: %v\n", th.GetCaller(), th.GetCallers(3)) - defer Logger.Debug(th.Trace("EXIT")) - id, resChan := p.chans.add() if err := p.writeFrame(cmd, id, payload); err != nil { Logger.Warnf("%v p.writeFrame(%v, %v, %v) err: %v\n", th.GetCaller(), cmd, id, payload, err) @@ -93,6 +90,7 @@ func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { return io.EOF } Logger.Debugf("%v received %v\n", th.GetCaller(), Frame(frame[0])) + Logger.Debugf("Received %v\n", Payload{Frame(frame[0]), frame[2:]}) if Frame(frame[0]) == FrameFailure { Logger.Warnf("%v writeFrame err: %v\n", th.GetCaller(), string(frame[2:])) diff --git a/pkg/router/port_list.go b/pkg/router/port_list.go index 5652d8de6..3a6301abc 100644 --- a/pkg/router/port_list.go +++ b/pkg/router/port_list.go @@ -1,7 +1,9 @@ package router import ( + "fmt" "math" + "strings" "sync" "github.com/skycoin/skywire/pkg/app" @@ -20,6 +22,14 @@ type portList struct { ports map[routing.Port]*portBind } +func (pl *portList) String() string { + var result []string + for k, v := range pl.ports { + result = append(result, fmt.Sprintf("routingPort: %v portBind:%v", k, v)) + } + return fmt.Sprintf("portList %v\n portBind: %v", pl.minPort, strings.Join(result, "\n")) +} + func newPortList(minPort routing.Port) *portList { return &portList{minPort: minPort, ports: make(map[routing.Port]*portBind, minPort)} } diff --git a/pkg/router/port_manager.go b/pkg/router/port_manager.go index 15bcd0565..b2bfd0608 100644 --- a/pkg/router/port_manager.go +++ b/pkg/router/port_manager.go @@ -138,13 +138,22 @@ func (pm *portManager) GetLoop(localPort routing.Port, remoteAddr routing.Addr) b, err := pm.Get(localPort) if err != nil { + fmt.Println("pm.Get err:", err) return nil, err } l := b.loops.get(remoteAddr) if l == nil { + fmt.Println("b.loops.get err:", err) return nil, errors.New("unknown loop") } return l, nil } + +// Portlist - debug only +type Portlist = portList + +func (pm *portManager) Ports() *portList { + return pm.ports +} diff --git a/pkg/router/router.go b/pkg/router/router.go index f8159806d..f8a5fa11c 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -102,7 +102,7 @@ func (r *Router) trFinish(_ error) { } func (r *Router) trLog() *logrus.Entry { - return r.Logger.WithField("M", th.GetCallerN(3)) + return r.Logger.WithField("_module", th.GetCallerN(3)) } // Serve starts transport listening loop. @@ -329,13 +329,16 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { } if appPacket.Loop.Remote.PubKey == r.config.PubKey { - r.trLog().Infof("r.config.PubKey: ", r.config.PubKey) + r.trLog().Info("r.config.PubKey: ", r.config.PubKey) return r.forwardLocalAppPacket(appPacket) } loop, err := r.pm.GetLoop(appPacket.Loop.Local.Port, appPacket.Loop.Remote) r.trLog().Error("r.pm.GetLoop: ", err) + r.trLog().Warn("loop: ", loop) + r.trLog().Errorf("ALL LOOPS: %v\n", r.pm.Ports()) + if err != nil { return err } @@ -343,6 +346,17 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { // r.Logger.Debugf("%v found loop: %v", th.GetCaller(), loop) tr := r.tm.Transport(loop.trID) + r.trLog().Info("trID %v ERRROR %v\n", loop.trID, err) + r.trLog().Errorf("r.tm.transports: %v\n", r.tm.Transports()) + + // 9df41ada-be66-5640-bce2-917988069706 + // trID := uuid.MustParse("9df41ada-be66-5640-bce2-917988069706") + // trID := uuid.MustParse("9e566688-8717-5855-b1b7-92586ffc8c9b") + // fmt.Printf("trID %v ERRROR %v\n", trID, err) + trID := transport.MakeTransportID(r.config.PubKey, appPacket.Loop.Remote.PubKey, "tcp-transport", true) + + tr = r.tm.Transport(trID) + fmt.Print("TRANSPORT: ", tr) if tr == nil { r.trLog().Error("Loop: ", loop) r.trLog().Error("OOOOOOO") diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 0ced30c85..7cb096c35 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -510,3 +510,8 @@ func (tm *Manager) IsSetupPK(pk cipher.PubKey) bool { } return false } + +// Transports returns map of transports +func (tm *Manager) Transports() map[uuid.UUID]*ManagedTransport { + return tm.transports +} diff --git a/pkg/transport/tcp_transport.go b/pkg/transport/tcp_transport.go index 6750c01ea..0b89a6550 100644 --- a/pkg/transport/tcp_transport.go +++ b/pkg/transport/tcp_transport.go @@ -210,8 +210,6 @@ type filePKTable struct { // FilePubKeyTable returns file based implementation of the PubKeyTable. func FilePubKeyTable(dbFile string) (PubKeyTable, error) { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("EXIT")) path, err := filepath.Abs(dbFile) if err != nil { diff --git a/pkg/visor/multihead.go b/pkg/visor/multihead.go new file mode 100644 index 000000000..bb2ce1a2d --- /dev/null +++ b/pkg/visor/multihead.go @@ -0,0 +1,263 @@ +//+build !no_ci + +package visor + +import ( + "bytes" + "encoding/json" + "fmt" + stdlog "log" + "net/http" + "os" + "path/filepath" + "regexp" + "strings" + "sync" + "testing" + "time" + + "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/pkg/routing" +) + +type multiheadLog struct { + mx sync.Mutex + records []string +} + +func (mhl *multiheadLog) Write(p []byte) (n int, err error) { + mhl.mx.Lock() + defer mhl.mx.Unlock() + mhl.records = append(mhl.records, string(p)) + return len(p), nil +} + +func (mhl *multiheadLog) filter(pattern string, match bool) []string { + var result []string + r := regexp.MustCompile(pattern) + for _, line := range mhl.records { + matched := r.MatchString(line) + if (matched && match) || (!matched && !match) { + result = append(result, line) + } + } + return result +} + +// MultiHead allows to run group of nodes in the single process +type MultiHead struct { + baseCfg Config + ipTemplate string + ipPool []string + cfgPool []Config + nodes []*Node + + Log *multiheadLog + initErrs chan error + startErrs chan error + stopErrs chan error + + repOnce sync.Once + startOnce sync.Once + stopOnce sync.Once +} + +func newMultiHead() *MultiHead { + mhLog := multiheadLog{records: []string{}} + return &MultiHead{Log: &mhLog} +} + +func (mh *MultiHead) readBaseConfig(cfgFile string) error { + rdr, err := os.Open(filepath.Clean(cfgFile)) + if err != nil { + return err + } + baseCfg := Config{} + if err := json.NewDecoder(rdr).Decode(&baseCfg); err != nil { + return err + } + baseCfg.PubKeysFile, _ = filepath.Abs("testdata/hosts.pubkeys") // nolint: errcheck + baseCfg.AppsPath, _ = filepath.Abs("../../apps") // nolint: errcheck + baseCfg.Routing.Table.Type = "memory" + + mh.baseCfg = baseCfg + return nil +} + +func (mh *MultiHead) initIPPool(ipTemplate string, n uint) { + + ipPool := make([]string, n) + for i := uint(1); i < n+1; i++ { + ipPool[i-1] = fmt.Sprintf(ipTemplate, i) + } + mh.ipTemplate = ipTemplate + mh.ipPool = ipPool + +} + +func (mh *MultiHead) initCfgPool() { + mh.cfgPool = make([]Config, len(mh.ipPool)) + localPathTmplt := "/tmp/multihead" + baseCfg := mh.baseCfg + + for i := 0; i < len(mh.ipPool); i++ { + ip := fmt.Sprintf(mh.ipTemplate, i+1) + localPath := fmt.Sprintf(localPathTmplt, i+1) + pk, sk, _ := cipher.GenerateDeterministicKeyPair([]byte(ip)) //nolint: errcheck + + baseCfg.Node.StaticPubKey = pk + baseCfg.Node.StaticSecKey = sk + baseCfg.LocalPath = localPath + baseCfg.Interfaces.RPCAddress = fmt.Sprintf("%s:3435", ip) + baseCfg.Transport.LogStore.Location = fmt.Sprintf("%s/transport_logs", localPath) + + baseCfg.Apps = []AppConfig{ + AppConfig{ + App: "skychat", + AutoStart: true, + Port: routing.Port(1), + Args: []string{ + "-addr", + fmt.Sprintf("%v:8001", ip), + }, + }} + baseCfg.TCPTransportAddr = fmt.Sprintf("%v:9119", ip) + + mh.cfgPool[i] = baseCfg + } + +} + +func (mh *MultiHead) pkAliases() []strsub { + subs := make([]strsub, len(mh.cfgPool)+1) + for i := 0; i < len(mh.cfgPool); i++ { + subs[i] = strsub{ + old: fmt.Sprintf("%v", mh.cfgPool[i].Node.StaticPubKey), + new: fmt.Sprintf("PK(%v)", mh.ipPool[i]), + } + } + return append(subs, strsub{ + fmt.Sprintf("%v", cipher.PubKey{}), "PK{NULL}"}) +} + +func (mh *MultiHead) initNodes() { + mh.nodes = make([]*Node, len(mh.cfgPool)) + mh.initErrs = make(chan error, len(mh.cfgPool)) + mh.startErrs = make(chan error, len(mh.cfgPool)) + mh.stopErrs = make(chan error, len(mh.cfgPool)) + + logging.DisableColors() + logging.SetOutputTo(mh.Log) + stdlog.SetOutput(mh.Log) + + var err error + for i := 0; i < len(mh.nodes); i++ { + logger := NewTaggedMasterLogger(mh.ipPool[i], mh.pkAliases()) + logger.Out = mh.Log + // logger.SetReportCaller(true) + stdlog.SetPrefix(mh.ipPool[i]) + + mh.nodes[i], err = NewNode(&mh.cfgPool[i], logger) + if err != nil { + mh.initErrs <- fmt.Errorf("error %v starting node %v", err, i) + } + } +} + +func (mh *MultiHead) startNodes(postdelay time.Duration) { + mh.startOnce.Do(func() { + mh.startErrs = make(chan error, len(mh.nodes)) + for i := 0; i < len(mh.nodes); i++ { + go func(n int) { + if err := mh.nodes[n].Start(); err != nil { + mh.startErrs <- fmt.Errorf("error %v starting node %v", err, n) + } + }(i) + } + time.Sleep(postdelay) + }) +} + +func (mh *MultiHead) stopNodes(predelay time.Duration) { + mh.stopOnce.Do(func() { + time.Sleep(predelay) + mh.stopErrs = make(chan error, len(mh.nodes)) + for i := 0; i < len(mh.nodes); i++ { + go func(n int) { + if err := mh.nodes[n].Close(); err != nil { + mh.stopErrs <- fmt.Errorf("error %v stopping node %v", err, n) + } + }(i) + } + }) +} + +func makeMultiHeadN(n uint) *MultiHead { + mh := newMultiHead() + cfgFile, _ := filepath.Abs("../../integration/tcp-tr/nodeA.json") //nolint: errcheck + err := mh.readBaseConfig(cfgFile) + if err != nil { + panic(err) + } + mh.initIPPool("skyhost_%03d", n) + mh.initCfgPool() + mh.initNodes() + return mh +} + +func (mh *MultiHead) errReport() string { + mh.repOnce.Do(func() { + close(mh.initErrs) + close(mh.startErrs) + close(mh.stopErrs) + }) + collectErrs := func(tmplt string, errs chan error) string { + recs := make([]string, len(errs)+1) + for err := range errs { + recs = append(recs, fmt.Sprintf("%v", err)) + } + recs = append(recs, fmt.Sprintf(tmplt, len(errs))) + return strings.Join(recs, "\n") + } + return strings.Join([]string{ + collectErrs("init errors: %v", mh.initErrs), + collectErrs("start errors: %v", mh.startErrs), + collectErrs("stop errors: %v", mh.stopErrs), + }, "") +} + +func (mh *MultiHead) genPubKeysFile() string { + recs := make([]string, len(mh.cfgPool)) + for i := 0; i < len(mh.cfgPool); i++ { + recs[i] = fmt.Sprintf("%s\t%s\n", mh.cfgPool[i].Node.StaticPubKey, mh.ipPool[i]) + } + return strings.Join(recs, "") +} + +func (mh *MultiHead) sendMessage(sender, receiver uint, message string) (*http.Response, error) { + url := fmt.Sprintf("http://%s:8001/message", mh.ipPool[sender]) + msgData := map[string]string{ + "recipient": mh.cfgPool[receiver].Node.StaticPubKey.String(), + "message": message, + } + data, _ := json.Marshal(msgData) // nolint: errcheck + return http.Post(url, "application/json", bytes.NewBuffer(data)) // nolint: gosec + +} + +// RunExample runs Example-function in multihead env +func (mh *MultiHead) RunExample(example func(mh *MultiHead)) { + mh.startNodes(time.Second) + example(mh) + mh.stopNodes(time.Second) +} + +// RunTest runs test function in multihead env +func (mh *MultiHead) RunTest(t *testing.T, name string, tfunc func(mh *MultiHead)) { + mh.startNodes(time.Second) + t.Run(name, func(t *testing.T) { + tfunc(mh) + }) + mh.stopNodes(time.Second) +} diff --git a/pkg/visor/multihead_base_test.go b/pkg/visor/multihead_base_test.go new file mode 100644 index 000000000..b7c35b4c7 --- /dev/null +++ b/pkg/visor/multihead_base_test.go @@ -0,0 +1,86 @@ +// +build !no_ci + +package visor + +import ( + "fmt" + "path/filepath" + "testing" + "time" + + "github.com/skycoin/skywire/internal/testhelpers" +) + +func Example_newMultiHead() { + + mh := newMultiHead() + _, _ = mh.Log.Write([]byte("initMultiHead success")) //nolint: errcheck + fmt.Printf("%v\n", mh.Log.records) + + // Output: [initMultiHead success] +} + +func Example_readBaseConfig() { + mh := newMultiHead() + cfgFile, _ := filepath.Abs("testdata/node.json") //nolint: errcheck + err := mh.readBaseConfig(cfgFile) + fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) + + // Output: mh.ReadBaseConfig success: true +} + +func ExampleMultiHead_initIPPool() { + mh := newMultiHead() + mh.initIPPool("12.12.12.%d", 16) + fmt.Printf("IP pool length: %v\n", len(mh.ipPool)) + + // Output: IP pool length: 16 +} + +func Example_initCfgPool() { + mh := newMultiHead() + cfgFile, _ := filepath.Abs("../../integration/tcp-tr/nodeA.json") //nolint: errcheck + err := mh.readBaseConfig(cfgFile) + fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) + mh.initIPPool("skyhost%03d", 16) + mh.initCfgPool() + fmt.Printf("len(mh.cfgPool): %v\n", len(mh.cfgPool)) + + // Output: mh.ReadBaseConfig success: true + // len(mh.cfgPool): 16 +} + +func ExampleMultiHead_initNodes() { + mh := newMultiHead() + cfgFile, _ := filepath.Abs("testdata/node.json") //nolint: errcheck + err := mh.readBaseConfig(cfgFile) + fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) + mh.initIPPool("skyhost_%03d", 1) + mh.initCfgPool() + mh.initNodes() + fmt.Print(mh.errReport()) + + // Output: mh.ReadBaseConfig success: true + // init errors: 0 + // start errors: 0 + // stop errors: 0 +} + +func TestMultiHead_startNodes(t *testing.T) { + delay := time.Second + mh := makeMultiHeadN(32) + mh.startNodes(delay) + mh.stopNodes(delay) + + testhelpers.NoErrorWithinTimeoutN( + mh.initErrs, mh.startErrs, mh.stopErrs, + ) +} + +func TestMultiHead_genPubKeysFile(t *testing.T) { + mh := makeMultiHeadN(128) + pkFile := mh.genPubKeysFile() + + fmt.Println(pkFile) + t.Log(pkFile) +} diff --git a/pkg/visor/multihead_test.go b/pkg/visor/multihead_test.go index ee4eb3445..de7d01530 100644 --- a/pkg/visor/multihead_test.go +++ b/pkg/visor/multihead_test.go @@ -3,336 +3,84 @@ package visor import ( - "bytes" "context" - "encoding/json" "fmt" - "net/http" "os" - "path/filepath" - "regexp" "strings" - "sync" + "testing" "time" - "github.com/skycoin/dmsg/cipher" - "github.com/skycoin/skywire/pkg/router" "github.com/skycoin/skywire/pkg/routing" + "github.com/stretchr/testify/require" ) -type multiheadLog struct { - mx sync.Mutex - records []string -} - -func (mhl *multiheadLog) Write(p []byte) (n int, err error) { - mhl.mx.Lock() - defer mhl.mx.Unlock() - mhl.records = append(mhl.records, string(p)) - return len(p), nil -} - /* Prepare IP aliases: -$ for ((i=1; i<=16; i++)){_ ip addr add 12.12.12.$i/32 dev lo} +$ for ((i=1; i<=16; i++)){sudo ip addr add 12.12.12.$i/32 dev lo} */ -type MultiHead struct { - baseCfg Config - ipTemplate string - ipPool []string - cfgPool []Config - nodes []*Node - - Log *multiheadLog - initErrs chan error - startErrs chan error - stopErrs chan error - - repOnce sync.Once - startOnce sync.Once - stopOnce sync.Once -} - -func initMultiHead() *MultiHead { - mhLog := multiheadLog{records: []string{}} - return &MultiHead{Log: &mhLog} -} - -func Example_initMultiHead() { - - mh := initMultiHead() - _, _ = mh.Log.Write([]byte("initMultiHead success")) //nolint: errcheck - fmt.Printf("%v\n", mh.Log.records) - - // Output: [initMultiHead success] -} - -func (mh *MultiHead) readBaseConfig(cfgFile string) error { - rdr, err := os.Open(filepath.Clean(cfgFile)) - if err != nil { - return err - } - baseCfg := Config{} - if err := json.NewDecoder(rdr).Decode(&baseCfg); err != nil { - return err - } - baseCfg.PubKeysFile, _ = filepath.Abs("../../integration/tcp-tr/hosts.pubkeys") // nolint: errcheck - baseCfg.AppsPath, _ = filepath.Abs("../../apps") // nolint: errcheck - baseCfg.Routing.Table.Type = "memory" - - mh.baseCfg = baseCfg - return nil -} - -func Example_readBaseConfig() { - mh := initMultiHead() - cfgFile, _ := filepath.Abs("../../integration/tcp-tr/nodeA.json") //nolint: errcheck - err := mh.readBaseConfig(cfgFile) - fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) - - // Output: mh.ReadBaseConfig success: true -} - -func (mh *MultiHead) initIPPool(ipTemplate string, n uint) { - - ipPool := make([]string, n) - for i := uint(1); i < n+1; i++ { - ipPool[i-1] = fmt.Sprintf(ipTemplate, i) - } - mh.ipTemplate = ipTemplate - mh.ipPool = ipPool - -} - -func ExampleMultiHead_initIPPool() { - mh := initMultiHead() - mh.initIPPool("12.12.12.%d", 16) - fmt.Printf("IP pool length: %v\n", len(mh.ipPool)) - - // Output: IP pool length: 16 -} - -func (mh *MultiHead) initCfgPool() { - mh.cfgPool = make([]Config, len(mh.ipPool)) - localPathTmplt := "/tmp/multihead" - baseCfg := mh.baseCfg - - for i := 0; i < len(mh.ipPool); i++ { - ip := fmt.Sprintf(mh.ipTemplate, i+1) - localPath := fmt.Sprintf(localPathTmplt, i+1) - pk, sk, _ := cipher.GenerateDeterministicKeyPair([]byte(ip)) //nolint: errcheck - - baseCfg.Node.StaticPubKey = pk - baseCfg.Node.StaticSecKey = sk - baseCfg.LocalPath = localPath - baseCfg.Interfaces.RPCAddress = fmt.Sprintf("%s:3435", ip) - baseCfg.Transport.LogStore.Location = fmt.Sprintf("%s/transport_logs", localPath) - - baseCfg.Apps = []AppConfig{ - AppConfig{ - App: "skychat", - AutoStart: true, - Port: routing.Port(1), - Args: []string{ - "-addr", - fmt.Sprintf("%v:8001", ip), - }, - }} - baseCfg.TCPTransportAddr = fmt.Sprintf("%v:9119", ip) - - mh.cfgPool[i] = baseCfg - } - -} - -func Example_initCfgPool() { - mh := initMultiHead() - cfgFile, _ := filepath.Abs("../../integration/tcp-tr/nodeA.json") //nolint: errcheck - err := mh.readBaseConfig(cfgFile) - fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) - mh.initIPPool("skyhost%03d", 16) - mh.initCfgPool() - fmt.Printf("len(mh.cfgPool): %v\n", len(mh.cfgPool)) - - // Output: mh.ReadBaseConfig success: true - // len(mh.cfgPool): 16 -} - -func (mh *MultiHead) pkAliases() []strsub { - subs := make([]strsub, len(mh.cfgPool)+1) - for i := 0; i < len(mh.cfgPool); i++ { - subs[i] = strsub{ - old: fmt.Sprintf("%v", mh.cfgPool[i].Node.StaticPubKey), - new: fmt.Sprintf("PK(%v)", mh.ipPool[i]), - } - } - return append(subs, strsub{ - fmt.Sprintf("%v", cipher.PubKey{}), "PK{NULL}"}) -} - -func (mh *MultiHead) initNodes() { - mh.nodes = make([]*Node, len(mh.cfgPool)) - mh.initErrs = make(chan error, len(mh.cfgPool)) - mh.startErrs = make(chan error, len(mh.cfgPool)) - mh.stopErrs = make(chan error, len(mh.cfgPool)) - - var err error - for i := 0; i < len(mh.nodes); i++ { - logger := NewTaggedMasterLogger(mh.ipPool[i], mh.pkAliases()) - logger.Out = mh.Log - logger.SetReportCaller(true) - - mh.nodes[i], err = NewNode(&mh.cfgPool[i], logger) - if err != nil { - mh.initErrs <- fmt.Errorf("error %v starting node %v", err, i) - } - } -} - -func ExampleMultiHead_initNodes() { - mh := initMultiHead() - cfgFile, _ := filepath.Abs("../../integration/tcp-tr/nodeA.json") //nolint: errcheck - err := mh.readBaseConfig(cfgFile) - fmt.Printf("mh.ReadBaseConfig success: %v\n", err == nil) - mh.initIPPool("skyhost_%03d", 16) - mh.initCfgPool() - mh.initNodes() - - fmt.Print(mh.errReport()) +func ExampleMultiHead_RunExample() { + mh := makeMultiHeadN(3) - // Output: mh.ReadBaseConfig success: true + mh.RunExample(func(mh *MultiHead) { + fmt.Print("Hi! Testing start-stop") + }) + fmt.Printf("%v\n", mh.errReport()) + // Output: + // Hi! Testing start-stop // init errors: 0 // start errors: 0 // stop errors: 0 } -func (mh *MultiHead) startNodes(postdelay time.Duration) { - mh.startOnce.Do(func() { - mh.startErrs = make(chan error, len(mh.nodes)) - for i := 0; i < len(mh.nodes); i++ { - go func(n int) { - if err := mh.nodes[n].Start(); err != nil { - mh.startErrs <- fmt.Errorf("error %v starting node %v", err, n) - } - }(i) - } - time.Sleep(postdelay) - }) -} - -func (mh *MultiHead) stopNodes(predelay time.Duration) { - mh.stopOnce.Do(func() { - time.Sleep(predelay) - mh.stopErrs = make(chan error, len(mh.nodes)) - for i := 0; i < len(mh.nodes); i++ { - go func(n int) { - if err := mh.nodes[n].Close(); err != nil { - mh.stopErrs <- fmt.Errorf("error %v stopping node %v", err, n) - } - }(i) - } - }) -} +func ExampleMultiHead_RunExample_logrus() { + mh := makeMultiHeadN(3) -func makeMultiHeadN(n uint) *MultiHead { - mh := initMultiHead() - cfgFile, _ := filepath.Abs("../../integration/tcp-tr/nodeA.json") //nolint: errcheck - err := mh.readBaseConfig(cfgFile) - if err != nil { - panic(err) - } - mh.initIPPool("skyhost_%03d", n) - mh.initCfgPool() - mh.initNodes() - return mh -} - -func (mh *MultiHead) errReport() string { - mh.repOnce.Do(func() { - close(mh.initErrs) - close(mh.startErrs) - close(mh.stopErrs) + mh.RunExample(func(mh *MultiHead) { + fmt.Print("Hi! Testing start-stop") }) - collectErrs := func(tmplt string, errs chan error) string { - recs := make([]string, len(errs)+1) - for err := range errs { - recs = append(recs, fmt.Sprintf("%v", err)) - } - recs = append(recs, fmt.Sprintf(tmplt, len(errs))) - return strings.Join(recs, "\n") - } - return strings.Join([]string{ - collectErrs("init errors: %v", mh.initErrs), - collectErrs("start errors: %v", mh.startErrs), - collectErrs("stop errors: %v", mh.stopErrs), - }, "") -} - -func ExampleMultiHead_startNodes() { - delay := time.Second - n := uint(32) - mh := makeMultiHeadN(n) - mh.startNodes(delay) - - mh.stopNodes(delay) fmt.Printf("%v\n", mh.errReport()) - // Output: init errors: 0 + // Output: + // Hi! Testing start-stop + // init errors: 0 // start errors: 0 // stop errors: 0 } -func (mh *MultiHead) sendMessage(sender, receiver uint, message string) (*http.Response, error) { - url := fmt.Sprintf("http://%s:8001/message", mh.ipPool[sender]) - msgData := map[string]string{ - "recipient": mh.cfgPool[receiver].Node.StaticPubKey.String(), - "message": message, - } - data, _ := json.Marshal(msgData) // nolint: errcheck - return http.Post(url, "application/json", bytes.NewBuffer(data)) // nolint: gosec +func ExampleMultiHead_sendMessage_msg001() { + mh := makeMultiHeadN(2) + mh.RunExample(func(mh *MultiHead) { + _, err := mh.sendMessage(0, 0, "Hello001") + _, err = mh.sendMessage(1, 1, "Hello001") + fmt.Printf("err: %v", err) + }) -} + fmt.Printf("%v\n", mh.errReport()) -func (mh *MultiHead) genPubKeysFile() string { - recs := make([]string, len(mh.cfgPool)) - for i := 0; i < len(mh.cfgPool); i++ { - recs[i] = fmt.Sprintf("%s\t%s\n", mh.cfgPool[i].Node.StaticPubKey, mh.ipPool[i]) + recs := mh.Log.filter(`.*received.*"message"`, true) + if len(recs) == 1 { + fmt.Println("skyhost_001 received message") } - return strings.Join(recs, "") -} - -func Example_genPubKeysFile() { - mh := makeMultiHeadN(128) - pkFile := mh.genPubKeysFile() + fmt.Println(mh.Log.records) + fmt.Println(recs) - fmt.Printf("pkFile generated with %v records\n", len(strings.Split(pkFile, "\n"))) - _, _ = os.Stderr.WriteString(pkFile) //nolint: errcheck - // Output: pkFile generated with 129 records -} - -func (mhl *multiheadLog) filter(pattern string, match bool) []string { - var result []string - r := regexp.MustCompile(pattern) - - for _, line := range mhl.records { - matched := r.MatchString(line) - if (matched && match) || (!matched && !match) { - result = append(result, line) - } - } - return result + // Output: err: + // init errors: 0 + // start errors: 0 + // stop errors: 0 + // skyhost_001 received message } -func ExampleMultiHead_sendMessage_msg001() { +func TestMultiHead_sendMessage_msg001(t *testing.T) { mh := makeMultiHeadN(1) - mh.startNodes(time.Second) - - _, err := mh.sendMessage(0, 0, "Hello001") - fmt.Printf("err: %v", err) + mh.RunTest(t, "msg001", func(mh *MultiHead) { + resp, err := mh.sendMessage(0, 0, "Hello001") + require.NoError(t, err) + require.Equal(t, resp.StatusCode, 200) + }) - mh.stopNodes(time.Second * 3) fmt.Printf("%v\n", mh.errReport()) recs := mh.Log.filter(`skyhost_001.*received.*"message"`, true) @@ -345,20 +93,23 @@ func ExampleMultiHead_sendMessage_msg001() { // start errors: 0 // stop errors: 0 // skyhost_001 received message - } func ExampleMultiHead_sendMessage_msg002() { - mh := makeMultiHeadN(2) + mh := makeMultiHeadN(128) mh.RunExample(func(mh *MultiHead) { - _, err := mh.sendMessage(0, 1, "Hello002") - fmt.Printf("err: %v", err) + for i := uint(2); i < 120; i++ { + resp, err := mh.sendMessage(i, i+1, fmt.Sprintf("Hello%03d-%03d", i, i+1)) + fmt.Printf("resp: %v \nerr: %v\n", resp, err) + resp2, err2 := mh.sendMessage(i+1, i, fmt.Sprintf("Hello%03d-%03d", i+1, i)) + fmt.Printf("resp: %v \nerr: %v\n", resp2, err2) + time.Sleep(time.Millisecond * 10) + } + time.Sleep(time.Second * 60) }) - // fmt.Printf("%v\n", mh.errReport()) - // fmt.Printf("%v", mh.Log.filter(`skychat`, false)) fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) - // fmt.Printf("%v", mh.Log.filter(`skychat`, false)) + fmt.Println(mh.Log.filter(`.*received.*`, true)) // Output: err: // init errors: 0 @@ -367,25 +118,6 @@ func ExampleMultiHead_sendMessage_msg002() { } -func (mh *MultiHead) RunExample(example func(mh *MultiHead)) { - mh.startNodes(time.Second) - example(mh) - mh.stopNodes(time.Second) -} - -func ExampleMultiHead_confirmAppPacket() { - mh := makeMultiHeadN(2) - - mh.RunExample(func(mh *MultiHead) { - fmt.Printf("HHIIII!") - }) - - fmt.Printf("%v\n", mh.errReport()) - fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) - - // Output: ZZZZ -} - func ExampleMultihead_forwardAppPacket() { mh := makeMultiHeadN(2) mh.RunExample(func(mh *MultiHead) { @@ -429,13 +161,12 @@ func ExampleMultihead_forwardAppPacket() { } func ExampleMultiHead_sendMessage_msg003() { - mh := makeMultiHeadN(1) + mh := makeMultiHeadN(128) mh.RunExample(func(mh *MultiHead) { _, err := mh.sendMessage(0, 0, "Hello003") fmt.Printf("err: %v", err) }) - fmt.Printf("%v\n", mh.errReport()) fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) diff --git a/pkg/visor/testdata/hosts.pubkeys b/pkg/visor/testdata/hosts.pubkeys new file mode 100644 index 000000000..bc9205cbd --- /dev/null +++ b/pkg/visor/testdata/hosts.pubkeys @@ -0,0 +1,128 @@ +024195ae0d46eb0195c9ddabcaf62bc894316594ea2e92570f269238c5b5f817d1 skyhost_001:9119 +037dfc42ae5ca494f5646d09e6532ea39bea7954a915409925277f4604a34968b3 skyhost_002:9119 +0256f26f187b42c10c3a4b93453fd25add22dcfe07d4908a9ae4e7f0daf3b01086 skyhost_003:9119 +028941a0da17be26331db993584b83b83eb532b9486fd4cc6844a5fe4c79aa588d skyhost_004:9119 +03b4d7d5536b2310c369d6ce733221ab50f23a66b38ccc0f21557237cd93a86b41 skyhost_005:9119 +029524e7499335844e7f4cd238376c33562fce73ee549c4e7b1803e6a9324863f9 skyhost_006:9119 +03373c96227afbb25fcf8d8e5e057ed63e38b976abcd579611b38dc370cf9e23dd skyhost_007:9119 +032257488128d0bbe134fd4bc51d23189e9633fc9e3f31aaf366051b55c2024609 skyhost_008:9119 +0314bb3c470d207e1293535155040f48d25659c632c784185a0bb32550e9d80a52 skyhost_009:9119 +03891f6a2d8e9f23926d1ef0381285a411cbb7c1e62001ec08568a3dc034c50c34 skyhost_010:9119 +031b44a9d314abd95b83a9642b37dc8579cc7a4f95cf313091aa43986d6b38658c skyhost_011:9119 +0379194d7d7b94ce17219d2dc40a98cc2303e53148b15889acf83094f20afd75b3 skyhost_012:9119 +020397f54e134275aa3a7213857d43e6232a743ba48b021c07a7414654f974a20e skyhost_013:9119 +02619da7ef4bd2b3bc37960409b48769e61c4dbaaaf2a2ed1bb7980e321a10ac13 skyhost_014:9119 +0227f786985528ed4781d23d6fad50ca2f24f3b44581c58a38186a49d9a89fb4b3 skyhost_015:9119 +03ffc5618426cfa3e354dcc744e9128ac3aa69e56b9fef5e53aed6a4ddc85438ce skyhost_016:9119 +03989f5e40e44908185b4b58149f1bfdcb9ab2ef69c42a9f2305c20ea720aca6d6 skyhost_017:9119 +0239ca4ba0974c7855fe12e3d5c8e14336c927f8182fcfd1fd75405eb50de2ff7a skyhost_018:9119 +038f5afd9e68a75fca81e4eb14da081caf222816720cf3d456a7ea1fb2fbed9f65 skyhost_019:9119 +02a91ce458d2eae811b9925b7777c8511b136845a844577f10ae2b369f66107970 skyhost_020:9119 +0276e24892d65da1eb06b916da87e49eba27c90c4163040ceccae70749ff0a5b55 skyhost_021:9119 +0374f01d37952afbd3453e4b19cb99e360878e153e7c33cb10c98fd7c7079e7b0e skyhost_022:9119 +038e5ded2ef960ba405c62c306b1fa2d54802e6a213eb3bf822359997a9ac6f1c2 skyhost_023:9119 +02363f36a734aaafe7b0efebbbea7ac0bc3fa6231ba080cf97cb75399d511a3191 skyhost_024:9119 +020e38059b2c20de8487c1454cc66b2bbdc5af28c5776f9d58b2f74c936400f502 skyhost_025:9119 +03a9ae02bd27d9d21e5ad298ee5495fad68e94249882eb647fb703cbccd6853a0e skyhost_026:9119 +035a8f8cac27afa6299648d4e357b1a176f1d7e0a6af6847703360b8be300e21cf skyhost_027:9119 +024b516849bcacf045ab42344e355c07e4706d59d891682235e95fecc604a667c4 skyhost_028:9119 +02f6e9918cabe989e6b58436695370da50e0de56b9387e995b7880270aef9ba449 skyhost_029:9119 +039c3fa89c11e3382e66fe7906d25211f683e2fada29b190206656d29c972e748f skyhost_030:9119 +031c6b929639c5e12772539ae5d2a2b22603424ece435afaa50d9ef2fa112ac2a2 skyhost_031:9119 +0200e83d2655a38eef538a6171e5036e1b8f58da297047a81126c6a7915d836acb skyhost_032:9119 +031509ef53b1224ba5af2438d89049fe4869daf9d5e4e05eaab12c3320fbde6108 skyhost_033:9119 +035283050a5fd29dba173028a869f84ce843e97a9a6829851787486ee338c7ed7d skyhost_034:9119 +02701d64f1f0da77154510630a9403a97f036390e82cc807c2ef28820ce1982eee skyhost_035:9119 +0263a63a291442f0ca6caf0392daec59f6feeb3c96485a32e2874740b79536ca15 skyhost_036:9119 +02d6717b3ae11b7fd18f94a35018f682d020214ba48cedf5fd78d2204f0e90a913 skyhost_037:9119 +023b5024c8a3ef31efaecd446a98c54873fee41718f92c4228c51fe98402be8245 skyhost_038:9119 +03d3f5f2b745a3475cae14c9941b787def0e0a419091b012b08d6d6b535dca9ea3 skyhost_039:9119 +033de8084253e14c0e981cd23a6cf7dbe864f5c0cbadefa82146b3fe7b9b74f530 skyhost_040:9119 +0377c9c1f0bd94c02caea0dba1c3724daa8db31e0013b44c4922c5d922ceb880c2 skyhost_041:9119 +03755050134b5374bdac64551d139e008a2e574efec5a51eae0acda18c12ca80aa skyhost_042:9119 +036a27032f413fbc3564c064ed5d2a740c8bfb7ceb46f83820212f76ea68d92790 skyhost_043:9119 +02ba617ae92281f587836f36c13176dbea93bdbb8e100463d8606155fc3709fa49 skyhost_044:9119 +031d4719f4d5190fdee7b5daf92506cfc0391e2349ebdfaec6eb11c115672fbcee skyhost_045:9119 +03700dd365d20b5172b2c8884f3756ba2ffcab2447495a408aa672873ac436aa25 skyhost_046:9119 +034ff0e79d675b85cf0e7c7a53c46df3e86542d2102ad6561d88d5d04e924d010f skyhost_047:9119 +036f9b581ea0548d22126355338b8f469d73510f6099ff4ad4f60da947f63d39ed skyhost_048:9119 +022e1b56b7c6efc7e41390cefe585f37bf7f780de39ce9e15f40dd0ea1c3c4799a skyhost_049:9119 +022788a9dd824ff8beea80b35243c4507d02d5ea74eb3a93655746d820365fd581 skyhost_050:9119 +02c3a80bd7ded6c65e54332edf67f5c22f1f8536f207c4c3188656cb147814cd2d skyhost_051:9119 +027a09e20c425345286b04b0c2708dc206dce456d1031aa5573794c229e9973bf5 skyhost_052:9119 +029339bbc8719cd540b7f159ec1eda6921bcf91b78b3d9f8b1058dc7e44eab1ab8 skyhost_053:9119 +03ab1868f2a54ce41f689e81415ad51353395601d0af493298518015d7802396ca skyhost_054:9119 +02e5f274c4ceb87571bb84235708b5f879df5611c6a571e0f7107e50538f9774cc skyhost_055:9119 +024f1994a8e705a88ebfdf7dfeab6688806cdc7b3df34f1d67f69f63eec46df3b9 skyhost_056:9119 +030d50e27c94e5adde1e9427ed60417667f1dc228dbfcc70a0b9430d23a84afd25 skyhost_057:9119 +03836101fb58e3e1d2004aac1b0b9673a84101478aedbb1f7a01910e2d1cbdd6a1 skyhost_058:9119 +020999a7e05996483f8d9cd3bbfff15c6a8ab7742313c59e9a51cf9ffd6cccc8dd skyhost_059:9119 +02442601636264d662ca6030264f3f72480ea5f7ae30379d122563bff341dceef7 skyhost_060:9119 +032b4854d0ab1738d8b83729ab182906dadc6b5c07e01dc64927e94754609762fa skyhost_061:9119 +030cc432d13049fc8efca4de4232249078fad3569a30c231d30cee58aa72ba9204 skyhost_062:9119 +021fc87218b60dadf35b2628cf9becc65ab16aae5a24bc0f818b5d5b68c6c9b055 skyhost_063:9119 +038f6398f3cdaae818ad161c89565abbf4bd7f0f5f3e140dd181be7bcb5dfbdcfc skyhost_064:9119 +02593b933ee557e0062470dffc4bb1fa62da060ea71363737a156e070f5710251a skyhost_065:9119 +02a6c4f2c982621434ce0ae1064f52bfe88d0e16c28263567c36497a9f179f36b3 skyhost_066:9119 +0264b5070214ea86bb3a58eed38b227206c1757c9dd89c45d4484a46cbd6ae29a2 skyhost_067:9119 +0340bc08384cceddb27c5ea19186bc725c362576b5bf44eb44818260f2c418e076 skyhost_068:9119 +03bf5effc923dd0af5e87853c42c9e0348cfafd17987e7b6cd7477d804eab627fe skyhost_069:9119 +023e5d2e629381a788bcdeede3dede7f33157932c36a0749a8421afeb30b526e99 skyhost_070:9119 +036376d86e676b0601b95e33f459b5645eb791c7d40270850535066a1c8281a02c skyhost_071:9119 +0377a1d965886626d7407a48a2d92e3a142931bdf4f84f1a3c71cf20619ee047d0 skyhost_072:9119 +02314e3a5cf8bd8b3a1397cfb7423d91316fc1bd154adac2e1e5406a773963f583 skyhost_073:9119 +03d635622ba19390f9038994be5cac5aaaf83118577ca601f14b2273015e9741ed skyhost_074:9119 +03b3b0d95c26c152cc7e6641a905971d9f7742cc5f309dce599dba093452930a33 skyhost_075:9119 +031a9a280dbac05b98c2a5c71fc7da6e1f173449efb0b5028381bdd96d9fb36027 skyhost_076:9119 +02dc94d4b74319ccb744e79d6a3c2c3cccc5904d9df3ae3f6a1d170167f7d95e30 skyhost_077:9119 +035607e5f3028e43e89f612a2d5594f73876416413c3fdfa0fd69d5a23a841cc53 skyhost_078:9119 +02e8314a7e7dac102df36cf9a768b5cb2621ca7063f4767d966ca33cfef50f848c skyhost_079:9119 +03c3ae9aaa47c2bd2104562ec97fa0604137a72139837ebee2d16ec6f5a869bd3a skyhost_080:9119 +0282d04f44be258ced29a9d0ea219bfc848d6aed7ffe1d9132d421c71445e4f240 skyhost_081:9119 +028468d2cc703150a238319a0473f1255533065def8dd6bd9fd0612585697d89ef skyhost_082:9119 +0278223c57c34c36835da8291cb3cd5a4b11b044b9154d3103e7f8d9e5a06a4d0e skyhost_083:9119 +0348cd1be0137c38a01e306f7827f942b4607559a7273ee8c28db9a1d9a4700d69 skyhost_084:9119 +037beff859d8625dcd00f9f60c9aa46d4fbb0df11be0cbf833f56d107a09c9845f skyhost_085:9119 +020f2a586d17ee24b5db9e8f509f24f6d2c9e38435a970f9f0013cdad53d1ddfee skyhost_086:9119 +03a086a7a5713d00ee6334fbf0b6d39fe3f4a7f8644a16eded80a59682121b79fe skyhost_087:9119 +02de4c0e7ddf7a7b8bd9e49612ae61167d006440979c3a3c827257cb63ed6d5e62 skyhost_088:9119 +0242d56324e19e5f3d410c1445c59dea76222d00a1443ac80ab6b02cfb45a3fdb2 skyhost_089:9119 +02dabcba7c968331ffd4935e936aa3226b95c9288d4ea9df008ddd2a1de1c41d2c skyhost_090:9119 +0280eae0b6e242160eb0f630a8a8f98298eab7262a92296d7a097d6d0997d6bb14 skyhost_091:9119 +0398ef1a92cc17439410b97381724d798448f1023be5f9bd07c8dc64cce4f0b55a skyhost_092:9119 +038455d7d541f486eab847202b621a6d414877d02b3610bc3f5d70cf8e45a51dbb skyhost_093:9119 +02363018f6c6c2803e817151fa70f7899c225a9bba15a974704c70233cad96f07b skyhost_094:9119 +02f0dd73fe88a3faded006a9f6f5cb0452c165f67c480ab1f82504b76c66378152 skyhost_095:9119 +0383956d6a6be011c06fdec7e6e76a0499d62eda049cc187bb46a92fcc55c757ff skyhost_096:9119 +0278fc58753706286b8889f2b2f54cc63b36a905c31211315cc4c39803a92b614a skyhost_097:9119 +02d9f7bdf334a2019b7dee343d2473ac2319ee196d5c230353e50741208b9acba7 skyhost_098:9119 +02457ad23282c2b4e27127ce6f12778ae0720d176940608a7ca9d136f303ce70e9 skyhost_099:9119 +03fa1fca4491d8d7c145bc1301a2555efc0067d20797aa7d20b2307f3b57ce049a skyhost_100:9119 +030823b04f9c149a9121c104aee5657ebab931c52ff57eacbabbd0eda4469eca9e skyhost_101:9119 +036d883f7a0a164b8e672dd54a7781d0bb6128c2210a70599c306d408de57001b8 skyhost_102:9119 +036ba4d2f3a2ebff490f0b6e6ae7f8bac1fec88739b16eddb09cb103b6d4488dea skyhost_103:9119 +03a04fe8bb302b6a36f716dc66846b9ae8aafabe216254dc6852b4c31e0f67a3c5 skyhost_104:9119 +03b25ccefde473c68eb0f55e6210f17b108839867b5d14e27bdbb4258b12d7fb5f skyhost_105:9119 +0292ad1a2fcd3e7843411b21243538c330c60a9b31a25322e352a122dab3016d8f skyhost_106:9119 +02da5d9f9ab64360b855015d749ed74ec59e61c37ccd5f071cec5b2fa5067de7f8 skyhost_107:9119 +0333c80071daa716138725ee5204bc1f9e56e048bae1de4110865a633d02836323 skyhost_108:9119 +031e0f740c45ed6a5cc3295fa0c022507d35eb8fe5bb2862a7b604b3df497cfa7a skyhost_109:9119 +025a8088dc7c067f0c936ef7d4507ee71c9f42fdab4f299604612575ba7a48e444 skyhost_110:9119 +02816ce9cf3965698d301e4babb9fb17d691f34b66501de7034c7891320f2f5bcf skyhost_111:9119 +03a8d141c399d36111a5e9ba3778ba63905f5f153cf1d9c501d01c3ad8fea5758f skyhost_112:9119 +02ea963cc62b7722aead7142e2a171d24b20411c07eae7fdd6df863d7fffc0aace skyhost_113:9119 +0254ab50fa28894e1f67f32676b6c84c3ffad45d192dc69d30b2e068c90456eb17 skyhost_114:9119 +032c1dc027b7dc565b600c1dcad9ad73863a7fb3c0fff4f8a9fc6fb72a89d641a9 skyhost_115:9119 +0267c9824031a83bd52c7e02d6a6343caf8697e90e09a0d341bf4e578a1a98edaf skyhost_116:9119 +02e0531dea2b6c2b58fe0da35afb27b7936808de5d81c07f613d711883511729d4 skyhost_117:9119 +0287927ab07ad31a70fbafa9355dcaa43f95c4844fae5ef614dc3c0e51e338c7c0 skyhost_118:9119 +02dac42d560a4bab65c35f6b8f7ef71920f8c6bb3fcc55806308ef4275ab42125a skyhost_119:9119 +035bd1ecc493c1b79542447af3793ea5238ab46e36f1e6c8210ff39a472c5bd9d0 skyhost_120:9119 +037ef72928c7b23ebde3fbd07412430cee37489ba3410cb18b4024c321dcf28921 skyhost_121:9119 +031ddf1b3b5b2602008933775b2e256386610ce059fdae2048d993e726fe244522 skyhost_122:9119 +03759272e7425915148458e23eb34aa95705cf6225d8e18e4b884752aa2c63ae9a skyhost_123:9119 +029c9f50b8a2372baa951ed65b26b6019cc78707c0c29939ee5babd740c036f360 skyhost_124:9119 +02db0e1d76c11fee7dda0f1c3eb2d4ab4452255c67da0cf01cb08a26f81fba2ceb skyhost_125:9119 +0269679c529dc17efca197e667f1a4c1aa36c837097db5420356c84d32ce58c3d4 skyhost_126:9119 +0317dae55594f1c5c6e5105422853772755453894bc274af3267c42ede3be5e973 skyhost_127:9119 +0391684363b22b72b82e7419762dc33e0fec6ae1a2481ffd06e260d9e95214b473 skyhost_128:9119 \ No newline at end of file diff --git a/pkg/visor/testdata/node.json b/pkg/visor/testdata/node.json new file mode 100755 index 000000000..1bb1b0f23 --- /dev/null +++ b/pkg/visor/testdata/node.json @@ -0,0 +1,53 @@ +{ + "version": "1.0", + "node": { + "static_public_key": "02fffa3ffd07630bf5565493a990f182ea7de56a9c14abbe041959a8e4667d3447", + "static_secret_key": "4be3f22d89d0bade8c5bf0084919f891e5e2dd0d4db0bc139318dfa576ca0237" + }, + "messaging": { + "discovery": "https://messaging.discovery.skywire.skycoin.net", + "server_count": 1 + }, + "transport": { + "discovery": "https://transport.discovery.skywire.skycoin.net", + "log_store": { + "type": "file", + "location": "./local/tcp-tr/nodeA/transport-logs" + } + }, + "routing": { + "setup_nodes": [ + "0324579f003e6b4048bae2def4365e634d8e0e3054a20fc7af49daf2a179658557" + ], + "route_finder": "https://routefinder.skywire.skycoin.net/", + "route_finder_timeout": "10s", + "table": { + "type": "boltdb", + "location": "./local/tcp-tr/nodeA/routing.db" + } + }, + "apps": [ + { + "app": "skychat", + "version": "1.0", + "auto_start": true, + "port": 1, + "args": [ + "-addr", + "192.168.1.2:8001" + ] + } + ], + "trusted_nodes": [], + "hypervisors": [], + "apps_path": "./apps", + "local_path": "./local", + "transport_type": "tcp-transport", + "pubkeys_file": "./integration/tcp-tr/hosts.pubkeys", + "tcptransport_addr": "192.168.1.2:9119", + "shutdown_timeout": "10s", + "interfaces": { + "rpc": "192.168.1.2:3435" + }, + "log_level": "info" +} \ No newline at end of file diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 7f4b7905d..d43e50a5b 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -22,7 +22,6 @@ import ( "github.com/skycoin/dmsg/noise" "github.com/skycoin/skycoin/src/util/logging" - th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/internal/therealproxy" "github.com/skycoin/skywire/internal/therealssh" "github.com/skycoin/skywire/pkg/app" @@ -255,8 +254,12 @@ func NewNode(config *Config, masterLogger *logging.MasterLogger) (*Node, error) // Start spawns auto-started Apps, starts router and RPC interfaces . func (node *Node) Start() error { - node.logger.Debug(th.Trace("ENTER")) - defer node.logger.Debug(th.Trace("ENTER")) + if node == nil { + return errors.New("not initialized node") + } + // node.logger.Debug(th.Trace("ENTER")) + // defer node.logger.Debug(th.Trace("ENTER")) + node.logger.Info("started node") ctx := context.Background() @@ -288,7 +291,9 @@ func (node *Node) Start() error { rpcSvr := rpc.NewServer() if err := rpcSvr.RegisterName(RPCPrefix, &RPC{node: node}); err != nil { return fmt.Errorf("rpc server created failed: %s", err) + } + if node.rpcListener != nil { node.logger.Info("Starting RPC interface on ", node.rpcListener.Addr()) go rpcSvr.Accept(node.rpcListener) From 7635a5a8d70fe1c6cbb0fff3c3598138e4435c67 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Fri, 23 Aug 2019 10:20:38 +0300 Subject: [PATCH 22/26] some cleanups --- cmd/apps/skychat/chat.go | 32 +- .../therealproxy-client.go | 2 +- go.mod | 1 + go.sum | 2 + internal/therealssh/client.go | 6 +- pkg/app/app.go | 38 +- pkg/router/router.go | 39 ++- pkg/util/env/env.go | 27 ++ pkg/visor/multihead.go | 4 +- pkg/visor/multihead_test.go | 44 ++- vendor/github.com/alecthomas/repr/.travis.yml | 4 + vendor/github.com/alecthomas/repr/COPYING | 21 ++ vendor/github.com/alecthomas/repr/README.md | 90 +++++ vendor/github.com/alecthomas/repr/repr.go | 325 ++++++++++++++++++ vendor/modules.txt | 2 + 15 files changed, 569 insertions(+), 68 deletions(-) create mode 100644 pkg/util/env/env.go create mode 100644 vendor/github.com/alecthomas/repr/.travis.yml create mode 100644 vendor/github.com/alecthomas/repr/COPYING create mode 100644 vendor/github.com/alecthomas/repr/README.md create mode 100644 vendor/github.com/alecthomas/repr/repr.go diff --git a/cmd/apps/skychat/chat.go b/cmd/apps/skychat/chat.go index acc46d20f..84bfea6a7 100644 --- a/cmd/apps/skychat/chat.go +++ b/cmd/apps/skychat/chat.go @@ -23,27 +23,26 @@ import ( th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" "github.com/skycoin/skywire/pkg/routing" + "github.com/skycoin/skywire/pkg/util/env" ) var ( - addr = flag.String("addr", ":8000", "address to bind") - logger = logging.NewMasterLogger().PackageLogger("chat") - r = netutil.NewRetrier(50*time.Millisecond, 0, 2) - + addr = flag.String("addr", ":8000", "address to bind") + logger = logging.NewMasterLogger().PackageLogger("chat") + retrier = netutil.NewRetrier( + env.Duration("SKYWIRE_CHAT_RETRY_BACKOFF", 50*time.Millisecond), + env.UInt32("SKYWIRE_CHAT_RETRY_TIMES", 5), + env.UInt32("SKYWIRE_CHAT_RETRY_FACTOR", 2)) chatApp *app.App clientCh chan string chatConns map[cipher.PubKey]net.Conn connsMu sync.Mutex - trcLog = logger.WithField("_module", th.GetCallerN(4)) + trcLog = logger.WithField("_module", th.GetCallerN(3)) + trStart = func() error { trcLog.Debug("ENTER"); return nil } + trFinish = func(_ error) { trcLog.Debug(th.Trace("EXIT")) } ) -func trStart() error { // nolint:unparam - logger.Debug(th.Trace("ENTER")) - return nil -} -func trFinish(_ error) { logger.Debug(th.Trace("EXIT")) } - func main() { flag.Parse() @@ -74,8 +73,6 @@ func main() { } func listenLoop() { - defer trFinish(trStart()) - for { conn, err := chatApp.Accept() if err != nil { @@ -93,7 +90,8 @@ func listenLoop() { } func handleConn(conn net.Conn) { - defer trFinish(trStart()) + // defer trFinish(trStart()) + var cntr uint64 raddr := conn.RemoteAddr().(routing.Addr) @@ -123,7 +121,7 @@ func handleConn(conn net.Conn) { } func messageHandler(w http.ResponseWriter, req *http.Request) { - defer trFinish(trStart()) + // defer trFinish(trStart()) data := map[string]string{} if err := json.NewDecoder(req.Body).Decode(&data); err != nil { @@ -149,7 +147,7 @@ func messageHandler(w http.ResponseWriter, req *http.Request) { if !ok { var err error - err = r.Do(func() error { + err = retrier.Do(func() error { atomic.AddUint64(&cntr, 1) trcLog.Debugf("dial %v addr:%v\n", cntr, addr) conn, err = chatApp.Dial(addr) @@ -180,7 +178,7 @@ func messageHandler(w http.ResponseWriter, req *http.Request) { } func sseHandler(w http.ResponseWriter, req *http.Request) { - defer trFinish(trStart()) + // defer trFinish(trStart()) f, ok := w.(http.Flusher) if !ok { diff --git a/cmd/apps/therealproxy-client/therealproxy-client.go b/cmd/apps/therealproxy-client/therealproxy-client.go index 57dca5da2..383f6194f 100644 --- a/cmd/apps/therealproxy-client/therealproxy-client.go +++ b/cmd/apps/therealproxy-client/therealproxy-client.go @@ -19,7 +19,7 @@ import ( const socksPort = 3 -var r = netutil.NewRetrier(time.Second, 5, 2) +var r = netutil.NewRetrier(time.Second, 1, 2) func main() { var addr = flag.String("addr", ":1080", "Client address to listen on") diff --git a/go.mod b/go.mod index 42e8a9113..967bdf836 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/skycoin/skywire go 1.12 require ( + github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1 github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 github.com/blang/semver v3.5.1+incompatible // indirect github.com/boltdb/bolt v1.3.1 // indirect diff --git a/go.sum b/go.sum index e53e2c2d2..44e84a938 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1 h1:GDQdwm/gAcJcLAKQQZGOJ4knlw+7rfEQQcmwTbt4p5E= +github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY= diff --git a/internal/therealssh/client.go b/internal/therealssh/client.go index 35d4fb713..bd95d9dcf 100644 --- a/internal/therealssh/client.go +++ b/internal/therealssh/client.go @@ -15,9 +15,13 @@ import ( "github.com/skycoin/skywire/internal/netutil" "github.com/skycoin/skywire/pkg/routing" + "github.com/skycoin/skywire/pkg/util/env" ) -var r = netutil.NewRetrier(50*time.Millisecond, 5, 2) +var r = netutil.NewRetrier( + env.Duration("SKYWIRE_SSH_RETRY_BACKOFF", 50*time.Millisecond), + env.UInt32("SKYWIRE_SSH_RETRY_TIMES", 5), + env.UInt32("SKYWIRE_SSH_RETRY_FACTOR", 2)) // Client proxies CLI's requests to a remote server. Control messages // are sent via RPC interface. PTY data is exchanged via unix socket. diff --git a/pkg/app/app.go b/pkg/app/app.go index 0736db186..c504f7fa7 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -30,7 +30,9 @@ const ( ) var ( - log = logging.MustGetLogger("app") + log = logging.MustGetLogger("app") + trStart = func() (_ error) { log.Debug(th.Trace("ENTER")); return } + trFinish = func(_ error) { log.Debug("FINISH"); return } ) // Config defines configuration parameters for App @@ -56,8 +58,7 @@ type App struct { // Command setups pipe connection and returns *exec.Cmd for an App // with initialized connection. func Command(config *Config, appsPath string, args []string) (net.Conn, *exec.Cmd, error) { - log.Printf(th.Trace("ENTER")) - defer log.Printf(th.Trace("EXIT")) + defer trFinish(trStart()) srvConn, clientConn, err := OpenPipeConn() if err != nil { @@ -74,6 +75,8 @@ func Command(config *Config, appsPath string, args []string) (net.Conn, *exec.Cm // SetupFromPipe connects to a pipe, starts protocol loop and performs // initialization request with the Server. func SetupFromPipe(config *Config, inFD, outFD uintptr) (*App, error) { + defer trFinish(trStart()) + pipeConn, err := NewPipeConn(inFD, outFD) if err != nil { return nil, fmt.Errorf("failed to open pipe: %s", err) @@ -101,13 +104,14 @@ func SetupFromPipe(config *Config, inFD, outFD uintptr) (*App, error) { // Setup setups app using default pair of pipes func Setup(config *Config) (*App, error) { + defer trFinish(trStart()) + return SetupFromPipe(config, DefaultIn, DefaultOut) } // Close implements io.Closer for an App. func (app *App) Close() error { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("EXIT")) + defer trFinish(trStart()) if app == nil { return nil @@ -137,8 +141,7 @@ func (app *App) Close() error { // Accept awaits for incoming loop confirmation request from a Node and // returns net.Conn for received loop. func (app *App) Accept() (net.Conn, error) { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("EXIT")) + defer trFinish(trStart()) addrs := <-app.acceptChan laddr := addrs[0] @@ -157,8 +160,7 @@ func (app *App) Accept() (net.Conn, error) { // Dial sends create loop request to a Node and returns net.Conn for created loop. func (app *App) Dial(raddr routing.Addr) (net.Conn, error) { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("EXIT")) + defer trFinish(trStart()) log.Debugf("%v CALLERS: %v\n", th.GetCaller(), th.GetCallers(3)) @@ -186,8 +188,7 @@ func (app *App) Addr() net.Addr { } func (app *App) protoHandler(frame Frame, payload []byte) (res interface{}, err error) { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("EXIT")) + defer trFinish(trStart()) switch frame { case FrameConfirmLoop: @@ -203,8 +204,7 @@ func (app *App) protoHandler(frame Frame, payload []byte) (res interface{}, err } func (app *App) handleProto() { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("EXIT")) + defer trFinish(trStart()) err := app.proto.Serve(app.protoHandler) if err != nil { @@ -213,8 +213,7 @@ func (app *App) handleProto() { } func (app *App) serveConn(loop routing.AddrLoop, conn io.ReadWriteCloser) { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("EXIT")) + defer trFinish(trStart()) log.Debugf("%v loop: %v conn: %T\n", th.GetCaller(), loop, conn) @@ -253,8 +252,7 @@ func (app *App) serveConn(loop routing.AddrLoop, conn io.ReadWriteCloser) { } func (app *App) forwardPacket(data []byte) error { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("EXIT")) + defer trFinish(trStart()) packet := &Packet{} if err := json.Unmarshal(data, packet); err != nil { @@ -274,8 +272,7 @@ func (app *App) forwardPacket(data []byte) error { } func (app *App) closeConn(data []byte) error { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("EXIT")) + defer trFinish(trStart()) var loop routing.AddrLoop if err := json.Unmarshal(data, &loop); err != nil { @@ -294,8 +291,7 @@ func (app *App) closeConn(data []byte) error { } func (app *App) confirmLoop(data []byte) error { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("EXIT")) + defer trFinish(trStart()) var addrs [2]routing.Addr if err := json.Unmarshal(data, &addrs); err != nil { diff --git a/pkg/router/router.go b/pkg/router/router.go index f8a5fa11c..6efd38987 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -9,11 +9,14 @@ import ( "net" "sync" "time" + "bytes" "github.com/sirupsen/logrus" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" + "github.com/alecthomas/repr" + th "github.com/skycoin/skywire/internal/testhelpers" "github.com/skycoin/skywire/pkg/app" routeFinder "github.com/skycoin/skywire/pkg/route-finder/client" @@ -21,6 +24,7 @@ import ( "github.com/skycoin/skywire/pkg/setup" "github.com/skycoin/skywire/pkg/transport" "github.com/skycoin/skywire/pkg/transport/dmsg" + ) const ( @@ -92,6 +96,9 @@ func New(config *Config) *Router { return r } +func (r *Router) trLog() *logrus.Entry { + return r.Logger.WithField("_module", th.GetCallerN(3)) +} func (r *Router) trStart() (_ error) { r.trLog().Debug("ENTER") return @@ -101,10 +108,6 @@ func (r *Router) trFinish(_ error) { return } -func (r *Router) trLog() *logrus.Entry { - return r.Logger.WithField("_module", th.GetCallerN(3)) -} - // Serve starts transport listening loop. func (r *Router) Serve(ctx context.Context) error { defer r.trFinish(r.trStart()) @@ -227,8 +230,7 @@ func (r *Router) ForwardAppPacket(_ *app.Protocol, packet *app.Packet) error { // Close safely stops Router. func (r *Router) Close() error { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + defer r.trFinish(r.trStart()) if r == nil { return nil @@ -248,8 +250,7 @@ func (r *Router) Close() error { } func (r *Router) serveTransport(rw io.ReadWriter) error { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + defer r.trFinish(r.trStart()) packet := make(routing.Packet, 6) if _, err := io.ReadFull(rw, packet); err != nil { @@ -276,8 +277,8 @@ func (r *Router) serveTransport(rw io.ReadWriter) error { // ForwardPacket forwards payload according to rule func (r *Router) ForwardPacket(payload []byte, rule routing.Rule) error { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + defer r.trFinish(r.trStart()) + r.Logger.Debugf("%v %v %v\n", th.GetCaller(), payload, rule) packet := routing.MakePacket(rule.RouteID(), payload) @@ -298,8 +299,7 @@ func (r *Router) ForwardPacket(payload []byte, rule routing.Rule) error { } func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + defer r.trFinish(r.trStart()) laddr := routing.Addr{Port: rule.LocalPort()} raddr := routing.Addr{PubKey: rule.RemotePK(), Port: rule.RemotePort()} @@ -317,6 +317,16 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { return nil } +func reprS(v interface{}) string { + w := bytes.NewBuffer(nil) + + options := []repr.Option{repr.OmitEmpty(true), repr.IgnoreGoStringer()} + // options = []Option{repr.NoIndent()} + p := repr.New(w, options...) + p.Print(v) + return w.String() +} + func (r *Router) forwardAppPacket(appPacket *app.Packet) error { defer r.trFinish(r.trStart()) @@ -337,7 +347,8 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { r.trLog().Error("r.pm.GetLoop: ", err) r.trLog().Warn("loop: ", loop) - r.trLog().Errorf("ALL LOOPS: %v\n", r.pm.Ports()) + r.trLog().Errorf("ALL LOOPS: %v\n", reprS(r.pm.Ports())) + // repr.Print(r.pm.Ports()) if err != nil { return err @@ -346,7 +357,7 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { // r.Logger.Debugf("%v found loop: %v", th.GetCaller(), loop) tr := r.tm.Transport(loop.trID) - r.trLog().Info("trID %v ERRROR %v\n", loop.trID, err) + r.trLog().Errorf("r.tm.transports: %v\n", r.tm.Transports()) // 9df41ada-be66-5640-bce2-917988069706 diff --git a/pkg/util/env/env.go b/pkg/util/env/env.go new file mode 100644 index 000000000..0d20f0766 --- /dev/null +++ b/pkg/util/env/env.go @@ -0,0 +1,27 @@ +package env + +import ( + "os" + "strconv" + "time" +) + +// Int32 returns parsed int32 value of environment variable +func UInt32(name string, defvalue uint32) uint32 { + if envVar, ok := os.LookupEnv(name); ok { + if value, err := strconv.Atoi(envVar); err == nil { + return uint32(value) + } + } + return defvalue +} + +// Duration returns parsed time.Duration value of environment variable +func Duration(name string, defvalue time.Duration) time.Duration { + if envVar, ok := os.LookupEnv(name); ok { + if value, err := time.ParseDuration(envVar); err == nil { + return value + } + } + return defvalue +} diff --git a/pkg/visor/multihead.go b/pkg/visor/multihead.go index bb2ce1a2d..e70ce41c4 100644 --- a/pkg/visor/multihead.go +++ b/pkg/visor/multihead.go @@ -18,6 +18,7 @@ import ( "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/pkg/routing" ) @@ -150,13 +151,12 @@ func (mh *MultiHead) initNodes() { logging.DisableColors() logging.SetOutputTo(mh.Log) stdlog.SetOutput(mh.Log) + stdlog.SetPrefix("[package-level]") var err error for i := 0; i < len(mh.nodes); i++ { logger := NewTaggedMasterLogger(mh.ipPool[i], mh.pkAliases()) logger.Out = mh.Log - // logger.SetReportCaller(true) - stdlog.SetPrefix(mh.ipPool[i]) mh.nodes[i], err = NewNode(&mh.cfgPool[i], logger) if err != nil { diff --git a/pkg/visor/multihead_test.go b/pkg/visor/multihead_test.go index de7d01530..a504957d8 100644 --- a/pkg/visor/multihead_test.go +++ b/pkg/visor/multihead_test.go @@ -10,9 +10,10 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/skycoin/skywire/pkg/router" "github.com/skycoin/skywire/pkg/routing" - "github.com/stretchr/testify/require" ) /* Prepare IP aliases: @@ -49,19 +50,20 @@ func ExampleMultiHead_RunExample_logrus() { // stop errors: 0 } -func ExampleMultiHead_sendMessage_msg001() { +func ExampleMultiHead_sendMessage_Local() { mh := makeMultiHeadN(2) mh.RunExample(func(mh *MultiHead) { - _, err := mh.sendMessage(0, 0, "Hello001") - _, err = mh.sendMessage(1, 1, "Hello001") - fmt.Printf("err: %v", err) + resp, err := mh.sendMessage(0, 0, "Hello001") + fmt.Printf("resp.Status: %v err: %v", resp.StatusCode, err) + resp2, err2 := mh.sendMessage(1, 1, "Hello002") + fmt.Printf("resp.Status: %v err: %v", resp2.StatusCode, err2) }) fmt.Printf("%v\n", mh.errReport()) recs := mh.Log.filter(`.*received.*"message"`, true) - if len(recs) == 1 { - fmt.Println("skyhost_001 received message") + if len(recs) == 2 { + fmt.Println("received message") } fmt.Println(mh.Log.records) fmt.Println(recs) @@ -73,10 +75,10 @@ func ExampleMultiHead_sendMessage_msg001() { // skyhost_001 received message } -func TestMultiHead_sendMessage_msg001(t *testing.T) { +func TestMultiHead_sendMessage_Local(t *testing.T) { mh := makeMultiHeadN(1) - mh.RunTest(t, "msg001", func(mh *MultiHead) { - resp, err := mh.sendMessage(0, 0, "Hello001") + mh.RunTest(t, "Local", func(mh *MultiHead) { + resp, err := mh.sendMessage(1, 1, "Hello001") require.NoError(t, err) require.Equal(t, resp.StatusCode, 200) }) @@ -95,8 +97,8 @@ func TestMultiHead_sendMessage_msg001(t *testing.T) { // skyhost_001 received message } -func ExampleMultiHead_sendMessage_msg002() { - mh := makeMultiHeadN(128) +func ExampleMultiHead_sendMessage_msg255() { + mh := makeMultiHeadN(2) mh.RunExample(func(mh *MultiHead) { for i := uint(2); i < 120; i++ { resp, err := mh.sendMessage(i, i+1, fmt.Sprintf("Hello%03d-%03d", i, i+1)) @@ -118,6 +120,24 @@ func ExampleMultiHead_sendMessage_msg002() { } +func ExampleMultiHead_sendMessage_TCP() { + mh := makeMultiHeadN(3) + mh.RunExample(func(mh *MultiHead) { + resp, err := mh.sendMessage(1, 2, "Hello") + fmt.Printf("resp: %v \nerr: %v\n", resp, err) + time.Sleep(time.Second) + }) + + fmt.Printf("%v\n", strings.Join(mh.Log.records, "")) + fmt.Println(mh.Log.filter(`.*received.*`, true)) + + // Output: err: + // init errors: 0 + // start errors: 0 + // stop errors: 0 + +} + func ExampleMultihead_forwardAppPacket() { mh := makeMultiHeadN(2) mh.RunExample(func(mh *MultiHead) { diff --git a/vendor/github.com/alecthomas/repr/.travis.yml b/vendor/github.com/alecthomas/repr/.travis.yml new file mode 100644 index 000000000..57b35c710 --- /dev/null +++ b/vendor/github.com/alecthomas/repr/.travis.yml @@ -0,0 +1,4 @@ +sudo: false +language: go +install: go get -t -v ./... +go: 1.9 diff --git a/vendor/github.com/alecthomas/repr/COPYING b/vendor/github.com/alecthomas/repr/COPYING new file mode 100644 index 000000000..e477b0787 --- /dev/null +++ b/vendor/github.com/alecthomas/repr/COPYING @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Alec Thomas + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/alecthomas/repr/README.md b/vendor/github.com/alecthomas/repr/README.md new file mode 100644 index 000000000..04a8a89b3 --- /dev/null +++ b/vendor/github.com/alecthomas/repr/README.md @@ -0,0 +1,90 @@ +# Python's repr() for Go [![](https://godoc.org/github.com/alecthomas/repr?status.svg)](http://godoc.org/github.com/alecthomas/repr) [![Build Status](https://travis-ci.org/alecthomas/repr.png)](https://travis-ci.org/alecthomas/repr) + +This package attempts to represent Go values in a form that can be used almost directly in Go source +code. + +Unfortunately, some values (such as pointers to basic types) can not be represented directly in Go. +These values will be represented as `&`. eg. `&23` + +## Example + +```go +type test struct { + S string + I int + A []int +} + +func main() { + repr.Print(&test{ + S: "String", + I: 123, + A: []int{1, 2, 3}, + }) +} +``` + +Outputs + +``` +&main.test{S: "String", I: 123, A: []int{1, 2, 3}} +``` + +## Why repr and not [pp](https://github.com/k0kubun/pp)? + +pp is designed for printing coloured output to consoles, with (seemingly?) no way to disable this. If you don't want coloured output (eg. for use in diffs, logs, etc.) repr is for you. + +## Why repr and not [go-spew](https://github.com/davecgh/go-spew)? + +Repr deliberately contains much less metadata about values. It is designed to (generally) be copyable directly into source code. + +Compare go-spew: + +```go +(parser.expression) (len=1 cap=1) { + (parser.alternative) (len=1 cap=1) { + ([]interface {}) (len=1 cap=1) { + (*parser.repitition)(0xc82000b220)({ + expression: (parser.expression) (len=2 cap=2) { + (parser.alternative) (len=1 cap=1) { + ([]interface {}) (len=1 cap=1) { + (parser.str) (len=1) "a" + } + }, + (parser.alternative) (len=1 cap=1) { + ([]interface {}) (len=1 cap=1) { + (*parser.self)(0x593ef0)({ + }) + } + } + } + }) + } + } +} +``` + +To repr: + +```go +parser.expression{ + parser.alternative{ + []interface {}{ + &parser.repitition{ + expression: parser.expression{ + parser.alternative{ + []interface {}{ + parser.str("a"), + }, + }, + parser.alternative{ + []interface {}{ + &parser.self{ }, + }, + }, + }, + }, + }, + }, +} +``` diff --git a/vendor/github.com/alecthomas/repr/repr.go b/vendor/github.com/alecthomas/repr/repr.go new file mode 100644 index 000000000..7499ba0d5 --- /dev/null +++ b/vendor/github.com/alecthomas/repr/repr.go @@ -0,0 +1,325 @@ +// Package repr attempts to represent Go values in a form that can be copy-and-pasted into source +// code directly. +// +// Some values (such as pointers to basic types) can not be represented directly in +// Go. These values will be output as `&`. eg. `&23` +package repr + +import ( + "bytes" + "fmt" + "io" + "os" + "reflect" + "unsafe" +) + +var ( + // "Real" names of basic kinds, used to differentiate type aliases. + realKindName = map[reflect.Kind]string{ + reflect.Bool: "bool", + reflect.Int: "int", + reflect.Int8: "int8", + reflect.Int16: "int16", + reflect.Int32: "int32", + reflect.Int64: "int64", + reflect.Uint: "uint", + reflect.Uint8: "uint8", + reflect.Uint16: "uint16", + reflect.Uint32: "uint32", + reflect.Uint64: "uint64", + reflect.Uintptr: "uintptr", + reflect.Float32: "float32", + reflect.Float64: "float64", + reflect.Complex64: "complex64", + reflect.Complex128: "complex128", + reflect.Array: "array", + reflect.Chan: "chan", + reflect.Func: "func", + reflect.Map: "map", + reflect.Slice: "slice", + reflect.String: "string", + } + + goStringerType = reflect.TypeOf((*fmt.GoStringer)(nil)).Elem() + + byteSliceType = reflect.TypeOf([]byte{}) +) + +// Default prints to os.Stdout with two space indentation. +var Default = New(os.Stdout, Indent(" ")) + +// An Option modifies the default behaviour of a Printer. +type Option func(o *Printer) + +// Indent output by this much. +func Indent(indent string) Option { return func(o *Printer) { o.indent = indent } } + +// NoIndent disables indenting. +func NoIndent() Option { return Indent("") } + +// OmitEmpty sets whether empty field members should be omitted from output. +func OmitEmpty(omitEmpty bool) Option { return func(o *Printer) { o.omitEmpty = omitEmpty } } + +// IgnoreGoStringer disables use of the .GoString() method. +func IgnoreGoStringer() Option { return func(o *Printer) { o.ignoreGoStringer = true } } + +// Hide excludes the given types from representation, instead just printing the name of the type. +func Hide(ts ...interface{}) Option { + return func(o *Printer) { + for _, t := range ts { + rt := reflect.Indirect(reflect.ValueOf(t)).Type() + o.exclude[rt] = true + } + } +} + +// AlwaysIncludeType always includes explicit type information for each item. +func AlwaysIncludeType() Option { return func(o *Printer) { o.alwaysIncludeType = true } } + +// Printer represents structs in a printable manner. +type Printer struct { + indent string + omitEmpty bool + ignoreGoStringer bool + alwaysIncludeType bool + exclude map[reflect.Type]bool + w io.Writer +} + +// New creates a new Printer on w with the given Options. +func New(w io.Writer, options ...Option) *Printer { + p := &Printer{ + w: w, + indent: " ", + omitEmpty: true, + exclude: map[reflect.Type]bool{}, + } + for _, option := range options { + option(p) + } + return p +} + +func (p *Printer) nextIndent(indent string) string { + if p.indent != "" { + return indent + p.indent + } + return "" +} + +func (p *Printer) thisIndent(indent string) string { + if p.indent != "" { + return indent + } + return "" +} + +// Print the values. +func (p *Printer) Print(vs ...interface{}) { + for i, v := range vs { + if i > 0 { + fmt.Fprint(p.w, " ") + } + p.reprValue(map[reflect.Value]bool{}, reflect.ValueOf(v), "") + } +} + +// Println prints each value on a new line. +func (p *Printer) Println(vs ...interface{}) { + for i, v := range vs { + if i > 0 { + fmt.Fprint(p.w, " ") + } + p.reprValue(map[reflect.Value]bool{}, reflect.ValueOf(v), "") + } + fmt.Fprintln(p.w) +} + +func (p *Printer) reprValue(seen map[reflect.Value]bool, v reflect.Value, indent string) { // nolint: gocyclo + if seen[v] { + fmt.Fprint(p.w, "...") + return + } + seen[v] = true + defer delete(seen, v) + + if v.Kind() == reflect.Invalid || (v.Kind() == reflect.Ptr || v.Kind() == reflect.Map || v.Kind() == reflect.Chan || v.Kind() == reflect.Slice || v.Kind() == reflect.Func || v.Kind() == reflect.Interface) && v.IsNil() { + fmt.Fprint(p.w, "nil") + return + } + if p.exclude[v.Type()] { + fmt.Fprintf(p.w, "%s...", v.Type().Name()) + return + } + t := v.Type() + + if t == byteSliceType { + fmt.Fprintf(p.w, "[]byte(%q)", v.Interface()) + return + } + + // If we can't access a private field directly with reflection, try and do so via unsafe. + if !v.CanInterface() && v.CanAddr() { + uv := reflect.NewAt(t, unsafe.Pointer(v.UnsafeAddr())).Elem() + if uv.CanInterface() { + v = uv + } + } + // Attempt to use fmt.GoStringer interface. + if !p.ignoreGoStringer && t.Implements(goStringerType) { + fmt.Fprint(p.w, v.Interface().(fmt.GoStringer).GoString()) + return + } + in := p.thisIndent(indent) + ni := p.nextIndent(indent) + switch v.Kind() { + case reflect.Slice, reflect.Array: + if p.omitEmpty && v.Len() == 0 { + return + } + fmt.Fprintf(p.w, "%s{", v.Type()) + if v.Len() == 0 { + fmt.Fprint(p.w, "}") + } else { + if p.indent != "" { + fmt.Fprintf(p.w, "\n") + } + for i := 0; i < v.Len(); i++ { + e := v.Index(i) + fmt.Fprintf(p.w, "%s", ni) + p.reprValue(seen, e, ni) + if p.indent != "" { + fmt.Fprintf(p.w, ",\n") + } else if i < v.Len()-1 { + fmt.Fprintf(p.w, ", ") + } + } + fmt.Fprintf(p.w, "%s}", in) + } + + case reflect.Chan: + fmt.Fprintf(p.w, "make(") + fmt.Fprintf(p.w, "%s", v.Type()) + fmt.Fprintf(p.w, ", %d)", v.Cap()) + + case reflect.Map: + fmt.Fprintf(p.w, "%s{", v.Type()) + if p.indent != "" && v.Len() != 0 { + fmt.Fprintf(p.w, "\n") + } + for i, k := range v.MapKeys() { + kv := v.MapIndex(k) + fmt.Fprintf(p.w, "%s", ni) + p.reprValue(seen, k, ni) + fmt.Fprintf(p.w, ": ") + p.reprValue(seen, kv, ni) + if p.indent != "" { + fmt.Fprintf(p.w, ",\n") + } else if i < v.Len()-1 { + fmt.Fprintf(p.w, ", ") + } + } + fmt.Fprintf(p.w, "%s}", in) + + case reflect.Struct: + fmt.Fprintf(p.w, "%s{", v.Type()) + if p.indent != "" && v.NumField() != 0 { + fmt.Fprintf(p.w, "\n") + } + for i := 0; i < v.NumField(); i++ { + t := v.Type().Field(i) + f := v.Field(i) + if p.omitEmpty && isZero(f) { + continue + } + fmt.Fprintf(p.w, "%s%s: ", ni, t.Name) + p.reprValue(seen, f, ni) + if p.indent != "" { + fmt.Fprintf(p.w, ",\n") + } else if i < v.NumField()-1 { + fmt.Fprintf(p.w, ", ") + } + } + fmt.Fprintf(p.w, "%s}", indent) + + case reflect.Ptr: + if v.IsNil() { + fmt.Fprintf(p.w, "nil") + return + } + fmt.Fprintf(p.w, "&") + p.reprValue(seen, v.Elem(), indent) + + case reflect.String: + if t.Name() != "string" || p.alwaysIncludeType { + fmt.Fprintf(p.w, "%s(%q)", t, v.String()) + } else { + fmt.Fprintf(p.w, "%q", v.String()) + } + + case reflect.Interface: + if v.IsNil() { + fmt.Fprintf(p.w, "interface {}(nil)") + } else { + p.reprValue(seen, v.Elem(), indent) + } + + default: + if t.Name() != realKindName[t.Kind()] || p.alwaysIncludeType { + fmt.Fprintf(p.w, "%s(%v)", t, v) + } else { + fmt.Fprintf(p.w, "%v", v) + } + } +} + +// String returns a string representing v. +func String(v interface{}, options ...Option) string { + w := bytes.NewBuffer(nil) + options = append([]Option{NoIndent()}, options...) + p := New(w, options...) + p.Print(v) + return w.String() +} + +func extractOptions(vs ...interface{}) (args []interface{}, options []Option) { + for _, v := range vs { + if o, ok := v.(Option); ok { + options = append(options, o) + } else { + args = append(args, v) + } + } + return +} + +// Println prints v to os.Stdout, one per line. +func Println(vs ...interface{}) { + args, options := extractOptions(vs...) + New(os.Stdout, options...).Println(args...) +} + +// Print writes a representation of v to os.Stdout, separated by spaces. +func Print(vs ...interface{}) { + args, options := extractOptions(vs...) + New(os.Stdout, options...).Print(args...) +} + +func isZero(v reflect.Value) bool { + switch v.Kind() { + case reflect.Array, reflect.String: + return v.Len() == 0 + case reflect.Bool: + return !v.Bool() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return v.Int() == 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return v.Uint() == 0 + case reflect.Float32, reflect.Float64: + return v.Float() == 0 + case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: + return v.IsNil() + } + return false +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 9f7216578..3823e04b8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,3 +1,5 @@ +# github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1 +github.com/alecthomas/repr # github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc github.com/alecthomas/template github.com/alecthomas/template/parse From d9e63569e2f8fceea945249b25ce6a8fed48dfe3 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Mon, 26 Aug 2019 08:19:38 +0300 Subject: [PATCH 23/26] encore un efforti --- .../therealproxy-client.go | 2 +- pkg/router/router.go | 131 +++++++++--------- pkg/router/types.go | 58 ++++++++ pkg/visor/multihead.go | 5 +- pkg/visor/multihead_base_test.go | 2 +- pkg/visor/multihead_test.go | 7 +- 6 files changed, 128 insertions(+), 77 deletions(-) create mode 100644 pkg/router/types.go diff --git a/cmd/apps/therealproxy-client/therealproxy-client.go b/cmd/apps/therealproxy-client/therealproxy-client.go index 383f6194f..4f13b77b6 100644 --- a/cmd/apps/therealproxy-client/therealproxy-client.go +++ b/cmd/apps/therealproxy-client/therealproxy-client.go @@ -19,7 +19,7 @@ import ( const socksPort = 3 -var r = netutil.NewRetrier(time.Second, 1, 2) +var r = netutil.NewRetrier(time.Millisecond*50, 5, 2) func main() { var addr = flag.String("addr", ":1080", "Client address to listen on") diff --git a/pkg/router/router.go b/pkg/router/router.go index 6efd38987..78c76df2a 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -2,6 +2,7 @@ package router import ( + "bytes" "context" "errors" "fmt" @@ -9,7 +10,6 @@ import ( "net" "sync" "time" - "bytes" "github.com/sirupsen/logrus" "github.com/skycoin/dmsg/cipher" @@ -24,7 +24,6 @@ import ( "github.com/skycoin/skywire/pkg/setup" "github.com/skycoin/skywire/pkg/transport" "github.com/skycoin/skywire/pkg/transport/dmsg" - ) const ( @@ -97,7 +96,7 @@ func New(config *Config) *Router { } func (r *Router) trLog() *logrus.Entry { - return r.Logger.WithField("_module", th.GetCallerN(3)) + return r.Logger.WithField("func", th.GetCallerN(3)) } func (r *Router) trStart() (_ error) { r.trLog().Debug("ENTER") @@ -215,6 +214,7 @@ func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) // CreateLoop implements PactetRouter.CreateLoop func (r *Router) CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) { + r.Logger.Debug("CreateLoop ENTER") return r.requestLoop(conn, raddr) } @@ -319,8 +319,8 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { func reprS(v interface{}) string { w := bytes.NewBuffer(nil) - - options := []repr.Option{repr.OmitEmpty(true), repr.IgnoreGoStringer()} + + options := []repr.Option{repr.OmitEmpty(true), repr.IgnoreGoStringer()} // options = []Option{repr.NoIndent()} p := repr.New(w, options...) p.Print(v) @@ -329,11 +329,6 @@ func reprS(v interface{}) string { func (r *Router) forwardAppPacket(appPacket *app.Packet) error { defer r.trFinish(r.trStart()) - - r.trLog().Errorf("CALLERZZZ: %v\n", th.GetCallers(1)) - - r.trLog().Error("appPacket: ", appPacket) - if r == nil { return errors.New("router not initialized") } @@ -343,49 +338,47 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { return r.forwardLocalAppPacket(appPacket) } - loop, err := r.pm.GetLoop(appPacket.Loop.Local.Port, appPacket.Loop.Remote) - r.trLog().Error("r.pm.GetLoop: ", err) - - r.trLog().Warn("loop: ", loop) - r.trLog().Errorf("ALL LOOPS: %v\n", reprS(r.pm.Ports())) - // repr.Print(r.pm.Ports()) + if r.isPubKeyLocal(appPacket.Loop.Remote.PubKey) { + r.trLog().Info("Found in pubkeys file: ", appPacket.Loop.Remote.PubKey) + return r.forwardTCPAppPacket(appPacket) + } + loop, err := r.pm.GetLoop(appPacket.Loop.Local.Port, appPacket.Loop.Remote) if err != nil { return err } - - // r.Logger.Debugf("%v found loop: %v", th.GetCaller(), loop) - tr := r.tm.Transport(loop.trID) - + if tr == nil { + r.trLog().Debug("unknown transport") + return errors.New("unknown transport") + } r.trLog().Errorf("r.tm.transports: %v\n", r.tm.Transports()) // 9df41ada-be66-5640-bce2-917988069706 // trID := uuid.MustParse("9df41ada-be66-5640-bce2-917988069706") // trID := uuid.MustParse("9e566688-8717-5855-b1b7-92586ffc8c9b") // fmt.Printf("trID %v ERRROR %v\n", trID, err) - trID := transport.MakeTransportID(r.config.PubKey, appPacket.Loop.Remote.PubKey, "tcp-transport", true) + // trID := transport.MakeTransportID(r.config.PubKey, appPacket.Loop.Remote.PubKey, "tcp-transport", true) - tr = r.tm.Transport(trID) - fmt.Print("TRANSPORT: ", tr) - if tr == nil { - r.trLog().Error("Loop: ", loop) - r.trLog().Error("OOOOOOO") - r.Logger.Errorf("%v r.tm.Transport: tr==nil", th.GetCaller()) - return errors.New("unknown transport") + // tr = r.tm.Transport(trID) + // fmt.Print("TRANSPORT: ", tr) + // if tr == nil { + // r.trLog().Error("Loop: ", loop) + // r.trLog().Error("OOOOOOO") + // r.Logger.Errorf("%v r.tm.Transport: tr==nil", th.GetCaller()) + // return errors.New("unknown transport") - } + // } - r.Logger.Debugf("%v ATTEMPT TO FIND %v", th.GetCaller(), appPacket.Loop.Remote.PubKey) - if r.isPubKeyLocal(appPacket.Loop.Remote.PubKey) { - r.Logger.Infof("%v isPubKeyLocal = true", th.GetCaller()) - // return r.forwardTCPAppPacket(packet) - } + // r.Logger.Debugf("%v ATTEMPT TO FIND %v", th.GetCaller(), appPacket.Loop.Remote.PubKey) + // if r.isPubKeyLocal(appPacket.Loop.Remote.PubKey) { + // r.Logger.Infof("%v isPubKeyLocal = true", th.GetCaller()) + // // return r.forwardTCPAppPacket(packet) + // } rPacket := routing.MakePacket(loop.routeID, appPacket.Payload) r.Logger.Infof("%v Forwarded App packet from LocalPort %d using route ID %d", th.GetCaller(), appPacket.Loop.Local.Port, loop.routeID) _, err = tr.Write(rPacket) - if err != nil { r.Logger.Warnf("%v tr.Write: %v", err) } @@ -394,8 +387,7 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { } func (r *Router) forwardLocalAppPacket(appPacket *app.Packet) error { - r.Logger.Info(th.Trace("ENTER")) - defer r.Logger.Info(th.Trace("EXIT")) + defer r.trFinish(r.trStart()) b, err := r.pm.Get(appPacket.Loop.Remote.Port) if err != nil { @@ -417,33 +409,33 @@ func (r *Router) forwardLocalAppPacket(appPacket *app.Packet) error { return errSend } -func (r *Router) forwardTCPAppPacket(appPacket *app.Packet) error { // nolint: unused - r.Logger.Info(th.Trace("ENTER")) - defer r.Logger.Info(th.Trace("EXIT")) +func (r *Router) forwardTCPAppPacket(appPacket *app.Packet) error { + defer r.trFinish(r.trStart()) - b, err := r.pm.Get(appPacket.Loop.Remote.Port) + loop, err := r.pm.GetLoop(appPacket.Loop.Local.Port, appPacket.Loop.Remote) if err != nil { - r.Logger.Warnf("%v r.pm.Get: %v\n", th.GetCaller(), err) - return nil + return err } - - p := &app.Packet{ - Loop: routing.AddrLoop{ - Local: routing.Addr{Port: appPacket.Loop.Remote.Port}, - Remote: routing.Addr{PubKey: appPacket.Loop.Remote.PubKey, Port: appPacket.Loop.Local.Port}, - }, - Payload: appPacket.Payload, + trID := transport.MakeTransportID(r.config.PubKey, appPacket.Loop.Remote.PubKey, "tcp-transport", true) + tr := r.tm.Transport(trID) + if tr == nil { + r.trLog().Debug("unknown transport") + return errors.New("unknown transport") } - errSend := b.conn.Send(app.FrameSend, p, nil) - if errSend != nil { - r.Logger.Warnf("%v b.conn.Send: %v\n", th.GetCaller(), err) + r.trLog().Errorf("r.tm.transports: %v\n", r.tm.Transports()) + + rPacket := routing.MakePacket(loop.routeID, appPacket.Payload) + r.Logger.Infof("%v Forwarded App packet from LocalPort %d using route ID %d", th.GetCaller(), appPacket.Loop.Local.Port, loop.routeID) + _, err = tr.Write(rPacket) + if err != nil { + r.Logger.Warnf("%v tr.Write: %v", err) } - return errSend + + return err } func (r *Router) isPubKeyLocal(pk cipher.PubKey) bool { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + defer r.trFinish(r.trStart()) for _, fct := range r.tm.Factories() { switch tfct := fct.(type) { @@ -457,8 +449,8 @@ func (r *Router) isPubKeyLocal(pk cipher.PubKey) bool { } func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing.Addr, error) { - r.Logger.Info(th.Trace("ENTER")) - defer r.Logger.Info(th.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) if r == nil { return raddr, nil @@ -476,7 +468,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing r.Logger.Warnf("%v r.ConfirmLocalLoop err: %v", err) return routing.Addr{}, fmt.Errorf("confirm: %s", err) } - r.Logger.Infof("%v: r.confirmLocalLoop Created local loop on port %d", th.GetCaller(), laddr.Port) + r.trLog().Infof("r.confirmLocalLoop Created local loop on port %d", laddr.Port) return laddr, nil } @@ -500,9 +492,10 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing r.Logger.Debugf("%v laddr: %v raddr: %v \n", th.GetCaller(), laddr, raddr) if err := r.confirmTCPLoop(laddr, raddr); err != nil { r.Logger.Warnf("%v r.ConfirmTCPLoop err: %v", th.GetCaller(), err) + } - r.Logger.Debugf("%v laddr: %v raddr: %v \n", th.GetCaller(), laddr, raddr) - return laddr, nil + r.trLog().Debugf("laddr: %v raddr: %v \n", th.GetCaller(), raddr) + return laddr, err } proto, tr, err := r.setupProto(context.Background()) @@ -526,8 +519,8 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing } func (r *Router) confirmLocalLoop(laddr, raddr routing.Addr) error { - r.Logger.Info(th.Trace("ENTER")) - defer r.Logger.Info(th.Trace("EXIT")) + r.Logger.Debug(th.Trace("ENTER")) + defer r.Logger.Debug(th.Trace("EXIT")) b, err := r.pm.Get(raddr.Port) if err != nil { @@ -546,28 +539,28 @@ func (r *Router) confirmLocalLoop(laddr, raddr routing.Addr) error { func (r *Router) confirmTCPLoop(laddr, raddr routing.Addr) error { r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Info(th.Trace("EXIT")) + defer r.Logger.Debug(th.Trace("EXIT")) - r.Logger.Debugf("%v: laddr: %v, raddr: %v", th.GetCaller(), laddr, raddr) + r.trLog().Debugf("%v: laddr: %v, raddr: %v", th.GetCaller(), laddr, raddr) // portBind pb, err := r.pm.Get(raddr.Port) if err != nil { - r.Logger.Warnf("%v r.pm.Get %v", th.GetCaller(), err) + r.trLog().Warnf("%v r.pm.Get %v", th.GetCaller(), err) return err } addrs := [2]routing.Addr{raddr, laddr} if err = pb.conn.Send(app.FrameConfirmLoop, addrs, nil); err != nil { - r.Logger.Warnf("%v b.conn.Send %v", th.GetCaller(), err) + r.trLog().Warnf("%v b.conn.Send %v", th.GetCaller(), err) return err } tr, err := r.tm.CreateDataTransport(context.Background(), raddr.PubKey, "tcp-transport", true) if err != nil { - r.Logger.Warnf("%v r.tm.CreateDataTransport err: %v\n", th.GetCaller(), err) + r.trLog().Warnf("r.tm.CreateDataTransport err: %v\n", th.GetCaller(), err) } - r.Logger.Debugf("%v r.tm.CreateDataTransport tr: %T success: %v\n", th.GetCaller(), tr, err == nil) + r.trLog().Debugf("r.tm.CreateDataTransport tr: %T success: %v\n", tr, err == nil) return nil diff --git a/pkg/router/types.go b/pkg/router/types.go new file mode 100644 index 000000000..ef6f9669b --- /dev/null +++ b/pkg/router/types.go @@ -0,0 +1,58 @@ +package router + +/* + +import ( + "io" + "sync" + + "github.com/google/uuid" + "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/pkg/routing" +) + + +type portManager struct { + ports *portList + logger *logging.Logger +} + +type portList struct { + sync.Mutex + minPort routing.Port //type Port uint16 + ports map[routing.Port]*portBind +} + +type portBind struct { + conn *appProtocol + loops *loopList +} + +type appProtocol struct { + rw io.ReadWriteCloser + chans *appChanList +} + +type appChanList struct { + sync.Mutex + chans map[byte]chan []byte +} + +type loopList struct { + sync.Mutex + loops map[routing.Addr]loop // key: remote address (pk+port), value: forwarding transport and route ID. +} + +// Addr is routing.Addr +type Addr struct { + PubKey cipher.PubKey `json:"pk"` + Port routing.Port `json:"port"` //uint16 +} + + +type loop struct { + trID uuid.UUID //forwarding transport + routeID routing.RouteID // type RouteID uint32 +} +*/ \ No newline at end of file diff --git a/pkg/visor/multihead.go b/pkg/visor/multihead.go index e70ce41c4..0c31c8ca2 100644 --- a/pkg/visor/multihead.go +++ b/pkg/visor/multihead.go @@ -1,4 +1,4 @@ -//+build !no_ci +//+build multihead, !no_ci package visor @@ -99,7 +99,7 @@ func (mh *MultiHead) initIPPool(ipTemplate string, n uint) { func (mh *MultiHead) initCfgPool() { mh.cfgPool = make([]Config, len(mh.ipPool)) - localPathTmplt := "/tmp/multihead" + localPathTmplt := "/tmp/multihead/node%03d" baseCfg := mh.baseCfg for i := 0; i < len(mh.ipPool); i++ { @@ -127,7 +127,6 @@ func (mh *MultiHead) initCfgPool() { mh.cfgPool[i] = baseCfg } - } func (mh *MultiHead) pkAliases() []strsub { diff --git a/pkg/visor/multihead_base_test.go b/pkg/visor/multihead_base_test.go index b7c35b4c7..84e7e8072 100644 --- a/pkg/visor/multihead_base_test.go +++ b/pkg/visor/multihead_base_test.go @@ -1,4 +1,4 @@ -// +build !no_ci +// +build multihead, !no_ci package visor diff --git a/pkg/visor/multihead_test.go b/pkg/visor/multihead_test.go index a504957d8..c356e61c7 100644 --- a/pkg/visor/multihead_test.go +++ b/pkg/visor/multihead_test.go @@ -1,4 +1,4 @@ -// +build !no_ci +// +build !no_ci, multihead package visor @@ -85,7 +85,7 @@ func TestMultiHead_sendMessage_Local(t *testing.T) { fmt.Printf("%v\n", mh.errReport()) - recs := mh.Log.filter(`skyhost_001.*received.*"message"`, true) + recs := mh.Log.filter(`.*received.*"message"`, true) if len(recs) == 1 { fmt.Println("skyhost_001 received message") } @@ -124,6 +124,7 @@ func ExampleMultiHead_sendMessage_TCP() { mh := makeMultiHeadN(3) mh.RunExample(func(mh *MultiHead) { resp, err := mh.sendMessage(1, 2, "Hello") + fmt.Printf("resp: %v \nerr: %v\n", resp, err) time.Sleep(time.Second) }) @@ -138,7 +139,7 @@ func ExampleMultiHead_sendMessage_TCP() { } -func ExampleMultihead_forwardAppPacket() { +func ExampleMultiHead_forwardAppPacket() { mh := makeMultiHeadN(2) mh.RunExample(func(mh *MultiHead) { cfgA, _ := mh.cfgPool[0], mh.cfgPool[1] From fef6c40b5a401344233683968de35be501d64b6b Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Mon, 26 Aug 2019 08:44:54 +0300 Subject: [PATCH 24/26] restored logging --- cmd/apps/skychat/chat.go | 12 +++--- pkg/app/app.go | 4 +- pkg/app/protocol.go | 14 +++---- pkg/router/app_manager.go | 22 +++++----- pkg/router/port_manager.go | 2 +- pkg/router/router.go | 76 +++++++++++++++++----------------- pkg/routing/packet.go | 4 +- pkg/transport/manager.go | 12 +++--- pkg/transport/tcp_transport.go | 16 +++---- pkg/visor/visor.go | 4 +- 10 files changed, 84 insertions(+), 82 deletions(-) diff --git a/cmd/apps/skychat/chat.go b/cmd/apps/skychat/chat.go index 84bfea6a7..4c0b05d7c 100644 --- a/cmd/apps/skychat/chat.go +++ b/cmd/apps/skychat/chat.go @@ -39,8 +39,8 @@ var ( connsMu sync.Mutex trcLog = logger.WithField("_module", th.GetCallerN(3)) - trStart = func() error { trcLog.Debug("ENTER"); return nil } - trFinish = func(_ error) { trcLog.Debug(th.Trace("EXIT")) } + trStart = func() error { trcLog.Info("ENTER"); return nil } + trFinish = func(_ error) { trcLog.Info(th.Trace("EXIT")) } ) func main() { @@ -102,13 +102,13 @@ func handleConn(conn net.Conn) { buf := make([]byte, 32*1024) n, err := conn.Read(buf) if err != nil { - trcLog.Debug("failed to read packet:", err) + trcLog.Info("failed to read packet:", err) return } clientMsg, err := json.Marshal(map[string]string{"sender": raddr.PubKey.Hex(), "message": string(buf[:n])}) if err != nil { - trcLog.Debug("Failed to marshal json: ", err) + trcLog.Info("Failed to marshal json: ", err) } select { case clientCh <- string(clientMsg): @@ -136,7 +136,7 @@ func messageHandler(w http.ResponseWriter, req *http.Request) { } addr := routing.Addr{PubKey: pk, Port: 1} - trcLog.Debug("addr: ", addr) + trcLog.Info("addr: ", addr) connsMu.Lock() conn, ok := chatConns[pk] @@ -154,7 +154,7 @@ func messageHandler(w http.ResponseWriter, req *http.Request) { return err }) if err != nil { - trcLog.Debug("err: ", err) + trcLog.Info("err: ", err) http.Error(w, err.Error(), http.StatusBadRequest) return } diff --git a/pkg/app/app.go b/pkg/app/app.go index c504f7fa7..27af5b3b3 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -31,8 +31,8 @@ const ( var ( log = logging.MustGetLogger("app") - trStart = func() (_ error) { log.Debug(th.Trace("ENTER")); return } - trFinish = func(_ error) { log.Debug("FINISH"); return } + trStart = func() (_ error) { log.Info(th.Trace("ENTER")); return } + trFinish = func(_ error) { log.Info("FINISH"); return } ) // Config defines configuration parameters for App diff --git a/pkg/app/protocol.go b/pkg/app/protocol.go index a7c31d717..baad4e22e 100644 --- a/pkg/app/protocol.go +++ b/pkg/app/protocol.go @@ -73,7 +73,7 @@ func NewProtocol(rw io.ReadWriteCloser) *Protocol { // Send sends command Frame with payload and awaits for response. func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { - Logger.Debug(th.Trace("ENTER")) + Logger.Info(th.Trace("ENTER")) Logger.Debugf("%v cmd: %v, payload: %v\n", th.GetCaller(), cmd, payload) Logger.Debugf("%v CALLERS: %v\n", th.GetCaller(), th.GetCallers(3)) @@ -106,8 +106,8 @@ func (p *Protocol) Send(cmd Frame, payload, res interface{}) error { // Serve reads incoming frame, passes it to the handleFunc and writes results. func (p *Protocol) Serve(handleFunc func(Frame, []byte) (interface{}, error)) error { - Logger.Debug(th.Trace("ENTER")) - defer Logger.Debug(th.Trace("EXIT")) + Logger.Info(th.Trace("ENTER")) + defer Logger.Info(th.Trace("EXIT")) var cntr uint64 for { @@ -160,8 +160,8 @@ func (p *Protocol) Serve(handleFunc func(Frame, []byte) (interface{}, error)) er // Close closes underlying ReadWriter. func (p *Protocol) Close() error { - Logger.Debug(th.Trace("ENTER")) - defer Logger.Debug(th.Trace("EXIT")) + Logger.Info(th.Trace("ENTER")) + defer Logger.Info(th.Trace("EXIT")) if p == nil { return nil @@ -171,8 +171,8 @@ func (p *Protocol) Close() error { } func (p *Protocol) writeFrame(frame Frame, id byte, payload interface{}) (err error) { - Logger.Debug(th.Trace("ENTER")) - defer Logger.Debug(th.Trace("EXIT")) + Logger.Info(th.Trace("ENTER")) + defer Logger.Info(th.Trace("EXIT")) var data []byte if err, ok := payload.(error); ok { diff --git a/pkg/router/app_manager.go b/pkg/router/app_manager.go index 6809ccdfd..fcc186dbd 100644 --- a/pkg/router/app_manager.go +++ b/pkg/router/app_manager.go @@ -23,7 +23,7 @@ type appManager struct { func (am *appManager) serveHandler(frame app.Frame, payload []byte) (res interface{}, err error) { am.Logger.WithField("payload", app.Payload{Frame: frame, Data: payload}). Debug(th.Trace("ENTER")) - defer am.Logger.Debug(th.Trace("EXIT")) + defer am.Logger.Info(th.Trace("EXIT")) switch frame { case app.FrameInit: @@ -46,15 +46,15 @@ func (am *appManager) serveHandler(frame app.Frame, payload []byte) (res interfa } func (am *appManager) Serve() error { - am.Logger.Debug(th.Trace("ENTER")) - defer am.Logger.Debug(th.Trace("EXIT")) + am.Logger.Info(th.Trace("ENTER")) + defer am.Logger.Info(th.Trace("EXIT")) return am.proto.Serve(am.serveHandler) } func (am *appManager) initApp(payload []byte) error { - am.Logger.Debug(th.Trace("ENTER")) - defer am.Logger.Debug(th.Trace("EXIT")) + am.Logger.Info(th.Trace("ENTER")) + defer am.Logger.Info(th.Trace("EXIT")) var config app.Config if err := json.Unmarshal(payload, &config); err != nil { @@ -78,8 +78,8 @@ func (am *appManager) initApp(payload []byte) error { } func (am *appManager) setupLoop(payload []byte) (routing.Addr, error) { - am.Logger.Debug(th.Trace("ENTER")) - defer am.Logger.Debug(th.Trace("EXIT")) + am.Logger.Info(th.Trace("ENTER")) + defer am.Logger.Info(th.Trace("EXIT")) var raddr routing.Addr if err := json.Unmarshal(payload, &raddr); err != nil { @@ -90,8 +90,8 @@ func (am *appManager) setupLoop(payload []byte) (routing.Addr, error) { } func (am *appManager) handleCloseLoop(payload []byte) error { - am.Logger.Debug(th.Trace("ENTER")) - defer am.Logger.Debug(th.Trace("EXIT")) + am.Logger.Info(th.Trace("ENTER")) + defer am.Logger.Info(th.Trace("EXIT")) var loop routing.AddrLoop if err := json.Unmarshal(payload, &loop); err != nil { @@ -102,8 +102,8 @@ func (am *appManager) handleCloseLoop(payload []byte) error { } func (am *appManager) forwardAppPacket(payload []byte) error { - am.Logger.Debug(th.Trace("ENTER")) - defer am.Logger.Debug(th.Trace("EXIT")) + am.Logger.Info(th.Trace("ENTER")) + defer am.Logger.Info(th.Trace("EXIT")) packet := &app.Packet{} if err := json.Unmarshal(payload, packet); err != nil { diff --git a/pkg/router/port_manager.go b/pkg/router/port_manager.go index b2bfd0608..b7fc2002d 100644 --- a/pkg/router/port_manager.go +++ b/pkg/router/port_manager.go @@ -23,7 +23,7 @@ type portManager struct { } func (pm *portManager) debug(args ...interface{}) { - pm.logger.Debug(args) + pm.logger.Info(args) } func newPortManager(minPort routing.Port, logger *logging.Logger) *portManager { diff --git a/pkg/router/router.go b/pkg/router/router.go index 78c76df2a..941156373 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -96,14 +96,14 @@ func New(config *Config) *Router { } func (r *Router) trLog() *logrus.Entry { - return r.Logger.WithField("func", th.GetCallerN(3)) + return r.Logger.WithField("func", th.GetCallerN(4)) } func (r *Router) trStart() (_ error) { - r.trLog().Debug("ENTER") + r.trLog().Info("ENTER") return } func (r *Router) trFinish(_ error) { - r.trLog().Debug("EXIT") + r.trLog().Info("EXIT") return } @@ -176,8 +176,10 @@ func (r *Router) handleTransport(tp transport.Transport, isAccepted, isSetup boo // ServeApp handles App packets from the App connection on provided port. func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error { - defer r.trFinish(r.trStart()) - r.Logger.Info("Starting router") + r.Logger.Infof("%v ENTER", th.GetCaller()) + defer r.Logger.Infof("%v EXIT", th.GetCaller()) + + r.Logger.Infof("%v Starting router", th.GetCaller()) r.wg.Add(1) defer r.wg.Done() @@ -214,7 +216,7 @@ func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) // CreateLoop implements PactetRouter.CreateLoop func (r *Router) CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) { - r.Logger.Debug("CreateLoop ENTER") + r.Logger.Info("CreateLoop ENTER") return r.requestLoop(conn, raddr) } @@ -279,12 +281,12 @@ func (r *Router) serveTransport(rw io.ReadWriter) error { func (r *Router) ForwardPacket(payload []byte, rule routing.Rule) error { defer r.trFinish(r.trStart()) - r.Logger.Debugf("%v %v %v\n", th.GetCaller(), payload, rule) + r.Logger.Infof("%v %v %v\n", th.GetCaller(), payload, rule) packet := routing.MakePacket(rule.RouteID(), payload) tr := r.tm.Transport(rule.TransportID()) if tr == nil { - r.Logger.Debugf("%v: packet %v \n", th.GetCaller(), packet) + r.Logger.Infof("%v: packet %v \n", th.GetCaller(), packet) // tr = r.tm.CreateDataTransport(context.Background(), ra) return errors.New("unknown transport") @@ -349,7 +351,7 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { } tr := r.tm.Transport(loop.trID) if tr == nil { - r.trLog().Debug("unknown transport") + r.trLog().Info("unknown transport") return errors.New("unknown transport") } r.trLog().Errorf("r.tm.transports: %v\n", r.tm.Transports()) @@ -370,7 +372,7 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { // } - // r.Logger.Debugf("%v ATTEMPT TO FIND %v", th.GetCaller(), appPacket.Loop.Remote.PubKey) + // r.Logger.Infof("%v ATTEMPT TO FIND %v", th.GetCaller(), appPacket.Loop.Remote.PubKey) // if r.isPubKeyLocal(appPacket.Loop.Remote.PubKey) { // r.Logger.Infof("%v isPubKeyLocal = true", th.GetCaller()) // // return r.forwardTCPAppPacket(packet) @@ -419,7 +421,7 @@ func (r *Router) forwardTCPAppPacket(appPacket *app.Packet) error { trID := transport.MakeTransportID(r.config.PubKey, appPacket.Loop.Remote.PubKey, "tcp-transport", true) tr := r.tm.Transport(trID) if tr == nil { - r.trLog().Debug("unknown transport") + r.trLog().Info("unknown transport") return errors.New("unknown transport") } r.trLog().Errorf("r.tm.transports: %v\n", r.tm.Transports()) @@ -449,8 +451,8 @@ func (r *Router) isPubKeyLocal(pk cipher.PubKey) bool { } func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing.Addr, error) { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) if r == nil { return raddr, nil @@ -489,12 +491,12 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing } if r.isPubKeyLocal(raddr.PubKey) { - r.Logger.Debugf("%v laddr: %v raddr: %v \n", th.GetCaller(), laddr, raddr) + r.Logger.Infof("%v laddr: %v raddr: %v \n", th.GetCaller(), laddr, raddr) if err := r.confirmTCPLoop(laddr, raddr); err != nil { r.Logger.Warnf("%v r.ConfirmTCPLoop err: %v", th.GetCaller(), err) } - r.trLog().Debugf("laddr: %v raddr: %v \n", th.GetCaller(), raddr) + r.trLog().Infof("laddr: %v raddr: %v \n", th.GetCaller(), raddr) return laddr, err } @@ -519,12 +521,12 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing } func (r *Router) confirmLocalLoop(laddr, raddr routing.Addr) error { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) b, err := r.pm.Get(raddr.Port) if err != nil { - r.Logger.Debugf("%v r.pm.Get %v", th.GetCaller(), err) + r.Logger.Infof("%v r.pm.Get %v", th.GetCaller(), err) return err } @@ -538,10 +540,10 @@ func (r *Router) confirmLocalLoop(laddr, raddr routing.Addr) error { } func (r *Router) confirmTCPLoop(laddr, raddr routing.Addr) error { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) - r.trLog().Debugf("%v: laddr: %v, raddr: %v", th.GetCaller(), laddr, raddr) + r.trLog().Infof("%v: laddr: %v, raddr: %v", th.GetCaller(), laddr, raddr) // portBind pb, err := r.pm.Get(raddr.Port) @@ -560,15 +562,15 @@ func (r *Router) confirmTCPLoop(laddr, raddr routing.Addr) error { if err != nil { r.trLog().Warnf("r.tm.CreateDataTransport err: %v\n", th.GetCaller(), err) } - r.trLog().Debugf("r.tm.CreateDataTransport tr: %T success: %v\n", tr, err == nil) + r.trLog().Infof("r.tm.CreateDataTransport tr: %T success: %v\n", tr, err == nil) return nil } func (r *Router) confirmLoop(l routing.AddrLoop, rule routing.Rule) error { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) b, err := r.pm.Get(l.Local.Port) if err != nil { @@ -588,15 +590,15 @@ func (r *Router) confirmLoop(l routing.AddrLoop, rule routing.Rule) error { } func (r *Router) closeLoop(loop routing.AddrLoop) error { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) if err := r.destroyLoop(loop); err != nil { r.Logger.Warnf("Failed to remove loop: %s", err) } if r.isPubKeyLocal(loop.Remote.PubKey) { - r.Logger.Debug("%v isPubKeyLocal() == true", th.GetCaller()) + r.Logger.Info("%v isPubKeyLocal() == true", th.GetCaller()) return nil } proto, tr, err := r.setupProto(context.Background()) @@ -619,8 +621,8 @@ func (r *Router) closeLoop(loop routing.AddrLoop) error { } func (r *Router) loopClosed(loop routing.AddrLoop) error { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) b, err := r.pm.Get(loop.Local.Port) if err != nil { @@ -640,8 +642,8 @@ func (r *Router) loopClosed(loop routing.AddrLoop) error { } func (r *Router) destroyLoop(loop routing.AddrLoop) error { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) if r == nil { return nil @@ -662,8 +664,8 @@ func (r *Router) destroyLoop(loop routing.AddrLoop) error { } func (r *Router) setupProto(ctx context.Context) (*setup.Protocol, transport.Transport, error) { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) if len(r.config.SetupNodes) == 0 { r.Logger.Warnf("%v : route setup: no nodes", th.GetCaller()) @@ -681,8 +683,8 @@ func (r *Router) setupProto(ctx context.Context) (*setup.Protocol, transport.Tra } func (r *Router) fetchBestRoutes(source, destination cipher.PubKey) (fwd routing.Route, rev routing.Route, err error) { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) r.Logger.Infof("[Router.fetchBestRoutes] Requesting new routes from %s to %s", source, destination) timer := time.NewTimer(time.Second * 10) @@ -705,8 +707,8 @@ fetchRoutesAgain: // IsSetupTransport checks whether `tr` is running in the `setup` mode. func (r *Router) IsSetupTransport(tr *transport.ManagedTransport) bool { - r.Logger.Debug(th.Trace("ENTER")) - defer r.Logger.Debug(th.Trace("EXIT")) + r.Logger.Info(th.Trace("ENTER")) + defer r.Logger.Info(th.Trace("EXIT")) for _, pk := range r.config.SetupNodes { if tr.RemotePK() == pk { diff --git a/pkg/routing/packet.go b/pkg/routing/packet.go index 6495ac743..7b8fa5598 100644 --- a/pkg/routing/packet.go +++ b/pkg/routing/packet.go @@ -16,8 +16,8 @@ type Packet []byte // MakePacket constructs a new Packet. If payload size is more than // uint16, MakePacket will panic. func MakePacket(id RouteID, payload []byte) Packet { - log.Debug(th.Trace("ENTER")) - defer log.Debug(th.Trace("ENTER")) + log.Info(th.Trace("ENTER")) + defer log.Info(th.Trace("ENTER")) if len(payload) > math.MaxUint16 { panic("packet size exceeded") diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 7cb096c35..92f408d34 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -214,8 +214,8 @@ func (tm *Manager) Serve(ctx context.Context) error { // CreateSetupTransport begins to attempt to establish setup transports to the given 'remote' node. func (tm *Manager) CreateSetupTransport(ctx context.Context, remote cipher.PubKey, tpType string) (Transport, error) { - tm.Logger.Debug(th.Trace("ENTER")) - defer tm.Logger.Debug(th.Trace("EXIT")) + tm.Logger.Info(th.Trace("ENTER")) + defer tm.Logger.Info(th.Trace("EXIT")) factory, ok := tm.factories[tpType] if !ok { @@ -233,8 +233,8 @@ func (tm *Manager) CreateSetupTransport(ctx context.Context, remote cipher.PubKe // CreateDataTransport begins to attempt to establish data transports to the given 'remote' node. func (tm *Manager) CreateDataTransport(ctx context.Context, remote cipher.PubKey, tpType string, public bool) (*ManagedTransport, error) { - tm.Logger.Debug(th.Trace("ENTER")) - defer tm.Logger.Debug(th.Trace("EXIT")) + tm.Logger.Info(th.Trace("ENTER")) + defer tm.Logger.Info(th.Trace("EXIT")) factory, ok := tm.factories[tpType] if !ok { @@ -327,8 +327,8 @@ func (tm *Manager) Close() error { } func (tm *Manager) dialTransport(ctx context.Context, factory Factory, remote cipher.PubKey, public bool) (Transport, *Entry, error) { - tm.Logger.Debug(th.Trace("ENTER")) - defer tm.Logger.Debug(th.Trace("EXIT")) + tm.Logger.Info(th.Trace("ENTER")) + defer tm.Logger.Info(th.Trace("EXIT")) if tm.isClosing() { return nil, nil, errors.New("transport.Manager is closing. Skipping dialing transport") diff --git a/pkg/transport/tcp_transport.go b/pkg/transport/tcp_transport.go index 0b89a6550..f0086c546 100644 --- a/pkg/transport/tcp_transport.go +++ b/pkg/transport/tcp_transport.go @@ -30,8 +30,8 @@ type TCPFactory struct { // NewTCPFactory constructs a new TCP Factory func NewTCPFactory(pk cipher.PubKey, pubkeysFile string, tcpAddr string, logger *logging.Logger) (Factory, error) { - logger.Debug(th.Trace("ENTER")) - defer logger.Debug(th.Trace("EXIT")) + logger.Info(th.Trace("ENTER")) + defer logger.Info(th.Trace("EXIT")) pkTbl, err := FilePubKeyTable(pubkeysFile) if err != nil { @@ -54,8 +54,8 @@ func NewTCPFactory(pk cipher.PubKey, pubkeysFile string, tcpAddr string, logger // Accept accepts a remotely-initiated Transport. func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { - f.Logger.Debug(th.Trace("ENTER")) - defer f.Logger.Debug(th.Trace("EXIT")) + f.Logger.Info(th.Trace("ENTER")) + defer f.Logger.Info(th.Trace("EXIT")) conn, err := f.Lsr.AcceptTCP() if err != nil { @@ -77,8 +77,8 @@ func (f *TCPFactory) Accept(ctx context.Context) (Transport, error) { // Dial initiates a Transport with a remote node. func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, error) { - f.Logger.Debug(th.Trace("ENTER")) - defer f.Logger.Debug(th.Trace("EXIT")) + f.Logger.Info(th.Trace("ENTER")) + defer f.Logger.Info(th.Trace("EXIT")) addr := f.PkTable.RemoteAddr(remote) if addr == "" { @@ -115,8 +115,8 @@ func (f *TCPFactory) Dial(ctx context.Context, remote cipher.PubKey) (Transport, // Close implements io.Closer func (f *TCPFactory) Close() error { - f.Logger.Debug(th.Trace("ENTER")) - defer f.Logger.Debug(th.Trace("EXIT")) + f.Logger.Info(th.Trace("ENTER")) + defer f.Logger.Info(th.Trace("EXIT")) if f == nil { return nil diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index d43e50a5b..421dc0c7e 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -257,8 +257,8 @@ func (node *Node) Start() error { if node == nil { return errors.New("not initialized node") } - // node.logger.Debug(th.Trace("ENTER")) - // defer node.logger.Debug(th.Trace("ENTER")) + // node.logger.Info(th.Trace("ENTER")) + // defer node.logger.Info(th.Trace("ENTER")) node.logger.Info("started node") ctx := context.Background() From 3ac70d056978e22aff9fcaa6a3a193a52a2eef15 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Mon, 26 Aug 2019 09:46:52 +0300 Subject: [PATCH 25/26] routing.Loop renamed to routing.AddressPair --- pkg/app/app.go | 22 ++++----- pkg/app/app_test.go | 30 +++++------ pkg/app/packet.go | 4 +- pkg/app/packet_test.go | 2 +- pkg/app/payload.go | 2 +- pkg/router/app_manager.go | 10 ++-- pkg/router/app_manager_test.go | 8 +-- pkg/router/mockrouter.go | 4 +- pkg/router/port_manager.go | 10 ++-- pkg/router/route_manager.go | 6 +-- pkg/router/route_manager_test.go | 16 +++--- pkg/router/router.go | 85 +++++++++++++------------------- pkg/router/router_close_test.go | 2 +- pkg/router/router_setup_test.go | 6 +-- pkg/router/router_test.go | 16 +++--- pkg/routing/loop.go | 12 ++--- pkg/setup/node.go | 6 +-- pkg/setup/node_test.go | 4 +- 18 files changed, 115 insertions(+), 130 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 27af5b3b3..aabb0b39b 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -51,7 +51,7 @@ type App struct { acceptChan chan [2]routing.Addr doneChan chan struct{} - conns map[routing.AddrLoop]io.ReadWriteCloser + conns map[routing.AddressPair]io.ReadWriteCloser mu sync.Mutex } @@ -87,7 +87,7 @@ func SetupFromPipe(config *Config, inFD, outFD uintptr) (*App, error) { proto: NewProtocol(pipeConn), acceptChan: make(chan [2]routing.Addr), doneChan: make(chan struct{}), - conns: make(map[routing.AddrLoop]io.ReadWriteCloser), + conns: make(map[routing.AddressPair]io.ReadWriteCloser), } go app.handleProto() @@ -148,8 +148,8 @@ func (app *App) Accept() (net.Conn, error) { raddr := addrs[1] // TODO: Why is that? - loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} - // loop := routing.AddrLoop{Local: laddr, Remote: raddr} + loop := routing.AddressPair{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} + // loop := routing.AddressPair{Local: laddr, Remote: raddr} conn, out := net.Pipe() app.mu.Lock() app.conns[loop] = conn @@ -171,8 +171,8 @@ func (app *App) Dial(raddr routing.Addr) (net.Conn, error) { } // TODO: Why is that way? - loop := routing.AddrLoop{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} - // loop := routing.AddrLoop{Local: laddr, Remote: raddr} + loop := routing.AddressPair{Local: routing.Addr{Port: laddr.Port}, Remote: raddr} + // loop := routing.AddressPair{Local: laddr, Remote: raddr} conn, out := net.Pipe() app.mu.Lock() @@ -212,7 +212,7 @@ func (app *App) handleProto() { } } -func (app *App) serveConn(loop routing.AddrLoop, conn io.ReadWriteCloser) { +func (app *App) serveConn(loop routing.AddressPair, conn io.ReadWriteCloser) { defer trFinish(trStart()) log.Debugf("%v loop: %v conn: %T\n", th.GetCaller(), loop, conn) @@ -235,7 +235,7 @@ func (app *App) serveConn(loop routing.AddrLoop, conn io.ReadWriteCloser) { break } - packet := &Packet{Loop: loop, Payload: buf[:n]} + packet := &Packet{AddressPair: loop, Payload: buf[:n]} if err := app.proto.Send(FrameSend, packet, nil); err != nil { break } @@ -260,7 +260,7 @@ func (app *App) forwardPacket(data []byte) error { } app.mu.Lock() - conn := app.conns[packet.Loop] + conn := app.conns[packet.AddressPair] app.mu.Unlock() if conn == nil { @@ -274,7 +274,7 @@ func (app *App) forwardPacket(data []byte) error { func (app *App) closeConn(data []byte) error { defer trFinish(trStart()) - var loop routing.AddrLoop + var loop routing.AddressPair if err := json.Unmarshal(data, &loop); err != nil { return err } @@ -302,7 +302,7 @@ func (app *App) confirmLoop(data []byte) error { raddr := addrs[1] app.mu.Lock() - conn := app.conns[routing.AddrLoop{Local: laddr, Remote: raddr}] + conn := app.conns[routing.AddressPair{Local: laddr, Remote: raddr}] app.mu.Unlock() if conn != nil { diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index ab97cefba..1afdb23d7 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -39,7 +39,7 @@ func TestAppDial(t *testing.T) { in, out := net.Pipe() proto := NewProtocol(out) - app := &App{proto: NewProtocol(in), conns: make(map[routing.AddrLoop]io.ReadWriteCloser)} + app := &App{proto: NewProtocol(in), conns: make(map[routing.AddressPair]io.ReadWriteCloser)} go app.handleProto() dataCh := make(chan []byte) @@ -65,13 +65,13 @@ func TestAppDial(t *testing.T) { assert.Equal(t, rpk.Hex()+":3", conn.RemoteAddr().String()) assert.Equal(t, lpk.Hex()+":2", conn.LocalAddr().String()) - require.NotNil(t, app.conns[routing.AddrLoop{Local: routing.Addr{Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}]) + require.NotNil(t, app.conns[routing.AddressPair{Local: routing.Addr{Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}]) require.NoError(t, conn.Close()) // Justified. Attempt to remove produces: FAIL time.Sleep(100 * time.Millisecond) - var loop routing.AddrLoop + var loop routing.AddressPair require.NoError(t, json.Unmarshal(<-dataCh, &loop)) assert.Equal(t, routing.Port(2), loop.Local.Port) assert.Equal(t, rpk, loop.Remote.PubKey) @@ -88,7 +88,7 @@ func TestAppAccept(t *testing.T) { lpk, _ := cipher.GenerateKeyPair() rpk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() - app := &App{proto: NewProtocol(in), acceptChan: make(chan [2]routing.Addr, 2), conns: make(map[routing.AddrLoop]io.ReadWriteCloser)} + app := &App{proto: NewProtocol(in), acceptChan: make(chan [2]routing.Addr, 2), conns: make(map[routing.AddressPair]io.ReadWriteCloser)} go app.handleProto() proto := NewProtocol(out) @@ -139,7 +139,7 @@ func TestAppWrite(t *testing.T) { appIn, appOut := net.Pipe() app := &App{proto: NewProtocol(in)} go app.handleProto() - go app.serveConn(routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}, appIn) + go app.serveConn(routing.AddressPair{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}, appIn) proto := NewProtocol(out) dataCh := make(chan []byte) @@ -162,10 +162,10 @@ func TestAppWrite(t *testing.T) { packet := &Packet{} require.NoError(t, json.Unmarshal(<-dataCh, packet)) - assert.Equal(t, rpk, packet.Loop.Remote.PubKey) - assert.Equal(t, routing.Port(3), packet.Loop.Remote.Port) - assert.Equal(t, routing.Port(2), packet.Loop.Local.Port) - assert.Equal(t, lpk, packet.Loop.Local.PubKey) + assert.Equal(t, rpk, packet.AddressPair.Remote.PubKey) + assert.Equal(t, routing.Port(3), packet.AddressPair.Remote.Port) + assert.Equal(t, routing.Port(2), packet.AddressPair.Local.Port) + assert.Equal(t, lpk, packet.AddressPair.Local.PubKey) assert.Equal(t, []byte("foo"), packet.Payload) require.NoError(t, proto.Close()) @@ -178,7 +178,7 @@ func TestAppRead(t *testing.T) { pk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() appIn, appOut := net.Pipe() - app := &App{proto: NewProtocol(in), conns: map[routing.AddrLoop]io.ReadWriteCloser{routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}}: appIn}} + app := &App{proto: NewProtocol(in), conns: map[routing.AddressPair]io.ReadWriteCloser{routing.AddressPair{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}}: appIn}} go app.handleProto() proto := NewProtocol(out) @@ -189,7 +189,7 @@ func TestAppRead(t *testing.T) { errCh := make(chan error) go func() { - errCh <- proto.Send(FrameSend, &Packet{routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}}, []byte("foo")}, nil) + errCh <- proto.Send(FrameSend, &Packet{routing.AddressPair{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}}, []byte("foo")}, nil) }() buf := make([]byte, 3) @@ -246,7 +246,7 @@ func TestAppCloseConn(t *testing.T) { rpk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() appIn, appOut := net.Pipe() - app := &App{proto: NewProtocol(in), conns: map[routing.AddrLoop]io.ReadWriteCloser{routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}: appIn}} + app := &App{proto: NewProtocol(in), conns: map[routing.AddressPair]io.ReadWriteCloser{routing.AddressPair{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}: appIn}} go app.handleProto() proto := NewProtocol(out) @@ -257,7 +257,7 @@ func TestAppCloseConn(t *testing.T) { errCh := make(chan error) go func() { - errCh <- proto.Send(FrameClose, routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}, nil) + errCh <- proto.Send(FrameClose, routing.AddressPair{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}, nil) }() _, err := appOut.Read(make([]byte, 3)) @@ -273,7 +273,7 @@ func TestAppClose(t *testing.T) { rpk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() appIn, appOut := net.Pipe() - app := &App{proto: NewProtocol(in), conns: map[routing.AddrLoop]io.ReadWriteCloser{routing.AddrLoop{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}: appIn}, doneChan: make(chan struct{})} + app := &App{proto: NewProtocol(in), conns: map[routing.AddressPair]io.ReadWriteCloser{routing.AddressPair{Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}: appIn}, doneChan: make(chan struct{})} go app.handleProto() proto := NewProtocol(out) @@ -296,7 +296,7 @@ func TestAppClose(t *testing.T) { _, err := appOut.Read(make([]byte, 3)) require.Equal(t, io.EOF, err) - var loop routing.AddrLoop + var loop routing.AddressPair require.NoError(t, json.Unmarshal(<-dataCh, &loop)) assert.Equal(t, lpk, loop.Local.PubKey) assert.Equal(t, routing.Port(2), loop.Local.Port) diff --git a/pkg/app/packet.go b/pkg/app/packet.go index dd596a1e4..9229115e1 100644 --- a/pkg/app/packet.go +++ b/pkg/app/packet.go @@ -4,6 +4,6 @@ import "github.com/skycoin/skywire/pkg/routing" // Packet represents message exchanged between App and Node. type Packet struct { - Loop routing.AddrLoop `json:"loop"` - Payload []byte `json:"payload"` + AddressPair routing.AddressPair `json:"loop"` + Payload []byte `json:"payload"` } diff --git a/pkg/app/packet_test.go b/pkg/app/packet_test.go index 4a40b5708..5464085c8 100644 --- a/pkg/app/packet_test.go +++ b/pkg/app/packet_test.go @@ -12,7 +12,7 @@ func ExamplePacket() { var lpk, rpk cipher.PubKey laddr := routing.Addr{Port: 0, PubKey: lpk} raddr := routing.Addr{Port: 0, PubKey: rpk} - loop := routing.AddrLoop{Local: laddr, Remote: raddr} + loop := routing.AddressPair{Local: laddr, Remote: raddr} fmt.Println(raddr.Network()) fmt.Printf("%v\n", raddr) diff --git a/pkg/app/payload.go b/pkg/app/payload.go index cb1972d65..4f7a721b3 100644 --- a/pkg/app/payload.go +++ b/pkg/app/payload.go @@ -34,7 +34,7 @@ func (p Payload) String() string { } return fmt.Sprintf("{FrameSend: %s}", packet) case FrameClose: - var loop routing.AddrLoop + var loop routing.AddressPair if err := json.Unmarshal(p.Data, &loop); err != nil { return fmt.Sprintf("{FrameClose. Error: %v, data: %v}", err, p.Data) } diff --git a/pkg/router/app_manager.go b/pkg/router/app_manager.go index fcc186dbd..72770ac8e 100644 --- a/pkg/router/app_manager.go +++ b/pkg/router/app_manager.go @@ -22,7 +22,7 @@ type appManager struct { func (am *appManager) serveHandler(frame app.Frame, payload []byte) (res interface{}, err error) { am.Logger.WithField("payload", app.Payload{Frame: frame, Data: payload}). - Debug(th.Trace("ENTER")) + Info(th.Trace("ENTER")) defer am.Logger.Info(th.Trace("EXIT")) switch frame { @@ -93,7 +93,7 @@ func (am *appManager) handleCloseLoop(payload []byte) error { am.Logger.Info(th.Trace("ENTER")) defer am.Logger.Info(th.Trace("EXIT")) - var loop routing.AddrLoop + var loop routing.AddressPair if err := json.Unmarshal(payload, &loop); err != nil { return err } @@ -109,7 +109,9 @@ func (am *appManager) forwardAppPacket(payload []byte) error { if err := json.Unmarshal(payload, packet); err != nil { return err } - am.Logger.Debugf("%v: %v\n", th.GetCaller(), packet) + am.Logger.Infof("%v: %v\n", th.GetCaller(), packet) - return am.router.ForwardAppPacket(am.proto, packet) + err := am.router.ForwardAppPacket(am.proto, packet) + am.Logger.Infof("%v am.router.ForwardAppPacket: %v", th.GetCaller(), err) + return err } diff --git a/pkg/router/app_manager_test.go b/pkg/router/app_manager_test.go index 20ce050ca..3607104af 100644 --- a/pkg/router/app_manager_test.go +++ b/pkg/router/app_manager_test.go @@ -91,7 +91,7 @@ func TestAppManagerSetupLoop(t *testing.T) { func TestAppManagerCloseLoop(t *testing.T) { in, out := net.Pipe() - // var inLoop routing.AddrLoop + // var inLoop routing.AddressPair r := new(MockRouter) am := &appManager{ Logger: logging.MustGetLogger("routesetup"), @@ -99,7 +99,7 @@ func TestAppManagerCloseLoop(t *testing.T) { proto: app.NewProtocol(out), appConf: &app.Config{AppName: "foo", AppVersion: "0.0.1"}, // &appCallbacks{ - // CloseLoop: func(conn *app.Protocol, loop routing.AddrLoop) error { + // CloseLoop: func(conn *app.Protocol, loop routing.AddressPair) error { // inLoop = loop // return nil // }, @@ -117,7 +117,7 @@ func TestAppManagerCloseLoop(t *testing.T) { lpk, _ := cipher.GenerateKeyPair() rpk, _ := cipher.GenerateKeyPair() - loop := routing.AddrLoop{ + loop := routing.AddressPair{ Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}} err := proto.Send(app.FrameClose, loop, nil) @@ -152,7 +152,7 @@ func TestAppManagerForward(t *testing.T) { lpk, _ := cipher.GenerateKeyPair() rpk, _ := cipher.GenerateKeyPair() packet := &app.Packet{Payload: []byte("foo"), - Loop: routing.AddrLoop{ + AddressPair: routing.AddressPair{ Local: routing.Addr{PubKey: lpk, Port: 2}, Remote: routing.Addr{PubKey: rpk, Port: 3}}} err := proto.Send(app.FrameSend, packet, nil) diff --git a/pkg/router/mockrouter.go b/pkg/router/mockrouter.go index 0d3f25a11..bb34b7b8e 100644 --- a/pkg/router/mockrouter.go +++ b/pkg/router/mockrouter.go @@ -22,7 +22,7 @@ type MockRouter struct { errChan chan error inPacket *app.Packet - inLoop routing.AddrLoop + inLoop routing.AddressPair } // Ports implements PacketRouter.Ports @@ -78,7 +78,7 @@ func (r *MockRouter) IsSetupTransport(tr *transport.ManagedTransport) bool { } // CloseLoop implements PacketRouter.CloseLoop -func (r *MockRouter) CloseLoop(conn *app.Protocol, loop routing.AddrLoop) error { +func (r *MockRouter) CloseLoop(conn *app.Protocol, loop routing.AddressPair) error { r.inLoop = loop return nil } diff --git a/pkg/router/port_manager.go b/pkg/router/port_manager.go index b7fc2002d..bf888a8da 100644 --- a/pkg/router/port_manager.go +++ b/pkg/router/port_manager.go @@ -12,11 +12,6 @@ import ( th "github.com/skycoin/skywire/internal/testhelpers" ) -// var ( -// logger = logging.MustGetLogger("router") -// debug = logger.Debug -// ) - type portManager struct { ports *portList logger *logging.Logger @@ -35,7 +30,10 @@ func (pm *portManager) Alloc(conn *app.Protocol) routing.Port { defer pm.debug(th.Trace("EXIT")) b := &portBind{conn, newLoopList()} - return pm.ports.add(b) + + rPort := pm.ports.add(b) + pm.logger.Info(th.GetCaller(), "Added port: ", rPort) + return rPort } func (pm *portManager) Open(port routing.Port, proto *app.Protocol) error { diff --git a/pkg/router/route_manager.go b/pkg/router/route_manager.go index d68182177..e29e7cb61 100644 --- a/pkg/router/route_manager.go +++ b/pkg/router/route_manager.go @@ -14,8 +14,8 @@ import ( ) type setupCallbacks struct { - ConfirmLoop func(loop routing.AddrLoop, rule routing.Rule) (err error) - LoopClosed func(loop routing.AddrLoop) error + ConfirmLoop func(loop routing.AddressPair, rule routing.Rule) (err error) + LoopClosed func(loop routing.AddressPair) error } type routeManager struct { @@ -42,7 +42,7 @@ func (rm *routeManager) GetRule(routeID routing.RouteID) (routing.Rule, error) { return rule, nil } -func (rm *routeManager) RemoveLoopRule(loop routing.AddrLoop) error { +func (rm *routeManager) RemoveLoopRule(loop routing.AddressPair) error { var appRouteID routing.RouteID var appRule routing.Rule err := rm.rt.RangeRules(func(routeID routing.RouteID, rule routing.Rule) bool { diff --git a/pkg/router/route_manager_test.go b/pkg/router/route_manager_test.go index 200a1cca5..a6c831032 100644 --- a/pkg/router/route_manager_test.go +++ b/pkg/router/route_manager_test.go @@ -47,11 +47,11 @@ func TestRouteManagerRemoveLoopRule(t *testing.T) { _, err := rt.AddRule(rule) require.NoError(t, err) - loop := routing.AddrLoop{Local: routing.Addr{Port: 3}, Remote: routing.Addr{PubKey: pk, Port: 3}} + loop := routing.AddressPair{Local: routing.Addr{Port: 3}, Remote: routing.Addr{PubKey: pk, Port: 3}} require.NoError(t, rm.RemoveLoopRule(loop)) assert.Equal(t, 1, rt.Count()) - loop = routing.AddrLoop{Local: routing.Addr{Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}} + loop = routing.AddressPair{Local: routing.Addr{Port: 2}, Remote: routing.Addr{PubKey: pk, Port: 3}} require.NoError(t, rm.RemoveLoopRule(loop)) assert.Equal(t, 0, rt.Count()) } @@ -120,10 +120,10 @@ func TestRouteManagerDeleteRules(t *testing.T) { func TestRouteManagerConfirmLoop(t *testing.T) { rt := manageRoutingTable(routing.InMemoryRoutingTable()) - var inLoop routing.AddrLoop + var inLoop routing.AddressPair var inRule routing.Rule callbacks := &setupCallbacks{ - ConfirmLoop: func(loop routing.AddrLoop, rule routing.Rule) (err error) { + ConfirmLoop: func(loop routing.AddressPair, rule routing.Rule) (err error) { inLoop = loop inRule = rule return nil @@ -146,7 +146,7 @@ func TestRouteManagerConfirmLoop(t *testing.T) { require.NoError(t, rt.SetRule(1, rule)) ld := routing.LoopData{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: pk, Port: 3, @@ -170,9 +170,9 @@ func TestRouteManagerConfirmLoop(t *testing.T) { func TestRouteManagerLoopClosed(t *testing.T) { rt := manageRoutingTable(routing.InMemoryRoutingTable()) - var inLoop routing.AddrLoop + var inLoop routing.AddressPair callbacks := &setupCallbacks{ - LoopClosed: func(loop routing.AddrLoop) error { + LoopClosed: func(loop routing.AddressPair) error { inLoop = loop return nil }, @@ -196,7 +196,7 @@ func TestRouteManagerLoopClosed(t *testing.T) { require.NoError(t, rt.SetRule(1, rule)) ld := routing.LoopData{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: pk, Port: 3, diff --git a/pkg/router/router.go b/pkg/router/router.go index 941156373..e909d7e94 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -55,7 +55,7 @@ type PacketRouter interface { ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error IsSetupTransport(tr *transport.ManagedTransport) bool CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) - CloseLoop(conn *app.Protocol, loop routing.AddrLoop) error + CloseLoop(conn *app.Protocol, loop routing.AddressPair) error ForwardAppPacket(conn *app.Protocol, packet *app.Packet) error } @@ -176,8 +176,7 @@ func (r *Router) handleTransport(tp transport.Transport, isAccepted, isSetup boo // ServeApp handles App packets from the App connection on provided port. func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error { - r.Logger.Infof("%v ENTER", th.GetCaller()) - defer r.Logger.Infof("%v EXIT", th.GetCaller()) + defer r.trFinish(r.trStart()) r.Logger.Infof("%v Starting router", th.GetCaller()) @@ -198,7 +197,7 @@ func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) for _, port := range r.pm.AppPorts(appProto) { for _, addr := range r.pm.Close(port) { - if err := r.closeLoop(routing.AddrLoop{Local: routing.Addr{Port: port}, Remote: addr}); err != nil { + if err := r.closeLoop(routing.AddressPair{Local: routing.Addr{Port: port}, Remote: addr}); err != nil { log.WithError(err).Warn("Failed to close loop") } } @@ -216,17 +215,19 @@ func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) // CreateLoop implements PactetRouter.CreateLoop func (r *Router) CreateLoop(conn *app.Protocol, raddr routing.Addr) (laddr routing.Addr, err error) { - r.Logger.Info("CreateLoop ENTER") + defer r.trFinish(r.trStart()) return r.requestLoop(conn, raddr) } // CloseLoop implements PactetRouter.CloseLoop -func (r *Router) CloseLoop(_ *app.Protocol, loop routing.AddrLoop) error { +func (r *Router) CloseLoop(_ *app.Protocol, loop routing.AddressPair) error { + defer r.trFinish(r.trStart()) return r.closeLoop(loop) } // ForwardAppPacket implements PactetRouter.ForwardAppPacket func (r *Router) ForwardAppPacket(_ *app.Protocol, packet *app.Packet) error { + defer r.trFinish(r.trStart()) return r.forwardAppPacket(packet) } @@ -306,7 +307,7 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { laddr := routing.Addr{Port: rule.LocalPort()} raddr := routing.Addr{PubKey: rule.RemotePK(), Port: rule.RemotePort()} - p := &app.Packet{Loop: routing.AddrLoop{Local: laddr, Remote: raddr}, Payload: payload} + p := &app.Packet{AddressPair: routing.AddressPair{Local: laddr, Remote: raddr}, Payload: payload} b, err := r.pm.Get(rule.LocalPort()) if err != nil { return err @@ -335,17 +336,22 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { return errors.New("router not initialized") } - if appPacket.Loop.Remote.PubKey == r.config.PubKey { - r.trLog().Info("r.config.PubKey: ", r.config.PubKey) + if appPacket.AddressPair.Remote.PubKey == r.config.PubKey { + r.trLog().Info("Forwarding local packet to: ", r.config.PubKey) return r.forwardLocalAppPacket(appPacket) } - if r.isPubKeyLocal(appPacket.Loop.Remote.PubKey) { - r.trLog().Info("Found in pubkeys file: ", appPacket.Loop.Remote.PubKey) + if r.isPubKeyLocal(appPacket.AddressPair.Remote.PubKey) { + r.trLog().Info("Forwarding tcp-transport packet to: ", appPacket.AddressPair.Remote.PubKey) return r.forwardTCPAppPacket(appPacket) } - loop, err := r.pm.GetLoop(appPacket.Loop.Local.Port, appPacket.Loop.Remote) + r.trLog().Info("Forwarding dmsg packet to: ", appPacket.AddressPair.Remote.PubKey) + return r.forwardLocalDMSGPacket(appPacket) +} + +func (r *Router) forwardLocalDMSGPacket(appPacket *app.Packet) error { + loop, err := r.pm.GetLoop(appPacket.AddressPair.Local.Port, appPacket.AddressPair.Remote) if err != nil { return err } @@ -354,32 +360,10 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { r.trLog().Info("unknown transport") return errors.New("unknown transport") } - r.trLog().Errorf("r.tm.transports: %v\n", r.tm.Transports()) - - // 9df41ada-be66-5640-bce2-917988069706 - // trID := uuid.MustParse("9df41ada-be66-5640-bce2-917988069706") - // trID := uuid.MustParse("9e566688-8717-5855-b1b7-92586ffc8c9b") - // fmt.Printf("trID %v ERRROR %v\n", trID, err) - // trID := transport.MakeTransportID(r.config.PubKey, appPacket.Loop.Remote.PubKey, "tcp-transport", true) - - // tr = r.tm.Transport(trID) - // fmt.Print("TRANSPORT: ", tr) - // if tr == nil { - // r.trLog().Error("Loop: ", loop) - // r.trLog().Error("OOOOOOO") - // r.Logger.Errorf("%v r.tm.Transport: tr==nil", th.GetCaller()) - // return errors.New("unknown transport") - - // } - - // r.Logger.Infof("%v ATTEMPT TO FIND %v", th.GetCaller(), appPacket.Loop.Remote.PubKey) - // if r.isPubKeyLocal(appPacket.Loop.Remote.PubKey) { - // r.Logger.Infof("%v isPubKeyLocal = true", th.GetCaller()) - // // return r.forwardTCPAppPacket(packet) - // } + r.trLog().Infof("r.tm.transports: %v\n", r.tm.Transports()) rPacket := routing.MakePacket(loop.routeID, appPacket.Payload) - r.Logger.Infof("%v Forwarded App packet from LocalPort %d using route ID %d", th.GetCaller(), appPacket.Loop.Local.Port, loop.routeID) + r.Logger.Infof("%v Forwarded App packet from LocalPort %d using route ID %d", th.GetCaller(), appPacket.AddressPair.Local.Port, loop.routeID) _, err = tr.Write(rPacket) if err != nil { r.Logger.Warnf("%v tr.Write: %v", err) @@ -391,16 +375,16 @@ func (r *Router) forwardAppPacket(appPacket *app.Packet) error { func (r *Router) forwardLocalAppPacket(appPacket *app.Packet) error { defer r.trFinish(r.trStart()) - b, err := r.pm.Get(appPacket.Loop.Remote.Port) + b, err := r.pm.Get(appPacket.AddressPair.Remote.Port) if err != nil { r.Logger.Warnf("%v r.pm.Get: %v\n", th.GetCaller(), err) return nil } p := &app.Packet{ - Loop: routing.AddrLoop{ - Local: routing.Addr{Port: appPacket.Loop.Remote.Port}, - Remote: routing.Addr{PubKey: appPacket.Loop.Remote.PubKey, Port: appPacket.Loop.Local.Port}, + AddressPair: routing.AddressPair{ + Local: routing.Addr{Port: appPacket.AddressPair.Remote.Port}, + Remote: routing.Addr{PubKey: appPacket.AddressPair.Remote.PubKey, Port: appPacket.AddressPair.Local.Port}, }, Payload: appPacket.Payload, } @@ -414,11 +398,11 @@ func (r *Router) forwardLocalAppPacket(appPacket *app.Packet) error { func (r *Router) forwardTCPAppPacket(appPacket *app.Packet) error { defer r.trFinish(r.trStart()) - loop, err := r.pm.GetLoop(appPacket.Loop.Local.Port, appPacket.Loop.Remote) + loop, err := r.pm.GetLoop(appPacket.AddressPair.Local.Port, appPacket.AddressPair.Remote) if err != nil { return err } - trID := transport.MakeTransportID(r.config.PubKey, appPacket.Loop.Remote.PubKey, "tcp-transport", true) + trID := transport.MakeTransportID(r.config.PubKey, appPacket.AddressPair.Remote.PubKey, "tcp-transport", true) tr := r.tm.Transport(trID) if tr == nil { r.trLog().Info("unknown transport") @@ -427,7 +411,7 @@ func (r *Router) forwardTCPAppPacket(appPacket *app.Packet) error { r.trLog().Errorf("r.tm.transports: %v\n", r.tm.Transports()) rPacket := routing.MakePacket(loop.routeID, appPacket.Payload) - r.Logger.Infof("%v Forwarded App packet from LocalPort %d using route ID %d", th.GetCaller(), appPacket.Loop.Local.Port, loop.routeID) + r.Logger.Infof("%v Forwarded App packet from LocalPort %d using route ID %d", th.GetCaller(), appPacket.AddressPair.Local.Port, loop.routeID) _, err = tr.Write(rPacket) if err != nil { r.Logger.Warnf("%v tr.Write: %v", err) @@ -447,6 +431,7 @@ func (r *Router) isPubKeyLocal(pk cipher.PubKey) bool { } } } + return false } @@ -481,7 +466,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing } ld := routing.LoopDescriptor{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Local: laddr, Remote: raddr, }, @@ -560,7 +545,7 @@ func (r *Router) confirmTCPLoop(laddr, raddr routing.Addr) error { tr, err := r.tm.CreateDataTransport(context.Background(), raddr.PubKey, "tcp-transport", true) if err != nil { - r.trLog().Warnf("r.tm.CreateDataTransport err: %v\n", th.GetCaller(), err) + r.trLog().Warnf("%v r.tm.CreateDataTransport err: %v\n", th.GetCaller(), err) } r.trLog().Infof("r.tm.CreateDataTransport tr: %T success: %v\n", tr, err == nil) @@ -568,7 +553,7 @@ func (r *Router) confirmTCPLoop(laddr, raddr routing.Addr) error { } -func (r *Router) confirmLoop(l routing.AddrLoop, rule routing.Rule) error { +func (r *Router) confirmLoop(l routing.AddressPair, rule routing.Rule) error { r.Logger.Info(th.Trace("ENTER")) defer r.Logger.Info(th.Trace("EXIT")) @@ -589,7 +574,7 @@ func (r *Router) confirmLoop(l routing.AddrLoop, rule routing.Rule) error { return nil } -func (r *Router) closeLoop(loop routing.AddrLoop) error { +func (r *Router) closeLoop(loop routing.AddressPair) error { r.Logger.Info(th.Trace("ENTER")) defer r.Logger.Info(th.Trace("EXIT")) @@ -598,7 +583,7 @@ func (r *Router) closeLoop(loop routing.AddrLoop) error { } if r.isPubKeyLocal(loop.Remote.PubKey) { - r.Logger.Info("%v isPubKeyLocal() == true", th.GetCaller()) + r.Logger.Infof("%v isPubKeyLocal() == true", th.GetCaller()) return nil } proto, tr, err := r.setupProto(context.Background()) @@ -620,7 +605,7 @@ func (r *Router) closeLoop(loop routing.AddrLoop) error { return nil } -func (r *Router) loopClosed(loop routing.AddrLoop) error { +func (r *Router) loopClosed(loop routing.AddressPair) error { r.Logger.Info(th.Trace("ENTER")) defer r.Logger.Info(th.Trace("EXIT")) @@ -641,7 +626,7 @@ func (r *Router) loopClosed(loop routing.AddrLoop) error { return nil } -func (r *Router) destroyLoop(loop routing.AddrLoop) error { +func (r *Router) destroyLoop(loop routing.AddressPair) error { r.Logger.Info(th.Trace("ENTER")) defer r.Logger.Info(th.Trace("EXIT")) diff --git a/pkg/router/router_close_test.go b/pkg/router/router_close_test.go index 2e018184e..537189f9d 100644 --- a/pkg/router/router_close_test.go +++ b/pkg/router/router_close_test.go @@ -103,7 +103,7 @@ func TestRouterCloseLoop(t *testing.T) { raddr := routing.Addr{PubKey: pk3, Port: 6} require.NoError(t, r.pm.SetLoop(5, raddr, &loop{})) - require.NoError(t, proto.Send(app.FrameClose, routing.AddrLoop{Local: routing.Addr{Port: 5}, Remote: raddr}, nil)) + require.NoError(t, proto.Send(app.FrameClose, routing.AddressPair{Local: routing.Addr{Port: 5}, Remote: raddr}, nil)) time.Sleep(100 * time.Millisecond) diff --git a/pkg/router/router_setup_test.go b/pkg/router/router_setup_test.go index 9a2b7f734..e7bb5561b 100644 --- a/pkg/router/router_setup_test.go +++ b/pkg/router/router_setup_test.go @@ -120,7 +120,7 @@ func TestRouterSetup(t *testing.T) { require.NoError(t, err) err = setup.ConfirmLoop(sProto, routing.LoopData{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: pk2, Port: 1, @@ -166,7 +166,7 @@ func TestRouterSetup(t *testing.T) { require.NoError(t, err) err = setup.ConfirmLoop(sProto, routing.LoopData{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: pk2, Port: 3, @@ -208,7 +208,7 @@ func TestRouterSetup(t *testing.T) { assert.Equal(t, routing.RuleApp, rule.Type()) require.NoError(t, setup.LoopClosed(sProto, routing.LoopData{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: pk2, Port: 3, diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index a9d5bec52..43b83fdcb 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -217,7 +217,7 @@ func TestRouterApp(t *testing.T) { tr2 := m2.Transport(tr.Entry.ID) sendErrCh := make(chan error, 1) go func() { - sendErrCh <- proto.Send(app.FrameSend, &app.Packet{Loop: routing.AddrLoop{Local: routing.Addr{Port: 6}, Remote: raddr}, Payload: []byte("bar")}, nil) + sendErrCh <- proto.Send(app.FrameSend, &app.Packet{AddressPair: routing.AddressPair{Local: routing.Addr{Port: 6}, Remote: raddr}, Payload: []byte("bar")}, nil) }() packet := make(routing.Packet, 9) @@ -234,9 +234,9 @@ func TestRouterApp(t *testing.T) { var aPacket app.Packet require.NoError(t, json.Unmarshal(<-dataCh, &aPacket)) - assert.Equal(t, pk2, aPacket.Loop.Remote.PubKey) - assert.Equal(t, routing.Port(5), aPacket.Loop.Remote.Port) - assert.Equal(t, routing.Port(6), aPacket.Loop.Local.Port) + assert.Equal(t, pk2, aPacket.AddressPair.Remote.PubKey) + assert.Equal(t, routing.Port(5), aPacket.AddressPair.Remote.Port) + assert.Equal(t, routing.Port(6), aPacket.AddressPair.Local.Port) assert.Equal(t, []byte("foo"), aPacket.Payload) require.NoError(t, r.Close()) @@ -302,7 +302,7 @@ func TestRouterLocalApp(t *testing.T) { sendErrCh := make(chan error, 1) go func() { packet := &app.Packet{ - Loop: routing.AddrLoop{Local: routing.Addr{Port: 5}, Remote: routing.Addr{PubKey: pk, Port: 6}}, Payload: []byte("foo"), + AddressPair: routing.AddressPair{Local: routing.Addr{Port: 5}, Remote: routing.Addr{PubKey: pk, Port: 6}}, Payload: []byte("foo"), } sendErrCh <- proto1.Send(app.FrameSend, packet, nil) }() @@ -312,9 +312,9 @@ func TestRouterLocalApp(t *testing.T) { packet := &app.Packet{} require.NoError(t, json.Unmarshal(<-dataCh, packet)) require.NoError(t, err) - assert.Equal(t, pk, packet.Loop.Remote.PubKey) - assert.Equal(t, routing.Port(5), packet.Loop.Remote.Port) - assert.Equal(t, routing.Port(6), packet.Loop.Local.Port) + assert.Equal(t, pk, packet.AddressPair.Remote.PubKey) + assert.Equal(t, routing.Port(5), packet.AddressPair.Remote.Port) + assert.Equal(t, routing.Port(6), packet.AddressPair.Local.Port) assert.Equal(t, []byte("foo"), packet.Payload) require.NoError(t, r.Close()) diff --git a/pkg/routing/loop.go b/pkg/routing/loop.go index 44717f78c..74f0360de 100644 --- a/pkg/routing/loop.go +++ b/pkg/routing/loop.go @@ -7,20 +7,20 @@ import ( "github.com/skycoin/dmsg/cipher" ) -// AddrLoop defines a loop over a pair of addresses. -type AddrLoop struct { +// AddressPair defines a loop over a pair of addresses. +type AddressPair struct { Local Addr Remote Addr } // TODO: discuss if we should add local PK to the output -func (l AddrLoop) String() string { +func (l AddressPair) String() string { return fmt.Sprintf("%s:%d <-> %s:%d", l.Local.PubKey, l.Local.Port, l.Remote.PubKey, l.Remote.Port) } // LoopDescriptor defines a loop over a pair of routes. type LoopDescriptor struct { - Loop AddrLoop + Loop AddressPair Forward Route Reverse Route Expiry time.Time @@ -51,6 +51,6 @@ func (l LoopDescriptor) String() string { // LoopData stores loop confirmation request data. type LoopData struct { - Loop AddrLoop `json:"loop"` - RouteID RouteID `json:"resp-rid,omitempty"` + Loop AddressPair `json:"loop"` + RouteID RouteID `json:"resp-rid,omitempty"` } diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 4365fe612..944236dec 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -95,7 +95,7 @@ func (sn *Node) serveTransport(tr transport.Transport) error { var ld routing.LoopData if err = json.Unmarshal(data, &ld); err == nil { err = sn.closeLoop(ld.Loop.Remote.PubKey, routing.LoopData{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Remote: ld.Loop.Local, Local: ld.Loop.Remote, }, @@ -134,7 +134,7 @@ func (sn *Node) createLoop(ld routing.LoopDescriptor) error { responder := ld.Responder() ldR := routing.LoopData{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: initiator, Port: ld.Loop.Local.Port, @@ -152,7 +152,7 @@ func (sn *Node) createLoop(ld routing.LoopDescriptor) error { } ldI := routing.LoopData{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: responder, Port: ld.Loop.Remote.Port, diff --git a/pkg/setup/node_test.go b/pkg/setup/node_test.go index bf099fd57..17bf0490d 100644 --- a/pkg/setup/node_test.go +++ b/pkg/setup/node_test.go @@ -97,7 +97,7 @@ func TestNode(t *testing.T) { // prepare loop creation (client_1 will use this to request loop creation with setup node). ld := routing.LoopDescriptor{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Local: routing.Addr{PubKey: clients[1].Local(), Port: 1}, Remote: routing.Addr{PubKey: clients[4].Local(), Port: 1}, }, @@ -214,7 +214,7 @@ func TestNode(t *testing.T) { // prepare loop data describing the loop that is to be closed. ld := routing.LoopData{ - Loop: routing.AddrLoop{ + Loop: routing.AddressPair{ Local: routing.Addr{ PubKey: clients[1].Local(), Port: 1, From 1e53cfe0d8494c3e137fe09ad3e8f5f0be3ec723 Mon Sep 17 00:00:00 2001 From: Alex Yu Date: Mon, 26 Aug 2019 09:54:02 +0300 Subject: [PATCH 26/26] LoopDescriptor, LoopData -> AddressPairDescriptor, AddressPairData --- pkg/router/route_manager.go | 4 ++-- pkg/router/route_manager_test.go | 4 ++-- pkg/router/router.go | 4 ++-- pkg/router/router_close_test.go | 4 ++-- pkg/router/router_setup_test.go | 8 ++++---- pkg/routing/loop.go | 28 ++++++++++++++-------------- pkg/setup/node.go | 16 ++++++++-------- pkg/setup/node_test.go | 8 ++++---- pkg/setup/protocol.go | 8 ++++---- 9 files changed, 42 insertions(+), 42 deletions(-) diff --git a/pkg/router/route_manager.go b/pkg/router/route_manager.go index e29e7cb61..5e9e74dbb 100644 --- a/pkg/router/route_manager.go +++ b/pkg/router/route_manager.go @@ -140,7 +140,7 @@ func (rm *routeManager) deleteRoutingRules(data []byte) ([]routing.RouteID, erro } func (rm *routeManager) confirmLoop(data []byte) error { - var ld routing.LoopData + var ld routing.AddressPairData if err := json.Unmarshal(data, &ld); err != nil { return err } @@ -190,7 +190,7 @@ func (rm *routeManager) confirmLoop(data []byte) error { } func (rm *routeManager) loopClosed(data []byte) error { - var ld routing.LoopData + var ld routing.AddressPairData if err := json.Unmarshal(data, &ld); err != nil { return err } diff --git a/pkg/router/route_manager_test.go b/pkg/router/route_manager_test.go index a6c831032..bc4eae6ac 100644 --- a/pkg/router/route_manager_test.go +++ b/pkg/router/route_manager_test.go @@ -145,7 +145,7 @@ func TestRouteManagerConfirmLoop(t *testing.T) { rule = routing.ForwardRule(time.Now(), 3, uuid.New()) require.NoError(t, rt.SetRule(1, rule)) - ld := routing.LoopData{ + ld := routing.AddressPairData{ Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: pk, @@ -195,7 +195,7 @@ func TestRouteManagerLoopClosed(t *testing.T) { rule = routing.ForwardRule(time.Now(), 3, uuid.New()) require.NoError(t, rt.SetRule(1, rule)) - ld := routing.LoopData{ + ld := routing.AddressPairData{ Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: pk, diff --git a/pkg/router/router.go b/pkg/router/router.go index e909d7e94..aa2c727ef 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -465,7 +465,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing return routing.Addr{}, fmt.Errorf("route finder: %s", err) } - ld := routing.LoopDescriptor{ + ld := routing.AddressPairDescriptor{ Loop: routing.AddressPair{ Local: laddr, Remote: raddr, @@ -596,7 +596,7 @@ func (r *Router) closeLoop(loop routing.AddressPair) error { } }() - ld := routing.LoopData{Loop: loop} + ld := routing.AddressPairData{Loop: loop} if err := setup.CloseLoop(proto, ld); err != nil { return fmt.Errorf("route setup: %s", err) } diff --git a/pkg/router/router_close_test.go b/pkg/router/router_close_test.go index 537189f9d..c3098e71e 100644 --- a/pkg/router/router_close_test.go +++ b/pkg/router/router_close_test.go @@ -74,7 +74,7 @@ func TestRouterCloseLoop(t *testing.T) { return } - var ld routing.LoopData + var ld routing.AddressPairData if err := json.Unmarshal(data, &ld); err != nil { errCh <- err return @@ -177,7 +177,7 @@ func TestRouterCloseLoopOnAppClose(t *testing.T) { return } - var ld routing.LoopData + var ld routing.AddressPairData if err := json.Unmarshal(data, &ld); err != nil { errCh <- err return diff --git a/pkg/router/router_setup_test.go b/pkg/router/router_setup_test.go index e7bb5561b..a5948aafa 100644 --- a/pkg/router/router_setup_test.go +++ b/pkg/router/router_setup_test.go @@ -119,7 +119,7 @@ func TestRouterSetup(t *testing.T) { appRouteID, err := setup.AddRule(sProto, routing.AppRule(time.Now().Add(time.Hour), 0, pk2, 1, 2)) require.NoError(t, err) - err = setup.ConfirmLoop(sProto, routing.LoopData{ + err = setup.ConfirmLoop(sProto, routing.AddressPairData{ Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: pk2, @@ -165,7 +165,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, routing.LoopData{ + err = setup.ConfirmLoop(sProto, routing.AddressPairData{ Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: pk2, @@ -207,7 +207,7 @@ func TestRouterSetup(t *testing.T) { require.NotNil(t, rule) assert.Equal(t, routing.RuleApp, rule.Type()) - require.NoError(t, setup.LoopClosed(sProto, routing.LoopData{ + require.NoError(t, setup.LoopClosed(sProto, routing.AddressPairData{ Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: pk2, @@ -301,7 +301,7 @@ func TestRouterSetupLoop(t *testing.T) { return } - var ld routing.LoopDescriptor + var ld routing.AddressPairDescriptor if err := json.Unmarshal(data, &ld); err != nil { errCh <- err return diff --git a/pkg/routing/loop.go b/pkg/routing/loop.go index 74f0360de..cd6def75a 100644 --- a/pkg/routing/loop.go +++ b/pkg/routing/loop.go @@ -14,12 +14,12 @@ type AddressPair struct { } // TODO: discuss if we should add local PK to the output -func (l AddressPair) String() string { - return fmt.Sprintf("%s:%d <-> %s:%d", l.Local.PubKey, l.Local.Port, l.Remote.PubKey, l.Remote.Port) +func (apd AddressPair) String() string { + return fmt.Sprintf("%s:%d <-> %s:%d", apd.Local.PubKey, apd.Local.Port, apd.Remote.PubKey, apd.Remote.Port) } -// LoopDescriptor defines a loop over a pair of routes. -type LoopDescriptor struct { +// AddressPairDescriptor defines a loop over a pair of routes. +type AddressPairDescriptor struct { Loop AddressPair Forward Route Reverse Route @@ -27,30 +27,30 @@ type LoopDescriptor struct { } // Initiator returns initiator of the Loop. -func (l LoopDescriptor) Initiator() cipher.PubKey { - if len(l.Forward) == 0 { +func (apd AddressPairDescriptor) Initiator() cipher.PubKey { + if len(apd.Forward) == 0 { panic("empty forward route") } - return l.Forward[0].From + return apd.Forward[0].From } // Responder returns responder of the Loop. -func (l LoopDescriptor) Responder() cipher.PubKey { - if len(l.Reverse) == 0 { +func (apd AddressPairDescriptor) Responder() cipher.PubKey { + if len(apd.Reverse) == 0 { panic("empty reverse route") } - return l.Reverse[0].From + return apd.Reverse[0].From } -func (l LoopDescriptor) String() string { +func (apd AddressPairDescriptor) String() string { return fmt.Sprintf("lport: %d. rport: %d. routes: %s/%s. expire at %s", - l.Loop.Local.Port, l.Loop.Remote.Port, l.Forward, l.Reverse, l.Expiry) + apd.Loop.Local.Port, apd.Loop.Remote.Port, apd.Forward, apd.Reverse, apd.Expiry) } -// LoopData stores loop confirmation request data. -type LoopData struct { +// AddressPairData stores loop confirmation request data. +type AddressPairData struct { Loop AddressPair `json:"loop"` RouteID RouteID `json:"resp-rid,omitempty"` } diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 944236dec..09558644c 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -87,14 +87,14 @@ func (sn *Node) serveTransport(tr transport.Transport) error { startTime := time.Now() switch sp { case PacketCreateLoop: - var ld routing.LoopDescriptor + var ld routing.AddressPairDescriptor if err = json.Unmarshal(data, &ld); err == nil { err = sn.createLoop(ld) } case PacketCloseLoop: - var ld routing.LoopData + var ld routing.AddressPairData if err = json.Unmarshal(data, &ld); err == nil { - err = sn.closeLoop(ld.Loop.Remote.PubKey, routing.LoopData{ + err = sn.closeLoop(ld.Loop.Remote.PubKey, routing.AddressPairData{ Loop: routing.AddressPair{ Remote: ld.Loop.Local, Local: ld.Loop.Remote, @@ -114,7 +114,7 @@ func (sn *Node) serveTransport(tr transport.Transport) error { return proto.WritePacket(RespSuccess, nil) } -func (sn *Node) createLoop(ld routing.LoopDescriptor) error { +func (sn *Node) createLoop(ld routing.AddressPairDescriptor) error { sn.Logger.Infof("Creating new Loop %s", ld) rRouteID, err := sn.createRoute(ld.Expiry, ld.Reverse, ld.Loop.Local.Port, ld.Loop.Remote.Port) if err != nil { @@ -133,7 +133,7 @@ func (sn *Node) createLoop(ld routing.LoopDescriptor) error { initiator := ld.Initiator() responder := ld.Responder() - ldR := routing.LoopData{ + ldR := routing.AddressPairData{ Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: initiator, @@ -151,7 +151,7 @@ func (sn *Node) createLoop(ld routing.LoopDescriptor) error { return fmt.Errorf("loop connect: %s", err) } - ldI := routing.LoopData{ + ldI := routing.AddressPairData{ Loop: routing.AddressPair{ Remote: routing.Addr{ PubKey: responder, @@ -213,7 +213,7 @@ func (sn *Node) createRoute(expireAt time.Time, route routing.Route, rport, lpor return routeID, nil } -func (sn *Node) connectLoop(on cipher.PubKey, ld routing.LoopData) error { +func (sn *Node) connectLoop(on cipher.PubKey, ld routing.AddressPairData) error { ctx := context.Background() tr, err := sn.messenger.Dial(ctx, on) @@ -242,7 +242,7 @@ func (sn *Node) Close() error { return sn.messenger.Close() } -func (sn *Node) closeLoop(on cipher.PubKey, ld routing.LoopData) error { +func (sn *Node) closeLoop(on cipher.PubKey, ld routing.AddressPairData) error { fmt.Printf(">>> BEGIN: closeLoop(%s, ld)\n", on) defer fmt.Printf(">>> END: closeLoop(%s, ld)\n", on) ctx := context.Background() diff --git a/pkg/setup/node_test.go b/pkg/setup/node_test.go index 17bf0490d..800151293 100644 --- a/pkg/setup/node_test.go +++ b/pkg/setup/node_test.go @@ -96,7 +96,7 @@ func TestNode(t *testing.T) { defer closeSetup() // prepare loop creation (client_1 will use this to request loop creation with setup node). - ld := routing.LoopDescriptor{ + ld := routing.AddressPairDescriptor{ Loop: routing.AddressPair{ Local: routing.Addr{PubKey: clients[1].Local(), Port: 1}, Remote: routing.Addr{PubKey: clients[4].Local(), Port: 1}, @@ -168,7 +168,7 @@ func TestNode(t *testing.T) { require.NoError(t, err) require.Equal(t, PacketConfirmLoop, pt) - var d routing.LoopData + var d routing.AddressPairData require.NoError(t, json.Unmarshal(pp, &d)) switch client { @@ -213,7 +213,7 @@ func TestNode(t *testing.T) { defer closeSetup() // prepare loop data describing the loop that is to be closed. - ld := routing.LoopData{ + ld := routing.AddressPairData{ Loop: routing.AddressPair{ Local: routing.Addr{ PubKey: clients[1].Local(), @@ -255,7 +255,7 @@ func TestNode(t *testing.T) { require.NoError(t, err) require.Equal(t, PacketLoopClosed, pt) - var d routing.LoopData + var d routing.AddressPairData require.NoError(t, json.Unmarshal(pp, &d)) require.Equal(t, ld.Loop.Remote, d.Loop.Local) require.Equal(t, ld.Loop.Local, d.Loop.Remote) diff --git a/pkg/setup/protocol.go b/pkg/setup/protocol.go index 648d95c73..9825de41a 100644 --- a/pkg/setup/protocol.go +++ b/pkg/setup/protocol.go @@ -130,7 +130,7 @@ func DeleteRule(p *Protocol, routeID routing.RouteID) error { } // CreateLoop sends CreateLoop setup request. -func CreateLoop(p *Protocol, ld routing.LoopDescriptor) error { +func CreateLoop(p *Protocol, ld routing.AddressPairDescriptor) error { if err := p.WritePacket(PacketCreateLoop, ld); err != nil { return err } @@ -138,7 +138,7 @@ func CreateLoop(p *Protocol, ld routing.LoopDescriptor) error { } // ConfirmLoop sends ConfirmLoop setup request. -func ConfirmLoop(p *Protocol, ld routing.LoopData) error { +func ConfirmLoop(p *Protocol, ld routing.AddressPairData) error { if err := p.WritePacket(PacketConfirmLoop, ld); err != nil { return err } @@ -146,7 +146,7 @@ func ConfirmLoop(p *Protocol, ld routing.LoopData) error { } // CloseLoop sends CloseLoop setup request. -func CloseLoop(p *Protocol, ld routing.LoopData) error { +func CloseLoop(p *Protocol, ld routing.AddressPairData) error { if err := p.WritePacket(PacketCloseLoop, ld); err != nil { return err } @@ -154,7 +154,7 @@ func CloseLoop(p *Protocol, ld routing.LoopData) error { } // LoopClosed sends LoopClosed setup request. -func LoopClosed(p *Protocol, ld routing.LoopData) error { +func LoopClosed(p *Protocol, ld routing.AddressPairData) error { if err := p.WritePacket(PacketLoopClosed, ld); err != nil { return err }