janoside / btc-rpc-explorer

Database-free, self-hosted Bitcoin explorer, via RPC to Bitcoin Core.
https://bitcoinexplorer.org
MIT License
1.54k stars 1.16k forks source link

Check for changes in bitcoind .cookie on RpcError #393

Closed Stadicus closed 1 year ago

Stadicus commented 2 years ago

Describe the new feature or enhancement

Bitcoin Core features the authentication cookie to grant RPC access without using rpcuser/rpcpassword, which is already supported by BTC RPC Explorer. The RaspiBolt guide relies on that method whenever possible.

If Bitcoin Core is restarted, the .cookie file is recreated with new access credentials. Currently, BTC RPC Explorer does not detect this change and just keeps trying to query bitcoind with the old credentials it read from the cookie on startup.

This renders the application unusable, until it is restarted (reading the new credentials and accessing bitcoind successfully again):

image

The cause is simple, and I hope a fix would be simple as well: check if the .cookie content changed (constantly, or maybe after the first RPC error), reload it, and always use the latest access credentials.

Additional context

The log contains a ton of error messages like the following, repeating constantly:

Dec 19 08:20:33 raspibolt3 npm[60857]: 2021-12-19T08:20:33.247Z btcexp:error Error RpcError-002: RpcError: 401 Unauthorized, json: {"message":"Unauthorized","body":"","code":401,"name":"RpcError","userData":{"request":{"method":"estimatesmartfee","parameters":[1,"CONSERVATIVE"]}}}, userData: [object Object] (json: {"request":"estimatesmartfee([1,\"CONSERVATIVE\"])"})
Dec 19 08:20:33 raspibolt3 npm[60857]: 2021-12-19T08:20:33.247Z btcexp:errorVerbose Stack: RpcError: Unauthorized
Dec 19 08:20:33 raspibolt3 npm[60857]:     at Parser.rpc (/home/btcrpcexplorer/btc-rpc-explorer/node_modules/btc-rpc-client/dist/src/parser.js:73:13)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at Client.command (/home/btcrpcexplorer/btc-rpc-explorer/node_modules/btc-rpc-client/dist/src/index.js:169:24)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at runMicrotasks (<anonymous>)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at processTicksAndRejections (internal/process/task_queues.js:95:5)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at async Object.rpcCall (/home/btcrpcexplorer/btc-rpc-explorer/app/api/rpcApi.js:509:20)
Dec 19 08:20:33 raspibolt3 npm[60857]: 2021-12-19T08:20:33.249Z btcexp:app Unhandled Rejection at: Promise Promise {
Dec 19 08:20:33 raspibolt3 npm[60857]:   <rejected> RpcError: Unauthorized
Dec 19 08:20:33 raspibolt3 npm[60857]:       at Parser.rpc (/home/btcrpcexplorer/btc-rpc-explorer/node_modules/btc-rpc-client/dist/src/parser.js:73:13)
Dec 19 08:20:33 raspibolt3 npm[60857]:       at Client.command (/home/btcrpcexplorer/btc-rpc-explorer/node_modules/btc-rpc-client/dist/src/index.js:169:24)
Dec 19 08:20:33 raspibolt3 npm[60857]:       at runMicrotasks (<anonymous>)
Dec 19 08:20:33 raspibolt3 npm[60857]:       at processTicksAndRejections (internal/process/task_queues.js:95:5)
Dec 19 08:20:33 raspibolt3 npm[60857]:       at async Object.rpcCall (/home/btcrpcexplorer/btc-rpc-explorer/app/api/rpcApi.js:509:20) {
Dec 19 08:20:33 raspibolt3 npm[60857]:     body: '',
Dec 19 08:20:33 raspibolt3 npm[60857]:     code: 401,
Dec 19 08:20:33 raspibolt3 npm[60857]:     userData: { request: [Object] }
Dec 19 08:20:33 raspibolt3 npm[60857]:   }
Dec 19 08:20:33 raspibolt3 npm[60857]: } reason: RpcError: Unauthorized
Dec 19 08:20:33 raspibolt3 npm[60857]:     at Parser.rpc (/home/btcrpcexplorer/btc-rpc-explorer/node_modules/btc-rpc-client/dist/src/parser.js:73:13)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at Client.command (/home/btcrpcexplorer/btc-rpc-explorer/node_modules/btc-rpc-client/dist/src/index.js:169:24)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at runMicrotasks (<anonymous>)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at processTicksAndRejections (internal/process/task_queues.js:95:5)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at async Object.rpcCall (/home/btcrpcexplorer/btc-rpc-explorer/app/api/rpcApi.js:509:20) {
Dec 19 08:20:33 raspibolt3 npm[60857]:   body: '',
Dec 19 08:20:33 raspibolt3 npm[60857]:   code: 401,
Dec 19 08:20:33 raspibolt3 npm[60857]:   userData: { request: { method: 'estimatesmartfee', parameters: [Array] } }
Dec 19 08:20:33 raspibolt3 npm[60857]: } stack: RpcError: Unauthorized
Dec 19 08:20:33 raspibolt3 npm[60857]:     at Parser.rpc (/home/btcrpcexplorer/btc-rpc-explorer/node_modules/btc-rpc-client/dist/src/parser.js:73:13)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at Client.command (/home/btcrpcexplorer/btc-rpc-explorer/node_modules/btc-rpc-client/dist/src/index.js:169:24)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at runMicrotasks (<anonymous>)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at processTicksAndRejections (internal/process/task_queues.js:95:5)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at async Object.rpcCall (/home/btcrpcexplorer/btc-rpc-explorer/app/api/rpcApi.js:509:20)
Dec 19 08:20:33 raspibolt3 npm[60857]: 2021-12-19T08:20:33.253Z btcexp:error Error RpcError-002: RpcError: 401 Unauthorized, json: {"message":"Unauthorized","body":"","code":401,"name":"RpcError","userData":{"request":{"method":"estimatesmartfee","parameters":[6,"CONSERVATIVE"]}}}, userData: [object Object] (json: {"request":"estimatesmartfee([6,\"CONSERVATIVE\"])"})
Dec 19 08:20:33 raspibolt3 npm[60857]: 2021-12-19T08:20:33.253Z btcexp:errorVerbose Stack: RpcError: Unauthorized
Dec 19 08:20:33 raspibolt3 npm[60857]:     at Parser.rpc (/home/btcrpcexplorer/btc-rpc-explorer/node_modules/btc-rpc-client/dist/src/parser.js:73:13)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at Client.command (/home/btcrpcexplorer/btc-rpc-explorer/node_modules/btc-rpc-client/dist/src/index.js:169:24)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at runMicrotasks (<anonymous>)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at processTicksAndRejections (internal/process/task_queues.js:95:5)
Dec 19 08:20:33 raspibolt3 npm[60857]:     at async Object.rpcCall (/home/btcrpcexplorer/btc-rpc-explorer/app/api/rpcApi.js:509:20)

