Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default values if required visor config fields are empty #246

Merged
merged 10 commits into from
Mar 25, 2020
79 changes: 21 additions & 58 deletions cmd/skywire-cli/commands/visor/gen-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package visor

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"path"
"path/filepath"
"time"

"github.com/SkycoinProject/skywire-mainnet/pkg/app/appcommon"

"github.com/SkycoinProject/skywire-mainnet/internal/skyenv"
"github.com/SkycoinProject/skywire-mainnet/pkg/app/appcommon"
"github.com/SkycoinProject/skywire-mainnet/pkg/restart"
"github.com/SkycoinProject/skywire-mainnet/pkg/routing"

"github.com/SkycoinProject/dmsg/cipher"
Expand Down Expand Up @@ -109,25 +106,17 @@ func localConfig() *visor.Config {

func defaultConfig() *visor.Config {
conf := &visor.Config{}
conf.Version = "1.0"

pk, sk := cipher.GenerateKeyPair()
conf.Visor.StaticPubKey = pk
conf.Visor.StaticSecKey = sk
conf.Visor = visor.NewKeyPair()

lIPaddr, err := getLocalIPAddress()
stcp, err := visor.DefaultSTCPConfig()
if err != nil {
logger.Warn(err)
}

conf.STCP.LocalAddr = lIPaddr

if testenv {
conf.Dmsg.Discovery = skyenv.TestDmsgDiscAddr
} else {
conf.Dmsg.Discovery = skyenv.DefaultDmsgDiscAddr
conf.STCP = stcp
}
conf.Dmsg.SessionsCount = 1

conf.Dmsg = visor.DefaultDmsgConfig()

ptyConf := defaultDmsgPtyConfig()
conf.DmsgPty = &ptyConf
Expand All @@ -139,44 +128,34 @@ func defaultConfig() *visor.Config {
defaultSkysocksConfig(""),
defaultSkysocksClientConfig(),
}
conf.TrustedVisors = []cipher.PubKey{}

if testenv {
conf.Transport.Discovery = skyenv.TestTpDiscAddr
} else {
conf.Transport.Discovery = skyenv.DefaultTpDiscAddr
}
conf.TrustedVisors = []cipher.PubKey{}

conf.Transport.LogStore.Type = "file"
conf.Transport.LogStore.Location = "./skywire/transport_logs"
conf.Transport = visor.DefaultTransportConfig()
conf.Routing = visor.DefaultRoutingConfig()

if testenv {
conf.Dmsg.Discovery = skyenv.TestDmsgDiscAddr
conf.Transport.Discovery = skyenv.TestTpDiscAddr
conf.Routing.RouteFinder = skyenv.TestRouteFinderAddr
} else {
conf.Routing.RouteFinder = skyenv.DefaultRouteFinderAddr
}

var sPK cipher.PubKey
if err := sPK.UnmarshalText([]byte(skyenv.DefaultSetupPK)); err != nil {
logger.WithError(err).Warnf("Failed to unmarshal default setup-node public key %s", skyenv.DefaultSetupPK)
}
conf.Routing.SetupNodes = []cipher.PubKey{sPK}
conf.Routing.RouteFinderTimeout = visor.Duration(10 * time.Second)

conf.Hypervisors = []visor.HypervisorConfig{}

conf.Uptime.Tracker = "uptime-tracker.skywire.skycoin.com"

conf.AppsPath = "./apps"
conf.LocalPath = "./local"
conf.UptimeTracker = visor.DefaultUptimeTrackerConfig()

conf.LogLevel = "info"
conf.AppsPath = visor.DefaultAppsPath
conf.LocalPath = visor.DefaultLocalPath

conf.ShutdownTimeout = visor.Duration(10 * time.Second)
conf.LogLevel = visor.DefaultLogLevel
conf.ShutdownTimeout = visor.DefaultTimeout

conf.Interfaces.RPCAddress = "localhost:3435"
conf.Interfaces = &visor.InterfaceConfig{
RPCAddress: "localhost:3435",
}

conf.AppServerAddr = appcommon.DefaultServerAddr
conf.RestartCheckDelay = restart.DefaultCheckDelay.String()

return conf
}
Expand Down Expand Up @@ -219,19 +198,3 @@ func defaultSkysocksClientConfig() visor.AppConfig {
Port: routing.Port(skyenv.SkysocksClientPort),
}
}

