Skip to content

Commit

Permalink
server bind now should pass all tests, will run tests now
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnaganog committed Aug 14, 2019
1 parent ac1d6e5 commit fe2bbc7
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 74 deletions.
4 changes: 2 additions & 2 deletions examples/firecomm/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const firecomm = require("../../index");

let certificate = path.join(__dirname, "/server.crt");

const stub = new firecomm.Stub(routeguide.RouteGuide, "localhost:3000"/*, {
const stub = new firecomm.Stub(routeguide.RouteGuide, "localhost:3000", {
certificate
}*/);
});

// const healthStub = new firecomm.HealthStub("localhost:3000");

Expand Down
4 changes: 2 additions & 2 deletions examples/firecomm/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ let certPath = path.join(__dirname, "/server.crt");
let keyPath = path.join(__dirname, "/server.key");

// const result =
server.bind(["0.0.0.0:3000", "192.168.10.82:3001"]/*, {
server.bind(["0.0.0.0:3000", "0.0.0.0:2999"], [{
privateKey: keyPath,
certificate: certPath
}*/);
}, null]);
// console.log({ result });
// console.log({ server });
// console.log(server.__proto__);
Expand Down
275 changes: 205 additions & 70 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,87 +179,222 @@ const server = new Server();
server.bind( '0.0.0.0:3000', {certificate, privateKey } )
*/
bind(PORT, config) {
if (!Array.isArray(PORT) && typeof PORT !== "string") {
throw new Error("PORT must be a string or array of strings.");
}
// Future: support an array of ports or unlimited args
let ports = new Array();
let configs = new Array();
if (!Array.isArray(PORT)) {
ports.push(PORT);
} else {
// going to assume its not an object and is in fact an array
PORT.forEach(e => ports.push(e));
}
if (!Array.isArray(config)) {
configs.push(config);
} else {
// going to assume its not an object and is in fact an array
config.forEach(e => configs.push(e));
}
if (ports.length !== configs.length && configs.length !== 1) {
throw new Error(
"Ports and configs lengths should match, unless you want the same certificate and key to encrypt all ports. In that case, ports is an array, and supply a single config."
);
}
let singleConfig = configs.length === 1;
bind(ports, sslConfigs) {
const boundPorts = [];

for (let i = 0; i < ports.length; i++) {
// if config does not exist, create an Insecure server
// else, take the contents of config (certificate and private key) and
// apply them to create an encrypted connection
if (!configs[i]) {
let readCertificate;
let readPrivateKey;
if (typeof ports !== "string" && !sslConfigs) {
throw new Error("PORT must be a string or array of strings.");
}
else if (typeof ports === "string" && !sslConfigs) {
boundPorts.push(
super.bind(ports[i], grpc.ServerCredentials.createInsecure())
super.bind(
ports,
grpc.ServerCredentials.createInsecure(),
)
)
return this;
}
else if (typeof ports === "string" && !Array.isArray(sslConfigs) && typeof sslConfigs === 'object') {
readCertificate = fs.readFileSync(
sslConfigs.certificate
);
readPrivateKey = fs.readFileSync(
sslConfigs.privateKey
);
} else {
// we are deconstructing keys here that have the PATH value to the file
let privateKey;
let certificate;
if (singleConfig) {
const {
privateKey: unreadPrivateKey,
certificate: unreadCertificate
} = configs[0];
privateKey = unreadPrivateKey;
certificate = unreadCertificate;
} else {
const {
privateKey: unreadPrivateKey,
certificate: unreadCertificate
} = configs[i];
privateKey = unreadPrivateKey;
certificate = unreadCertificate;
}
if (privateKey === undefined || certificate === undefined) {
throw new Error(
"If supplying credentials, config object must have privateKey and certificate properties"
);
}
// creating buffer data types here
let readCertificate = fs.readFileSync(certificate);
let readPrivateKey = fs.readFileSync(privateKey);

// create an array which is "A list of private key and certificate chain
// pairs to be used for authenticating the server"
let listOfObjects = [
{ private_key: readPrivateKey, cert_chain: readCertificate }
];

boundPorts.push(
super.bind(
ports[i],
grpc.ServerCredentials.createSsl(readCertificate, listOfObjects)
ports,
grpc.ServerCredentials.createSsl(
readCertificate,
[{
private_key: readPrivateKey,
cert_chain: readCertificate
}]
),
)
)
return this;
} else if (typeof ports === "string" && Array.isArray(sslConfigs)) {
readCertificate = fs.readFileSync(
sslConfigs[0].certificate
);
readPrivateKey = fs.readFileSync(
sslConfigs[0].privateKey
);
boundPorts.push(
super.bind(
ports,
grpc.ServerCredentials.createSsl(
readCertificate,
[{
private_key: readPrivateKey,
cert_chain: readCertificate
}]
),
)
)
return this;
} else if (Array.isArray(ports) && !sslConfigs) {
console.log(sslConfigs)
ports.forEach(port => {
boundPorts.push(
super.bind(
port,
grpc.ServerCredentials.createInsecure(),
)
)
})
return this;
} else if (Array.isArray(ports) && !Array.isArray(sslConfigs) && typeof sslConfigs === 'object') {
readCertificate = fs.readFileSync(
sslConfigs.certificate
);
readPrivateKey = fs.readFileSync(
sslConfigs.privateKey
);
ports.forEach(port => {
boundPorts.push(
super.bind(
port,
grpc.ServerCredentials.createSsl(
readCertificate,
[{
private_key: readPrivateKey,
cert_chain: readCertificate
}]
),
)
)
})
return this;
} else if (Array.isArray(ports) && Array.isArray(sslConfigs) && ports.length !== sslConfigs.length) {
readCertificate = fs.readFileSync(
sslConfigs[0].certificate
);
readPrivateKey = fs.readFileSync(
sslConfigs[0].privateKey
);
ports.forEach((port, index) => {
boundPorts.push(
super.bind(
port,
grpc.ServerCredentials.createSsl(
readCertificate,
[{
private_key: readPrivateKey,
cert_chain: readCertificate
}]
),
)
)
})
return this;
} else if (Array.isArray(ports) && Array.isArray(sslConfigs) && ports.length === sslConfigs.length) {
ports.forEach((port, index) => {
readCertificate = fs.readFileSync(
sslConfigs[0].certificate
);
readPrivateKey = fs.readFileSync(
sslConfigs[0].privateKey
);
boundPorts.push(
super.bind(
port,
grpc.ServerCredentials.createSsl(
readCertificate,
[{
private_key: readPrivateKey,
cert_chain: readCertificate
}]
),
)
)
})
return this;
}
}
// Future: support an array of ports or unlimited args
// let ports = [];
// let configs = [];
// if (!Array.isArray(PORT)) {
// ports.push(PORT);
// } else {
// // going to assume its not an object and is in fact an array
// PORT.forEach(e => ports.push(e));
// }
// if (!Array.isArray(config)) {
// configs.push(config);
// } else {
// // going to assume its not an object and is in fact an array
// config.forEach(e => configs.push(e));
// }
// if (ports.length !== configs.length && configs.length !== 1) {
// throw new Error(
// "Ports and configs lengths should match, unless you want the same certificate and key to encrypt all ports. In that case, ports is an array, and supply a single config."
// );
// }
// let singleConfig = configs.length === 1;
// const boundPorts = [];

// for (let i = 0; i < ports.length; i++) {
// // if config does not exist, create an Insecure server
// // else, take the contents of config (certificate and private key) and
// // apply them to create an encrypted connection
// // if (singleConfig && config === undefined) {
// // boundPorts.push(
// // super.bindAsync(ports[i], grpc.ServerCredentials.createInsecure())
// // );
// // continue;
// // }
// // else if (!configs[i]) {
// // boundPorts.push(
// // super.bindAsync(ports[i], grpc.ServerCredentials.createInsecure())
// // );
// // } else {
// // // we are deconstructing keys here that have the PATH value to the file
// // let privateKey;
// // let certificate;
// // if (singleConfig) {
// // const {
// // privateKey: unreadPrivateKey,
// // certificate: unreadCertificate
// // } = configs[0];
// // privateKey = unreadPrivateKey;
// // certificate = unreadCertificate;
// // } else {
// // const {
// // privateKey: unreadPrivateKey,
// // certificate: unreadCertificate
// // } = configs[i];
// // privateKey = unreadPrivateKey;
// // certificate = unreadCertificate;
// // }
// // if (privateKey === undefined || certificate === undefined) {
// // throw new Error(
// // "If supplying credentials, config object must have privateKey and certificate properties"
// // );
// // }
// // // creating buffer data types here
// // let readCertificate = fs.readFileSync(certificate);
// // let readPrivateKey = fs.readFileSync(privateKey);

// // // create an array which is "A list of private key and certificate chain
// // // pairs to be used for authenticating the server"
// // let listOfObjects = [
// // { private_key: readPrivateKey, cert_chain: readCertificate }
// // ];

// // boundPorts.push(
// // super.bindAsync(
// // ports[i],
// // grpc.ServerCredentials.createSsl(readCertificate, listOfObjects)
// // )
// // );
// }
// }
this._ports = this._ports.concat(boundPorts);
return this;
}

/**
* Starts your server instance.
*/
Expand Down

0 comments on commit fe2bbc7

Please sign in to comment.