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

Add Websocket support #49

Merged
merged 10 commits into from
Nov 20, 2020
Merged
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## [unreleased](https://github.com/NodeFactoryIo/vedran-daemon/tree/HEAD)

[Full Changelog](https://github.com/NodeFactoryIo/vedran-daemon/compare/v0.2.0...HEAD)

### Fixed

### Added
- Added websocket support [\#49](https://github.com/NodeFactoryIo/vedran-daemon/pull/49) ([mpetrun5](https://github.com/mpetrun5))

### Changed


## [v0.2.0](https://github.com/NodeFactoryIo/vedran-daemon/tree/v0.2.0)

[Full Changelog](https://github.com/NodeFactoryIo/vedran-daemon/compare/v0.1.2...v0.2.0)
Expand Down
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Note that if you need to do this, you probably want to add your Go bin directory
## Usage

```
$ vedran-daemon -h
$ ./vedran-daemon -h
Register vedran-daemon with load balancer and start sending telemetry

Usage:
Expand All @@ -27,10 +27,60 @@ Flags:
-h, --help help for vedran-daemon
--id string Vedran-daemon id string (required)
--lb string Target load balancer url (required)
--log-file string Path to logfile. If not set defaults to stdout
--log-level string Level of logging (eg. debug, info, warn, error) (default "info")
--node-metrics string Polkadot node metrics url (default "localhost:9615")
--node-rpc string Polkadot node rpc url (default "localhost:9933")
--node-ws string Polkadot node ws url (default "localhost:9944")
--payout-address string Payout address to which reward tokens will be sent (required)
```
## Starting daemon

First download latest prebuilt binary of vedran daemon [from releases](https://github.com/NodeFactoryIo/vedran-daemon/releases) and binary for [node](https://github.com/paritytech/polkadot/releases).
Daemon is expected to be started in conjuction with node and will wait for node if it is unavailable.


### Node
For starting node see [instructions](https://github.com/paritytech/polkadot/blob/master/README.md)

**NOTE node should be started with rpc cors disabled

Example:
`
./polkadot --rpc-cors=all
`

### Daemon
Daemon is started by invoking binary.

For example:
```
./vedran-daemon-linux-amd64 --id UuPrCMnkni --lb https://load-balancer.com --payout-address 15MCkjt3B59dNo5reMCWWpxY8QB8VpEbYLo2xHEjuuWsSmTU
```

It will register to load balancer and start sending pings and metrics to load balancer.
Port forwarding to local node is not needed and node and daemon can be in a private network because [http tunnel](https://en.wikipedia.org/wiki/HTTP_tunnel) is created between
node and load balancer on registration which communicate via daemon used as a proxy server.

### Required flags

`--id` - id string by which load balancer will distinguish between nodes - **CAUTION** this should be a unique string and should not be shared

`--lb` - public url of vedran load balancer

`--payout-address` - address of wallet to which reward tokens should be set - **CAUTION** - use valid address depending on network

### Other flags

`--log-level` - log level (debug, info, warn, error) - **DEFAULT** [error]

`--log-file` - path to file in which logs will be saved - **DEFAULT** [stdout]

`--node-metrics` - local url to node metrics - **DEFAULT** [http://localhost:9615]

`--node-rpc` - local url to node rpc endpoint - **DEFAULT** [http://localhost:9933]

`--node-ws` - local url to node websocket rpc endpoint - **DEFAULT** [http://localhost:9944]
mpetrunic marked this conversation as resolved.
Show resolved Hide resolved

## Development

Expand Down
9 changes: 9 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ var (
logLevel string
logFile string
nodeRPCURL string
nodeWSURL string
nodeMetricsURL string
id string
lbBaseURL string
payoutAddress string
lbURL *url.URL
metricsURL *url.URL
rpcURL *url.URL
wsURL *url.URL
)

var startCmd = &cobra.Command{
Expand Down Expand Up @@ -62,12 +64,18 @@ var startCmd = &cobra.Command{
return fmt.Errorf("Failed parsing rpc url: %v", err)
}

wsURL, err = url.Parse(nodeWSURL)
if err != nil {
return fmt.Errorf("Failed parsing ws url: %v", err)
}

return nil
},
}

func init() {
startCmd.Flags().StringVar(&nodeRPCURL, "node-rpc", "http://localhost:9933", "Polkadot node rpc url")
startCmd.Flags().StringVar(&nodeWSURL, "node-ws", "http://localhost:9944", "Polkadot node websocket url")
startCmd.Flags().StringVar(&nodeMetricsURL, "node-metrics", "http://localhost:9615", "Polkadot node metrics url")
startCmd.Flags().StringVar(&id, "id", "", "Vedran-daemon id string (required)")
startCmd.Flags().StringVar(&lbBaseURL, "lb", "", "Target load balancer url (required)")
Expand All @@ -88,6 +96,7 @@ func start(cmd *cobra.Command, _ []string) error {
telemetry := telemetry.NewTelemetry()
tunnel := &tunnel.Tunnel{
NodeRPCURL: rpcURL,
NodeWSURL: wsURL,
}

err := run.Start(tunnel, lbClient, nodeClient, telemetry, id, payoutAddress)
Expand Down
8 changes: 7 additions & 1 deletion internal/tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
// Tunnel is tunnel connection with load balancer
type Tunnel struct {
NodeRPCURL *url.URL
NodeWSURL *url.URL
}

// Tunneler defines methods for connecting to load balancer tunnel
Expand All @@ -35,11 +36,16 @@ func (t *Tunnel) StartTunnel(nodeID string, tunnelServerAddress string, token st
c, err := client.NewClient(&client.ClientConfig{
ServerAddress: tunnelServerAddress,
Tunnels: map[string]*client.Tunnel{
"default": {
"http": {
Protocol: Protocol,
Addr: t.NodeRPCURL.Host,
RemoteAddr: RemoteAddr,
},
"ws": {
Protocol: Protocol,
Addr: t.NodeWSURL.Host,
RemoteAddr: RemoteAddr,
},
},
Logger: log.NewEntry(log.New()),
AuthToken: token,
Expand Down