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

Dmsg Hypervisor PR #79

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cmd/hypervisor/commands/gen-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ var (
output string
replace bool
configLocType = pathutil.WorkingDirLoc
testenv bool
)

func init() {
rootCmd.AddCommand(genConfigCmd)
genConfigCmd.Flags().StringVarP(&output, "output", "o", "", "path of output config file. Uses default of 'type' flag if unspecified.")
genConfigCmd.Flags().BoolVarP(&replace, "replace", "r", false, "whether to allow rewrite of a file that already exists.")
genConfigCmd.Flags().VarP(&configLocType, "type", "m", fmt.Sprintf("config generation mode. Valid values: %v", pathutil.AllConfigLocationTypes()))
genConfigCmd.Flags().BoolVarP(&testenv, "testing-environment", "t", false, "whether to use production or test deployment service.")
}

var genConfigCmd = &cobra.Command{
Expand All @@ -40,11 +42,11 @@ var genConfigCmd = &cobra.Command{
var conf hypervisor.Config
switch configLocType {
case pathutil.WorkingDirLoc:
conf = hypervisor.GenerateWorkDirConfig()
conf = hypervisor.GenerateWorkDirConfig(testenv)
case pathutil.HomeLoc:
conf = hypervisor.GenerateHomeConfig()
conf = hypervisor.GenerateHomeConfig(testenv)
case pathutil.LocalLoc:
conf = hypervisor.GenerateLocalConfig()
conf = hypervisor.GenerateLocalConfig(testenv)
default:
log.Fatalln("invalid config type:", configLocType)
}
Expand Down
13 changes: 11 additions & 2 deletions cmd/hypervisor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package commands

import (
"fmt"
"net"
"net/http"
"os"

"github.com/SkycoinProject/dmsg"
"github.com/SkycoinProject/dmsg/disc"
"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -62,7 +63,15 @@ var rootCmd = &cobra.Command{

log.Infof("serving RPC on '%s'", rpcAddr)
go func() {
l, err := net.Listen("tcp", rpcAddr)
_, rpcPort, err := config.Interfaces.SplitRPCAddr()
if err != nil {
log.Fatalln("Failed to parse rpc port from rpc address:", err)
}

dmsgC := dmsg.NewClient(config.PK, config.SK, disc.NewHTTP(config.DmsgDiscovery), dmsg.DefaultConfig())
go dmsgC.Serve()

l, err := dmsgC.Listen(rpcPort)
if err != nil {
log.Fatalln("Failed to bind tcp port:", err)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/prometheus/client_golang v1.3.0
github.com/prometheus/common v0.7.0
github.com/sirupsen/logrus v1.4.2
github.com/skycoin/dmsg v0.0.0-20190805065636-70f4c32a994f // indirect
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
go.etcd.io/bbolt v1.3.3
Expand Down
13 changes: 13 additions & 0 deletions pkg/httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"net"
"net/http"
"strconv"
"strings"

"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/gorilla/handlers"
Expand Down Expand Up @@ -71,3 +73,14 @@ func WriteLog(writer io.Writer, params handlers.LogFormatterParams) {
log.WithError(err).Warn("Failed to write log")
}
}

// SplitRPCAddr returns host and port and whatever error results from parsing the rpc address interface
func SplitRPCAddr(rpcAddr string) (host string, port uint16, err error) {
addrToken := strings.Split(rpcAddr, ":")
uint64port, err := strconv.ParseUint(addrToken[1], 10, 16)
if err != nil {
return
}

return addrToken[0], uint16(uint64port), nil
}
42 changes: 28 additions & 14 deletions pkg/hypervisor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"time"

"github.com/SkycoinProject/dmsg/cipher"

"github.com/SkycoinProject/skywire-mainnet/internal/skyenv"
"github.com/SkycoinProject/skywire-mainnet/pkg/httputil"
"github.com/SkycoinProject/skywire-mainnet/pkg/util/pathutil"
)

Expand All @@ -35,19 +36,27 @@ func (hk *Key) UnmarshalText(text []byte) error {

// Config configures the hypervisor.
type Config struct {
PK cipher.PubKey `json:"public_key"`
SK cipher.SecKey `json:"secret_key"`
DBPath string `json:"db_path"` // Path to store database file.
EnableAuth bool `json:"enable_auth"` // Whether to enable user management.
Cookies CookieConfig `json:"cookies"` // Configures cookies (for session management).
Interfaces InterfaceConfig `json:"interfaces"` // Configures exposed interfaces.
PK cipher.PubKey `json:"public_key"`
SK cipher.SecKey `json:"secret_key"`
DBPath string `json:"db_path"` // Path to store database file.
EnableAuth bool `json:"enable_auth"` // Whether to enable user management.
Cookies CookieConfig `json:"cookies"` // Configures cookies (for session management).
Interfaces InterfaceConfig `json:"interfaces"` // Configures exposed interfaces.
DmsgDiscovery string `json:"dmsg_discovery"` // DmsgDiscovery address for dmsg usage
jdknives marked this conversation as resolved.
Show resolved Hide resolved
}

func makeConfig() Config {
func makeConfig(testenv bool) Config {
var c Config
pk, sk := cipher.GenerateKeyPair()
c.PK = pk
c.SK = sk

if testenv {
c.DmsgDiscovery = skyenv.TestDmsgDiscAddr
} else {
c.DmsgDiscovery = skyenv.DefaultDmsgDiscAddr
}

c.EnableAuth = true
c.Cookies.HashKey = cipher.RandByte(64)
c.Cookies.BlockKey = cipher.RandByte(32)
Expand All @@ -56,26 +65,26 @@ func makeConfig() Config {
}

// GenerateWorkDirConfig generates a config with default values and uses db from current working directory.
func GenerateWorkDirConfig() Config {
func GenerateWorkDirConfig(testenv bool) Config {
dir, err := os.Getwd()
if err != nil {
log.Fatalf("failed to generate WD config: %s", dir)
}
c := makeConfig()
c := makeConfig(testenv)
c.DBPath = filepath.Join(dir, "users.db")
return c
}

// GenerateHomeConfig generates a config with default values and uses db from user's home folder.
func GenerateHomeConfig() Config {
c := makeConfig()
func GenerateHomeConfig(testenv bool) Config {
c := makeConfig(testenv)
c.DBPath = filepath.Join(pathutil.HomeDir(), ".skycoin/hypervisor/users.db")
return c
}

// GenerateLocalConfig generates a config with default values and uses db from shared folder.
func GenerateLocalConfig() Config {
c := makeConfig()
func GenerateLocalConfig(testenv bool) Config {
c := makeConfig(testenv)
c.DBPath = "/usr/local/SkycoinProject/hypervisor/users.db"
return c
}
Expand Down Expand Up @@ -134,3 +143,8 @@ func (c *InterfaceConfig) FillDefaults() {
c.HTTPAddr = ":8080"
c.RPCAddr = ":7080"
}

// SplitRPCAddr returns host and port and whatever error results from parsing the rpc address interface
func (c *InterfaceConfig) SplitRPCAddr() (host string, port uint16, err error) {
return httputil.SplitRPCAddr(c.RPCAddr)
}
26 changes: 14 additions & 12 deletions pkg/hypervisor/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"errors"
"fmt"
"math/rand"
"net"
"net/http"
"net/rpc"
"strconv"
"strings"
"sync"
"time"

"github.com/SkycoinProject/dmsg"
"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/dmsg/noise"
"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
Expand All @@ -32,7 +32,7 @@ var (
)

type appNodeConn struct {
Addr *noise.Addr
Addr dmsg.Addr
Client visor.RPCClient
}

Expand Down Expand Up @@ -61,13 +61,14 @@ func NewNode(config Config) (*Node, error) {
}

// ServeRPC serves RPC of a Node.
func (m *Node) ServeRPC(lis net.Listener) error {
func (m *Node) ServeRPC(lis *dmsg.Listener) error {
for {
conn, err := noise.WrapListener(lis, m.c.PK, m.c.SK, false, noise.HandshakeXK).Accept()
conn, err := lis.Accept()
if err != nil {
return err
}
addr := conn.RemoteAddr().(*noise.Addr)
addr := conn.RemoteAddr().(dmsg.Addr)
log.Infoln("accepted: ", addr.PK)
m.mu.Lock()
m.nodes[addr.PK] = appNodeConn{
Addr: addr,
Expand Down Expand Up @@ -100,9 +101,9 @@ func (m *Node) AddMockData(config MockConfig) error {
}
m.mu.Lock()
m.nodes[pk] = appNodeConn{
Addr: &noise.Addr{
Addr: dmsg.Addr{
PK: pk,
Addr: mockAddr(fmt.Sprintf("0.0.0.0:%d", i)),
Port: uint16(i),
},
Client: client,
}
Expand Down Expand Up @@ -248,7 +249,7 @@ func (m *Node) getNodes() http.HandlerFunc {
summary = &visor.Summary{PubKey: pk}
}
summaries = append(summaries, summaryResp{
TCPAddr: c.Addr.Addr.String(),
TCPAddr: c.Addr.String(),
Online: err == nil,
Summary: summary,
})
Expand All @@ -267,7 +268,7 @@ func (m *Node) getNode() http.HandlerFunc {
return
}
httputil.WriteJSON(w, r, http.StatusOK, summaryResp{
TCPAddr: ctx.Addr.Addr.String(),
TCPAddr: ctx.Addr.String(),
Summary: summary,
})
})
Expand Down Expand Up @@ -366,6 +367,7 @@ type LogsRes struct {
func (m *Node) appLogsSince() http.HandlerFunc {
return m.withCtx(m.appCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) {
since := r.URL.Query().Get("since")
since = strings.Replace(since, " ", "+", 1) // we need to put '+' again that was replaced in the query string

// if time is not parseable or empty default to return all logs
t, err := time.Parse(time.RFC3339Nano, since)
Expand Down Expand Up @@ -613,7 +615,7 @@ func (m *Node) restart() http.HandlerFunc {
<<< Helper functions >>>
*/

func (m *Node) client(pk cipher.PubKey) (*noise.Addr, visor.RPCClient, bool) {
func (m *Node) client(pk cipher.PubKey) (dmsg.Addr, visor.RPCClient, bool) {
m.mu.RLock()
conn, ok := m.nodes[pk]
m.mu.RUnlock()
Expand All @@ -623,7 +625,7 @@ func (m *Node) client(pk cipher.PubKey) (*noise.Addr, visor.RPCClient, bool) {
type httpCtx struct {
// Node
PK cipher.PubKey
Addr *noise.Addr
Addr dmsg.Addr
RPC visor.RPCClient

// App
Expand Down
Loading