func getLocalIPAddress() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}

for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String() + ":7777", nil
}
}
}
return "", errors.New("could not find local IP address")
}
6 changes: 2 additions & 4 deletions cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ func (cfg *runCfg) readConfig() *runCfg {
cfg.logger.Fatalf("Failed to decode %s: %s", rdr, err)
}

fmt.Println("TCP Factory conf:", cfg.conf.STCP)

return cfg
}

Expand Down Expand Up @@ -206,8 +204,8 @@ func (cfg *runCfg) runVisor() *runCfg {
cfg.logger.Fatal("Failed to initialize visor: ", err)
}

if cfg.conf.Uptime.Tracker != "" {
uptimeTracker, err := utclient.NewHTTP(cfg.conf.Uptime.Tracker, cfg.conf.Visor.StaticPubKey, cfg.conf.Visor.StaticSecKey)
if cfg.conf.UptimeTracker != nil {
uptimeTracker, err := utclient.NewHTTP(cfg.conf.UptimeTracker.Addr, cfg.conf.Keys().StaticPubKey, cfg.conf.Keys().StaticSecKey)
if err != nil {
cfg.logger.Error("Failed to connect to uptime tracker: ", err)
} else {
Expand Down
23 changes: 19 additions & 4 deletions internal/skyenv/const.go → internal/skyenv/values.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package skyenv

import (
"github.com/SkycoinProject/dmsg/cipher"
)

// Constants for default services.
const (
DefaultTpDiscAddr = "http://transport.discovery.skywire.skycoin.com"
DefaultDmsgDiscAddr = "http://dmsg.discovery.skywire.skycoin.com"
DefaultRouteFinderAddr = "http://routefinder.skywire.skycoin.com"
DefaultSetupPK = "026c5a07de617c5c488195b76e8671bf9e7ee654d0633933e202af9e111ffa358d"
DefaultTpDiscAddr = "http://transport.discovery.skywire.skycoin.com"
DefaultDmsgDiscAddr = "http://dmsg.discovery.skywire.skycoin.com"
DefaultRouteFinderAddr = "http://routefinder.skywire.skycoin.com"
DefaultUptimeTrackerAddr = "http://uptime-tracker.skywire.skycoin.com"
DefaultSetupPK = "026c5a07de617c5c488195b76e8671bf9e7ee654d0633933e202af9e111ffa358d"
)

// MustDefaultSetupPK returns DefaultSetupPK as cipher.PubKey. It panics if unmarshaling fails.
func MustDefaultSetupPK() cipher.PubKey {
var sPK cipher.PubKey
if err := sPK.UnmarshalText([]byte(DefaultSetupPK)); err != nil {
panic(err)
}

return sPK
}

// Constants for testing deployment.
const (
TestTpDiscAddr = "http://transport.discovery.skywire.cc"
Expand Down
5 changes: 3 additions & 2 deletions pkg/app/appserver/mock_proc_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/app/mock_rpc_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 72 additions & 35 deletions pkg/snet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,59 +29,96 @@ const (
// Network types.
const (
DmsgType = dmsg.Type
STcpType = stcp.Type
STCPType = stcp.Type
)

var (
// ErrUnknownNetwork occurs on attempt to dial an unknown network type.
ErrUnknownNetwork = errors.New("unknown network type")
)

// Config represents a network configuration.
type Config struct {
PubKey cipher.PubKey
SecKey cipher.SecKey
TpNetworks []string // networks to be used with transports
// NetworkConfig is a common interface for network configs.
type NetworkConfig interface {
Type() string
}

// DmsgConfig defines config for Dmsg network.
type DmsgConfig struct {
Discovery string `json:"discovery"`
SessionsCount int `json:"sessions_count"`
}

DmsgDiscAddr string
DmsgMinSessions int
// Type returns DmsgType.
func (c *DmsgConfig) Type() string {
return DmsgType
}

// STCPConfig defines config for STCP network.
type STCPConfig struct {
LocalAddr string `json:"local_address"`
PubKeyTable map[cipher.PubKey]string `json:"pk_table"`
}

STCPLocalAddr string // if empty, don't listen.
STCPTable map[cipher.PubKey]string
// Type returns STCPType.
func (c *STCPConfig) Type() string {
return STCPType
}

// Config represents a network configuration.
type Config struct {
PubKey cipher.PubKey
SecKey cipher.SecKey
Dmsg *DmsgConfig
STCP *STCPConfig
}

// Network represents a network between nodes in Skywire.
type Network struct {
conf Config
dmsgC *dmsg.Client
stcpC *stcp.Client
conf Config
networks []string // networks to be used with transports
dmsgC *dmsg.Client
stcpC *stcp.Client
}

// New creates a network from a config.
func New(conf Config) *Network {
dmsgC := dmsg.NewClient(
conf.PubKey,
conf.SecKey,
disc.NewHTTP(conf.DmsgDiscAddr), &dmsg.Config{
MinSessions: conf.DmsgMinSessions,
})
dmsgC.SetLogger(logging.MustGetLogger("snet.dmsgC"))

stcpC := stcp.NewClient(
logging.MustGetLogger("snet.stcpC"),
conf.PubKey,
conf.SecKey,
stcp.NewTable(conf.STCPTable))
var dmsgC *dmsg.Client
var stcpC *stcp.Client

if conf.Dmsg != nil {
c := &dmsg.Config{
MinSessions: conf.Dmsg.SessionsCount,
}

dmsgC = dmsg.NewClient(conf.PubKey, conf.SecKey, disc.NewHTTP(conf.Dmsg.Discovery), c)
dmsgC.SetLogger(logging.MustGetLogger("snet.dmsgC"))
}

if conf.STCP != nil {
stcpC = stcp.NewClient(conf.PubKey, conf.SecKey, stcp.NewTable(conf.STCP.PubKeyTable))
stcpC.SetLogger(logging.MustGetLogger("snet.stcpC"))
}

return NewRaw(conf, dmsgC, stcpC)
}

// NewRaw creates a network from a config and a dmsg client.
func NewRaw(conf Config, dmsgC *dmsg.Client, stcpC *stcp.Client) *Network {
networks := make([]string, 0)

if dmsgC != nil {
networks = append(networks, DmsgType)
}

if stcpC != nil {
networks = append(networks, STCPType)
}

return &Network{
conf: conf,
dmsgC: dmsgC,
stcpC: stcpC,
conf: conf,
networks: networks,
dmsgC: dmsgC,
stcpC: stcpC,
}
}

Expand All @@ -93,9 +130,9 @@ func (n *Network) Init(_ context.Context) error {
time.Sleep(200 * time.Millisecond)
}

if n.stcpC != nil {
if n.conf.STCPLocalAddr != "" {
if err := n.stcpC.Serve(n.conf.STCPLocalAddr); err != nil {
if n.conf.STCP != nil {
if n.stcpC != nil && n.conf.STCP.LocalAddr != "" {
if err := n.stcpC.Serve(n.conf.STCP.LocalAddr); err != nil {
return fmt.Errorf("failed to initiate 'stcp': %v", err)
}
} else {
Expand Down Expand Up @@ -141,7 +178,7 @@ func (n *Network) LocalPK() cipher.PubKey { return n.conf.PubKey }
func (n *Network) LocalSK() cipher.SecKey { return n.conf.SecKey }

// TransportNetworks returns network types that are used for transports.
func (n *Network) TransportNetworks() []string { return n.conf.TpNetworks }
func (n *Network) TransportNetworks() []string { return n.networks }

// Dmsg returns underlying dmsg client.
func (n *Network) Dmsg() *dmsg.Client { return n.dmsgC }
Expand Down Expand Up @@ -170,7 +207,7 @@ func (n *Network) Dial(ctx context.Context, network string, pk cipher.PubKey, po
}

return makeConn(conn, network), nil
case STcpType:
case STCPType:
conn, err := n.stcpC.Dial(ctx, pk, port)
if err != nil {
return nil, err
Expand All @@ -192,7 +229,7 @@ func (n *Network) Listen(network string, port uint16) (*Listener, error) {
}

return makeListener(lis, network), nil
case STcpType:
case STCPType:
lis, err := n.stcpC.Listen(port)
if err != nil {
return nil, err
Expand Down
Loading