tlsjsonproxy
v1.0.0HTTP Proxy Server
This is a small tool developed using Node native modules only (no dependencies) that connects to a TLS-based JSON-RPC server that does not serve a HTTP interface (e.g. ElectrumX) and provides a local HTTP interface you can send your requests to.
For the sake of concurrency, the remote JSON-RPC server must implement the message ID handling (request's and response's "id" property).
This is a command line tool. Not intended to be used as NodeJS library.
How to install
Download this repo and just use the tlsjsonproxy.js file in command line.
For a global install, run npm i -g tlsjsonproxy
and use the "tlsjsonproxy" command instead.
How to use
First, try connecting to the remote server to check if it's up. Example:
openssl s_client -connect fortress.qtornado.com:50002
Try pasting a command, e.g.:
{ "method" : "blockchain.block.header", "params": [ 1], "id": "msg_id"}
If everything is all ok, you'll get something like:
{"jsonrpc": "2.0", "result": "010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e36299", "id": "msg_id"}
Now close the connection (Ctrl + C) and let's set up this script to allow us to perform the same request through a HTTP API. The usage is:
node tlsjsonproxy.js <remote server> <remote port> <local port> [<local server=0.0.0.0>]
- Remote server: the server to connect to
- Remote port: the port in the remote server
- Local port: local port in your machine where the HTTP server will listen at
- Local server (optional): the local network interface (0.0.0.0 for all, 127.0.0.1 to accept loopback connections only or the interface's IP address)
Example:
node tlsjsonproxy.js fortress.qtornado.com 50002 8080
Now you can perform the same request we did before, but using HTTP.
Note: the examples below were generated by Insomnia
Example with cURL command:
curl --request POST \
--url http://localhost:8081/ \
--header 'content-type: application/json' \
--data '{ "method" : "blockchain.block.header", "params": [ 1]}'
Example with Javascript's fetch:
fetch("http://localhost:8081/", {
"method": "POST",
"headers": {
"content-type": "application/json"
},
"body": {
"method": "blockchain.block.header",
"params": [
1
]
}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
Example with PHP cURL:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8081",
CURLOPT_URL => "http://localhost:8081/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{ \"method\" : \"blockchain.block.header\", \"params\": [ 1]}",
CURLOPT_HTTPHEADER => array(
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}