On the Bitcoin Core side, the log looks as follows:

2021-12-19T08:19:33Z ThreadRPCServer incorrect password attempt from 127.0.0.1:56266
2021-12-19T08:19:33Z ThreadRPCServer incorrect password attempt from 127.0.0.1:56268
2021-12-19T08:19:34Z ThreadRPCServer incorrect password attempt from 127.0.0.1:56270
2021-12-19T08:19:34Z ThreadRPCServer incorrect password attempt from 127.0.0.1:56272
2021-12-19T08:19:34Z ThreadRPCServer incorrect password attempt from 127.0.0.1:56274
2021-12-19T08:19:34Z ThreadRPCServer incorrect password attempt from 127.0.0.1:56298
2021-12-19T08:19:34Z ThreadRPCServer incorrect password attempt from 127.0.0.1:56300
2021-12-19T08:19:34Z ThreadRPCServer incorrect password attempt from 127.0.0.1:56302
2021-12-19T08:19:34Z ThreadRPCServer incorrect password attempt from 127.0.0.1:56304
2021-12-19T08:19:35Z ThreadRPCServer incorrect password attempt from 127.0.0.1:56306
2021-12-19T08:19:45Z UpdateTip: new best=00000000000000000008c87d4316bc4c7edaa51e4c61f2648ae3b6aa24a79f76 height=714776 version=0x2a966000 log2_work=93.237201 tx=696098242 date='2021-12-19T08:19:22Z' progress=1.000000 cache=80.6MiB(539933txo)
2021-12-19T08:20:09Z UpdateTip: new best=0000000000000000000acbc7b87e07d004fc7a832842b40b51678e7fbe899ffd height=714777 version=0x20000004 log2_work=93.237214 tx=696098283 date='2021-12-19T08:20:05Z' progress=1.000000 cache=80.6MiB(540062txo)
2021-12-19T08:20:31Z ThreadRPCServer incorrect password attempt from 127.0.0.1:57796
2021-12-19T08:20:31Z ThreadRPCServer incorrect password attempt from 127.0.0.1:57798
2021-12-19T08:20:31Z ThreadRPCServer incorrect password attempt from 127.0.0.1:57800
2021-12-19T08:20:31Z ThreadRPCServer incorrect password attempt from 127.0.0.1:57802
2021-12-19T08:20:31Z ThreadRPCServer incorrect password attempt from 127.0.0.1:57804
Stadicus commented 2 years ago

Are there any plans to fix this issue?