《对以太坊节点远程调用添加验证保护》
2018-12-13 本文已影响0人
胡萝卜生萝卜
Tag:Ethereum、nginx、basic auth、rpc
配置以太坊节点
- Ubuntu 平台安装以太坊
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
安装 Parity 启动程序
chmod 744 parity #更改二进制启动程序权限
配置 nginx 服务,为 RPC 端口添加 auth
- 安装 nginx
sudo apt install nginx
- 配置用于验证的用户名和密码
sudo htpasswd -c /etc/nginx/eth.htpasswd eth
将在/etc/nginx下创建名为 eth.htpasswd 的密码文件,用户名设置为 nginx。输入上面命令,会提示用户输入俩次密码。
- 修改 nginx 配置
sudo vi /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name localhost;
location /eth {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/parity.htpasswd;
proxy_pass http://localhost:9998;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
- 重载或重新启动 nginx
sudo service nginx reload
使用 Parity 启动以太坊
- Parity 启动示例
./parity \
--jsonrpc-apis 'web3,eth,pubsub,net,parity,parity_pubsub,traces,rpc,secretstore,shh,shh_pubsub' \ #指定 api 内容
--jsonrpc-interface 127.0.0.1 \ #指定允许 RPC 的访问地址
--jsonrpc-port=8545 \ #指定端口号
--jsonrpc-hosts="all" \
--chain=ropsten #指定期望的链
请求以太坊
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x353C13b940aA5eEd75AA97d477954289E7880BB8", "latest"],"id":1}' -H 'content-type:application/json' http://user:password@ip/path