From ef0bcbf405acf7f581a4f834a062b64763458fde Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Thu, 26 Apr 2018 21:37:00 +1000 Subject: [PATCH] Add 'auto' setting for geth CORS and websockets origin * 'auto' now supported for `rpcCorsDomain` and `wsOrigins` in the blockchain config. * 'auto' set to the default value in blockchain config for test and demo apps. --- lib/cmds/blockchain/geth_commands.js | 4 ++-- lib/index.js | 14 +++++++++++++- templates/boilerplate/config/blockchain.json | 4 ++-- templates/demo/config/blockchain.json | 4 ++-- test/test1/config/blockchain.json | 2 +- test_apps/contracts_app/blockchain.json | 4 ++-- test_apps/test_app/config/blockchain.json | 4 ++-- 7 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/cmds/blockchain/geth_commands.js b/lib/cmds/blockchain/geth_commands.js index e28e559c..151b3267 100644 --- a/lib/cmds/blockchain/geth_commands.js +++ b/lib/cmds/blockchain/geth_commands.js @@ -103,14 +103,14 @@ class GethCommands { if (config.wsOrigins) { if (config.wsOrigins === '*') { console.log('=================================='); - console.log('rpcCorsDomain set to *'); + console.log('wsOrigins set to *'); console.log('make sure you know what you are doing'); console.log('=================================='); } cmd += "--wsorigins \"" + config.wsOrigins + "\" "; } else { console.log('=================================='); - console.log('warning: cors is not set'); + console.log('warning: wsOrigins is not set'); console.log('=================================='); } } diff --git a/lib/index.js b/lib/index.js index e321a4be..340d1e54 100644 --- a/lib/index.js +++ b/lib/index.js @@ -39,7 +39,19 @@ class Embark { blockchain(env, client) { this.context = [constants.contexts.blockchain]; - return require('./cmds/blockchain/blockchain.js')(this.config.blockchainConfig, client, env).run(); + let blockchainConfig = this.config.blockchainConfig; + let storageConfig = this.config.storageConfig; + let webServerConfig = this.config.webServerConfig; + + if(blockchainConfig.rpcCorsDomain === 'auto') { + if(webServerConfig) blockchainConfig.rpcCorsDomain = `http://${webServerConfig.host}:${webServerConfig.port}`; + if(storageConfig) blockchainConfig.rpcCorsDomain += `${blockchainConfig.rpcCorsDomain.length ? ',' : ''}http://${storageConfig.host}:${storageConfig.port}`; + } + if(blockchainConfig.wsOrigins === 'auto') { + if(webServerConfig) blockchainConfig.wsOrigins = `http://${webServerConfig.host}:${webServerConfig.port}`; + if(storageConfig) blockchainConfig.wsOrigins += `${blockchainConfig.wsOrigins.length ? ',' : ''}http://${storageConfig.host}:${storageConfig.port}`; + } + return require('./cmds/blockchain/blockchain.js')(blockchainConfig, client, env).run(); } simulator(options) { diff --git a/templates/boilerplate/config/blockchain.json b/templates/boilerplate/config/blockchain.json index 1d0e0820..afff2072 100644 --- a/templates/boilerplate/config/blockchain.json +++ b/templates/boilerplate/config/blockchain.json @@ -9,12 +9,12 @@ "maxpeers": 0, "rpcHost": "localhost", "rpcPort": 8545, - "rpcCorsDomain": "http://localhost:8000", + "rpcCorsDomain": "auto", "account": { "password": "config/development/password" }, "targetGasLimit": 8000000, - "wsOrigins": "http://localhost:8000", + "wsOrigins": "auto", "wsRPC": true, "wsHost": "localhost", "wsPort": 8546, diff --git a/templates/demo/config/blockchain.json b/templates/demo/config/blockchain.json index 9dccb92f..638c8167 100644 --- a/templates/demo/config/blockchain.json +++ b/templates/demo/config/blockchain.json @@ -9,12 +9,12 @@ "maxpeers": 0, "rpcHost": "localhost", "rpcPort": 8545, - "rpcCorsDomain": "http://localhost:8000", + "rpcCorsDomain": "auto", "account": { "password": "config/development/password" }, "targetGasLimit": 8000000, - "wsOrigins": "http://localhost:8000", + "wsOrigins": "auto", "wsRPC": true, "wsHost": "localhost", "wsPort": 8546, diff --git a/test/test1/config/blockchain.json b/test/test1/config/blockchain.json index 090f4078..c65ebdcd 100644 --- a/test/test1/config/blockchain.json +++ b/test/test1/config/blockchain.json @@ -7,7 +7,7 @@ "nodiscover": true, "rpcHost": "localhost", "rpcPort": 8545, - "rpcCorsDomain": "http://localhost:8000", + "rpcCorsDomain": "auto", "account": { "password": "config/development/password" } diff --git a/test_apps/contracts_app/blockchain.json b/test_apps/contracts_app/blockchain.json index 8c3790a6..35edfb43 100644 --- a/test_apps/contracts_app/blockchain.json +++ b/test_apps/contracts_app/blockchain.json @@ -9,12 +9,12 @@ "maxpeers": 0, "rpcHost": "localhost", "rpcPort": 8545, - "rpcCorsDomain": "http://localhost:8000", + "rpcCorsDomain": "auto", "account": { "password": "development/password" }, "targetGasLimit": 8000000, - "wsOrigins": "http://localhost:8000", + "wsOrigins": "auto", "wsRPC": true, "wsHost": "localhost", "wsPort": 8546 diff --git a/test_apps/test_app/config/blockchain.json b/test_apps/test_app/config/blockchain.json index 41400449..6acbdfd5 100644 --- a/test_apps/test_app/config/blockchain.json +++ b/test_apps/test_app/config/blockchain.json @@ -9,12 +9,12 @@ "maxpeers": 0, "rpcHost": "localhost", "rpcPort": 8545, - "rpcCorsDomain": "http://localhost:8000", + "rpcCorsDomain": "auto", "account": { "password": "config/development/password" }, "targetGasLimit": 8000000, - "wsOrigins": "http://localhost:8000", + "wsOrigins": "auto", "wsRPC": true, "wsHost": "localhost", "wsPort": 8546