From 790d4f3f388eb533da11fd5d7904850039816415 Mon Sep 17 00:00:00 2001 From: Steve Power Date: Sun, 18 Mar 2018 17:58:10 +0000 Subject: [PATCH 1/2] basic implementation to enable remote iri over https --- index.js | 8 ++++-- lib/iotaproxy.js | 64 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c0e50b0..707f901 100644 --- a/index.js +++ b/index.js @@ -11,8 +11,12 @@ var iotaProxy = require('./lib/iotaproxy.js'); iotaProxy.start( { - host: 'http://iota.bitfinex.com', - port: 80, + // if provider is absent the host, port and protocol values will be used + // with sensible defaults + provider: '', // in the format "https://field.carriota.com:443" + host: 'iota.bitfinex.com', // the remote iri hostname + port: 80, // the port the remote iri is litening on + protocol: '', localPort: 14265, overrideAttachToTangle: true, timeout: 15 diff --git a/lib/iotaproxy.js b/lib/iotaproxy.js index 6f7f385..eb1f3c7 100644 --- a/lib/iotaproxy.js +++ b/lib/iotaproxy.js @@ -12,7 +12,9 @@ */ const http = require('http'); +const https = require('https'); const ccurlProvider = require('./ccurlprovider.js'); +const { URL } = require('url'); let iotaProxy = { @@ -27,8 +29,52 @@ let iotaProxy = start: function(options) { let opts = options || {}; - this.host = (opts.host ? opts.host : 'iota.bitfinex.com').replace('http://', ''); - this.port = (opts.port ? opts.port : 80); + + // create url from the supplied options + // if one can be set from the supplied options use that instead + // opts.provider is the most descriptive and portable, try for that first + if( opts.provider ){ + console.log('Using provider: ' + opts.provider); + try { + let url = new URL(opts.provider); + this.host = url.host; + this.port = url.port; + this.protocol = url.protocol; + } catch(e) { + console.log('The supplied url \'' + opts.provider + '\' is malformed. Trying other variables and default options'); + } + // if the opts.host is set use it + }else if( opts.host.length !== 0){ + console.log('Using host option: '+ opts.host); + this.host = opts.host; + + // if the opts.host is set try and use opts.port + if( opts.port.length !== 0 ){ + console.log('Using port option: '+ opts.port); + this.port = opts.port; + }else{ + this.port = 80; + console.log('Using default port: 80'); + } + + // if the opts.host is set try and use opts.protocol + if( opts.protocol.length !== 0 ){ + console.log('Using protocol option: '+ opts.protocol); + this.protocol = opts.protocol; + }else{ + this.protocol = 'http:'; + console.log('Using default protocol: http'); + } + // if no variables are set run with the default + }else{ + // supply a default url + let url = new URL('http://iota.bitfinex.com'); + console.log('no variables supplied, using default url ' + url.host); + this.host = url.host; + this.port = url.port; + this.protocol = url.protocol; + } + this.localPort = (opts.localPort ? opts.localPort : 14265); this.overrideAttachToTangle = (opts.overrideAttachToTangle ? true : false); this.timeout = (opts.timeout ? opts.timeout : 15); @@ -104,6 +150,7 @@ let iotaProxy = { hostname: that.host, port: that.port, + protocol: that.protocol, path: request.url, method: request.method }; @@ -125,7 +172,16 @@ let iotaProxy = }); } - let proxyRequest = http.request( + // switch here to turn on https + switch (that.protocol) { + case 'https:': + var transport = https; + break; + default: + var transport = http; + } + + let proxyRequest = transport.request( proxyRequestOptions, function (proxyResponse) { @@ -183,7 +239,7 @@ let iotaProxy = console.log('IOTA proxy server started'); console.log('POW timeout is set to ' + this.timeout + ' min'); console.log('Listening on port ' + this.localPort); - console.log('Relaying requests to ' + this.host + ':' + this.port); + console.log('Relaying requests to '+ this.protocol + '//' + this.host + ':' + this.port); }, From 3bcff5e5d6d311a44fd8dc9e8fabd4f6c309e7a7 Mon Sep 17 00:00:00 2001 From: Steve Power Date: Sun, 18 Mar 2018 20:00:14 +0000 Subject: [PATCH 2/2] Added instructions to the readme and example default protocol in index.js --- README.md | 25 +++++++++++++++++++------ index.js | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 85a422d..a586d0b 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Developers getting to know [iota.lib.js](https://github.com/iotaledger/iota.lib. "_COMMAND attachToTangle is not available on this node_" response after calling iota.api.sendTransfer(). Some others may have experienced the same error message when calling the REST API [attachToTangle](https://iota.readme.io/docs/attachtotangle) command. The attachToTangle command is a request to perform the PoW that is necessary when doing a transaction. - + Most public full nodes in the IOTA network do not support the [attachToTangle](https://iota.readme.io/docs/attachtotangle) command. By design it is expected that you do the PoW locally on your device, the network is not required to do this for you. @@ -30,7 +30,7 @@ and [ccurl](https://github.com/iotaledger/ccurl.git), and override the iota.api. > This repository contains code and precompiled libraries from [iotaledger/wallet](https://github.com/iotaledger/wallet). > Licence: GNU General Public License v3.0 - + --- ## Prerequisites @@ -64,8 +64,21 @@ and [ccurl](https://github.com/iotaledger/ccurl.git), and override the iota.api. var iotaProxy = require('./lib/iotaproxy.js'); iotaProxy.start( { - host: 'http://iota.bitfinex.com', - port: 80, + host: 'iota.bitfinex.com', + port: 80, + protocol: "http:", + localPort: 14265, + overrideAttachToTangle: true + } + ); + ``` + + or by using the provider setting: + ``` + var iotaProxy = require('./lib/iotaproxy.js'); + iotaProxy.start( + { + provider: "https://field.carriota.com:443", localPort: 14265, overrideAttachToTangle: true } @@ -97,7 +110,7 @@ and [ccurl](https://github.com/iotaledger/ccurl.git), and override the iota.api. 'port': 14265 }); ``` - - + + --- diff --git a/index.js b/index.js index 707f901..26e62c7 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ iotaProxy.start( provider: '', // in the format "https://field.carriota.com:443" host: 'iota.bitfinex.com', // the remote iri hostname port: 80, // the port the remote iri is litening on - protocol: '', + protocol: 'http:', localPort: 14265, overrideAttachToTangle: true, timeout: 15