diff --git a/node/chain.go b/node/chain.go index 92a916c2d..ef8e98743 100644 --- a/node/chain.go +++ b/node/chain.go @@ -3,12 +3,15 @@ package node import ( "encoding/json" + "errors" "fmt" + "github.com/pokt-network/pocket-core/util" "io/ioutil" - "net" + "net/http" + "net/url" "os" + "strconv" "sync" - "time" ) // A structure that specifies a non-native blockchain. @@ -22,6 +25,7 @@ type HostedChain struct { Blockchain `json:"blockchain"` Port string `json:"port"` Host string `json:"host"` + Path string `json:"path"` // url path for token based authentication Medium string `json:"medium"` // http, ws, tcp, etc. } @@ -81,11 +85,12 @@ func ChainToHosted(b Blockchain) HostedChain { } // "dialHC" attempts to connect to the specific host:port hosting the chain. -func dialHC(host string, port string) error { - if _, err := net.DialTimeout("tcp", host+":"+port, time.Duration(1*time.Second)); err != nil { - return err +func dialHC(u *url.URL) error { + resp, _ := http.Get(u.String()) + if resp.StatusCode == 400 || resp.StatusCode == 200 { + return nil } - return nil + return errors.New(strconv.Itoa(resp.StatusCode) + " : " + resp.Status) } // "TestChains" tests for hosted blockchain clients. @@ -94,10 +99,21 @@ func TestChains() { mux.Lock() defer mux.Unlock() for _, c := range hc { - if err := dialHC(c.Host, c.Port); err != nil { - fmt.Fprint(os.Stderr, c.Name+" client is not detected @ "+c.Host+":"+c.Port+"\n") + s, err := util.URLProto(c.Host + ":" + c.Port) + if err != nil { + ExitGracefully(err.Error()) + } + u, err := url.ParseRequestURI(s) + if err != nil { + ExitGracefully(err.Error()) + } + if c.Path != "" { + u.Path = c.Path + } + if err := dialHC(u); err != nil { + fmt.Fprint(os.Stderr, c.Name+" client is not detected @ "+u.String()+"\n") ExitGracefully(c.Name + " client isn't detected" + "\n") } - fmt.Println(c.Name + " NetID:" + c.NetID + " client is active and ready for service on port " + c.Port) + fmt.Println(c.Name + " NetID:" + c.NetID + " client is active and ready for service @ " + u.String()) } } diff --git a/plugin/rpc/rpc.go b/plugin/rpc/rpc.go index e8ea5daca..88b678bc5 100644 --- a/plugin/rpc/rpc.go +++ b/plugin/rpc/rpc.go @@ -7,15 +7,16 @@ import ( "github.com/pokt-network/pocket-core/util" "io/ioutil" "net/http" + "net/url" ) // "ExecuteRequest" takes in the raw json string and forwards it to the port -func ExecuteRequest(jsonStr []byte, host string, port string) (string, error) { - url, err := util.URLProto(host + ":" + port) +func ExecuteRequest(jsonStr []byte, u *url.URL) (string, error) { + ur, err := util.URLProto(u.String()) if err != nil { return "", err } - req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) + req, err := http.NewRequest("POST", ur, bytes.NewBuffer(jsonStr)) req.Close = true req.Header.Set("Content-Type", "application/json") resp, err := (&http.Client{}).Do(req) diff --git a/service/service.go b/service/service.go index 6b6d50b9c..949a51daa 100644 --- a/service/service.go +++ b/service/service.go @@ -3,12 +3,11 @@ package service import ( "encoding/json" - "errors" + "net/url" "os" "github.com/pokt-network/pocket-core/config" "github.com/pokt-network/pocket-core/const" - "github.com/pokt-network/pocket-core/logs" "github.com/pokt-network/pocket-core/node" "github.com/pokt-network/pocket-core/plugin/rpc" ) @@ -25,13 +24,14 @@ type Relay struct { func RouteRelay(relay Relay) (string, error) { if node.EnsureDWL(node.DWL(), relay.DevID) { hc := node.ChainToHosted(node.Blockchain{Name: relay.Blockchain, NetID: relay.NetworkID}) - port := hc.Port - host := hc.Host - if port == "" || host == "" { - logs.NewLog("Not a supported blockchain", logs.ErrorLevel, logs.JSONLogFormat) - return "This blockchain is not supported by this node", errors.New("not a supported blockchain") + u, err := url.ParseRequestURI(hc.Host + ":" + hc.Port) + if err != nil { + return "", err } - return rpc.ExecuteRequest([]byte(relay.Data), host, port) + if hc.Path != "" { + u.Path = hc.Path + } + return rpc.ExecuteRequest([]byte(relay.Data), u) } return "Invalid credentials", nil }