Use websockets in proxy (#779)

This commit is contained in:
KonradStaniec 2021-08-05 08:14:25 +02:00 committed by GitHub
parent 9aea669363
commit 4b1fa050e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 10 deletions

View File

@ -8,13 +8,15 @@
{.push raises: [Defect].}
import
confutils, confutils/std/net, chronicles,
eth/keys, eth/net/nat, eth/p2p/discoveryv5/[enr, node]
uri, confutils, confutils/std/net, chronicles,
eth/keys, eth/net/nat, eth/p2p/discoveryv5/[enr, node],
json_rpc/rpcproxy
const
DefaultListenAddress* = (static ValidIpAddress.init("0.0.0.0"))
DefaultAdminListenAddress* = (static ValidIpAddress.init("127.0.0.1"))
DefaultProxyAddress* = (static "http://127.0.0.1:8546")
DefaultClientConfig* = getHttpClientConfig(DefaultProxyAddress)
type
PortalCmd* = enum
@ -96,9 +98,9 @@ type
# it makes little sense to have default value here in final release, but until then
# it would be troublesome to add some fake uri param every time
proxyUri* {.
defaultValue: DefaultProxyAddress
defaultValue: DefaultClientConfig
desc: "uri of client to get data for unimplemented rpc methods"
name: "proxy-uri" .}: string
name: "proxy-uri" .}: ClientConfig
case cmd* {.
command
@ -141,3 +143,18 @@ proc parseCmdArg*(T: type PrivateKey, p: TaintedString): T
proc completeCmdArg*(T: type PrivateKey, val: TaintedString): seq[string] =
return @[]
proc parseCmdArg*(T: type ClientConfig, p: TaintedString): T
{.raises: [Defect, ConfigurationError].} =
let uri = parseUri(p)
if (uri.scheme == "http" or uri.scheme == "https"):
getHttpClientConfig(p)
elif (uri.scheme == "ws" or uri.scheme == "wss"):
getWebSocketClientConfig(p)
else:
raise newException(
ConfigurationError, "Proxy uri should have defined scheme (http/https/ws/wss)"
)
proc completeCmdArg*(T: type ClientConfig, val: TaintedString): seq[string] =
return @[]

View File

@ -68,11 +68,11 @@ proc run(config: PortalConf) {.raises: [CatchableError, Defect].} =
if config.rpcEnabled:
let ta = initTAddress(config.rpcAddress, config.rpcPort)
var rpcHttpServerWithProxy = newRpcHttpProxy([ta])
var rpcHttpServerWithProxy = RpcProxy.new([ta], config.proxyUri)
rpcHttpServerWithProxy.installEthApiHandlers()
# TODO for now we can only proxy to local node (or remote one without ssl) to make it possible
# to call infura https://github.com/status-im/nim-json-rpc/pull/101 needs to get merged for http client to support https/
waitFor rpcHttpServerWithProxy.start(config.proxyUri)
waitFor rpcHttpServerWithProxy.start()
let bridgeClient = initializeBridgeClient(config.bridgeUri)

View File

@ -20,7 +20,7 @@ import
# Can be done by just forwarding the rpc call, or by adding a call here, but
# that would introduce a unnecessary serializing/deserializing step.
proc installEthApiHandlers*(rpcServerWithProxy: var RpcHttpProxy)
proc installEthApiHandlers*(rpcServerWithProxy: var RpcProxy)
{.raises: [Defect, CatchableError].} =
# Supported API

2
vendor/nim-json-rpc vendored

@ -1 +1 @@
Subproject commit a138c410c5c482ba8352a19e2e3b8d1850d4823f
Subproject commit a1715e33ac6f3e2951cdae9643ebad393fb58eb2

2
vendor/nim-websock vendored

@ -1 +1 @@
Subproject commit eabf183e6de46ebea3087bb860ab03cf6dba44ac
Subproject commit 00440b6effcdfd1b75bfcca1b65d80a3ca298606