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

Make runner fail if proxy is not specified when scan is websocket #632

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions services/runner/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package runner

import (
"fmt"
"net/url"
)

// CheckProxyAgainstScan checks given proxy URL against the scan API.
// The proxy API must specified as HTTP(s) when the scan API is WebSocket.
func CheckProxyAgainstScan(scan, proxy string) error {
scanUrl, err := url.Parse(scan)
if err != nil {
return fmt.Errorf("invalid scan api url: %v", err)
}
// nothing to check if scan is already not websocket
if !(scanUrl.Scheme == "ws" || scanUrl.Scheme == "wss") {
return nil
}

if len(proxy) == 0 {
return ErrBadProxyAPI
}
proxyUrl, err := url.Parse(proxy)
if err != nil {
return fmt.Errorf("invalid proxy api url: %v", err)
}
// and proxy api must be either http or https
if !(proxyUrl.Scheme == "http" || proxyUrl.Scheme == "https") {
return ErrBadProxyAPI
}
return nil
}
54 changes: 54 additions & 0 deletions services/runner/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package runner

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestCheckProxyAgainstScan(t *testing.T) {
testCases := []struct {
name string
scan string
proxy string
valid bool
}{
{
name: "scan is http",
scan: "http://foo.bar",
proxy: "",
valid: true,
},
{
name: "scan is websocket, proxy is empty",
scan: "wss://foo.bar",
proxy: "",
valid: false,
},
{
name: "scan is websocket, proxy is websocket",
scan: "wss://foo.bar",
proxy: "wss://foo.bar",
valid: false,
},
{
name: "scan is websocket, proxy is http",
scan: "wss://foo.bar",
proxy: "http://any.thing",
valid: true,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
r := require.New(t)

err := CheckProxyAgainstScan(testCase.scan, testCase.proxy)
if testCase.valid {
r.NoError(err)
} else {
r.Error(err)
}
})
}
}
10 changes: 10 additions & 0 deletions services/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
log "github.com/sirupsen/logrus"
)

// Errprs
var (
ErrBadProxyAPI = fmt.Errorf("proxy api must be specified as http(s) when scan api is websocket")
)

// Runner receives and starts the latest updater and supervisor.
type Runner struct {
ctx context.Context
Expand Down Expand Up @@ -112,6 +117,11 @@ func (runner *Runner) doStartUpCheck() error {
if err != nil {
return fmt.Errorf("scan api check failed: %v", err)
}

if err := CheckProxyAgainstScan(runner.cfg.Scan.JsonRpc.Url, runner.cfg.JsonRpcProxy.JsonRpc.Url); err != nil {
return err
}

if runner.cfg.Trace.Enabled {
// ensure that the trace json-rpc api is reachable
err = ethereum.TestAPI(runner.ctx, runner.fixTestRpcUrl(runner.cfg.Trace.JsonRpc.Url))
Expand Down