add TLS support

This commit is contained in:
Maksim Fomenkov
2022-10-17 10:49:57 +03:00
parent 07d97029a5
commit 4d3826d29b
5 changed files with 44 additions and 12 deletions

View File

@@ -6,10 +6,10 @@ import util from "util";
export function cli(args) {
let opts = parseArgumentsIntoOptions(args);
if (!opts.url) {
console.log("Err: URL not specified!")
console.log("Err: URL not specified!!!")
return
}
describe(opts.url, (error, response) => {
describe(opts.url, opts.ca, opts.cert, opts.key, (error, response) => {
if (error) {
console.error(clc.red('Connection to ' + opts.url + ' failed! [' + error.message + ']'));
} else {
@@ -42,6 +42,9 @@ function parseArgumentsIntoOptions(rawArgs) {
const args = arg(
{
'--print': Boolean,
'--ca': String,
'--cert': String,
'--key': String,
'-p': '--print'
},
{
@@ -50,7 +53,10 @@ function parseArgumentsIntoOptions(rawArgs) {
);
return {
print: args['--print'] || false,
url: args._[0]
url: args._[0],
ca: args['--ca'],
cert: args['--cert'],
key: args['--key']
};
}

View File

@@ -1,6 +1,7 @@
const grpc = require("@grpc/grpc-js");
const path = require('path')
const protoLoader = require("@grpc/proto-loader");
const fs = require('fs');
const PROTO_PATH = path.join(__dirname, "../grpc/proto/blockchain.proto");
@@ -15,11 +16,21 @@ const options = {
const packageDefinition = protoLoader.loadSync(PROTO_PATH, options);
const emerald = grpc.loadPackageDefinition(packageDefinition).emerald
export function describe(url, handler) {
export function describe(url, ca, cert, key, handler) {
let credentials = grpc.credentials.createInsecure()
if (ca || cert || key) {
console.log("Using TLS")
credentials = grpc.credentials.createSsl(
ca ? fs.readFileSync(ca) : null,
key ? fs.readFileSync(key) : null,
cert ? fs.readFileSync(cert) : null
);
}
const client = new emerald.Blockchain(
url,
grpc.credentials.createInsecure()
credentials
);
console.log('Connecting to: ' + url + '...')
client.Describe({}, handler);
}