From 4b1fa050e9602157320dbc3e9c11a640afb28699 Mon Sep 17 00:00:00 2001 From: KonradStaniec Date: Thu, 5 Aug 2021 08:14:25 +0200 Subject: [PATCH] Use websockets in proxy (#779) --- fluffy/conf.nim | 25 +++++++++++++++++++++---- fluffy/fluffy.nim | 4 ++-- fluffy/rpc/eth_api.nim | 4 ++-- vendor/nim-json-rpc | 2 +- vendor/nim-websock | 2 +- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/fluffy/conf.nim b/fluffy/conf.nim index 6858b5e6a..598ff4168 100644 --- a/fluffy/conf.nim +++ b/fluffy/conf.nim @@ -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 @[] diff --git a/fluffy/fluffy.nim b/fluffy/fluffy.nim index 49a94204a..ce2c2f95e 100644 --- a/fluffy/fluffy.nim +++ b/fluffy/fluffy.nim @@ -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) diff --git a/fluffy/rpc/eth_api.nim b/fluffy/rpc/eth_api.nim index 4032c0c22..917fecb4e 100644 --- a/fluffy/rpc/eth_api.nim +++ b/fluffy/rpc/eth_api.nim @@ -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 @@ -96,4 +96,4 @@ proc installEthApiHandlers*(rpcServerWithProxy: var RpcHttpProxy) rpcServerWithProxy.registerProxyMethod("eth_syncing") - rpcServerWithProxy.registerProxyMethod("eth_uninstallFilter") + rpcServerWithProxy.registerProxyMethod("eth_uninstallFilter") diff --git a/vendor/nim-json-rpc b/vendor/nim-json-rpc index a138c410c..a1715e33a 160000 --- a/vendor/nim-json-rpc +++ b/vendor/nim-json-rpc @@ -1 +1 @@ -Subproject commit a138c410c5c482ba8352a19e2e3b8d1850d4823f +Subproject commit a1715e33ac6f3e2951cdae9643ebad393fb58eb2 diff --git a/vendor/nim-websock b/vendor/nim-websock index eabf183e6..00440b6ef 160000 --- a/vendor/nim-websock +++ b/vendor/nim-websock @@ -1 +1 @@ -Subproject commit eabf183e6de46ebea3087bb860ab03cf6dba44ac +Subproject commit 00440b6effcdfd1b75bfcca1b65d80a3ca298606