Skip to content

Commit

Permalink
Remote RPC: enable https (#424)
Browse files Browse the repository at this point in the history
* wallet service: remove unused migrate flag

* remote RPC: enable https

* remote RPC: handle path option for wallet's NodeClient

* Custom RPC modal: clean up

- prohibit remote over http
- remove network switcher: use wallet's network property
- autofill port based on network
- set default values as actual values instead of placeholders

Co-authored-by: Chi Kei Chan <chikeichan@gmail.com>
  • Loading branch information
pinheadmz and chikeichan committed Dec 23, 2021
1 parent 7683e3d commit bf50e44
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 52 deletions.
13 changes: 5 additions & 8 deletions app/background/node/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,20 +307,17 @@ export class NodeService extends EventEmitter {
apiKey,
} = rpc;

const portString = port ? `:${port}` : '';
const pathString = (!pathname || pathname === '/') ? '' : pathname;
const protoString = protocol || 'http';

const url = `${protoString}://${host}${portString}${pathString}`;

// Not really used after this point,
// but overwrite the local getAPIKey() just in case.
this.apiKey = apiKey;

return new NodeClient({
network: this.network,
apiKey,
url,
ssl: protocol === 'https',
host,
port: parseInt(port, 10),
path: pathname,
timeout: 30000,
});
}

Expand Down
17 changes: 15 additions & 2 deletions app/background/wallet/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,28 @@ class WalletService {
payload: this.walletApiKey,
});

// This is an unfortunate work-around for the fact that
// WalletNode doesn't accept a `nodePath` option to
// pass to NodeClient which gets passed to bcurl/client.
const nodeURL =
this.conn.pathname
?
this.conn.protocol + '://' +
'username_is_ignored:' + this.conn.apiKey + '@' +
this.conn.host + ':' + this.conn.port + this.conn.pathname
:
null;

this.node = new WalletNode({
network: this.networkName,
nodeHost: this.conn.host,
nodePort: this.conn.port || undefined,
nodePort: parseInt(this.conn.port, 10),
nodeApiKey: this.conn.apiKey,
nodeSSL: this.conn.protocol === 'https',
nodeURL,
apiKey: this.walletApiKey,
memory: false,
prefix: HSD_DATA_DIR,
migrate: 0,
logFile: true,
logConsole: false,
logLevel: 'debug',
Expand Down
55 changes: 13 additions & 42 deletions app/pages/Settings/CustomRPCConfigModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default class CustomRPCConfigModal extends Component {
protocol: 'http',
host: '',
pathname: '',
networkType: 'main',
port: '',
apiKey: '',
errorMessage: '',
Expand All @@ -55,12 +54,13 @@ export default class CustomRPCConfigModal extends Component {
async fetchCustomRPC() {
const conn = await connClient.getCustomRPC();

const {rpcPort} = Network.get(this.props.walletNetwork);

this.setState({
networkType: conn.networkType || 'main',
protocol: conn.protocol || 'http',
apiKey: conn.apiKey,
port: conn.port,
host: conn.host,
port: conn.port || rpcPort,
host: conn.host || '127.0.0.1',
pathname: conn.pathname,
});
}
Expand All @@ -72,15 +72,7 @@ export default class CustomRPCConfigModal extends Component {
return;
}

if (!this.state.confirming && this.isDangerousURL()) {
this.setState({
confirming: true,
});
return;
}

const {
networkType,
apiKey,
host,
port,
Expand Down Expand Up @@ -187,13 +179,16 @@ export default class CustomRPCConfigModal extends Component {
const {
host,
pathname,
networkType,
apiKey,
port,
protocol,
errorMessage,
} = this.state;

let {errorMessage} = this.state;
if (!errorMessage && this.isDangerousURL())
errorMessage = 'Remote connection over HTTP is prohibited.';


return (
<MiniModal
closeRoute="/settings/connection"
Expand All @@ -206,7 +201,7 @@ export default class CustomRPCConfigModal extends Component {
className="network-picker custom-rpc__network-picker"
items={[
{ label: 'http', value: 'http' },
// { label: 'https', value: 'https' },
{ label: 'https', value: 'https' },
]}
currentIndex={['http', 'https'].indexOf(protocol)}
onChange={(value) => this.setState({
Expand All @@ -222,7 +217,6 @@ export default class CustomRPCConfigModal extends Component {
type="text"
className="settings__input"
value={host}
placeholder={`127.0.0.1`}
onChange={e => this.setState({
host: e.target.value,
errorMessage: '',
Expand All @@ -236,33 +230,19 @@ export default class CustomRPCConfigModal extends Component {
type="text"
className="settings__input"
value={pathname}
placeholder={`/`}
onChange={e => this.setState({
pathname: e.target.value,
errorMessage: '',
})}
/>
</div>

<div className="settings__input-row">
<div className="settings__input-title">Network Type</div>
<NetworkPicker
className="custom-rpc__network-picker"
onNetworkChange={(net) => this.setState({
networkType: net,
port: Network.get(net).rpcPort,
errorMessage: '',
})}
/>
</div>

<div className="settings__input-row">
<div className="settings__input-title">Port</div>
<input
type="text"
className="settings__input"
value={port}
placeholder={`12037`}
onChange={e => this.setState({
port: e.target.value,
errorMessage: '',
Expand All @@ -273,6 +253,7 @@ export default class CustomRPCConfigModal extends Component {
<div className="settings__input-row">
<div className="settings__input-title">API Key</div>
<input
spellCheck={false}
type="text"
className="settings__input"
value={apiKey}
Expand All @@ -282,14 +263,6 @@ export default class CustomRPCConfigModal extends Component {
})}
/>
</div>
{
!errorMessage && this.state.confirming && (
<div className="rpc-modal-warning">
Remote connection over HTTP is not recommended. Are you sure you want to continue?
</div>
)
}

{
errorMessage && (
<div className="rpc-modal-warning">
Expand All @@ -305,13 +278,11 @@ export default class CustomRPCConfigModal extends Component {
Cancel
</button>
<button
className={c('settings__btn', {
'settings__btn--confirm': this.state.confirming,
})}
className={"settings__btn settings__btn--confirm"}
onClick={this.saveCustomRPC}
disabled={errorMessage}
>
{this.state.confirming ? 'Confirm' : 'Save'}
Save
</button>
</div>
</MiniModal>
Expand Down

0 comments on commit bf50e44

Please sign in to comment.